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

Нет описания правки
Нет описания правки
Строка 1: Строка 1:
local p = {}
local p = {}
local JsonPaths = require('Module:JsonPaths')
local JsonPaths = require('Module:JsonPaths')
local function trim(value)
    return mw.text.trim(value or "")
end


local function deepEqual(t1, t2)
local function deepEqual(t1, t2)
Строка 45: Строка 41:


local function findFieldInsensitive(tbl, fieldName)
local function findFieldInsensitive(tbl, fieldName)
    if type(tbl) ~= "table" then
        return nil
    end
     for key, value in pairs(tbl) do
     for key, value in pairs(tbl) do
         if type(key) == "string" and mw.ustring.lower(key) == mw.ustring.lower(fieldName) then
         if type(key) == "string" and mw.ustring.lower(key) == mw.ustring.lower(fieldName) then
Строка 55: Строка 47:
     end
     end
     return nil
     return nil
end
local function shallowMerge(base, extra)
    local result = {}
    if type(base) == "table" then
        for k, v in pairs(base) do
            result[k] = v
        end
    end
    if type(extra) == "table" then
        for k, v in pairs(extra) do
            result[k] = v
        end
    end
    return result
end
local function normalizeEntry(id, entry, defaultEntry)
    local merged = shallowMerge(defaultEntry, entry)
    merged.id = id
    return merged
end
local function collectEntries(spriteData)
    local result = {}
    local defaultEntry = spriteData.default
    local entries = spriteData.id or {}
    for id, entry in pairs(entries) do
        if type(entry) == "table" then
            table.insert(result, normalizeEntry(id, entry, defaultEntry))
        end
    end
    return result
end
end


local function getSpritePath(entry)
local function getSpritePath(entry)
    if type(entry) ~= "table" then
        return nil
    end
    if entry.sprite then
        return entry.sprite
    end
     local iconField = findFieldInsensitive(entry, "Icon")
     local iconField = findFieldInsensitive(entry, "Icon")
     local spriteField = findFieldInsensitive(entry, "Sprite")
     local spriteField = findFieldInsensitive(entry, "Sprite")
 
   
     if iconField and iconField.sprite then
     if iconField and iconField.sprite then
         return iconField.sprite
         return iconField.sprite
Строка 118: Строка 64:
         end
         end
     end
     end
    return nil
end


     return nil
local function isStateFirstKey(layer)
     for k, _ in pairs(layer) do
        return k == "state"
    end
    return false
end
end


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


     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")


     if entry.state then
     if iconBlock and iconBlock.state then
        addState(entry.state, entry.sprite or spritePath)
    elseif iconBlock and iconBlock.state then
         addState(iconBlock.state, iconBlock.sprite or spritePath)
         addState(iconBlock.state, iconBlock.sprite or spritePath)
     else
     else
Строка 174: Строка 125:
     end
     end


     if #result == 0 and ((iconBlock and (iconBlock.sprite or iconBlock.state)) or spritePath or entry.sprite) then
     if #result == 0 and (iconBlock and (iconBlock.sprite or iconBlock.state) or spritePath) then
         local s = (iconBlock and iconBlock.sprite) or entry.sprite or spritePath
         local s = (iconBlock and iconBlock.sprite) or spritePath
         addState(entry.state or "icon", s)
         addState("icon", s)
     end
     end


     return (#result > 0) and result or nil
     return (#result > 0) and result or nil
end
local function getBaseUrl(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
end


Строка 198: Строка 141:
             if deepEqual(findFieldInsensitive(entry, "Sprite"), findFieldInsensitive(group[1], "Sprite")) and
             if deepEqual(findFieldInsensitive(entry, "Sprite"), findFieldInsensitive(group[1], "Sprite")) and
               deepEqual(entry.EntityStorageVisuals, group[1].EntityStorageVisuals) and
               deepEqual(entry.EntityStorageVisuals, group[1].EntityStorageVisuals) and
               deepEqual(findFieldInsensitive(entry, "Icon"), findFieldInsensitive(group[1], "Icon")) and
               deepEqual(findFieldInsensitive(entry, "Icon"), findFieldInsensitive(group[1], "Icon")) then
              deepEqual(entry.sprite, group[1].sprite) and
              deepEqual(entry.state, group[1].state) then
                 table.insert(group, entry)
                 table.insert(group, entry)
                 found = true
                 found = true
Строка 228: Строка 169:
end
end


local function generateTemplate(entry, param, project)
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 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
Строка 238: Строка 187:
         local stateStr = ""
         local stateStr = ""
         if states then
         if states then
            local baseUrl = getBaseUrl(project)
             local links = {}
             local links = {}
             for _, item in ipairs(states) do
             for _, item in ipairs(states) do
                 local path = item.sprite or spritePath
                 local spritePathState = item.sprite
                 local stateName = item.state
                 local stateName = item.state
                 local url = baseUrl .. path .. "/" .. stateName .. ".png"
                 local url = baseUrl .. spritePathState .. "/" .. stateName .. ".png"
                 table.insert(links, "[" .. url .. " " .. stateName .. "]")
                 table.insert(links, "[" .. url .. " " .. stateName .. "]")
             end
             end
             stateStr = table.concat(links, ", ")
             stateStr = table.concat(links, ", ")
         end
         end
         return mw.getCurrentFrame():preprocess(
         return mw.getCurrentFrame():preprocess(
             "{{Entity Sprite/Image|" .. entry.id ..
             "{{Entity Sprite/Image|" .. entry.id ..
             "|" .. getBaseUrl(project) .. spritePath ..
             "|" .. baseUrl .. spritePath ..
             "|" .. stateStr .. "}}"
             "|" .. stateStr .. "}}"
         )
         )
Строка 263: Строка 210:
     local mode = frame.args[2]
     local mode = frame.args[2]


     local dataPage = JsonPaths.get("prototype/sprite.json")
    local project = JsonPaths.project(frame.args.path or "")
     local dataPage = JsonPaths.get("prototype/sprite.json", project)
    local baseUrl = getTextureBaseUrl(project)
 
     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
    local project = JsonPaths.project()
    local entries = collectEntries(spriteData)


     if action == "repeat" then
     if action == "repeat" then
         return generateRepeatTemplate(entries)
         return generateRepeatTemplate(spriteData)
     elseif action == "image" then
     elseif action == "image" then
         local result = {}
         local result = {}
         for _, entry in pairs(entries) do
         for _, entry in pairs(spriteData) do
             local template = generateTemplate(entry, action, project)
             local template = generateTemplate(entry, action, baseUrl)
             if template then
             if template then
                 table.insert(result, template)
                 table.insert(result, template)