Module:Monster: Difference between revisions

From Soul's Remnant Wiki
Jump to navigation Jump to search
(Created page with "-- Module:Monster local p = {} local data = require('Module:MonsterData') ---------------------------------------------------------- -- Return a single stat ---------------------------------------------------------- function p.get(frame) local args = frame.args local name = args.name or args[1] local stat = args.stat or args[2] local e = data[name] if not e then return "Monster not found: " .. tostring(name) end if not stat or not e[...")
 
No edit summary
Line 2: Line 2:
local p = {}
local p = {}
local data = require('Module:MonsterData')
local data = require('Module:MonsterData')
----------------------------------------------------------
-- Helper: Format drops
-- two_columns: boolean, whether to make it 2-column (row) or 1-column (infobox)
----------------------------------------------------------
local function formatDrops(drops, frame, two_columns)
    if not drops then return "—" end
    if type(drops) ~= "table" then return drops end
    local lines = {}
    for _, drop in ipairs(drops) do
        local drop_icon = drop.name
        if frame and frame.expandTemplate then
            drop_icon = frame:expandTemplate{
                title = "FaviconPaste",
                args = { Name = drop.name }
            }
        end
        local quantity = drop.quantity and (" " .. drop.quantity) or ""
        local chance = drop.chance and (" – " .. drop.chance) or ""
        local line = (two_columns and "<li>" or "* ") .. drop_icon .. quantity .. chance .. (two_columns and "</li>" or "")
        table.insert(lines, line)
    end
    if two_columns then
        return "<div style='column-count:2; -moz-column-count:2; -webkit-column-count:2; text-align:left'><ul>"
            .. table.concat(lines, "") .. "</ul></div>"
    else
        return "<div style='text-align:left'><ul>\n" .. table.concat(lines, "\n") .. "\n</ul></div>"
    end
end


----------------------------------------------------------
----------------------------------------------------------
Line 15: Line 46:
         return "Monster not found: " .. tostring(name)
         return "Monster not found: " .. tostring(name)
     end
     end
     if not stat or not e[stat] then
     if not stat or e[stat] == nil then
         return "Stat not found: " .. tostring(stat)
         return "Stat not found: " .. tostring(stat)
     end
     end
Line 23: Line 54:


----------------------------------------------------------
----------------------------------------------------------
-- Return a single wikitable row (two columns for drops with left-aligned bullet points)
-- Return a single wikitable row (2-column drops)
----------------------------------------------------------
----------------------------------------------------------
function p.row(frame)
function p.row(frame)
Line 34: Line 65:
     end
     end


     -- Handle drops
     -- Format drops (2 columns)
     local drops = e.drops or '—'
     local drops_html = formatDrops(e.drops, frame, true)
    if type(drops) == "table" then
 
        local lines = {}
    -- Format element
        for _, drop in ipairs(drops) do
    local element = e.element and string.format('<span class="Element %s">%s</span>', e.element, e.element) or '—'
            local icon = drop.icon and frame:expandTemplate{title = drop.icon} .. " " or ""
 
            local quantity = drop.quantity and drop.quantity .. " " or ""
    -- Image with name below image
            local chance = drop.chance and " – " .. drop.chance or ""
    local image_and_name = e.image and string.format(
            table.insert(lines, string.format("<li>%s[[%s]] %s%s</li>", icon, drop.name, quantity, chance))
         '<div style="text-align:center">[[File:%s|50px|link=%s]]<br>[[%s]]</div>',
        end
        e.image, name, name
         drops = "<div style='column-count:2; -moz-column-count:2; -webkit-column-count:2; text-align:left'><ul>"
     ) or ''
            .. table.concat(lines, "") .. "</ul></div>"
     end


     return string.format(
     return string.format(
         '|-\n| [[File:%s|50px]] || [[%s]] || %s || %s || %s || %s || %s || %s || %s || %s || %s',
         '|-\n| %s || %s || %s || %s || %s || %s || %s || %s || %s || %s',
         e.image or '',
         image_and_name,
        name,
         element,
         e.element or '',
         e.level or '',
         e.level or '',
         e.exp or '',
         e.exp or '',
Line 60: Line 88:
         e.mp or '',
         e.mp or '',
         e.spd or '',
         e.spd or '',
         drops
         drops_html
     )
     )
end
end


----------------------------------------------------------
----------------------------------------------------------
-- Generate a Portable Infobox (single-column drops with icons, left-aligned)
-- Return only drops (single-column)
----------------------------------------------------------
function p.drops(frame)
    local args = frame.args
    local name = args.name or args[1]
    local e = data[name]
 
    if not e then
        return "Monster not found: " .. tostring(name)
    end
 
    return formatDrops(e.drops, frame, false)
end
 
