|
|
| (не показаны 382 промежуточные версии этого же участника) |
| Строка 1: |
Строка 1: |
| local p = {} | | local p = {} |
| | local getArgs = require('Module:Arguments').getArgs |
|
| |
|
| -- Загрузка данных
| | function p.main(frame) |
| local function loadData(filePath)
| | local args = getArgs(frame, { removeBlanks = false }) |
| local page = mw.title.new(filePath) | | local name = args[1] or "" |
| local content = page and page:getContent() | | local attributes = args[2] or "" |
| return content and mw.text.jsonDecode(content) or nil
| | if name == "" then |
| end
| | return "<span class=\"error\">Ошибка: не указано имя файла.</span>" |
| | |
| -- Функция для поиска исследований по дисциплине
| |
| local function findResearchByDiscipline(dataCache, discipline)
| |
| local results = {} | |
| for _, research in ipairs(dataCache) do | |
| if research.technology and research.technology.discipline == discipline then
| |
| table.insert(results, research.technology)
| |
| end
| |
| end | | end |
| return results | | local ext = (args["ext"] or "png"):gsub("^%.", "") |
| end
| | local namespace = args["namespace"] or "Файл" |
| | local max = tonumber(args["max"]) or 50 |
| | local include_base = (args["base"] ~= "no") |
|
| |
|
| -- Таблица для перевода названий дисциплин
| | local found = {} |
| local disciplineMapping = { | |
| Arsenal = "Арсенал",
| |
| Industrial = "Промышленность",
| |
| Experimental = "Экспериментальное",
| |
| CivilianServices = "Обслуживание персонала"
| |
| } | |
|
| |
|
| -- Таблица для цветов по уровням
| | if include_base then |
| local tierColors = { | | local t = mw.title.new("Файл:" .. name .. "." .. ext) |
| [1] = "#54d554",
| | if t and t.exists then |
| [2] = "#ed9000",
| | table.insert(found, "") |
| [3] = "#d72a2a"
| | end |
| }
| |
| | |
| -- Универсальная функция для генерации списков
| |
| local function generateTemplate(list, templateType)
| |
| if not list or #list == 0 then
| |
| return "" | |
| end | | end |
|
| |
|
| local result = "<ul>" | | for i = 1, max do |
| for _, entry in ipairs(list) do
| | local t = mw.title.new("Файл:" .. name .. "-" .. i .. "." .. ext) |
| if entry and entry ~= "" then | | if t and t.exists then |
| if templateType == "prerequisite" then
| | table.insert(found, "-" .. i) |
| result = result .. '<li>{{#invoke:Entity Lookup|createimagetooltip|Файл:'
| |
| .. entry .. '.png|' .. entry
| |
| .. '|Мета=32x32px,link=}} '
| |
| .. entry .. '</li>'
| |
| elseif templateType == "unlock" then
| |
| result = result .. '<li>{{#invoke:Entity Lookup|createimagetooltip|Файл:'
| |
| .. entry .. '.png|' .. entry
| |
| .. '|Мета=32x32px,link=}} {{#invoke:Entity Lookup|getname|'
| |
| .. entry .. '}}</li>'
| |
| end
| |
| end | | end |
| end | | end |
| result = result .. "</ul>"
| |
|
| |
|
| return result | | if #found == 0 then |
| end | | return "" |
| | end |
|
| |
|
| function p.main(frame)
| | local before = "[[" .. namespace .. ":" .. name |
| -- Подключение CSS
| | local after = "." .. ext .. "|" .. attributes .. "]]" |
| local cssLink = frame:extensionTag('templatestyles', '', { | |
| src = 'Шаблон:Research/styles.css'
| |
| })
| |
|
| |
|
| local dataCache = loadData("User:IanComradeBot/Песочница.json") | | local parts = {} |
| if not data or type(data) ~= 'table' then | | table.insert(parts, "<choose before=\"" .. before .. "\" after=\"" .. after .. "\">") |
| return 'Ошибка: Невозможно загрузить данные из JSON.' | | for _, suf in ipairs(found) do |
| | table.insert(parts, "<option>" .. suf .. "</option>") |
| end | | end |
| | | table.insert(parts, "</choose>") |
| local discipline = frame.args[1] or ""
| |
| | |
| if discipline and discipline ~= "" then
| |
| local out = cssLink .. '<div class="research-group">'
| |
|
| |
|
| -- Получаем список исследований по дисциплине
| | return frame:preprocess(table.concat(parts, "\n")) |
| local researches = findResearchByDiscipline(dataCache, discipline)
| |
| | |
| if #researches == 0 then
| |
| out = out .. '<div style="color:red;">Нет исследований для дисциплины "' .. discipline .. '"</div>'
| |
| end
| |
| | |
| for _, tech in ipairs(researches) do
| |
| local disciplineName = disciplineMapping[tech.discipline] or "Неизвестная дисциплина"
| |
| local tierColor = tierColors[tech.tier] or "#FFFFFF"
| |
| local iconPath = tech.icon and tech.icon.sprite or nil
| |
| | |
| -- Генерация строк prerequisites и unlocks
| |
| local prerequisitesStr = generateTemplate(tech.technologyPrerequisites, "prerequisite")
| |
| local unlocksStr = generateTemplate(tech.recipeUnlocks, "unlock")
| |
| | |
| -- Формируем вывод с использованием шаблона
| |
| out = out .. mw.getCurrentFrame():preprocess(
| |
| '{{Prototypes/Механика/Исследование' ..
| |
| '|id=' .. tech.id ..
| |
| '|icon=' .. iconPath ..
| |
| '|name=' .. tech.name ..
| |
| '|discipline=' .. tech.discipline ..
| |
| '|tier=' .. tech.tier ..
| |
| '|tierColor=' .. tierColor ..
| |
| '|disciplineName=' .. disciplineName ..
| |
| '|cost=' .. tech.cost ..
| |
| '|prerequisites=' .. prerequisitesStr ..
| |
| '|unlocks=' .. unlocksStr ..
| |
| '}}'
| |
| )
| |
| end
| |
| | |
| out = out .. '</div>'
| |
| return out
| |
| else
| |
| return cssLink .. '<div style="color:red;">Дисциплина "' .. discipline .. '" не найдена.</div>'
| |
| end
| |
| end | | end |
|
| |
|
| return p | | return p |