Модуль:Prototypes/Механика/Исследование: различия между версиями
Материал из Space Station 14 Вики
Pok (обсуждение | вклад) мНет описания правки |
Pok (обсуждение | вклад) мНет описания правки |
||
| Строка 1: | Строка 1: | ||
local p = {} | local p = {} | ||
-- Функция для загрузки данных исследований из JSON-файла | -- Функция для загрузки данных исследований из JSON-файла | ||
local function loadResearchData() | local function loadResearchData() | ||
return mw.text.jsonDecode(mw.title.new("User:IanComradeBot/research_prototypes.json"):getContent()) | |||
end | end | ||
-- Кэш для ID машин | |||
local machineIDCache = {} | |||
-- Функция для поиска данных по ID | -- Функция для поиска данных по ID | ||
| Строка 20: | Строка 18: | ||
return nil | return nil | ||
end | end | ||
-- Функция для перевода ID плат в ID машин | -- Функция для перевода ID плат в ID машин | ||
local function translateBoardIDToMachineID(boardID) | local function translateBoardIDToMachineID(boardID) | ||
if machineIDCache[boardID] then | |||
return machineIDCache[boardID] | |||
end | |||
local data = mw.text.jsonDecode(mw.title.new("User:IanComradeBot/entity_prototypes.json"):getContent()) | |||
local board = data[boardID] | |||
if not board or not board.name then | |||
return nil | |||
end | |||
if not board.name:find("%(машинная плата%)") and not board.name:find("%(консольная плата%)") then | |||
return nil | |||
end | |||
local machineName = board.name:gsub(" %(машинная плата%)", ""):gsub(" %(консольная плата%)", "") | |||
local excludeWords = {"Unanchored", "Debug", "Admin", "Enabled"} | |||
for _, entity in pairs(data) do | |||
if entity.name == machineName then | |||
local shouldExclude = false | |||
for _, word in ipairs(excludeWords) do | |||
if entity.id:find(word) then | |||
shouldExclude = true | |||
break | |||
end | |||
end | |||
if not shouldExclude then | |||
machineIDCache[boardID] = entity.id | |||
return entity.id | |||
end | |||
end | |||
end | |||
return nil | |||
end | end | ||
-- Таблица для перевода названий дисциплин | -- Таблица для перевода названий дисциплин | ||
local disciplineMapping = { | local disciplineMapping = { | ||
Arsenal = "Арсенал", | |||
Industrial = "Промышленность", | |||
Experimental = "Экспериментальное", | |||
CivilianServices = "Обслуживание персонала" | |||
} | } | ||
-- Таблица для цветов по уровню | -- Таблица для цветов по уровню | ||
local tierColors = { | local tierColors = { | ||
[1] = "#54d554", | |||
[2] = "#ed9000", | |||
[3] = "#d72a2a" | |||
} | } | ||
function p.main(frame) | function p.main(frame) | ||
-- Подключение CSS | |||
local cssLink = frame:extensionTag('templatestyles', '', { | |||
src = 'Шаблон:Research/styles.css' | |||
}) | |||
local dataCache = loadResearchData() | |||
local id = frame.args.id or "" | |||
local icon = frame.args.icon or "" | |||
local customRecipeUnlocks = frame.args.customRecipeUnlocks or nil | |||
local tech = findResearchById(dataCache, id) | |||
if not tech then | |||
return cssLink .. '<div style="color:red;">Нет доступных исследований.</div>' | |||
end | |||
local disciplineName = disciplineMapping[tech.discipline] or "Неизвестная дисциплина" | |||
local tierColor = tierColors[tech.tier] or "#FFFFFF" | |||
local out = cssLink .. '<div class="research" id="' .. tech.discipline .. '">' | |||
out = out .. '<div class="research__images">[[Файл:' .. icon .. '.png|64x64px|центр|link=]]</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 class="research__unblocks">Разблокирует:' | |||
out = out .. '<ul>' | |||
local recipeUnlocks = customRecipeUnlocks and mw.text.split(customRecipeUnlocks, " ") or tech.recipeUnlocks | |||
for _, recipe in ipairs(recipeUnlocks) do | |||
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>') | |||
end | |||
out = out .. '</ul>' | |||
out = out .. '</div>' | |||
out = out .. '</div>' | |||
return out | |||
end | end | ||
return p | return p | ||
Версия от 12:02, 21 ноября 2024
Для документации этого модуля может быть создана страница Модуль:Prototypes/Механика/Исследование/doc
local p = {}
-- Функция для загрузки данных исследований из JSON-файла
local function loadResearchData()
return mw.text.jsonDecode(mw.title.new("User:IanComradeBot/research_prototypes.json"):getContent())
end
-- Кэш для ID машин
local machineIDCache = {}
-- Функция для поиска данных по ID
local function findResearchById(dataCache, id)
for _, tech in ipairs(dataCache) do
if tech.id == id then
return tech
end
end
return nil
end
-- Функция для перевода ID плат в ID машин
local function translateBoardIDToMachineID(boardID)
if machineIDCache[boardID] then
return machineIDCache[boardID]
end
local data = mw.text.jsonDecode(mw.title.new("User:IanComradeBot/entity_prototypes.json"):getContent())
local board = data[boardID]
if not board or not board.name then
return nil
end
if not board.name:find("%(машинная плата%)") and not board.name:find("%(консольная плата%)") then
return nil
end
local machineName = board.name:gsub(" %(машинная плата%)", ""):gsub(" %(консольная плата%)", "")
local excludeWords = {"Unanchored", "Debug", "Admin", "Enabled"}
for _, entity in pairs(data) do
if entity.name == machineName then
local shouldExclude = false
for _, word in ipairs(excludeWords) do
if entity.id:find(word) then
shouldExclude = true
break
end
end
if not shouldExclude then
machineIDCache[boardID] = entity.id
return entity.id
end
end
end
return nil
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 id = frame.args.id or ""
local icon = frame.args.icon or ""
local customRecipeUnlocks = frame.args.customRecipeUnlocks or nil
local tech = findResearchById(dataCache, id)
if not tech then
return cssLink .. '<div style="color:red;">Нет доступных исследований.</div>'
end
local disciplineName = disciplineMapping[tech.discipline] or "Неизвестная дисциплина"
local tierColor = tierColors[tech.tier] or "#FFFFFF"
local out = cssLink .. '<div class="research" id="' .. tech.discipline .. '">'
out = out .. '<div class="research__images">[[Файл:' .. icon .. '.png|64x64px|центр|link=]]</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 class="research__unblocks">Разблокирует:'
out = out .. '<ul>'
local recipeUnlocks = customRecipeUnlocks and mw.text.split(customRecipeUnlocks, " ") or tech.recipeUnlocks
for _, recipe in ipairs(recipeUnlocks) do
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>')
end
out = out .. '</ul>'
out = out .. '</div>'
out = out .. '</div>'
return out
end
return p