----------------------------------------------------------
-- Generate a Portable Infobox (single-column drops)
----------------------------------------------------------
----------------------------------------------------------
function p.infobox(frame)
function p.infobox(frame)
Line 78: Line 121:
     local infobox_params = {
     local infobox_params = {
         name = string.format('[[%s|%s]]', name, e.name or name),
         name = string.format('[[%s|%s]]', name, e.name or name),
         image = e.image and string.format('[[File:%s]]', e.image) or "",
         image = e.image and string.format('[[File:%s|link=%s]]', e.image, name) or "",
         element = e.element or "—",
         element = e.element and string.format('<span class="Element %s">%s</span>', e.element, e.element) or "—",
         level = e.level or "—",
         level = e.level or "—",
         exp = e.exp or "—",
         exp = e.exp or "—",
         hp = e.hp or "—",
         hp = e.hp or "—",
         atk = e.attack or "—",
         atk = e.atk or "—",
         def = e.defence or "—",
         def = e.def or "—",
         mp = e.mp or "—",
         mp = e.mp or "—",
         spd = e.spd or "—"
         spd = e.spd or "—",
        drops = formatDrops(e.drops, frame, false)
     }
     }
    -- Single-column drops, left-aligned bullet list
    local drops = e.drops or '—'
    if type(drops) == "table" then
        local lines = {}
        for _, drop in ipairs(drops) do
            local icon = drop.icon and "{{" .. drop.icon .. "}} " or ""
            local quantity = drop.quantity and drop.quantity .. " " or ""
            local chance = drop.chance and " – " .. drop.chance or ""
            table.insert(lines, string.format('* %s[[%s]] %s%s', icon, drop.name, quantity, chance))
        end
        -- Add newline before first bullet to ensure proper wikitext rendering
        infobox_params.drops = "<div style='text-align:left'><ul>\n" .. table.concat(lines, "\n") .. "\n</ul></div>"
    else
        infobox_params.drops = drops
    end


     return frame:expandTemplate{
     return frame:expandTemplate{

Revision as of 04:57, 3 November 2025

-- Module:Monster local p = {} local data = require('Module:MonsterData')


-- Helper: Format drops -- two_columns: boolean, whether to make it 2-column (row) or 1-column (infobox)


local function formatDrops(drops, frame, two_columns)

   if not drops then return "—" end
   if type(drops) ~= "table" then return drops end
   local lines = {}
   for _, drop in ipairs(drops) do
       local drop_icon = drop.name
       if frame and frame.expandTemplate then
           drop_icon = frame:expandTemplate{
               title = "FaviconPaste",
               args = { Name = drop.name }
           }
       end
       local quantity = drop.quantity and (" " .. drop.quantity) or ""
       local chance = drop.chance and (" – " .. drop.chance) or ""

local line = (two_columns and "

  • " or "* ") .. drop_icon .. quantity .. chance .. (two_columns and "
  • " or "") table.insert(lines, line) end if two_columns then return "

      " .. table.concat(lines, "") .. "

    "

       else
    

    return "

      \n" .. table.concat(lines, "\n") .. "\n

    "

       end
    

    end


    -- Return a single stat


    function p.get(frame)

       local args = frame.args
       local name = args.name or args[1]
       local stat = args.stat or args[2]
    
       local e = data[name]
       if not e then
           return "Monster not found: " .. tostring(name)
       end
       if not stat or e[stat] == nil then
           return "Stat not found: " .. tostring(stat)
       end
    
       return e[stat]
    

    end


    -- Return a single wikitable row (2-column drops)


    function p.row(frame)

       local args = frame.args
       local name = args.name or args[1]
       local e = data[name]
    
       if not e then
           return "Monster not found: " .. tostring(name)
       end
    
       -- Format drops (2 columns)
       local drops_html = formatDrops(e.drops, frame, true)
    
       -- Format element
       local element = e.element and string.format('%s', e.element, e.element) or '—'
    
       -- Image with name below image
       local image_and_name = e.image and string.format(
    

    '

    File:%s
    %s

    ',

           e.image, name, name
       ) or 
    
       return string.format(
           '|-\n| %s || %s || %s || %s || %s || %s || %s || %s || %s || %s',
           image_and_name,
           element,
           e.level or ,
           e.exp or ,
           e.hp or ,
           e.atk or ,
           e.def or ,
           e.mp or ,
           e.spd or ,
           drops_html
       )
    

    end


    -- Return only drops (single-column)


    function p.drops(frame)

       local args = frame.args
       local name = args.name or args[1]
       local e = data[name]
    
       if not e then
           return "Monster not found: " .. tostring(name)
       end
    
       return formatDrops(e.drops, frame, false)
    

    end


    -- Generate a Portable Infobox (single-column drops)


    function p.infobox(frame)

       local args = frame.args
       local name = args.name or args[1]
       local e = data[name]
    
       if not e then
           return "Monster not found: " .. tostring(name)
       end
    
       local infobox_params = {
           name = string.format('%s', name, e.name or name),
           image = e.image and string.format('File:%s', e.image, name) or "",
           element = e.element and string.format('%s', e.element, e.element) or "—",
           level = e.level or "—",
           exp = e.exp or "—",
           hp = e.hp or "—",
           atk = e.atk or "—",
           def = e.def or "—",
           mp = e.mp or "—",
           spd = e.spd or "—",
           drops = formatDrops(e.drops, frame, false)
       }
    
       return frame:expandTemplate{
           title = "Monster/Infobox",
           args = infobox_params
       }
    

    end

    return p