Модуль:Prototypes/Роль/Экипировка: различия между версиями
Материал из Space Station 14 Вики
Pok (обсуждение | вклад) мНет описания правки |
Pok (обсуждение | вклад) мНет описания правки |
||
| Строка 1: | Строка 1: | ||
-- Загрузка данных | -- Загрузка данных | ||
local jobData | local jobData = mw.loadData("Модуль:IanComradeBot/job.json/data") | ||
local gearData | local gearData = mw.loadData("Модуль:IanComradeBot/startingGear.json/data") | ||
local gearloadout | local gearloadout = mw.loadData("Модуль:IanComradeBot/loadout.json/data") | ||
local gearloadoutGroup= mw.loadData("Модуль:IanComradeBot/loadoutGroup.json/data") | local gearloadoutGroup = mw.loadData("Модуль:IanComradeBot/loadoutGroup.json/data") | ||
local p = {} | local p = {} | ||
-- Функция для получения элемента экипировки по ключу, учитывая возможность разного регистра первой буквы | |||
local function getEquipment(equipment, slot) | |||
if not equipment then return nil end | |||
local item = equipment[slot] | |||
if item then return item end | |||
local capitalized = slot:sub(1,1):upper() .. slot:sub(2) | |||
return equipment[capitalized] | |||
end | |||
function p.main(frame) | function p.main(frame) | ||
| Строка 16: | Строка 25: | ||
end | end | ||
-- Поиск должности по id | |||
local job = nil | local job = nil | ||
for _, v in pairs(jobData) do | for _, v in pairs(jobData) do | ||
| Строка 27: | Строка 37: | ||
end | end | ||
-- Получение id набора экипировки из должности | |||
local gearId = job.startingGear | local gearId = job.startingGear | ||
if not gearId then | if not gearId then | ||
| Строка 32: | Строка 43: | ||
end | end | ||
-- Поиск набора экипировки в gearData по id | |||
local gear = nil | local gear = nil | ||
for _, v in pairs(gearData) do | for _, v in pairs(gearData) do | ||
| Строка 48: | Строка 60: | ||
local sourceGear = gear | local sourceGear = gear | ||
local itemId = gear.equipment | local itemId = getEquipment(gear.equipment, slot) | ||
-- 1. Если в gearData для запрошенного слота ничего нет, | |||
-- ищем в gearloadout по составленному id (jobId .. slot) | |||
if not itemId then | if not itemId then | ||
local combinedId = jobId .. slot | local combinedId = jobId .. slot | ||
| Строка 60: | Строка 74: | ||
end | end | ||
if loadoutGear and loadoutGear.equipment then | if loadoutGear and loadoutGear.equipment then | ||
itemId = loadoutGear.equipment | itemId = getEquipment(loadoutGear.equipment, slot) | ||
sourceGear = loadoutGear | sourceGear = loadoutGear | ||
end | end | ||
end | end | ||
-- 2. Если всё ещё не нашли – ищем в gearloadoutGroup | |||
if not itemId then | if not itemId then | ||
local combinedId = jobId .. slot | local combinedId = jobId .. slot | ||
| Строка 84: | Строка 99: | ||
end | end | ||
if foundLoadout and foundLoadout.equipment then | if foundLoadout and foundLoadout.equipment then | ||
itemId = foundLoadout.equipment | itemId = getEquipment(foundLoadout.equipment, slot) | ||
sourceGear = foundLoadout | sourceGear = foundLoadout | ||
end | end | ||
| Строка 94: | Строка 109: | ||
end | end | ||
-- Дополнительная обработка для слота "back" | |||
if slot == "back" then | if slot == "back" then | ||
local storageBack = "" | local storageBack = "" | ||
Версия от 11:17, 13 февраля 2025
Для документации этого модуля может быть создана страница Модуль:Prototypes/Роль/Экипировка/doc
-- Загрузка данных
local jobData = mw.loadData("Модуль:IanComradeBot/job.json/data")
local gearData = mw.loadData("Модуль:IanComradeBot/startingGear.json/data")
local gearloadout = mw.loadData("Модуль:IanComradeBot/loadout.json/data")
local gearloadoutGroup = mw.loadData("Модуль:IanComradeBot/loadoutGroup.json/data")
local p = {}
-- Функция для получения элемента экипировки по ключу, учитывая возможность разного регистра первой буквы
local function getEquipment(equipment, slot)
if not equipment then return nil end
local item = equipment[slot]
if item then return item end
local capitalized = slot:sub(1,1):upper() .. slot:sub(2)
return equipment[capitalized]
end
function p.main(frame)
local args = frame.args
local slot = args[1] or ""
local jobId = args[2] or ""
if slot == "" or jobId == "" then
return "Ошибка: Не заданы необходимые параметры. Используйте: {{#invoke:IanComradeBot/gear|main|<slot>|<jobId>}}"
end
-- Поиск должности по id
local job = nil
for _, v in pairs(jobData) do
if v.id == jobId then
job = v
break
end
end
if not job then
return "Ошибка: не найдена должность с id " .. jobId
end
-- Получение id набора экипировки из должности
local gearId = job.startingGear
if not gearId then
return "Ошибка: у должности с id " .. jobId .. " не указан startingGear"
end
-- Поиск набора экипировки в gearData по id
local gear = nil
for _, v in pairs(gearData) do
if v.id == gearId then
gear = v
break
end
end
if not gear then
return "Ошибка: не найдена экипировка с id " .. gearId
end
if not gear.equipment then
return "Ошибка: в экипировке с id " .. gearId .. " отсутствует раздел equipment"
end
local sourceGear = gear
local itemId = getEquipment(gear.equipment, slot)
-- 1. Если в gearData для запрошенного слота ничего нет,
-- ищем в gearloadout по составленному id (jobId .. slot)
if not itemId then
local combinedId = jobId .. slot
local loadoutGear = nil
for _, v in pairs(gearloadout) do
if string.lower(v.id) == string.lower(combinedId) then
loadoutGear = v
break
end
end
if loadoutGear and loadoutGear.equipment then
itemId = getEquipment(loadoutGear.equipment, slot)
sourceGear = loadoutGear
end
end
-- 2. Если всё ещё не нашли – ищем в gearloadoutGroup
if not itemId then
local combinedId = jobId .. slot
local groupGear = nil
for _, v in pairs(gearloadoutGroup) do
if string.lower(v.id) == string.lower(combinedId) then
groupGear = v
break
end
end
if groupGear and groupGear.loadouts and #groupGear.loadouts > 0 then
local firstLoadoutId = groupGear.loadouts[1]
local foundLoadout = nil
for _, v in pairs(gearloadout) do
if string.lower(v.id) == string.lower(firstLoadoutId) then
foundLoadout = v
break
end
end
if foundLoadout and foundLoadout.equipment then
itemId = getEquipment(foundLoadout.equipment, slot)
sourceGear = foundLoadout
end
end
end
if not itemId then
return ""
end
-- Дополнительная обработка для слота "back"
if slot == "back" then
local storageBack = ""
if sourceGear.storage and sourceGear.storage.back then
for _, storageItem in ipairs(sourceGear.storage.back) do
storageBack = storageBack .. string.format("{{#invoke:Prototypes/Предмет/Содержание|main|%s}}", storageItem)
end
end
return frame:preprocess(string.format("{{#invoke:Prototypes/Предмет/Содержание|image|%s|%s}}", itemId, storageBack))
else
return frame:preprocess(string.format("{{#invoke:Prototypes/Предмет/Содержание|image|%s}}", itemId))
end
end
return p