Модуль:Prototypes/Механика/Частоты: различия между версиями

Материал из Space Station 14 Вики
Новая страница: «local p = {} local keysData = mw.loadData("Module:IanComradeBot/prototypes/encryption keys.json/data") local radioData = mw.loadData("Module:IanComradeBot/prototypes/radio channels.json/data") local fillsData = mw.loadData("Module:IanComradeBot/prototypes/fills/Item.json/data") local function trimQuotes(str) if not str then return "" end return (str:gsub("^['\"](.-)['\"]$", "%1")) end function p.render(frame) local args = frame.args local...»
 
мНет описания правки
Строка 10: Строка 10:
end
end


function p.render(frame)
function p.main(frame)
     local args = frame.args
     local args = frame.args
     local mode = args[1] or "radio"
     local mode = args[1] or "radio"
Строка 29: Строка 29:
             end
             end
         end
         end
         if not item then
         if not item then
             return "Ошибка: предмет с id '" .. itemId .. "' не найден."
             return ""
        end
        if not (item.ContainerFill and item.ContainerFill.containers and item.ContainerFill.containers.key_slots) then
            return ""
         end
         end
 
         for _, key in ipairs(item.ContainerFill.containers.key_slots) do
         if not (item.ContainerFill
             if key ~= "EncryptionKeyCommon" then
                and item.ContainerFill.containers
                table.insert(keySlots, key)
                and item.ContainerFill.containers.key_slots) then
            end
             return "Ошибка: для предмета '" .. itemId .. "' не определены ключевые слоты."
         end
         end
        keySlots = item.ContainerFill.containers.key_slots
     elseif mode == "key" then
     elseif mode == "key" then
         keySlots = { itemId }
         keySlots = { itemId }
Строка 57: Строка 55:
             end
             end
         end
         end
         if keyRecord and keyRecord.EncryptionKey and keyRecord.EncryptionKey.channels then
         if keyRecord and keyRecord.EncryptionKey and keyRecord.EncryptionKey.channels then
             for _, channel in ipairs(keyRecord.EncryptionKey.channels) do
             for _, channel in ipairs(keyRecord.EncryptionKey.channels) do
Строка 63: Строка 60:
             end
             end
         else
         else
             mw.log("Предупреждение: для ключа '" .. keyId .. "' не найдены данные или отсутствуют каналы.")
             return "Предупреждение: для ключа '" .. keyId .. "' не найдены данные или отсутствуют каналы."
         end
         end
     end
     end
Строка 76: Строка 73:
             end
             end
         end
         end
         if radioRecord then
         if radioRecord then
             local keycode = trimQuotes(radioRecord.keycode) or ""
             local keycode = trimQuotes(radioRecord.keycode) or ""
Строка 83: Строка 79:
             table.insert(resultSpans, span)
             table.insert(resultSpans, span)
         else
         else
             mw.log("Предупреждение: для канала '" .. channelName .. "' не найдены данные в radioData.")
             return "Предупреждение: для канала '" .. channelName .. "' не найдены данные в radioData."
         end
         end
     end
     end

Версия от 19:08, 14 февраля 2025

Для документации этого модуля может быть создана страница Модуль:Prototypes/Механика/Частоты/doc

local p = {}

local keysData  = mw.loadData("Module:IanComradeBot/prototypes/encryption keys.json/data")
local radioData = mw.loadData("Module:IanComradeBot/prototypes/radio channels.json/data")
local fillsData = mw.loadData("Module:IanComradeBot/prototypes/fills/Item.json/data")

local function trimQuotes(str)
    if not str then return "" end
    return (str:gsub("^['\"](.-)['\"]$", "%1"))
end

function p.main(frame)
    local args = frame.args
    local mode = args[1] or "radio"
    local itemId = args[2]
    
    if not itemId or itemId == "" then
        return "Ошибка: не указан id предмета/ключа."
    end

    local keySlots = {}

    if mode == "radio" then
        local item
        for _, data in pairs(fillsData) do
            if data.id == itemId then
                item = data
                break
            end
        end
        if not item then
            return ""
        end
        if not (item.ContainerFill and item.ContainerFill.containers and item.ContainerFill.containers.key_slots) then
            return ""
        end
        for _, key in ipairs(item.ContainerFill.containers.key_slots) do
            if key ~= "EncryptionKeyCommon" then
                table.insert(keySlots, key)
            end
        end
    elseif mode == "key" then
        keySlots = { itemId }
    else
        return "Ошибка: неизвестный режим '" .. mode .. "'."
    end

    local channels = {}
    for _, keyId in ipairs(keySlots) do
        local keyRecord
        for _, data in pairs(keysData) do
            if data.id == keyId then
                keyRecord = data
                break
            end
        end
        if keyRecord and keyRecord.EncryptionKey and keyRecord.EncryptionKey.channels then
            for _, channel in ipairs(keyRecord.EncryptionKey.channels) do
                table.insert(channels, channel)
            end
        else
            return "Предупреждение: для ключа '" .. keyId .. "' не найдены данные или отсутствуют каналы."
        end
    end

    local resultSpans = {}
    for _, channelName in ipairs(channels) do
        local radioRecord
        for _, data in pairs(radioData) do
            if data.id == channelName then
                radioRecord = data
                break
            end
        end
        if radioRecord then
            local keycode = trimQuotes(radioRecord.keycode) or ""
            local color   = trimQuotes(radioRecord.color)   or ""
            local span = string.format('<span style="color: %s">%s</span>', color, keycode)
            table.insert(resultSpans, span)
        else
            return "Предупреждение: для канала '" .. channelName .. "' не найдены данные в radioData."
        end
    end

    local output = '<span class="radio">' .. table.concat(resultSpans, " ") .. '</span>'
    return output
end

return p