Модуль:Prototypes/Роль/Экипировка
Для документации этого модуля может быть создана страница Модуль:Prototypes/Роль/Экипировка/doc
local jobData = mw.loadData("Модуль:IanComradeBot/job.json/data")
local gearData = mw.loadData("Модуль:IanComradeBot/startingGear.json/data")
local gearRoleLoadout = mw.loadData("Модуль:IanComradeBot/roleLoadout.json/data")
local gearloadout = mw.loadData("Модуль:IanComradeBot/loadout.json/data")
local gearloadoutGroup = mw.loadData("Модуль:IanComradeBot/loadoutGroup.json/data")
local p = {}
-- Поиск записи по id в массиве
local function findById(tbl, id)
if type(tbl) ~= "table" then return nil end
for _, v in ipairs(tbl) do
if v.id == id then
return v
end
end
return nil
end
-- Преобразование первой буквы в заглавную
local function capitalizeFirst(str)
if not str or str == "" then return "" end
return str:sub(1,1):upper() .. str:sub(2)
end
-- Формирование вывода предмета
local function buildResult(itemId, sourceGear, slot)
if slot == "back" and sourceGear.storage and sourceGear.storage.back then
local storageBack = ""
for _, storageItem in ipairs(sourceGear.storage.back) do
storageBack = storageBack .. string.format("{{Предмет|wrapper=1|repository=1|%s}}", storageItem)
end
return string.format("<div>{{Предмет|%s|repository=1|size=64px|label=|imageTooltip=1}} {{СollapsibleMenu|%s}}</div>", itemId, storageBack)
else
return string.format("{{Предмет|%s|repository=1|size=64px|label=|imageTooltip=1}}", itemId)
end
end
-- Обработка в режиме radio
local function processResult(itemId, result, radioMode, frame)
if radioMode then
return frame:preprocess(string.format("{{#invoke:Prototypes/Механика/Частоты|main|%s}}", itemId))
else
return frame:preprocess(result)
end
end
function p.main(frame)
local args = frame.args
local param = mw.text.trim(args[1] or "")
local idParam = mw.text.trim(args[2] or "")
if param == "" or idParam == "" then
return "Ошибка: Не заданы необходимые параметры. Используйте: {{#invoke:Прототип/Роль/Экипировка|main|<slot/special/radio>|<jobId или startingGearId>}}"
end
-- Режим special: импланты
if param == "special" then
local job = findById(jobData, capitalizeFirst(idParam))
if not job then return "" end
local specials = job.special or {}
local implantsList = {}
for _, special in ipairs(specials) do
if special["!type"] == "AddImplantSpecial" and special.implants then
for _, implant in ipairs(special.implants) do
table.insert(implantsList, string.format("{{#invoke:Entity Lookup|getname|%s}}", implant))
end
end
end
if #implantsList > 0 then
return frame:preprocess(table.concat(implantsList, ", "))
else
return ""
end
end
local radioMode = false
local slot = param
if param == "radio" then
radioMode = true
slot = "ears"
end
local itemId = ""
local sourceGear = nil
-- Пытаемся найти запись в roleLoadout
local roleLoadoutId = "Job" .. capitalizeFirst(idParam)
local roleLoadoutData = findById(gearRoleLoadout, roleLoadoutId)
if not roleLoadoutData then
-- Нет групповых лоадаутов: сначала пробуем ручной ввод gearId
local gear = findById(gearData, idParam)
if gear then
-- ручной режим: idParam — это startingGearId
local gearEquipment = gear.equipment or {}
itemId = gearEquipment[slot] or ""
sourceGear = gear
else
-- fallback: берём startingGear из job
local job = findById(jobData, capitalizeFirst(idParam))
if not job or not job.startingGear then
return "Ошибка: не найдена экипировка или должность с id " .. idParam
end
local gear2 = findById(gearData, job.startingGear)
if not gear2 then
return "Ошибка: не найдена экипировка с id " .. job.startingGear
end
local gearEquipment2 = gear2.equipment or {}
itemId = gearEquipment2[slot] or ""
sourceGear = gear2
end
else
-- Есть групповые лоадауты: классический режим через roleLoadout
local jobId = capitalizeFirst(idParam)
local job = findById(jobData, jobId)
if not job then
return "Ошибка: не найдена должность с id " .. jobId
end
local gearId = job.startingGear
if not gearId then
return "Ошибка: у должности с id " .. jobId .. " не указан startingGear"
end
local gear = findById(gearData, gearId)
if not gear then
return "Ошибка: не найдена экипировка с id " .. gearId
end
local gearEquipment = gear.equipment or {}
itemId = gearEquipment[slot] or ""
sourceGear = gear
if itemId == "" then
for _, groupId in ipairs(roleLoadoutData.groups or {}) do
if groupId ~= "Trinkets" then
local group = findById(gearloadoutGroup, groupId)
if group and type(group.loadouts) == "table" then
for _, loadoutID in ipairs(group.loadouts) do
local loadout = findById(gearloadout, loadoutID)
if loadout and not loadout.effects and loadout.equipment and loadout.equipment[slot] then
itemId = loadout.equipment[slot]
sourceGear = loadout
break
end
end
end
end
if itemId ~= "" then break end
end
end
end
if itemId == "" then
return ""
end
local result = buildResult(itemId, sourceGear, slot)
return processResult(itemId, result, radioMode, frame)
end
return p