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

мНет описания правки
мНет описания правки
Метка: отменено
(не показана 351 промежуточная версия этого же участника)
Строка 1: Строка 1:
local p = {}
local p = {}


-- Функция для загрузки данных исследований из JSON-файла
local function trim(s)
local function loadResearchData()
     if not s then return s end
     return mw.text.jsonDecode(mw.title.new("User:IanComradeBot/Песочница.json"):getContent())
    return (s:gsub('^%s*(.-)%s*$', '%1'))
end
end


-- Таблица для перевода названий дисциплин
local function strip_trailing_digits(s)
local disciplineMapping = {
     return s:gsub('%d+$', '')
     Arsenal = "Арсенал",
end
    Industrial = "Промышленность",
    Experimental = "Экспериментальное",
    CivilianServices = "Обслуживание персонала"
}


-- Таблица для цветов по уровням
local function split_first_underscore(k)
local tierColors = {
     local a, b = k:match('^([^_]+)_(.+)$')
    [1] = "#54d554",
     return a, b
    [2] = "#ed9000",
end
    [3] = "#d72a2a"
}
 
function p.main(frame)
    -- Подключение CSS
     local cssLink = frame:extensionTag('templatestyles', '', {
        src = 'Шаблон:Research/styles.css'
    })
 
    local dataCache = loadResearchData()
 
    -- Получаем ID и иконку из параметров
    local researchId = frame.args[1] or ""
    local icon = frame.args[2] or ""
 
     if researchId and researchId ~= "" then
        local out = cssLink .. '<div class="research-group">'


-- Поиск исследования по ID
local function collect_labels_from_args(args)
local tech = nil
    local meta = {}
for _, research in ipairs(dataCache) do
    local seen = {}
    if research.technology and research.technology.id == researchId then
    for k, v in pairs(args) do
        tech = research.technology
        if type(k) == 'string' and k:find('_', 1, true) then
        break
             local base, rem = split_first_underscore(k)
    end
             if base and rem then
end
                 local label = strip_trailing_digits(rem)
                 label = trim(label)
if not tech then
                if label ~= '' then
    out = out .. '<div style="color:red;">Исследование с ID "' .. researchId .. '" не найдено.</div>'
                    meta[base] = meta[base] or {}
else
                    if not seen[base] then seen[base] = {} end
    local tierColor = tierColors[tech.tier] or "#FFFFFF"
                    if not seen[base][label] then
    local disciplineName = disciplineMapping[tech.discipline] or "Неизвестная дисциплина"
                        table.insert(meta[base], label)
             local iconPath = icon ~= "" and icon or (tech.icon and tech.icon.sprite or nil)
                        seen[base][label] = true
 
            -- Формирование строки prerequisites
            local prerequisitesStr = ""
             if tech.technologyPrerequisites and #tech.technologyPrerequisites > 0 then
                 prerequisitesStr = '<ul>'
                 for _, prerequisite in ipairs(tech.technologyPrerequisites) do
                    if prerequisite and prerequisite ~= "" then  
                        prerequisitesStr = prerequisitesStr .. '<li>{{#invoke:Entity Lookup|createimagetooltip|Файл:'
                            .. prerequisite .. '.png|' .. prerequisite
                            .. '|Мета=32x32px,link=}}'
                            .. prerequisite .. '</li>'
                     end
                     end
                 end
                 end
                prerequisitesStr = prerequisitesStr .. '</ul>'
             end
             end
        end
    end
    return meta
end
local function render_from_args(args)
    local field = args[1] or args.field or args["field"]
    local label = args[2] or args.label or args["label"]
    field = field and trim(field) or ''
    label = label and trim(label) or ''


            -- Формирование строки unlocks
    -- map base -> label -> value
             local unlocksStr = ""
    local map = {}
             if tech.recipeUnlocks and #tech.recipeUnlocks > 0 then
    for k, v in pairs(args) do
                 unlocksStr = '<ul>'
        if type(k) == 'string' and k:find('_', 1, true) then
                 for _, recipe in ipairs(tech.recipeUnlocks) do
             local base, rem = split_first_underscore(k)
                    if recipe and recipe ~= "" then  
             if base and rem then
                        unlocksStr = unlocksStr .. '<li>{{#invoke:Entity Lookup|createimagetooltip|Файл:'  
                 local lab = strip_trailing_digits(rem)
                            .. recipe .. '.png|' .. recipe
                 lab = trim(lab)
                            .. '|Мета=32x32px,link=}} {{#invoke:Entity Lookup|getname|'
                map[base] = map[base] or {}
                            .. recipe .. '}}</li>'
                local cur = map[base][lab]
                     end
                if cur then
                    map[base][lab] = cur .. '\n' .. tostring(v)
                else
                     map[base][lab] = tostring(v)
                 end
                 end
                unlocksStr = unlocksStr .. '</ul>'
             end
             end
        end
    end
    if field ~= '' and label ~= '' then
        local base = map[field]
        if base then return base[label] or '' end
        return ''
    end
    if args[1] and args[2] then
        local f = trim(args[1]); local l = trim(args[2])
        local base = map[f]
        if base then return base[l] or '' end
    end
    return ''
end


            -- Шаблон для отображения блока исследования
function p.main(frame)
            out = out .. frame:expandTemplate({
    local args = frame.args or {}
                title = 'Prototypes/Механика/Исследование',
    local field = args[1]
                args = {
                    id = tech.id,
                    icon = iconPath,
                    name = tech.name,
                    discipline = tech.discipline,
                    tier = tech.tier,
                    tierColor = tierColor,
                    disciplineName = tech.discipline,
                    cost = tech.cost,
                    prerequisites = mw.getCurrentFrame():preprocess(prerequisitesStr),
                    unlocks = mw.getCurrentFrame():preprocess(unlocksStr)
                }
            })
        end


         return out
    if field == "meta" then
    else
         local meta = collect_labels_from_args(args)
         return cssLink .. '<div style="color:red;">Не указан ID исследования.</div>'
         return mw.text.jsonEncode(meta)
     end
     end
    return render_from_args(args)
end
end


return p
return p