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

Нет описания правки
Нет описания правки
 
(не показано 429 промежуточных версий этого же участника)
Строка 1: Строка 1:
local p = {}
local p = {}
 
local getArgs = require('Module:Arguments').getArgs
-- Функция для загрузки данных исследований из JSON-файла
local function loadResearchData()
return mw.text.jsonDecode(mw.title.new("Участник:IanComradeBot/Песочница.json"):getContent())
end
 
-- Функция для поиска исследований по дисциплине
local function findResearchByDiscipline(dataCache, discipline)
local results = {}
for _, research in ipairs(dataCache) do
if research.discipline == discipline then
table.insert(results, research)
end
end
return results
end
 
-- Функция для поиска исследования по ID
local function findResearchById(dataCache, id)
for _, research in ipairs(dataCache) do
if research.id == id then
return research
end
end
return nil
end
 
-- Таблица для перевода названий дисциплин
local disciplineMapping = {
Arsenal = "Арсенал",
Industrial = "Промышленность",
Experimental = "Экспериментальное",
CivilianServices = "Обслуживание персонала"
}
 
-- Таблица для цветов по уровню
local tierColors = {
[1] = "#54d554",
[2] = "#ed9000",
[3] = "#d72a2a"
}


function p.main(frame)
function p.main(frame)
-- Подключение CSS
    local args = getArgs(frame, { removeBlanks = false })
local cssLink = frame:extensionTag('templatestyles', '', {
    local name = args[1] or ""
src = 'Шаблон:Research/styles.css'
    local attributes = args[2] or ""
})
    if name == "" then
 
        return "<span class=\"error\">Ошибка: не указано имя файла.</span>"
local dataCache = loadResearchData()
    end
local idOrDiscipline = frame.args.id or ""
    local ext = (args["ext"] or "png"):gsub("^%.", "")  
    local namespace = args["namespace"] or "Файл"
    local max = tonumber(args["max"]) or 50
    local include_base = (args["base"] ~= "no")


if disciplineMapping[idOrDiscipline] then
    local found = {}
-- Если это дисциплина, выводим все исследования данной группы
local researches = findResearchByDiscipline(dataCache, idOrDiscipline)
local out = cssLink .. '<div class="research-group">'


for _, tech in ipairs(researches) do
    if include_base then
local disciplineName = disciplineMapping[tech.discipline] or "Неизвестное исследование"
        local t = mw.title.new("Файл:" .. name .. "." .. ext)
local tierColor = tierColors[tech.tier] or "#FFFFFF"
        if t and t.exists then
local iconPath = tech.icon.sprite
            table.insert(found, "")
        end
    end


out = out .. '<div class="research" id="' .. tech.discipline .. '">'
    for i = 1, max do
out = out .. '<div class="research__images">{{#invoke:Entity Sprite|main|path=' .. iconPath .. '}}</div>'
        local t = mw.title.new("Файл:" .. name .. "-" .. i .. "." .. ext)
out = out .. '<div class="research__name">' .. tech.name .. '[[Файл:' .. tech.discipline .. '.png|16px|link=]]</div>'
        if t and t.exists then
out = out .. '<div class="research__type">'
            table.insert(found, "-" .. i)
out = out .. '<div>Уровень: <span style="color:' .. tierColor .. ';">' .. tech.tier .. '</span></div>'
        end
out = out .. '<div class="research__technology">' .. disciplineName .. '</div>'
    end
out = out .. '<div>Стоимость: <span style="color:#DA70D6;">' .. tech.cost .. '</span></div>'
out = out .. '</div>'
out = out .. '</div>'
end


out = out .. '</div>'
    if #found == 0 then
return out
        return ""
else
    end
-- Если это ID, выводим одно исследование
local tech = findResearchById(dataCache, idOrDiscipline)
if not tech then
return cssLink .. '<div style="color:red;">Исследование с ID или дисциплиной "' .. idOrDiscipline .. '" не найдено.</div>'
end


local disciplineName = disciplineMapping[tech.discipline] or "Неизвестное исследование"
    local before = "[[" .. namespace .. ":" .. name
local tierColor = tierColors[tech.tier] or "#FFFFFF"
    local after = "." .. ext .. "|" .. attributes .. "]]"
local iconPath = tech.icon.sprite


-- Блок данных
    local parts = {}
local out = cssLink .. '<div class="research" id="' .. tech.discipline .. '">'
    table.insert(parts, "<choose before=\"" .. before .. "\" after=\"" .. after .. "\">")
out = out .. '<div class="research__images">{{#invoke:Entity Sprite|main|path=' .. iconPath .. '}}</div>'
    for _, suf in ipairs(found) do
out = out .. '<div class="research__name">' .. tech.name .. '[[Файл:' .. tech.discipline .. '.png|16px|link=]]</div>'
        table.insert(parts, "<option>" .. suf .. "</option>")
out = out .. '<div class="research__type">'
    end
out = out .. '<div>Уровень: <span style="color:' .. tierColor .. ';">' .. tech.tier .. '</span></div>'
    table.insert(parts, "</choose>")
out = out .. '<div class="research__technology">' .. disciplineName .. '</div>'
out = out .. '<div>Стоимость: <span style="color:#DA70D6;">' .. tech.cost .. '</span></div>'
out = out .. '</div>'
out = out .. '</div>'


return out
    return frame:preprocess(table.concat(parts, "\n"))
end
end
end


return p
return p