Модуль:Prototypes/Роль/Экипировка: различия между версиями
Pok (обсуждение | вклад) мНет описания правки |
Pok (обсуждение | вклад) мНет описания правки |
||
| Строка 1: | Строка 1: | ||
local jobData = mw.loadData("Модуль:IanComradeBot/job.json/data") | local jobData = mw.loadData("Модуль:IanComradeBot/job.json/data") | ||
local gearData = mw.loadData("Модуль:IanComradeBot/startingGear.json/data") | local gearData = mw.loadData("Модуль:IanComradeBot/startingGear.json/data") | ||
| Строка 8: | Строка 7: | ||
local p = {} | local p = {} | ||
-- | -- Поиск записи по id в массиве | ||
local function | local function findById(tbl, id) | ||
if type(tbl) ~= "table" then return nil end | |||
for | for _, v in ipairs(tbl) do | ||
if v.id then | if v.id == id then | ||
return v | |||
end | end | ||
end | end | ||
return | return nil | ||
end | end | ||
-- | -- Преобразование первой буквы в заглавную | ||
local function capitalizeFirst(str) | local function capitalizeFirst(str) | ||
if not str or str == "" then return "" end | if not str or str == "" then return "" end | ||
| Строка 39: | Строка 24: | ||
end | end | ||
-- | -- Формирование вывода предмета | ||
local function buildResult(itemId, sourceGear, slot) | local function buildResult(itemId, sourceGear, slot) | ||
if slot == "back" and sourceGear.storage and sourceGear.storage.back then | if slot == "back" and sourceGear.storage and sourceGear.storage.back then | ||
local storageBack = "" | local storageBack = "" | ||
for _, storageItem in ipairs(sourceGear.storage.back) do | for _, storageItem in ipairs(sourceGear.storage.back) do | ||
storageBack = storageBack .. string.format("{{Предмет|%s}}", storageItem) | storageBack = storageBack .. string.format("{{Предмет|wrapper=1|repository=1|%s}}", storageItem) | ||
end | end | ||
return string.format("<div>{{Предмет|%s|repository=|size=64px|label=|imageTooltip=}} {{СollapsibleMenu|%s}}</div>", itemId, storageBack) | return string.format("<div>{{Предмет|%s|repository=1|size=64px|label=|imageTooltip=1}} {{СollapsibleMenu|%s}}</div>", itemId, storageBack) | ||
else | else | ||
return string.format("{{Предмет|%s|repository=|size=64px|label=|imageTooltip=}}", itemId) | return string.format("{{Предмет|%s|repository=1|size=64px|label=|imageTooltip=1}}", itemId) | ||
end | end | ||
end | end | ||
-- | -- Обработка в режиме radio | ||
local function processResult(itemId, result, radioMode, frame) | local function processResult(itemId, result, radioMode, frame) | ||
if radioMode then | if radioMode then | ||
| Строка 65: | Строка 50: | ||
local param = mw.text.trim(args[1] or "") | local param = mw.text.trim(args[1] or "") | ||
local idParam = mw.text.trim(args[2] or "") | local idParam = mw.text.trim(args[2] or "") | ||
if param == "" or idParam == "" then | if param == "" or idParam == "" then | ||
return "Ошибка: Не заданы необходимые параметры. Используйте: {{#invoke: | return "Ошибка: Не заданы необходимые параметры. Используйте: {{#invoke:Прототип/Роль/Экипировка|main|<slot/special/radio>|<jobId или startingGearId>}}" | ||
end | end | ||
-- Режим | -- Режим special: импланты | ||
if param == "special" then | if param == "special" then | ||
local job = findById( | local job = findById(jobData, capitalizeFirst(idParam)) | ||
if not job then | if not job then return "" end | ||
local specials = job.special or {} | local specials = job.special or {} | ||
local implantsList = {} | local implantsList = {} | ||
| Строка 85: | Строка 67: | ||
end | end | ||
end | end | ||
if #implantsList > 0 then | if #implantsList > 0 then | ||
return frame:preprocess(table.concat(implantsList, ", ")) | return frame:preprocess(table.concat(implantsList, ", ")) | ||
| Строка 93: | Строка 74: | ||
end | end | ||
local radioMode = false | local radioMode = false | ||
local slot = param | local slot = param | ||
| Строка 104: | Строка 84: | ||
local sourceGear = nil | local sourceGear = nil | ||
-- | -- Пытаемся найти запись в roleLoadout | ||
local roleLoadoutId = "Job" .. capitalizeFirst(idParam) | local roleLoadoutId = "Job" .. capitalizeFirst(idParam) | ||
local roleLoadoutData = findById( | local roleLoadoutData = findById(gearRoleLoadout, roleLoadoutId) | ||
if not roleLoadoutData then | if not roleLoadoutData then | ||
-- | -- Нет групповых лоадаутов: сначала пробуем ручной ввод gearId | ||
local gear = findById( | local gear = findById(gearData, idParam) | ||
if gear then | |||
return "Ошибка: не найдена экипировка с id " .. | -- ручной режим: 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 | end | ||
else | else | ||
-- | -- Есть групповые лоадауты: классический режим через roleLoadout | ||
local jobId = capitalizeFirst(idParam) | local jobId = capitalizeFirst(idParam) | ||
local job = findById( | local job = findById(jobData, jobId) | ||
if not job then | if not job then | ||
return "Ошибка: не найдена должность с id " .. jobId | return "Ошибка: не найдена должность с id " .. jobId | ||
end | end | ||
local gearId = job.startingGear | local gearId = job.startingGear | ||
if not gearId then | if not gearId then | ||
return "Ошибка: у должности с id " .. jobId .. " не указан startingGear" | return "Ошибка: у должности с id " .. jobId .. " не указан startingGear" | ||
end | end | ||
local gear = findById(gearData, gearId) | |||
local gear = findById( | |||
if not gear then | if not gear then | ||
return "Ошибка: не найдена экипировка с id " .. gearId | return "Ошибка: не найдена экипировка с id " .. gearId | ||
end | end | ||
local gearEquipment = gear.equipment or {} | local gearEquipment = gear.equipment or {} | ||
itemId = gearEquipment[slot] or "" | itemId = gearEquipment[slot] or "" | ||
sourceGear = gear | sourceGear = gear | ||
if itemId == "" then | if itemId == "" then | ||
for _, groupId in ipairs(roleLoadoutData.groups or {}) do | for _, groupId in ipairs(roleLoadoutData.groups or {}) do | ||
if groupId ~= "Trinkets" then | if groupId ~= "Trinkets" then | ||
local | local group = findById(gearloadoutGroup, groupId) | ||
if | if group and type(group.loadouts) == "table" then | ||
for _, loadoutID in ipairs( | for _, loadoutID in ipairs(group.loadouts) do | ||
local loadout = findById( | local loadout = findById(gearloadout, loadoutID) | ||
if loadout and not loadout.effects and loadout.equipment and loadout.equipment[slot] then | if loadout and not loadout.effects and loadout.equipment and loadout.equipment[slot] then | ||
itemId = loadout.equipment[slot] | itemId = loadout.equipment[slot] | ||
| Строка 162: | Строка 147: | ||
end | end | ||
end | end | ||
if itemId == "" then | if itemId == "" then | ||
return "" | return "" | ||