Module:Inventory: Difference between revisions

From Roat Pkz
Jump to navigation Jump to search
No edit summary
Tag: Reverted
No edit summary
Tag: Reverted
Line 2: Line 2:


local hasc = require('Module:Paramtest').has_content
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


function formatAmount(_x)
-- Parse item string and check if GIF icons are allowed
     local x = tonumber(_x) or 1
local function parseItem(ret, itemString, equipSlot, stats, quantity, clickable)
     if x < 100000 then
     local itemString = itemString
         return x, 'qty-1'
    local allowGif = false -- Set default to not allow GIF icons
    elseif x < 10000000 then
    local itemName = ""
        return tostring(math.floor(x/1000))..'K', 'qty-100k'
 
    else
    -- Look for a ;g at the end of the string and separate them
         return tostring(math.floor(x/1000000))..'M', 'qty-10m'
     if hasc(itemString) then
         allowGif = itemString:sub(-2) == ";g"
        if allowGif then
            itemName = itemString:sub(0, -3)
        else
            itemName = itemString or ""
         end
     end
     end
   
    return ret, itemName, allowGif, equipSlot, stats, quantity, clickable
end
end


function p.main(frame)
-- Formats a <div> element for items
     local bgType = frame.args.bgType
local function makeDiv(ret, itemName, allowGif, equipSlot, stats, quantity, clickable)
     if bgType == nil then
     local itemName = itemName
         bgType = "inventory"
    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
     end
    local args = frame:getParent().args
 
     local items = {}
     local imgType = allowGif == true and "gif" or "png"
    for i=1,28 do
 
        local v = mw.text.trim(args[i] or '')
    local equipSlotText = ""
        local item_x
   
        local amt_x = 1
    -- Check if the item name is not empty
        local note_x = false
    if itemName ~= '' then
        local clickable_x = false -- Added line
         if clickable then
         if hasc(v) then
             -- Construct a clickable link
             v = v:gsub('[][]','')
            equipSlotText = string.format("[[File:%s.%s|%s|link=]]", names[1], imgType, names[2])
            amt_x = tonumber(v:match('\\(%d+)')) or 1
        else
            note_x = v:match(';n') == ';n'
             -- Construct a plain image without a link
             clickable_x = v:match(';c=yes') == ';c=yes' or v:match(';c=true') == ';c=true' -- Modified line
             equipSlotText = string.format("[[File:%s.%s|link=%s]]", names[1], imgType, names[2])
             local v2 = mw.text.split(v,'[;\\]')
            item_x = v2[1]
         end
         end
        table.insert(items,{item=item_x,amt=amt_x,isnoted=note_x,clickable=clickable_x}) -- Modified line
     end
     end
     local align = args.align
 
     local acss
    -- Create the divs
    if hasc(align) then
     local topDiv = ret
         align = align:lower()
        :tag("div")
         if align == 'right' then
        :addClass(topDivClass)
             align = "storage-right"
 
        elseif align == 'left' then
     local nestedDiv = topDiv
            align = "storage-left"
        :tag("div")
        else
         :addClass("equipment-plinkp")
            align = "storage-center"
         :wikitext(equipSlotText)
        end
 
    if quantity then
        local amt, quantclass = formatAmount(quantity)
        nestedDiv:tag('span')
             :addClass('inv-quantity-text')
            :addClass(quantclass)
            :wikitext(amt)
    end
 
    if stats and itemName ~= '' then
        -- Add stat-related code here if needed
    end
 
    if itemName ~= '' then
        topDiv:addClass("equipment-blank")
     end
     end
    return p._main(items,align,bgType)
end
end


