Module:Equipment: Difference between revisions

From Roat Pkz
Jump to navigation Jump to search
No edit summary
No edit summary
Line 51: Line 51:
     local equipSlotText = ""
     local equipSlotText = ""
      
      
    -- Check if the item name is not empty
-- Use proper item icon/link and background, if item is present or not
    if itemName ~= '' then
if itemName == '' then
        if clickable then
    equipSlotText = ""
            -- Construct a clickable link to the article about the item using the plinkp2 template
else
            equipSlotText = string.format("{{plinkp2|%s}}", names[1], names[2])
    local link
        else
    if clickable then
            -- Construct a plain image without a link using the plinkp template
        -- Construct a clickable link to the article about the item
            equipSlotText = string.format("{{plinkp|%s}}", names[1], names[2])
        link = string.format("[[%s|%s]]", names[2], names[2])
        end
    else
        -- Just the item name if not clickable
        link = names[2]
     end
     end
    equipSlotText = string.format("[[File:%s.%s|%s]]", names[1], imgType, link)
end


    -- Create the divs
-- Create the divs
    local topDiv = ret
local topDiv = ret
        :tag("div")
    :tag("div")
        :addClass(topDivClass)
    :addClass(topDivClass)


    local nestedDiv = topDiv
local nestedDiv = topDiv
        :tag("div")
    :tag("div")
        :addClass("equipment-plinkp")
    :addClass("equipment-plinkp")
        :wikitext(equipSlotText)
    :wikitext(equipSlotText)


    if quantity then
if quantity then
        local amt, quantclass = formatAmount(quantity)
    local amt, quantclass = formatAmount(quantity)
        nestedDiv:tag('span')
    nestedDiv:tag('span')
            :addClass('inv-quantity-text')
        :addClass('inv-quantity-text')
            :addClass(quantclass)
        :addClass(quantclass)
            :wikitext(amt)
        :wikitext(amt)
    end
end
 
    if stats and itemName ~= '' then
        -- Add stat-related code here if needed
    end


    if itemName ~= '' then
        topDiv:addClass("equipment-blank")
    end
end
end



Revision as of 11:26, 6 May 2024

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

local p = {}

local hasc = require('Module:Paramtest').has_content
local itemstats = require('Module:FetchItemStats')
local yesno = require('Module:Yesno')
local lang = mw.language.getContentLanguage()
local GEPrice = require('Module:Exchange')._price

-- Parse item string and check if GIF icons are allowed
local function parseItem(ret, itemString, equipSlot, stats, quantity, clickableArg)
    local itemString = itemString
    local allowGif = false -- Set default to not allow GIF icons
    local itemName = ""
    local clickable = clickableArg -- Default clickable state

    -- Look for a ;g at the end of the string and separate them
    if hasc(itemString) then
        allowGif = itemString:sub(-2) == ";g"
        if allowGif then
            itemName = itemString:sub(0, -3)
        else
            itemName = itemString or ""
        end
    end

    -- Adjust clickable based on input arguments
    if clickableArg == nil then
        -- If no specific clickable argument is provided, default to false
        clickable = false
    elseif clickableArg == "yes" then
        -- If the argument is explicitly "yes", make the item clickable
        clickable = true
    end

    return ret, itemName, allowGif, equipSlot, stats, quantity, clickable
end

-- Formats a <div> element for items
local function makeDiv(ret, itemName, allowGif, equipSlot, stats, quantity, clickable)
    local itemName = itemName
    local allowGif = allowGif
    local topDivClass = "equipment-" .. equipSlot

    local names = mw.text.split(itemName, ";")
    if #names <= 1 or names[2] == 'g' then
        names[2] = itemstats.defaultVersion(names[1]) or names[1]
    end

    local imgType = allowGif == true and "gif" or "png"

    local equipSlotText = ""
    
 -- Use proper item icon/link and background, if item is present or not
if itemName == '' then
    equipSlotText = ""
else
    local link
    if clickable then
        -- Construct a clickable link to the article about the item
        link = string.format("[[%s|%s]]", names[2], names[2])
    else
        -- Just the item name if not clickable
        link = names[2]
    end
    equipSlotText = string.format("[[File:%s.%s|%s]]", names[1], imgType, link)
end

-- Create the divs
local topDiv = ret
    :tag("div")
    :addClass(topDivClass)

local nestedDiv = topDiv
    :tag("div")
    :addClass("equipment-plinkp")
    :wikitext(equipSlotText)

if quantity then
    local amt, quantclass = formatAmount(quantity)
    nestedDiv:tag('span')
        :addClass('inv-quantity-text')
        :addClass(quantclass)
        :wikitext(amt)
end

end

function p.main(frame)
    local args = frame:getParent().args
    local buttons = yesno(args.buttons, false)
    local stats = yesno(args.stats, false)

    local align
    if args.align == "right" then
        align = "equipment-right"
    elseif args.align == "center" then
        align = "equipment-center"
    else
        align = "equipment-left"
    end
    -- general 'equipment' class for the whole thing
    align = "equipment " .. align

    local tbl = mw.html.create("table"):addClass(align):tag("tr")
    local ret = tbl:tag("td"):tag("div")
    if stats then
        stats = {
            astab = 0,
            aslash = 0,
            acrush = 0,
            amagic = 0,
            arange = 0,
            dstab = 0,
            dslash = 0,
            dcrush = 0,
            dmagic = 0,
            drange = 0,
            str = 0,
            rstr = 0,
            mdmg = 0,
            prayer = 0,
            weight = 0,
            ikods = {},
        }
    end

    if buttons then
        ret:addClass("equipment-div-buttons")
    else
        ret:addClass("equipment-div")
    end

    -- Fetch equipment and create divs
    makeDiv(parseItem(ret, args.head, "head", stats, nil, not yesno(args.headClickable, true)))
    makeDiv(parseItem(ret, args.cape, "cape", stats, nil, not yesno(args.capeClickable, true)))
    makeDiv(parseItem(ret, args.neck, "neck", stats, nil, not yesno(args.neckClickable, true)))
    makeDiv(parseItem(ret, args.ammo, "ammo", stats, args.ammoquantity, not yesno(args.ammoClickable, true)))
    makeDiv(parseItem(ret, args.weapon, "weapon", stats, args.weaponquantity, not yesno(args.weaponClickable, true)))
    makeDiv(parseItem(ret, args.torso, "torso", stats, nil, not yesno(args.torsoClickable, true)))
    makeDiv(parseItem(ret, args.shield, "shield", stats, nil, not yesno(args.shieldClickable, true)))
    makeDiv(parseItem(ret, args.legs, "legs", stats, nil, not yesno(args.legsClickable, true)))
    makeDiv(parseItem(ret, args.gloves, "gloves", stats, nil, not yesno(args.glovesClickable, true)))
    makeDiv(parseItem(ret, args.hands, "gloves", stats, nil, not yesno(args.handsClickable, true)))
    makeDiv(parseItem(ret, args.boots, "boots", stats, nil, not yesno(args.bootsClickable, true)))
    makeDiv(parseItem(ret, args.ring, "ring", stats, nil, not yesno(args.ringClickable, true)))
    
    if stats then
        -- Add stat-related code here if needed
    end

    return tbl:done()
end

return p