Module:Inventory: Difference between revisions

From Roat Pkz
Jump to navigation Jump to search
No edit summary
Tag: Reverted
No edit summary
Tags: Manual revert 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


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


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


function p.main(frame)
function p._main(items,align,bgType)
    local args = frame:getParent().args
     local className = bgType .. "table"
    local buttons = yesno(args.buttons, false)
     local ret = mw.html.create('table')
     local stats = yesno(args.stats, false)
        :addClass(className)
 
        :addClass(align)
    local align
     local item = 0
    if args.align == "right" then
     for i=1,7 do
        align = "equipment-right"
        local ret_row = ret:tag('tr')
    elseif args.align == "center" then
        for j=1,4 do
        align = "equipment-center"
             item = item + 1
    else
             local itemx = items[item].item
        align = "equipment-left"
             local amtx = items[item].amt
    end
             local amtx_f, amtx_c = formatAmount(amtx)
    -- general 'equipment' class for the whole thing
             local notex = items[item].isnoted
    align = "equipment " .. align
             local clickable_x = items[item].clickable
 
             local td = ret_row:tag('td')
     local tbl = mw.html.create("table"):addClass(align):tag("tr")
             if hasc(itemx) then
     local ret = tbl:tag("td"):tag("div")
                if clickable_x then
     if stats then
                    td:wikitext(string.format('[[File:%s.png|%s|link=%s|32x32px|frameless]]', itemx, itemx))
        stats = {
                else
            astab = 0,
                    td:wikitext(string.format('[[File:%s.png|link=|32x32px|frameless]]', itemx, itemx, itemx))
            aslash = 0,
                end
            acrush = 0,
                if notex then
             amagic = 0,
                    td:addClass('noted-item')
             arange = 0,
                end
             dstab = 0,
                if amtx > 1 or notex then
             dslash = 0,
                    td:tag('span')
            dcrush = 0,
                      :addClass('inv-quantity-text')
             dmagic = 0,
                      :addClass(amtx_c)
             drange = 0,
                      :wikitext(amtx_f)
             str = 0,
                end
            rstr = 0,
            end
            mdmg = 0,
         end
            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 tbl:done()
     return ret
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

function formatAmount(_x)
    local x = tonumber(_x) or 1
    if x < 100000 then
        return x, 'qty-1'
    elseif x < 10000000 then
        return tostring(math.floor(x/1000))..'K', 'qty-100k'
    else
        return tostring(math.floor(x/1000000))..'M', 'qty-10m'
    end
end

function p.main(frame)
    local bgType = frame.args.bgType
    if bgType == nil then
        bgType = "inventory"
    end
    local args = frame:getParent().args
    local items = {}
    for i=1,28 do
        local v = mw.text.trim(args[i] or '')
        local item_x
        local amt_x = 1
        local note_x = false
        local clickable_x = false -- Added line
        if hasc(v) then
            v = v:gsub('[][]','')
            amt_x = tonumber(v:match('\\(%d+)')) or 1
            note_x = v:match(';n') == ';n'
            clickable_x = v:match(';c=yes') == ';c=yes' or v:match(';c=true') == ';c=true' -- Modified line
            local v2 = mw.text.split(v,'[;\\]')
            item_x = v2[1]
        end
        table.insert(items,{item=item_x,amt=amt_x,isnoted=note_x,clickable=clickable_x}) -- Modified line
    end
    local align = args.align
    local acss
    if hasc(align) then
        align = align:lower()
        if align == 'right' then
            align = "storage-right"
        elseif align == 'left' then
            align = "storage-left"
        else
            align = "storage-center"
        end
    end
    return p._main(items,align,bgType)
end

function p._main(items,align,bgType)
    local className = bgType .. "table"
    local ret = mw.html.create('table')
        :addClass(className)
        :addClass(align)
    local item = 0
    for i=1,7 do
        local ret_row = ret:tag('tr')
        for j=1,4 do
            item = item + 1
            local itemx = items[item].item
            local amtx = items[item].amt
            local amtx_f, amtx_c = formatAmount(amtx)
            local notex = items[item].isnoted
            local clickable_x = items[item].clickable
            local td = ret_row:tag('td')
            if hasc(itemx) then
                if clickable_x then
                    td:wikitext(string.format('[[File:%s.png|%s|link=%s|32x32px|frameless]]', itemx, itemx))
                else
                    td:wikitext(string.format('[[File:%s.png|link=|32x32px|frameless]]', itemx, itemx, itemx))
                end
                if notex then
                    td:addClass('noted-item')
                end
                if amtx > 1 or notex then
                    td:tag('span')
                      :addClass('inv-quantity-text')
                      :addClass(amtx_c)
                      :wikitext(amtx_f)
                end
            end
        end
    end

    return ret
end

return p