Module:Sandbox/Aurelain/HeroClassSkills: Difference between revisions

From Heroes of Might and Magic: Olden Era Official Wiki
Created page with "-- This module is intended as a supporting muscle for the HeroClassSkills template. local p = {} -- Displays a variable ------------------------------------------------------------------------------------------------------------------------ local function dump(target) return '<pre>' .. mw.dumpObject(target) .. '</pre>' end ------------------------------------------------------------------------------------------------------------------------ function p.percent(fram..."
 
No edit summary
 
(23 intermediate revisions by the same user not shown)
Line 1: Line 1:
-- This module is intended as a supporting muscle for the HeroClassSkills template.
-- This module is intended as a supporting muscle for the HeroClassSkills template.
local p = {}
local p = {}
local UtilQuery = require('Module:UtilQuery')
local FACTION = {
    human = 'humans', -- changed!
    undead = 'necros', -- changed!
    nature = 'nature',
    demon = 'demons', -- changed!
    unfrozen = 'unfrozen',
    dungeon = 'dungeon',
}


-- Displays a variable
-- Displays a variable
Line 6: Line 15:
local function dump(target)
local function dump(target)
     return '<pre>' .. mw.dumpObject(target) .. '</pre>'
     return '<pre>' .. mw.dumpObject(target) .. '</pre>'
end
------------------------------------------------------------------------------------------------------------------------
local function collectChances(id)
    local type, faction = id:match("(.*)_(.*)")
    local adaptedId = FACTION[faction] .. '_' .. type .. '_skills_table'
    local result = UtilQuery.run([[
        tables =
            SkillRollWeight = S
        |where =
            S.table_id = '%s'
        |fields =
            S.band_kind = band_kind,
            S.skill_id = skill_id,
            S.weight = weight
    ]], adaptedId)
    return result
end
------------------------------------------------------------------------------------------------------------------------
local function consolidateChances(chancesBulk)
    local chances = {}
    for _, row in ipairs(chancesBulk) do
        local id = row.skill_id or ''
        local entry = chances[id]
        if not entry then
            chances[id] = {
                id = id,
                default = 0,
            }
        end
        local band_kind = row.band_kind or 'default'
        local weight = row.weight
        chances[id][band_kind] = tonumber(weight)
    end
    return chances
end
------------------------------------------------------------------------------------------------------------------------
local function computeTotal(chances)
    local total = {0,0,0,0}
    for _, info in pairs(chances) do
        total[1] = total[1] + info.default
        total[2] = total[2] + (info.magic_levels or info.default)
        total[3] = total[3] + (info.signature_levels or info.default)
        total[4] = total[4] + (info.level_20_mega or info.default)
    end
    return table.concat(total, ',')
end
------------------------------------------------------------------------------------------------------------------------
local function formatChances(chances)
    local lines = {}
    for id, info in pairs(chances) do
        local parts = {}
        local main = info.default
        table.insert(parts, main)
        table.insert(parts, info.magic_levels ~= main and info.magic_levels or '')
        table.insert(parts, info.signature_levels ~= main and info.signature_levels or '')
        table.insert(parts, info.level_20_mega ~= main and info.level_20_mega or '')
        while #parts > 0 and parts[#parts] == '' do
            table.remove(parts)
        end
        local cleanId = id:gsub('skill_', '')
        table.insert(lines, '| ' .. cleanId .. ' = ' .. table.concat(parts, ','))
    end
    return table.concat(lines, '\n')
end
------------------------------------------------------------------------------------------------------------------------
function p.populate(frame)
    local lang = frame.args.lang
    local id = frame.args.id;
    local chancesBulk = collectChances(id);
    local chances = consolidateChances(chancesBulk);
    local total = computeTotal(chances);
    local formattedChances = formatChances(chances);
    --do return dump(formattedChances) end
    local cleanTitle = frame:getTitle():gsub('Module:', '')
    return frame:preprocess(mw.ustring.format([[
        {{%s/base
        | lang = %s
        | id = %s
        | total = %s
        %s
        }}
    ]], cleanTitle, lang, id, total, formattedChances))
end
end


