Jump to content

Module:Infobox: Difference between revisions

From Apogea Wiki
Dane (talk | contribs)
Set first column width to 32px (via update-page on MediaWiki MCP Server)
Dane (talk | contribs)
Add monster function with XP and Respawn stats (via update-page on MediaWiki MCP Server)
Line 3: Line 3:
-- Stat definitions: key -> { text, icon, isBonus }
-- Stat definitions: key -> { text, icon, isBonus }
local stats = {
local stats = {
    -- Shared stats
    hp = { text = 'HP', icon = 'Health.png', isBonus = true },
    mp = { text = 'MP', icon = 'Mana.png', isBonus = true },
    arm = { text = 'Armor', icon = 'Armor.png', isBonus = true },
    def = { text = 'Defense', icon = 'Defense.png', isBonus = true },
    ms = { text = 'Move Speed', icon = 'Move_Speed.png', isBonus = true },
    as = { text = 'Attack Speed', icon = 'Attack_Speed.png', isBonus = true },
   
    -- Item-only stats
     wgt = { text = 'Weight', icon = 'Weight.png' },
     wgt = { text = 'Weight', icon = 'Weight.png' },
     rng = { text = 'Range', icon = 'Range.png' },
     rng = { text = 'Range', icon = 'Range.png' },
     dmg = { text = 'Damage', icon = 'Damage.png' },
     dmg = { text = 'Damage', icon = 'Damage.png' },
     eq = { text = 'Equip Size', icon = 'Weight.png' },
     eq = { text = 'Equip Size', icon = 'Weight.png' },
    hp = { text = 'HP', icon = 'Health.png', isBonus = true },
    mp = { text = 'MP', icon = 'Mana.png', isBonus = true },
     ab = { text = 'Ability', icon = 'Ability.png', isBonus = true },
     ab = { text = 'Ability', icon = 'Ability.png', isBonus = true },
     mag = { text = 'Magic', icon = 'Magic.png', isBonus = true },
     mag = { text = 'Magic', icon = 'Magic.png', isBonus = true },
    arm = { text = 'Armor', icon = 'Armor.png', isBonus = true },
    def = { text = 'Defense', icon = 'Defense.png', isBonus = true },
    ms = { text = 'Move Speed', icon = 'Move_Speed.png', isBonus = true },
    as = { text = 'Attack Speed', icon = 'Attack_Speed.png', isBonus = true },
     hpregen = { text = 'HP Regen', icon = 'Health_Regen.png', isBonus = true },
     hpregen = { text = 'HP Regen', icon = 'Health_Regen.png', isBonus = true },
     mpregen = { text = 'MP Regen', icon = 'Mana_Regen.png', isBonus = true },
     mpregen = { text = 'MP Regen', icon = 'Mana_Regen.png', isBonus = true },
   
    -- Monster-only stats
    xp = { text = 'Experience', icon = 'Experience.png' },
    respawn = { text = 'Respawn Time', icon = 'Respawn_Time.png' },
}
}


Line 174: Line 181:
      
      
     return box:finish() .. categories
     return box:finish() .. categories
end
function p.monster(frame)
    local args = frame:getParent().args
   
    local box = p.new(frame)
        :addClass('monster-infobox')
   
    local sprite = args.sprite or args.name
    local previewContent = frame:expandTemplate{
        title = 'MonsterPreview',
        args = { sprite, '128', mode = args.mode or 'auto', file = args.file or '' }
    }
   
    box:addHeader(args.name or 'Unknown')
      :addImage(previewContent)
      :addStat('hp', args.hp)
      :addStat('xp', args.xp)
      :addStat('arm', args.arm)
      :addStat('def', args.def)
      :addStat('as', args.as)
      :addStat('ms', args.ms)
      :addStat('respawn', args.respawn)
   
    return box:finish() .. '[[Category:Monsters]]'
end
end


return p
return p

Revision as of 08:51, 22 January 2026

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

local p = {}

-- Stat definitions: key -> { text, icon, isBonus }
local stats = {
    -- Shared stats
    hp = { text = 'HP', icon = 'Health.png', isBonus = true },
    mp = { text = 'MP', icon = 'Mana.png', isBonus = true },
    arm = { text = 'Armor', icon = 'Armor.png', isBonus = true },
    def = { text = 'Defense', icon = 'Defense.png', isBonus = true },
    ms = { text = 'Move Speed', icon = 'Move_Speed.png', isBonus = true },
    as = { text = 'Attack Speed', icon = 'Attack_Speed.png', isBonus = true },
    
    -- Item-only stats
    wgt = { text = 'Weight', icon = 'Weight.png' },
    rng = { text = 'Range', icon = 'Range.png' },
    dmg = { text = 'Damage', icon = 'Damage.png' },
    eq = { text = 'Equip Size', icon = 'Weight.png' },
    ab = { text = 'Ability', icon = 'Ability.png', isBonus = true },
    mag = { text = 'Magic', icon = 'Magic.png', isBonus = true },
    hpregen = { text = 'HP Regen', icon = 'Health_Regen.png', isBonus = true },
    mpregen = { text = 'MP Regen', icon = 'Mana_Regen.png', isBonus = true },
    
    -- Monster-only stats
    xp = { text = 'Experience', icon = 'Experience.png' },
    respawn = { text = 'Respawn Time', icon = 'Respawn_Time.png' },
}

