Toggle menu
Toggle preferences menu
Toggle personal menu
Not logged in
Your IP address will be publicly visible if you make any edits.

Join the Playtest on Steam Now: SpiritVale

Module:GameSkills: Difference between revisions

From SpiritVale Wiki
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]
        local pluginName = HERO_BAR_SLOT_ASSIGNMENT[slotIndex]
if not pluginName then
        if not pluginName then
return heroBarBox(slotIndex, nil, "", true)
                return heroBarBox(slotIndex, nil, "", true)
end
        end


local res = safeCallPlugin(pluginName, rec, ctx)
        local res = safeCallPlugin(pluginName, rec, ctx)
if not res or not res.inner or res.inner == "" then
        if not res or not res.inner or res.inner == "" then
return heroBarBox(slotIndex, nil, "", true)
                return heroBarBox(slotIndex, nil, "", true)
end
        end


return heroBarBox(slotIndex, res.classes, res.inner, false)
        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]
        local pluginName = HERO_MODULE_SLOT_ASSIGNMENT[slotIndex]
if not pluginName then
        if not pluginName then
return moduleBox(slotIndex, nil, "", true)
                return nil
end
        end


local res = safeCallPlugin(pluginName, rec, ctx)
        local res = safeCallPlugin(pluginName, rec, ctx)
if not res or not res.inner or res.inner == "" then
        if not res or isEmptyModuleContent(res.inner) then
return moduleBox(slotIndex, nil, "", true)
                return nil
end
        end


return moduleBox(slotIndex, res.classes, res.inner, false)
        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")
        local grid = mw.html.create("div")
grid:addClass("hero-modules-grid")
        grid:addClass("hero-modules-grid")
for slot = 1, 4 do
 
grid:wikitext(renderModuleSlot(slot, rec, ctx))
        local slots = {}
end
        for slot = 1, 4 do
return tostring(grid)
                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