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

мНет описания правки
мНет описания правки
Метка: отменено
(не показано 386 промежуточных версий этого же участника)
Строка 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 function findResearchByDiscipline(dataCache, discipline)
    return s:gsub('%d+$', '')
local results = {}
for _, research in ipairs(dataCache) do
if research.technology and research.technology.discipline == discipline then
table.insert(results, research.technology)
end
end
return results
end
end


-- Функция для поиска исследования по ID
local function split_first_underscore(k)
local function findResearchById(dataCache, id)
    local a, b = k:match('^([^_]+)_(.+)$')
for _, research in ipairs(dataCache) do
    return a, b
if research.technology and research.technology.id == id then
return research.technology
end
end
return nil
end
end


-- Таблица для перевода названий дисциплин
local function collect_labels_from_args(args)
local disciplineMapping = {
    local meta = {}
Arsenal = "Арсенал",
    local seen = {}
Industrial = "Промышленность",
    for k, v in pairs(args) do
Experimental = "Экспериментальное",
        if type(k) == 'string' and k:find('_', 1, true) then
CivilianServices = "Обслуживание персонала"
            local base, rem = split_first_underscore(k)
}
            if base and rem then
                local label = strip_trailing_digits(rem)
                label = trim(label)
                if label ~= '' then
                    meta[base] = meta[base] or {}
                    if not seen[base] then seen[base] = {} end
                    if not seen[base][label] then
                        table.insert(meta[base], label)
                        seen[base][label] = true
                    end
                end
            end
        end
    end
    return meta
end


-- Таблица для цветов по уровням
local function render_from_args(args)
local tierColors = {
    local field = args[1] or args.field or args["field"]
[1] = "#54d554",
    local label = args[2] or args.label or args["label"]
[2] = "#ed9000",
    field = field and trim(field) or ''
[3] = "#d72a2a"
    label = label and trim(label) or ''
}


function p.main(frame)
    -- map base -> label -> value
-- Подключение CSS
    local map = {}
local cssLink = frame:extensionTag('templatestyles', '', {
    for k, v in pairs(args) do
src = 'Шаблон:Research/styles.css'
        if type(k) == 'string' and k:find('_', 1, true) then
})
            local base, rem = split_first_underscore(k)
            if base and rem then
                local lab = strip_trailing_digits(rem)
                lab = trim(lab)
                map[base] = map[base] or {}
                local cur = map[base][lab]
                if cur then
                    map[base][lab] = cur .. '\n' .. tostring(v)
                else
                    map[base][lab] = tostring(v)
                end
            end
        end
    end


local dataCache = loadResearchData()
    if field ~= '' and label ~= '' then
local discipline = frame.args[1] or ""
        local base = map[field]
        if base then return base[label] or '' end
        return ''
    end


if discipline and discipline ~= "" then
    if args[1] and args[2] then
-- Инициализация строки вывода
        local f = trim(args[1]); local l = trim(args[2])
local out = cssLink .. '<div class="research-group">'
        local base = map[f]
        if base then return base[l] or '' end
    end


-- Получаем список исследований по дисциплине
    return ''
local researches = findResearchByDiscipline(dataCache, discipline)
end


if #researches == 0 then
function p.main(frame)
out = out .. '<div style="color:red;">Нет исследований для дисциплины "' .. discipline .. '"</div>'
    local args = frame.args or {}
end
    local has_any = false
 
    for k, _ in pairs(args) do
for _, tech in ipairs(researches) do
        has_any = true; break
local disciplineName = disciplineMapping[tech.discipline] or "Неизвестная дисциплина"
    end
local tierColor = tierColors[tech.tier] or "#FFFFFF"
    if not has_any then return collect_labels_from_args(args) end
local iconPath = tech.icon and tech.icon.sprite or nil
 
-- Основной блок исследования
out = out .. '<div class="research" id="' .. tech.id .. '">'
out = out .. frame:preprocess('<div class="research__images">[[Файл:' .. iconPath .. ']]</div>')
out = out .. frame:preprocess('<div class="research__name">' .. tech.name .. '[[Файл:' .. tech.discipline .. '.png|16px|link=]]</div>')
out = out .. '<div class="research__type">'
out = out .. '<div>Уровень: <span style="color:' .. tierColor .. ';">' .. tech.tier .. '</span></div>'
out = out .. '<div class="research__technology">' .. disciplineName .. '</div>'
out = out .. '<div>Стоимость: <span style="color:#DA70D6;">' .. tech.cost .. '</span></div>'
out = out .. '</div>'
 
-- Блок необходимых исследований
if tech.technologyPrerequisites and #tech.technologyPrerequisites > 0 then
out = out .. '<div class="research__technologies-prerequisites">Необходимые исследования:'
out = out .. '<ul>'
 
    for _, prerequisite in ipairs(tech.technologyPrerequisites) do
        if prerequisite and prerequisite ~= "" then
            out = out .. frame:preprocess('<li>{{#invoke:Entity Lookup|createimagetooltip|Файл:'
                .. prerequisite .. '.png|' .. prerequisite
                .. '|Мета=32x32px,link=}} {{#invoke:Entity Lookup|getname|'
                .. prerequisite .. '}}</li>')
        end
    end
out = out .. '</ul>'
out = out .. '</div>'
end
 
-- Блок открываемых рецептов
if tech.recipeUnlocks and #tech.recipeUnlocks > 0 then
    out = out .. '<div class="research__technologies-unlocks">Разблокирует:'
    out = out .. '<ul>'
    for _, recipe in ipairs(tech.recipeUnlocks) do
        if recipe and recipe ~= "" then  
            out = out .. frame:preprocess('<li>{{#invoke:Entity Lookup|createimagetooltip|Файл:'
                .. recipe .. '.png|' .. recipe
                .. '|Мета=32x32px,link=}} {{#invoke:Entity Lookup|getname|'
                .. recipe .. '}}</li>')
        end
    end
    out = out .. '</ul>'
    out = out .. '</div>'
end
 
out = out .. '</div>'
end


out = out .. '</div>'
    return render_from_args(args)
return out
else
return cssLink .. '<div style="color:red;">Дисциплина "' .. discipline .. '" не найдена.</div>'
end
end
end


return p
return p