Модуль:Песочница/Pok: различия между версиями

Нет описания правки
Нет описания правки
Строка 65: Строка 65:
     end
     end
     return nil
     return nil
end
local function isStateFirstKey(layer)
    for k, _ in pairs(layer) do
        return k == "state"
    end
    return false
end
end


Строка 82: Строка 75:


     local spritePath = getSpritePath(entry)
     local spritePath = getSpritePath(entry)
     local iconBlock = findFieldInsensitive(entry, "Icon")
     local iconBlock = findFieldInsensitive(entry, "Icon")
     local spriteBlock = findFieldInsensitive(entry, "Sprite")
     local spriteBlock = findFieldInsensitive(entry, "Sprite")
Строка 133: Строка 125:
end
end


local function generateRepeatTemplate(data)
local function getTextureBaseUrl(project)
    if project == "Goob" then
        return "https://github.com/space-syndicate/Goob-Station/blob/master/Resources/Textures/"
    end
 
    return "https://github.com/space-syndicate/space-station-14/blob/master/Resources/Textures/"
end
 
local function generateRepeatTemplate(data, project)
     local spriteGroups = {}
     local spriteGroups = {}


Строка 158: Строка 158:
             local idLinks = {}
             local idLinks = {}
             for _, entry in pairs(group) do
             for _, entry in pairs(group) do
                 table.insert(idLinks, "[[:Файл:" .. entry.id .. ".png]]")
                local prefix = (project ~= "" and JsonPaths.has(entry.id)) and (project .. ":") or ""
                 table.insert(idLinks, "[[:Файл:" .. prefix .. entry.id .. ".png]]")
             end
             end
             table.insert(result, mw.getCurrentFrame():preprocess(
             table.insert(result, mw.getCurrentFrame():preprocess(
Строка 169: Строка 170:
end
end


local function getTextureBaseUrl(project)
local function generateTemplate(entry, baseUrl, project)
    if project == "Goob" then
        return "https://github.com/space-syndicate/Goob-Station/blob/master/Resources/Textures/"
    end
 
    return "https://github.com/space-syndicate/space-station-14/blob/master/Resources/Textures/"
end
 
local function generateTemplate(entry, param, baseUrl)
     local spritePath = getSpritePath(entry)
     local spritePath = getSpritePath(entry)
     if not entry.id or not spritePath then
     if not entry.id or not spritePath then
Строка 183: Строка 176:
     end
     end


     if param == "image" then
     local prefix = (project ~= "" and JsonPaths.has(entry.id)) and (project .. ":") or ""
        local states = getSpriteStates(entry)
 
        local stateStr = ""
    local states = getSpriteStates(entry)
        if states then
    local stateStr = ""
            local links = {}
 
            for _, item in ipairs(states) do
    if states then
                local spritePathState = item.sprite
        local links = {}
                local stateName = item.state
        for _, item in ipairs(states) do
                local url = baseUrl .. spritePathState .. "/" .. stateName .. ".png"
            local url = baseUrl .. item.sprite .. "/" .. item.state .. ".png"
                table.insert(links, "[" .. url .. " " .. stateName .. "]")
            table.insert(links, "[" .. url .. " " .. item.state .. "]")
            end
            stateStr = table.concat(links, ", ")
         end
         end
         return mw.getCurrentFrame():preprocess(
         stateStr = table.concat(links, ", ")
            "{{Entity Sprite/Image|" .. entry.id ..
            "|" .. baseUrl .. spritePath ..
            "|" .. stateStr .. "}}"
        )
     end
     end


     return nil
     return mw.getCurrentFrame():preprocess(
        "{{Entity Sprite/Image|" .. prefix .. entry.id ..
        "|" .. baseUrl .. spritePath ..
        "|" .. stateStr .. "}}"
    )
end
end


function p.main(frame)
function p.main(frame)
     local action = frame.args[1]
     local action = frame.args[1]
    local mode = frame.args[2]


     local project = JsonPaths.project(frame.args.path or "")
     local project = JsonPaths.project()
    local dataPage = JsonPaths.get("prototype/sprite.json")
     local baseUrl = getTextureBaseUrl(project)
     local baseUrl = getTextureBaseUrl(project)


    local dataPage = JsonPaths.get("prototype/sprite.json")
     local spriteData = mw.loadData(dataPage)
     local spriteData = mw.loadData(dataPage)
     if not spriteData or type(spriteData) ~= "table" then
     if not spriteData or type(spriteData) ~= "table" then
         return "Ошибка: Невозможно загрузить данные из JSON (" .. dataPage .. ")."
         return "Ошибка загрузки JSON: " .. dataPage
     end
     end


     if action == "repeat" then
     if action == "repeat" then
         return generateRepeatTemplate(spriteData)
         return generateRepeatTemplate(spriteData, project)
     elseif action == "image" then
     elseif action == "image" then
         local result = {}
         local result = {}
         for _, entry in pairs(spriteData) do
         for _, entry in pairs(spriteData) do
             local template = generateTemplate(entry, action, baseUrl)
             local t = generateTemplate(entry, baseUrl, project)
             if template then
             if t then
                 table.insert(result, template)
                 table.insert(result, t)
             end
             end
         end
         end
         return table.concat(result, "\n")
         return table.concat(result, "\n")
    else
        return nil
     end
     end
    return nil
end
end


return p
return p