Module:InfoboxNPC

From August Wiki
Revision as of 03:15, 2 March 2025 by Steve (talk | contribs)
Jump to navigation Jump to search

Documentation for this module may be created at Module:InfoboxNPC/doc

local p = {}

function p.renderInfobox(frame)
    local args = frame.args
    local parent = frame:getParent()

    if parent then
        args = parent.args
    end

    local html = [[
<table class="infobox no-parenthesis-style infobox-item">
    <tr>
      <th colspan="24" class="infobox-header">]] .. (args.name or "Unknown Monster") .. [[</th>
    </tr>
    <tr>
      <td colspan="24" class="infobox-padding"></td>
    </tr>]]

    -- Character Image (conditional)
    if args.image and args.image ~= "" then
        html = html .. [[
    <tr>
      <td colspan="24" class="infobox-image infobox-full-width-content">]] .. "[[File:" .. args.image .. "|32px]]" ..
                   [[</td>
    </tr>
    <tr>
      <td colspan="24" class="infobox-padding"></td>
    </tr>]]
    end

    -- Basic Information (conditional for each row)
    local hasBasicInfo = false

    -- Released
    if args.released and args.released ~= "" then
        html = html .. [[
    <tr>
      <th colspan="8">Released</th>
      <td colspan="16">]] .. args.released .. [[</td>
    </tr>]]
        hasBasicInfo = true
    end

    -- Members
    if args.members and args.members ~= "" then
        html = html .. [[
    <tr>
      <th colspan="8">Members</th>
      <td colspan="16">]] .. args.members .. [[</td>
    </tr>]]
        hasBasicInfo = true
    end

    -- Combat level
    if args.combat_level and args.combat_level ~= "" then
        html = html .. [[
    <tr>
      <th colspan="8">Combat level</th>
      <td colspan="16">]] .. args.combat_level .. [[</td>
    </tr>]]
        hasBasicInfo = true
    end

    -- Size
    if args.size and args.size ~= "" then
        html = html .. [[
    <tr>
      <th colspan="8">Size</th>
      <td colspan="16">]] .. args.size .. [[</td>
    </tr>]]
        hasBasicInfo = true
    end

    -- Examine
    if args.examine and args.examine ~= "" then
        html = html .. [[
    <tr>
      <th colspan="8">Examine</th>
      <td colspan="16">]] .. args.examine .. [[</td>
    </tr>]]
        hasBasicInfo = true
    end

    -- League Information
    if args.league_region and args.league_region ~= "" then
        html = html .. [[
    <tr class="leagues-global-flag">
      <th colspan="8">League region</th>
      <td colspan="16">]] .. args.league_region .. [[</td>
    </tr>]]
        hasBasicInfo = true
    end

    -- Add padding if at least one basic info field was displayed
    if hasBasicInfo then
        html = html .. [[
    <tr>
      <td colspan="24" class="infobox-padding"></td>
    </tr>]]
    end

    -- Check if any combat info fields have values
    local hasCombatInfo = false
    local combatInfoFields = {"xp_bonus", "max_hit", "aggressive", "poisonous", "attack_style", "attack_speed",
                              "respawn_time"}

    for _, field in ipairs(combatInfoFields) do
        if args[field] and args[field] ~= "" then
            hasCombatInfo = true
            break
        end
    end

    -- Combat Information Section (only if at least one field has a value)
    if hasCombatInfo then
        html = html .. [[
    <tr>
      <th colspan="24" class="infobox-subheader">Combat info</th>
    </tr>
    <tr>
      <td colspan="24" class="infobox-padding"></td>
    </tr>]]

        -- XP Bonus
        if args.xp_bonus and args.xp_bonus ~= "" then
            html = html .. [[
    <tr>
      <th colspan="8">XP bonus</th>
      <td colspan="16">]] .. args.xp_bonus .. [[</td>
    </tr>]]
        end

        -- Max hit
        if args.max_hit and args.max_hit ~= "" then
            html = html .. [[
    <tr>
      <th colspan="8">Max hit</th>
      <td colspan="16">]] .. args.max_hit .. [[</td>
    </tr>]]
        end

        -- Aggressive
        if args.aggressive and args.aggressive ~= "" then
            html = html .. [[
    <tr>
      <th colspan="8">Aggressive</th>
      <td colspan="16">]] .. args.aggressive .. [[</td>
    </tr>]]
        end

        -- Poisonous
        if args.poisonous and args.poisonous ~= "" then
            html = html .. [[
    <tr>
      <th colspan="8">Poisonous</th>
      <td colspan="16">]] .. args.poisonous .. [[</td>
    </tr>]]
        end

        -- Attack style
        if args.attack_style and args.attack_style ~= "" then
            html = html .. [[
    <tr>
      <th colspan="8">Attack style</th>
      <td colspan="16">]] .. args.attack_style .. [[</td>
    </tr>]]
        end

        -- Attack speed
        if args.attack_speed and args.attack_speed ~= "" then
            html = html .. [[
    <tr>
      <th colspan="8">Attack speed</th>
      <td colspan="16">]] .. args.attack_speed .. [[</td>
    </tr>]]
        end

        -- Respawn time
        if args.respawn_time and args.respawn_time ~= "" then
            html = html .. [[
    <tr>
      <th colspan="8">Respawn time</th>
      <td colspan="16">]] .. args.respawn_time .. [[</td>
    </tr>]]
        end

        html = html .. [[
    <tr>
      <td colspan="24" class="infobox-padding"></td>
    </tr>]]
    end

    -- Check if any slayer info fields have values
    local hasSlayerInfo = false
    local slayerInfoFields = {"slayer_level", "slayer_xp", "slayer_category", "slayer_masters"}

    for _, field in ipairs(slayerInfoFields) do
        if args[field] and args[field] ~= "" then
            hasSlayerInfo = true
            break
        end
    end

    -- Slayer Section (only if at least one field has a value)
    if hasSlayerInfo then
        local slayerIcon = args.slayer_icon or "Slayer_icon.png"

        html = html .. [[
    <tr>
      <th colspan="24" class="infobox-subheader">]] .. "[[File:" .. slayerIcon .. "|23px]] Slayer info" .. [[</th>
    </tr>
    <tr>
      <td colspan="24" class="infobox-padding"></td>
    </tr>]]

        -- Slayer level
        if args.slayer_level and args.slayer_level ~= "" then
            html = html .. [[
    <tr>
      <th colspan="8">Slayer level</th>
      <td colspan="16">]] .. args.slayer_level .. [[</td>
    </tr>]]
        end

        -- Slayer XP
        if args.slayer_xp and args.slayer_xp ~= "" then
            html = html .. [[
    <tr>
      <th colspan="8">Slayer XP</th>
      <td colspan="16">]] .. args.slayer_xp .. [[</td>
    </tr>]]
        end

        -- Category
        if args.slayer_category and args.slayer_category ~= "" then
            html = html .. [[
    <tr>
      <th colspan="8">Category</th>
      <td colspan="16">]] .. args.slayer_category .. [[</td>
    </tr>]]
        end

        -- Assigned by
        if args.slayer_masters and args.slayer_masters ~= "" then
            html = html .. [[
    <tr>
      <th colspan="8">Assigned by</th>
      <td colspan="16">]] .. args.slayer_masters .. [[</td>
    </tr>]]
        end

        html = html .. [[
    <tr>
      <td colspan="24" class="infobox-padding"></td>
    </tr>]]
    end

    -- Check if any combat stats have values
    local hasCombatStats = false
    local combatStatFields = {"hitpoints", "attack", "strength", "defence", "magic", "ranged"}

    for _, field in ipairs(combatStatFields) do
        if args[field] and args[field] ~= "" then
            hasCombatStats = true
            break
        end
    end

    -- Combat Stats Section (only if at least one stat has a value)
    if hasCombatStats then
        local combatIcon = args.combat_icon or "Multicombat.png"
        local hitpointsIcon = args.hitpoints_icon or "Hitpoints_icon.png"
        local attackIcon = args.attack_icon or "Attack_icon.png"
        local strengthIcon = args.strength_icon or "Strength_icon.png"
        local defenceIcon = args.defence_icon or "Defence_icon.png"
        local magicIcon = args.magic_icon or "Magic_icon.png"
        local rangedIcon = args.ranged_icon or "Ranged_icon.png"

        html = html .. [[
    <tr>
      <th colspan="24" class="infobox-subheader">]] .. "[[File:" .. combatIcon .. "|19px]] Combat stats" .. [[</th>
    </tr>
    <tr>
      <td colspan="24" class="infobox-padding"></td>
    </tr>
    <tr>
      <th colspan="4" class="infobox-nested">]] .. "[[File:" .. hitpointsIcon .. "|23px]]" .. [[</th>
      <th colspan="4" class="infobox-nested">]] .. "[[File:" .. attackIcon .. "|25px]]" .. [[</th>
      <th colspan="4" class="infobox-nested">]] .. "[[File:" .. strengthIcon .. "|16px]]" .. [[</th>
      <th colspan="4" class="infobox-nested">]] .. "[[File:" .. defenceIcon .. "|17px]]" .. [[</th>
      <th colspan="4" class="infobox-nested">]] .. "[[File:" .. magicIcon .. "|25px]]" .. [[</th>
      <th colspan="4" class="infobox-nested">]] .. "[[File:" .. rangedIcon .. "|23px]]" .. [[</th>
    </tr>
    <tr>
      <td colspan="4" class="infobox-nested">]] .. (args.hitpoints or "") .. [[</td>
      <td colspan="4" class="infobox-nested">]] .. (args.attack or "") .. [[</td>
      <td colspan="4" class="infobox-nested">]] .. (args.strength or "") .. [[</td>
      <td colspan="4" class="infobox-nested">]] .. (args.defence or "") .. [[</td>
      <td colspan="4" class="infobox-nested">]] .. (args.magic or "") .. [[</td>
      <td colspan="4" class="infobox-nested">]] .. (args.ranged or "") .. [[</td>
    </tr>
    <tr>
      <td colspan="24" class="infobox-padding"></td>
    </tr>]]
    end

    -- Check if any aggressive stats have values
    local hasAggressiveStats = false
    local aggressiveStatFields = {"attack_bonus", "strength_bonus", "magic_bonus", "magic_damage", "ranged_bonus",
                                  "ranged_strength"}

    for _, field in ipairs(aggressiveStatFields) do
        if args[field] and args[field] ~= "" then
            hasAggressiveStats = true
            break
        end
    end

    -- Aggressive stats section (only if at least one stat has a value)
    if hasAggressiveStats then
        local attackIcon = args.attack_icon or "Attack_icon.png"
        local strengthIcon = args.strength_icon or "Strength_icon.png"
        local magicIcon = args.magic_icon or "Magic_icon.png"
        local magicDamageIcon = args.magic_damage_icon or "Magic_Damage_icon.png"
        local rangedIcon = args.ranged_icon or "Ranged_icon.png"
        local rangedStrengthIcon = args.ranged_strength_icon or "Ranged_Strength_icon.png"

        html = html .. [[
    <tr>
      <th colspan="24" class="infobox-subheader">]] .. "[[File:" .. attackIcon .. "|25px]] Aggressive stats" .. [[</th>
    </tr>
    <tr>
      <td colspan="24" class="infobox-padding"></td>
    </tr>
    <tr>
      <th colspan="4" class="infobox-nested">]] .. "[[File:" .. attackIcon .. "|25px]]" .. [[</th>
      <th colspan="4" class="infobox-nested">]] .. "[[File:" .. strengthIcon .. "|16px]]" .. [[</th>
      <th colspan="4" class="infobox-nested">]] .. "[[File:" .. magicIcon .. "|25px]]" .. [[</th>
      <th colspan="4" class="infobox-nested">]] .. "[[File:" .. magicDamageIcon .. "|28px]]" .. [[</th>
      <th colspan="4" class="infobox-nested">]] .. "[[File:" .. rangedIcon .. "|23px]]" .. [[</th>
      <th colspan="4" class="infobox-nested">]] .. "[[File:" .. rangedStrengthIcon .. "|26px]]" .. [[</th>
    </tr>
    <tr>
      <td colspan="4" class="infobox-nested">]] .. (args.attack_bonus or "") .. [[</td>
      <td colspan="4" class="infobox-nested">]] .. (args.strength_bonus or "") .. [[</td>
      <td colspan="4" class="infobox-nested">]] .. (args.magic_bonus or "") .. [[</td>
      <td colspan="4" class="infobox-nested">]] .. (args.magic_damage or "") .. [[</td>
      <td colspan="4" class="infobox-nested">]] .. (args.ranged_bonus or "") .. [[</td>
      <td colspan="4" class="infobox-nested">]] .. (args.ranged_strength or "") .. [[</td>
    </tr>
    <tr>
      <td colspan="24" class="infobox-padding"></td>
    </tr>]]
    end

    -- Check if any melee defence stats have values
    local hasMeleeDefence = false
    local meleeDefenceFields = {"stab_def", "slash_def", "crush_def"}

    for _, field in ipairs(meleeDefenceFields) do
        if args[field] and args[field] ~= "" then
            hasMeleeDefence = true
            break
        end
    end

    -- Check if any magic defence stats have values
    local hasMagicDefence = false
    local magicDefenceFields = {"magic_def"}

    for _, field in ipairs(magicDefenceFields) do
        if args[field] and args[field] ~= "" then
            hasMagicDefence = true
            break
        end
    end

    -- Check if any ranged defence stats have values
    local hasRangedDefence = false
    local rangedDefenceFields = {"ranged_def"}

    for _, field in ipairs(rangedDefenceFields) do
        if args[field] and args[field] ~= "" then
            hasRangedDefence = true
            break
        end
    end

    if hasRangedDefence then
        local rangedDefenceIcon = args.ranged_defence_icon or "Ranged_defence_icon.png"
    end

    if hasMagicDefence then
        local magicDefenceIcon = args.magic_defence_icon or "Magic_defence_icon.png"
    end

    -- Melee Defence Section (only if at least one defence stat has a value)
    if hasMeleeDefence or hasMagicDefence or hasRangedDefence then -- Check all types of defence.
        local defenceIcon = args.defence_icon or "Defence_icon.png"
        local stabIcon = args.stab_icon or "White_dagger.png"
        local slashIcon = args.slash_icon or "White_scimitar.png"
        local crushIcon = args.crush_icon or "White_warhammer.png"
        local rangedDefenceIcon = args.ranged_defence_icon or "Ranged_defence_icon.png"
        local magicDefenceIcon = args.magic_defence_icon or "Magic_defence_icon.png"

        html = html .. [[
    <tr>
        <th colspan="24" class="infobox-subheader">]] .. "[[File:" .. defenceIcon .. "|17px]] Defences" .. [[</th>
    </tr>
    <tr>
        <td colspan="24" class="infobox-padding"></td>
    </tr>
    <tr>
        <th colspan="2" class="infobox-padding"></th>
        <th colspan="4" class="infobox-nested">]] .. "[[File:" .. stabIcon .. "|25px]]" .. [[</th>
        <th colspan="4" class="infobox-nested">]] .. "[[File:" .. slashIcon .. "|27px]]" .. [[</th>
        <th colspan="4" class="infobox-nested">]] .. "[[File:" .. crushIcon .. "|25px]]" .. [[</th>
        <th colspan="4" class="infobox-nested">]] .. "[[File:" .. rangedDefenceIcon .. "|25px]]" .. [[</th>
        <th colspan="4" class="infobox-nested">]] .. "[[File:" .. magicDefenceIcon .. "|25px]]" .. [[</th>
        <th colspan="2" class="infobox-padding"></th>
    </tr>
    <tr>
        <td colspan="2" class="infobox-padding"></td>
        <td colspan="4" class="infobox-nested">]] .. (args.stab_def or "0") .. [[</td>
        <td colspan="4" class="infobox-nested">]] .. (args.slash_def or "0") .. [[</td>
        <td colspan="4" class="infobox-nested">]] .. (args.crush_def or "0") .. [[</td>
        <td colspan="4" class="infobox-nested">]] .. (args.ranged_def or "0") .. [[</td>
        <td colspan="4" class="infobox-nested">]] .. (args.magic_def or "0") .. [[</td>
        <td colspan="2" class="infobox-padding"></td>
    </tr>
    <tr>
        <td colspan="24" class="infobox-padding"></td>
    </tr>]]
    end

    -- Check if any immunity values have been specified
    local hasImmunities = false
    local immunityFields = {"poison_immunity", "venom_immunity", "cannon_immunity", "thrall_immunity"}

    for _, field in ipairs(immunityFields) do
        if args[field] and args[field] ~= "" then
            hasImmunities = true
            break
        end
    end

    -- Immunities Section (only if at least one immunity has a value)
    if hasImmunities then
        html = html .. [[
    <tr>
      <th colspan="24" class="infobox-subheader">Immunities</th>
    </tr>
    <tr>
      <td colspan="24" class="infobox-padding"></td>
    </tr>]]

        -- Poison immunity
        if args.poison_immunity and args.poison_immunity ~= "" then
            html = html .. [[
    <tr>
      <th colspan="8">Poison</th>
      <td colspan="16">]] .. args.poison_immunity .. [[</td>
    </tr>]]
        end

        -- Venom immunity
        if args.venom_immunity and args.venom_immunity ~= "" then
            html = html .. [[
    <tr>
      <th colspan="8">Venom</th>
      <td colspan="16">]] .. args.venom_immunity .. [[</td>
    </tr>]]
        end

        -- Cannon immunity
        if args.cannon_immunity and args.cannon_immunity ~= "" then
            html = html .. [[
    <tr>
      <th colspan="8">Cannons</th>
      <td colspan="16">]] .. args.cannon_immunity .. [[</td>
    </tr>]]
        end

        -- Thrall immunity
        if args.thrall_immunity and args.thrall_immunity ~= "" then
            html = html .. [[
    <tr>
      <th colspan="8">Thralls</th>
      <td colspan="16">]] .. args.thrall_immunity .. [[</td>
    </tr>]]
        end

        html = html .. [[
    <tr>
      <td colspan="24" class="infobox-padding"></td>
    </tr>]]
    end

    -- DPS Calculator Button (optional, can be parameterized if needed)
    if args.include_dps_calc and args.include_dps_calc == "yes" then
        html = html .. [[
    <tr>
      <td colspan="24" class="dps-calc-button infobox-full-width-content">DPS calculator button here</td>
    </tr>]]
    end

    -- Advanced data section (if monster_id is provided)
    if args.monster_id and args.monster_id ~= "" then
        html = html .. [[
    <tr class="advanced-data">
      <th colspan="24" class="infobox-subheader">Advanced data</th>
    </tr>
    <tr class="advanced-data">
      <td colspan="24" class="infobox-padding"></td>
    </tr>
    <tr class="advanced-data">
      <th colspan="8">Monster ID</th>
      <td colspan="16">]] .. args.monster_id .. [[</td>
    </tr>
    <tr class="advanced-data">
      <td colspan="24" class="infobox-padding"></td>
    </tr>]]
    end

    html = html .. [[
  </tr>
</table>]]

    return html
end

-- Function for testing/debugging - outputs what parameters were received
function p.debug(frame)
    local args = frame:getParent().args
    local output = "Received parameters:<br>"

    for k, v in pairs(args) do
        output = output .. k .. " = " .. v .. "<br>"
    end

    return output
end

return p