Module:Sandbox/Vergir: Difference between revisions

Jump to navigation Jump to search
Vergir (talk | contribs)
Test copy of Module:AbilityTable with proposed per-prop Spirit scaling renderer inlined (with help from vergir-bot LLM)
Vergir (talk | contribs)
Fix: iterate Upgrades with ipairs (loadJsonData tables have no length) (with help from vergir-bot LLM)
Line 86: Line 86:


     local base  = spirit_scalings(ability, labels)
     local base  = spirit_scalings(ability, labels)
     local tiers = {}
     local tiers = {}   -- { tier = i, list = ... } in tier order
     local upgrades = ability["Upgrades"]
     local upgrades = ability["Upgrades"]
     if type(upgrades) == "table" then
     if type(upgrades) == "table" then
        -- ipairs rather than #: mw.loadJsonData tables have no length.
         for i, tier in ipairs(upgrades) do
         for i, tier in ipairs(upgrades) do
             if type(tier) == "table" then tiers[i] = spirit_scalings(tier, labels) end
             if type(tier) == "table" then
                table.insert(tiers, { tier = i, list = spirit_scalings(tier, labels) })
            end
         end
         end
     end
     end
Line 107: Line 110:
     end
     end
     note_keys(base)
     note_keys(base)
     for _, list in pairs(tiers) do note_keys(list) end
     for _, t in ipairs(tiers) do note_keys(t.list) end


     local function label_of(key)
     local function label_of(key)
Line 131: Line 134:


     local notes = {}
     local notes = {}
     for i = 1, (type(upgrades) == "table" and #upgrades or 0) do
     for _, t in ipairs(tiers) do
         local list = tiers[i]
         local i, list = t.tier, t.list
         if list then
         local groups, group_order = {}, {}  -- Multiply upgrades, grouped by multiplier
            local groups, group_order = {}, {}  -- Multiply upgrades, grouped by multiplier
        for _, up in ipairs(list) do
            for _, up in ipairs(list) do
            local label  = label_of(up.key)
                local label  = label_of(up.key)
            local current = running[up.key]
                local current = running[up.key]
            if up.multiply then
                if up.multiply then
                local group = groups[up.value]
                    local group = groups[up.value]
                if not group then
                    if not group then
                    group = { labels = {}, totals = {} }
                        group = { labels = {}, totals = {} }
                    groups[up.value] = group
                        groups[up.value] = group
                    table.insert(group_order, up.value)
                        table.insert(group_order, up.value)
                end
                    end
                table.insert(group.labels, label)
                    table.insert(group.labels, label)
                if current then
                    if current then
                    running[up.key] = current * up.value
                        running[up.key] = current * up.value
                     table.insert(group.totals, "×" .. format_spirit(running[up.key]))
                        table.insert(group.totals, "×" .. format_spirit(running[up.key]))
                     else
                        table.insert(group.totals, "?")
                    end
                elseif current then
                    running[up.key] = current + up.value
                    table.insert(notes, "T" .. i .. " upgrade adds +×" .. format_spirit(up.value)
                        .. " " .. label .. " scaling (total ×" .. format_spirit(running[up.key]) .. ")")
                 else
                 else
                    running[up.key] = up.value
                     table.insert(group.totals, "?")
                     table.insert(notes, "T" .. i .. " upgrade grants ×" .. format_spirit(up.value)
                        .. " " .. label .. " scaling")
                 end
                 end
            elseif current then
                running[up.key] = current + up.value
                table.insert(notes, "T" .. i .. " upgrade adds +×" .. format_spirit(up.value)
                    .. " " .. label .. " scaling (total ×" .. format_spirit(running[up.key]) .. ")")
            else
                running[up.key] = up.value
                table.insert(notes, "T" .. i .. " upgrade grants ×" .. format_spirit(up.value)
                    .. " " .. label .. " scaling")
             end
             end
            for _, m in ipairs(group_order) do
        end
                local group = groups[m]
        for _, m in ipairs(group_order) do
                table.insert(notes, "T" .. i .. " upgrade multiplies " .. join_names(group.labels)
            local group = groups[m]
                    .. " scaling by " .. format_spirit(m)
            table.insert(notes, "T" .. i .. " upgrade multiplies " .. join_names(group.labels)
                    .. " (total " .. table.concat(group.totals, ", ") .. ")")
                .. " scaling by " .. format_spirit(m)
            end
                .. " (total " .. table.concat(group.totals, ", ") .. ")")
         end
         end
     end
     end