Модуль:Prototypes/Механика/Исследование: различия между версиями
Pok (обсуждение | вклад) мНет описания правки |
Pok (обсуждение | вклад) мНет описания правки |
||
| Строка 1: | Строка 1: | ||
local p = {} | local p = {} | ||
local dataCache = nil -- Кэш для данных | local dataCache = nil -- Кэш для данных плат | ||
local researchDataCache = nil -- Кэш для данных исследований | |||
-- Функция для загрузки данных из JSON-файла | -- Функция для загрузки данных плат из JSON-файла | ||
local function loadData() | local function loadData() | ||
if not dataCache then | if not dataCache then | ||
| Строка 10: | Строка 11: | ||
end | end | ||
-- Функция для перевода ID плат в ID машин | -- Функция для загрузки данных исследований из JSON-файла | ||
local function loadResearchData() | |||
if not researchDataCache then | |||
researchDataCache = mw.text.jsonDecode(mw.title.new("User:IanComradeBot/research_prototypes.json"):getContent()) | |||
end | |||
return researchDataCache | |||
end | |||
-- Кэш для машинных ID | |||
local machineIDCache = {} | |||
-- Функция для перевода ID плат в ID машин с кэшированием | |||
local function translateBoardIDToMachineID(boardID) | local function translateBoardIDToMachineID(boardID) | ||
-- Загружаем данные | if machineIDCache[boardID] then | ||
return machineIDCache[boardID] | |||
end | |||
-- Загружаем данные плат | |||
local data = loadData() | local data = loadData() | ||
-- Поиск платы по ID | -- Поиск платы по ID | ||
local board = data[boardID] | local board = data[boardID] | ||
if not board or not board.name then | if not board or not board.name then | ||
return | return nil | ||
end | end | ||
-- Проверка | -- Проверка на машинные или консольные платы | ||
if not board.name:find("%(машинная плата%)") and not board.name:find("%(консольная плата%)") then | if not board.name:find("%(машинная плата%)") and not board.name:find("%(консольная плата%)") then | ||
return | return nil | ||
end | end | ||
-- Удаление | -- Удаление фраз из имени платы | ||
local machineName = board.name:gsub(" %(машинная плата%)", ""):gsub(" %(консольная плата%)", "") | local machineName = board.name:gsub(" %(машинная плата%)", ""):gsub(" %(консольная плата%)", "") | ||
-- | -- Исключения | ||
local excludeWords = {"Unanchored", "Debug", "Admin", "Enabled"} | local excludeWords = {"Unanchored", "Debug", "Admin", "Enabled"} | ||
-- Поиск машины по новому | -- Поиск машины по новому имени | ||
for _, entity in pairs(data) do | for _, entity in pairs(data) do | ||
if entity.name == machineName then | if entity.name == machineName then | ||
local shouldExclude = false | local shouldExclude = false | ||
for _, word in ipairs(excludeWords) do | for _, word in ipairs(excludeWords) do | ||
| Строка 43: | Строка 58: | ||
end | end | ||
end | end | ||
if not shouldExclude then | if not shouldExclude then | ||
return entity.id | machineIDCache[boardID] = entity.id | ||
return entity.id | |||
end | end | ||
end | end | ||
end | end | ||
return | return nil | ||
end | end | ||
function p.main(frame) | function p.main(frame) | ||
-- Подключение CSS | -- Подключение CSS | ||
local cssLink = frame:extensionTag('templatestyles', '', { | local cssLink = frame:extensionTag('templatestyles', '', { | ||
src = 'Шаблон:Research/styles.css' | src = 'Шаблон:Research/styles.css' | ||
}) | }) | ||
local id = frame.args.id or "" | local id = frame.args.id or "" | ||
local icon = frame.args.icon or "" | local icon = frame.args.icon or "" | ||
local customRecipeUnlocks = frame.args.customRecipeUnlocks or nil | local customRecipeUnlocks = frame.args.customRecipeUnlocks or nil | ||
-- | -- Загружаем данные исследований из кэша | ||
local data = | local data = loadResearchData() | ||
local out = cssLink | local out = cssLink | ||
local found = false | local found = false | ||
local disciplineName = "" | local disciplineName = "" | ||
-- | -- Определение дисциплины и отображение исследований | ||
for discipline, technologies in pairs(data) do | for discipline, technologies in pairs(data) do | ||
for _, tech in ipairs(technologies) do | for _, tech in ipairs(technologies) do | ||
| Строка 82: | Строка 98: | ||
})[discipline] | })[discipline] | ||
local tierColor = ({ | local tierColor = ({ | ||
[1] = "#54d554", | [1] = "#54d554", | ||
[2] = "#ed9000", | [2] = "#ed9000", | ||
[3] = "#d72a2a" | [3] = "#d72a2a" | ||
})[tech.tier] | })[tech.tier] | ||
out = out .. '<div class="research" id="' .. discipline .. '">' | out = out .. '<div class="research" id="' .. discipline .. '">' | ||
out = out .. '<div class="research__images">[[Файл:' .. icon .. '.png|64x64px|центр|link=]]</div>' | out = out .. '<div class="research__images">[[Файл:' .. icon .. '.png|64x64px|центр|link=]]</div>' | ||
| Строка 101: | Строка 115: | ||
out = out .. '<ul>' | out = out .. '<ul>' | ||
-- Используем | -- Используем кастомные recipeUnlocks или значения из json файла | ||
local recipeUnlocks | local recipeUnlocks = customRecipeUnlocks and mw.text.split(customRecipeUnlocks, " ") or tech.recipeUnlocks | ||
for _, recipe in ipairs(recipeUnlocks) do | for _, recipe in ipairs(recipeUnlocks) do | ||
local machineID = translateBoardIDToMachineID(recipe) or recipe | local machineID = translateBoardIDToMachineID(recipe) or recipe | ||
out = out .. frame:preprocess('<li>{{#invoke:Entity Lookup|createimagetooltip|Файл:' .. machineID .. '.png|' .. machineID .. '|Мета=32x32px,link=}} {{#invoke:Entity Lookup|getname|' .. machineID .. '}}</li>') | out = out .. frame:preprocess('<li>{{#invoke:Entity Lookup|createimagetooltip|Файл:' .. machineID .. '.png|' .. machineID .. '|Мета=32x32px,link=}} {{#invoke:Entity Lookup|getname|' .. machineID .. '}}</li>') | ||