function p._main(items,align,bgType)
function p.main(frame)
     local className = bgType .. "table"
    local args = frame:getParent().args
     local ret = mw.html.create('table')
    local buttons = yesno(args.buttons, false)
        :addClass(className)
    local stats = yesno(args.stats, false)
        :addClass(align)
 
     local item = 0
     local align
    for i=1,7 do
    if args.align == "right" then
        local ret_row = ret:tag('tr')
        align = "equipment-right"
        for j=1,4 do
    elseif args.align == "center" then
             item = item + 1
        align = "equipment-center"
             local itemx = items[item].item
    else
             local amtx = items[item].amt
        align = "equipment-left"
             local amtx_f, amtx_c = formatAmount(amtx)
    end
             local notex = items[item].isnoted
    -- general 'equipment' class for the whole thing
             local clickable_x = items[item].clickable
    align = "equipment " .. align
             local td = ret_row:tag('td')
 
            if hasc(itemx) then
     local tbl = mw.html.create("table"):addClass(align):tag("tr")
                if clickable_x then
    local ret = tbl:tag("td"):tag("div")
                    td:wikitext(string.format('[[File:%s.png|%s|link=%s|32x32px|frameless]]', itemx, itemx))
     if stats then
                else
        stats = {
                    td:wikitext(string.format('[[File:%s.png|link=|32x32px|frameless]]', itemx, itemx, itemx))
            astab = 0,
                end
            aslash = 0,
                if notex then
            acrush = 0,
                    td:addClass('noted-item')
            amagic = 0,
                end
            arange = 0,
                if amtx > 1 or notex then
            dstab = 0,
                    td:tag('span')
            dslash = 0,
                      :addClass('inv-quantity-text')
            dcrush = 0,
                      :addClass(amtx_c)
             dmagic = 0,
                      :wikitext(amtx_f)
             drange = 0,
                end
             str = 0,
            end
             rstr = 0,
         end
            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, yesno(args.headClickable, false)))
    makeDiv(parseItem(ret, args.cape, "cape", stats, nil, yesno(args.capeClickable, false)))
    makeDiv(parseItem(ret, args.neck, "neck", stats, nil, yesno(args.neckClickable, false)))
    makeDiv(parseItem(ret, args.ammo, "ammo", stats, args.ammoquantity, yesno(args.ammoClickable, false)))
    makeDiv(parseItem(ret, args.weapon, "weapon", stats, args.weaponquantity, yesno(args.weaponClickable, false)))
    makeDiv(parseItem(ret, args.torso, "torso", stats, nil, yesno(args.torsoClickable, false)))
    makeDiv(parseItem(ret, args.shield, "shield", stats, nil, yesno(args.shieldClickable, false)))
    makeDiv(parseItem(ret, args.legs, "legs", stats, nil, yesno(args.legsClickable, false)))
    makeDiv(parseItem(ret, args.gloves, "gloves", stats, nil, yesno(args.glovesClickable, false)))
    makeDiv(parseItem(ret, args.hands, "gloves", stats, nil, yesno(args.handsClickable, false)))
    makeDiv(parseItem(ret, args.boots, "boots", stats, nil, yesno(args.bootsClickable, false)))
    makeDiv(parseItem(ret, args.ring, "ring", stats, nil, yesno(args.ringClickable, false)))
   
    if stats then
         -- Add stat-related code here if needed
     end
     end


     return ret
     return tbl:done()
end
end


return p
return p

Revision as of 11:54, 6 May 2024

Documentation for this module may be created at Module:Inventory/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, clickable)
    local itemString = itemString
    local allowGif = false -- Set default to not allow GIF icons
    local itemName = ""

    -- 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
    
    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 = ""
    
    -- Check if the item name is not empty
    if itemName ~= '' then
        if clickable then
            -- Construct a clickable link
            equipSlotText = string.format("[[File:%s.%s|%s|link=]]", names[1], imgType, names[2])
        else
            -- Construct a plain image without a link
            equipSlotText = string.format("[[File:%s.%s|link=%s]]", names[1], imgType, names[2])
        end
    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

    if stats and itemName ~= '' then
        -- Add stat-related code here if needed
    end

    if itemName ~= '' then
        topDiv:addClass("equipment-blank")
    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, yesno(args.headClickable, false)))
    makeDiv(parseItem(ret, args.cape, "cape", stats, nil, yesno(args.capeClickable, false)))
    makeDiv(parseItem(ret, args.neck, "neck", stats, nil, yesno(args.neckClickable, false)))
    makeDiv(parseItem(ret, args.ammo, "ammo", stats, args.ammoquantity, yesno(args.ammoClickable, false)))
    makeDiv(parseItem(ret, args.weapon, "weapon", stats, args.weaponquantity, yesno(args.weaponClickable, false)))
    makeDiv(parseItem(ret, args.torso, "torso", stats, nil, yesno(args.torsoClickable, false)))
    makeDiv(parseItem(ret, args.shield, "shield", stats, nil, yesno(args.shieldClickable, false)))
    makeDiv(parseItem(ret, args.legs, "legs", stats, nil, yesno(args.legsClickable, false)))
    makeDiv(parseItem(ret, args.gloves, "gloves", stats, nil, yesno(args.glovesClickable, false)))
    makeDiv(parseItem(ret, args.hands, "gloves", stats, nil, yesno(args.handsClickable, false)))
    makeDiv(parseItem(ret, args.boots, "boots", stats, nil, yesno(args.bootsClickable, false)))
    makeDiv(parseItem(ret, args.ring, "ring", stats, nil, yesno(args.ringClickable, false)))
    
    if stats then
        -- Add stat-related code here if needed
    end

    return tbl:done()
end

return p