Модуль:Песочница/Pok
Для документации этого модуля может быть создана страница Модуль:Песочница/Pok/doc
local p = {}
-- Функция для загрузки данных исследований из JSON-файла
local function loadResearchData()
return mw.text.jsonDecode(mw.title.new("User:IanComradeBot/Песочница.json"):getContent())
end
-- Функция для поиска исследований по дисциплине
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
return results
end
-- Таблица для перевода названий дисциплин
local disciplineMapping = {
Arsenal = "Арсенал",
Industrial = "Промышленность",
Experimental = "Экспериментальное",
CivilianServices = "Обслуживание персонала"
}
-- Таблица для цветов по уровню
local tierColors = {
[1] = "#54d554",
[2] = "#ed9000",
[3] = "#d72a2a"
}
function p.main(frame)
-- Подключение CSS
local cssLink = frame:extensionTag('templatestyles', '', {
src = 'Шаблон:Research/styles.css'
})
local dataCache = loadResearchData()
local discipline = frame.args[1] or ""
if discipline then
-- Если это дисциплина, выводим все исследования данной группы
local researches = findResearchByDiscipline(dataCache, discipline)
for _, tech in ipairs(researches) do
local disciplineName = disciplineMapping[tech.discipline] or "Неизвестное исследование"
local tierColor = tierColors[tech.tier] or "#FFFFFF"
local iconPath = tech.icon.sprite
out = out .. '<div class="research" id="' .. tech.discipline .. '">'
out = out .. '<div class="research__images">{{#invoke:Entity Sprite|main|path|' .. iconPath .. '}}</div>'
out = out .. '<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>'
out = out .. '</div>'
end
-- Блок необходимых исследований
if tech.technologyPrerequisites and #tech.technologyPrerequisites > 0 then
out = out .. '<div class="research__technologies-prerequisites">Необходимые исследования:'
out = out .. '<ul>'
for _, prerequisiteId in ipairs(tech.technologyPrerequisites) do
local prerequisiteTech = findResearchById(dataCache, prerequisiteId)
if prerequisiteTech then
out = out .. '<li>' .. prerequisiteTech.name .. '</li>'
else
out = out .. '<li>Неизвестное исследование (' .. prerequisiteId .. ')</li>'
end
end
out = out .. '</ul>'
out = out .. '</div>'
else
out = out .. ''
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
out = out .. '<li>' .. recipe .. '</li>'
end
out = out .. '</ul>'
out = out .. '</div>'
else
out = out .. '<div class="research__technologies-unlocks">Не разблокирует ничего.</div>'
end
return out
else
return cssLink .. '<div style="color:red;">Дисциплина "' .. discipline .. '" не найдена.</div>'
end
end
return p