Module:GameSkills: Difference between revisions
From SpiritVale Wiki
More actions
No edit summary |
No edit summary |
||
| Line 1,589: | Line 1,589: | ||
-- renderHeroBarSlot: render a hero-bar slot by plugin assignment. | -- renderHeroBarSlot: render a hero-bar slot by plugin assignment. | ||
local function renderHeroBarSlot(slotIndex, rec, ctx) | local function renderHeroBarSlot(slotIndex, rec, ctx) | ||
local pluginName = HERO_BAR_SLOT_ASSIGNMENT[slotIndex] | |||
if not pluginName then | |||
return heroBarBox(slotIndex, nil, "", true) | |||
end | |||
local res = safeCallPlugin(pluginName, rec, ctx) | |||
if not res or not res.inner or res.inner == "" then | |||
return heroBarBox(slotIndex, nil, "", true) | |||
end | |||
return heroBarBox(slotIndex, res.classes, res.inner, false) | |||
end | |||
-- isEmptyModuleContent: true when a module slot has no meaningful content. | |||
local function isEmptyModuleContent(inner) | |||
if inner == nil then return true end | |||
local trimmed = mw.text.trim(tostring(inner)) | |||
if trimmed == "" or trimmed == "—" then | |||
return true | |||
end | |||
local withoutTags = mw.text.trim(mw.ustring.gsub(trimmed, "<[^>]+>", "")) | |||
return (withoutTags == "" or withoutTags == "—") | |||
end | |||
-- mergeClasses: merge string/table classes with an optional extra class. | |||
local function mergeClasses(base, extra) | |||
local list = {} | |||
local function add(item) | |||
if type(item) == "string" then | |||
for cls in mw.ustring.gmatch(item, "[^%s]+") do | |||
table.insert(list, cls) | |||
end | |||
elseif type(item) == "table" then | |||
for _, c in ipairs(item) do | |||
add(c) | |||
end | |||
end | |||
end | |||
add(base) | |||
add(extra) | |||
if #list == 0 then | |||
return nil | |||
elseif #list == 1 then | |||
return list[1] | |||
end | |||
return list | |||
end | end | ||
-- renderModuleSlot: render a hero-module slot by plugin assignment. | -- renderModuleSlot: render a hero-module slot by plugin assignment. | ||
local function renderModuleSlot(slotIndex, rec, ctx) | local function renderModuleSlot(slotIndex, rec, ctx) | ||
local pluginName = HERO_MODULE_SLOT_ASSIGNMENT[slotIndex] | |||
if not pluginName then | |||
return nil | |||
end | |||
local res = safeCallPlugin(pluginName, rec, ctx) | |||
if not res or isEmptyModuleContent(res.inner) then | |||
return nil | |||
end | |||
return { | |||
inner = res.inner, | |||
classes = res.classes, | |||
} | |||
end | end | ||
| Line 1,632: | Line 1,676: | ||
-- buildHeroModulesUI: build the 2x2 module grid row (4 slots). | -- buildHeroModulesUI: build the 2x2 module grid row (4 slots). | ||
local function buildHeroModulesUI(rec, ctx) | local function buildHeroModulesUI(rec, ctx) | ||
local grid = mw.html.create("div") | |||
grid:addClass("hero-modules-grid") | |||
local slots = {} | |||
for slot = 1, 4 do | |||
slots[slot] = renderModuleSlot(slot, rec, ctx) | |||
end | |||
local hasModules = false | |||
for _, pair in ipairs({ { 1, 2 }, { 3, 4 } }) do | |||
local left = slots[pair[1]] | |||
local right = slots[pair[2]] | |||
if left or right then | |||
if left and right then | |||
grid:wikitext(moduleBox(pair[1], left.classes, left.inner, false)) | |||
grid:wikitext(moduleBox(pair[2], right.classes, right.inner, false)) | |||
elseif left then | |||
grid:wikitext(moduleBox(pair[1], mergeClasses(left.classes, "hero-module-full"), left.inner, false)) | |||
elseif right then | |||
grid:wikitext(moduleBox(pair[2], mergeClasses(right.classes, "hero-module-full"), right.inner, false)) | |||
end | |||
hasModules = true | |||
end | |||
end | |||
if not hasModules then | |||
return "" | |||
end | |||
return tostring(grid) | |||
end | end | ||