-- Other icons
local icons = {
    type = { icon = 'Question_Mark.png', text = 'Type' },
}

local function formatBonus(value)
    if not value or value == '' then
        return nil
    end
    local num = tonumber(value)
    if num and num > 0 then
        return '+' .. value
    end
    return value
end

local function makeIcon(frame, icon, tooltip)
    local img = frame:preprocess('{{#spritescale:' .. icon .. '|stat}}')
    return '<span title="' .. mw.text.encode(tooltip) .. '">' .. img .. '</span>'
end

function p.new(frame)
    local builder = {
        root = mw.html.create('table'),
        frame = frame
    }
    
    builder.root
        :addClass('wikitable')
        :addClass('infobox')
        :css('float', 'right')
        :css('clear', 'right')
        :css('margin', '0 0 1em 1em')
        :css('width', '250px')
    
    setmetatable(builder, { __index = p })
    return builder
end

function p:addClass(class)
    self.root:addClass(class)
    return self
end

function p:addHeader(text)
    self.root:tag('tr')
        :tag('th')
            :attr('colspan', 2)
            :css('text-align', 'center')
            :wikitext(text)
    return self
end

function p:addImage(content)
    self.root:tag('tr')
        :tag('td')
            :attr('colspan', 2)
            :css('text-align', 'center')
            :wikitext(content)
    return self
end

function p:addRow(label, value, iconKey)
    if not value or value == '' then
        return self
    end
    
    local row = self.root:tag('tr')
    local th = row:tag('th')
        :css('text-align', 'right')
        :css('width', '32px')
    
    local iconData = icons[iconKey]
    if iconData then
        th:wikitext(makeIcon(self.frame, iconData.icon, label))
    else
        th:wikitext(label)
    end
    
    row:tag('td')
        :css('text-align', 'left')
        :wikitext(value)
    return self
end

function p:addStat(key, value)
    if not value or value == '' then
        return self
    end
    
    local stat = stats[key]
    if not stat then
        return self
    end
    
    local displayValue = stat.isBonus and formatBonus(value) or value
    
    local row = self.root:tag('tr')
    local th = row:tag('th')
        :css('text-align', 'right')
        :css('width', '32px')
    
    if stat.icon then
        th:wikitext(makeIcon(self.frame, stat.icon, stat.text))
    else
        th:wikitext(stat.text)
    end
    
    row:tag('td')
        :css('text-align', 'left')
        :wikitext(displayValue)
    return self
end

function p:finish()
    return tostring(self.root)
end

function p.item(frame)
    local args = frame:getParent().args
    
    local box = p.new(frame)
        :addClass('item-infobox')
    
    local sprite = args.sprite or args.name
    local spriteContent = frame:expandTemplate{
        title = 'Sprite',
        args = { sprite, '64', class = 'pageimage' }
    }
    
    box:addHeader(args.name or 'Unknown')
       :addImage(spriteContent)
       :addRow('Slot', args.slot)
       :addRow('Type', args.category and string.format('[[:Category:%s|%s]]', args.category, args.category), 'type')
       :addStat('wgt', args.wgt)
       :addStat('rng', args.rng)
       :addStat('dmg', args.dmg)
       :addStat('eq', args.eq)
       :addStat('hp', args.hp)
       :addStat('mp', args.mp)
       :addStat('ab', args.ab)
       :addStat('mag', args.mag)
       :addStat('arm', args.arm)
       :addStat('def', args.def)
       :addStat('ms', args.ms)
       :addStat('as', args.as)
       :addStat('hpregen', args.hpregen)
       :addStat('mpregen', args.mpregen)
    
    local categories = ''
    if args.category then
        categories = categories .. '[[Category:' .. args.category .. ']]'
    end
    
    return box:finish() .. categories
end

function p.monster(frame)
    local args = frame:getParent().args
    
    local box = p.new(frame)
        :addClass('monster-infobox')
    
    local sprite = args.sprite or args.name
    local previewContent = frame:expandTemplate{
        title = 'MonsterPreview',
        args = { sprite, '128', mode = args.mode or 'auto', file = args.file or '' }
    }
    
    box:addHeader(args.name or 'Unknown')
       :addImage(previewContent)
       :addStat('hp', args.hp)
       :addStat('xp', args.xp)
       :addStat('arm', args.arm)
       :addStat('def', args.def)
       :addStat('as', args.as)
       :addStat('ms', args.ms)
       :addStat('respawn', args.respawn)
    
    return box:finish() .. '[[Category:Monsters]]'
end

return p