------------------------------------------------------------------------------------------------------------------------
------------------------------------------------------------------------------------------------------------------------
function p.percent(frame)
function p.percent(frame)
     local a = frame.args[1]
     local a = tonumber(frame.args[1])
     local b = frame.args[2]
     local b = tonumber(frame.args[2])
     return string.format("%.2f", a/b)
    if not a or not b then
        return ''
    end
     return string.format("%.2f", 100 * a / b)
end
end


return p
return p

Latest revision as of 12:26, 8 August 2026

Documentation for this module may be created at Module:Sandbox/Aurelain/HeroClassSkills/doc

-- This module is intended as a supporting muscle for the HeroClassSkills template.
local p = {}
local UtilQuery = require('Module:UtilQuery')
local FACTION = {
    human = 'humans', -- changed!
    undead = 'necros', -- changed!
    nature = 'nature',
    demon = 'demons', -- changed!
    unfrozen = 'unfrozen',
    dungeon = 'dungeon',
}

-- Displays a variable
------------------------------------------------------------------------------------------------------------------------
local function dump(target)
    return '<pre>' .. mw.dumpObject(target) .. '</pre>'
end

------------------------------------------------------------------------------------------------------------------------
local function collectChances(id)
    local type, faction = id:match("(.*)_(.*)")
    local adaptedId = FACTION[faction] .. '_' .. type .. '_skills_table'
    local result = UtilQuery.run([[
        tables =
            SkillRollWeight = S
        |where =
            S.table_id = '%s'
        |fields =
            S.band_kind = band_kind,
            S.skill_id = skill_id,
            S.weight = weight
    ]], adaptedId)
    return result
end

------------------------------------------------------------------------------------------------------------------------
local function consolidateChances(chancesBulk)
    local chances = {}
    for _, row in ipairs(chancesBulk) do
        local id = row.skill_id or ''
        local entry = chances[id]
        if not entry then
            chances[id] = {
                id = id,
                default = 0,
            }
        end
        local band_kind = row.band_kind or 'default'
        local weight = row.weight
        chances[id][band_kind] = tonumber(weight)
    end
    return chances
end

------------------------------------------------------------------------------------------------------------------------
local function computeTotal(chances)
    local total = {0,0,0,0}
    for _, info in pairs(chances) do
        total[1] = total[1] + info.default
        total[2] = total[2] + (info.magic_levels or info.default)
        total[3] = total[3] + (info.signature_levels or info.default)
        total[4] = total[4] + (info.level_20_mega or info.default)
    end
    return table.concat(total, ',')
end

------------------------------------------------------------------------------------------------------------------------
local function formatChances(chances)
    local lines = {}
    for id, info in pairs(chances) do
        local parts = {}
        local main = info.default
        table.insert(parts, main)
        table.insert(parts, info.magic_levels ~= main and info.magic_levels or '')
        table.insert(parts, info.signature_levels ~= main and info.signature_levels or '')
        table.insert(parts, info.level_20_mega ~= main and info.level_20_mega or '')
        while #parts > 0 and parts[#parts] == '' do
            table.remove(parts)
        end
        local cleanId = id:gsub('skill_', '')
        table.insert(lines, '| ' .. cleanId .. ' = ' .. table.concat(parts, ','))
    end
    return table.concat(lines, '\n')
end

------------------------------------------------------------------------------------------------------------------------
function p.populate(frame)
    local lang = frame.args.lang
    local id = frame.args.id;

    local chancesBulk = collectChances(id);
    local chances = consolidateChances(chancesBulk);
    local total = computeTotal(chances);
    local formattedChances = formatChances(chances);
    --do return dump(formattedChances) end

    local cleanTitle = frame:getTitle():gsub('Module:', '')
    return frame:preprocess(mw.ustring.format([[
        {{%s/base
        | lang = %s
        | id = %s
        | total = %s
        %s
        }}
    ]], cleanTitle, lang, id, total, formattedChances))
end

------------------------------------------------------------------------------------------------------------------------
function p.percent(frame)
    local a = tonumber(frame.args[1])
    local b = tonumber(frame.args[2])
    if not a or not b then
        return ''
    end
    return string.format("%.2f", 100 * a / b)
end

return p