Module:Augmenting Uses

From Idlescape Wiki
Revision as of 05:41, 3 July 2024 by HASH (talk | contribs) (Created)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to navigation Jump to search

local p = {}
local findId = require("Module:FindId")
local img = require("Module:Img")
local craftingAugmentingData = mw.loadData("Module:CraftingAugmenting/data")
local itemsData = mw.loadData("Module:Items/data")

local augmentationBonusNames ={
    ["toolBoost.mining"] = "[[mining]]",
    ["toolBoost.foraging"] = "[[foraging]]",
    ["offensiveAccuracyAffinityRating.Ice"] = "[[Combat#Offensive Stats|Ice Accuracy]]",
    ["offensiveAccuracyAffinityRating.Nature"] = "[[Combat#Offensive Stats|Nature Accuracy]]",
    ["offensiveAccuracyAffinityRating.Fire"] = "[[Combat#Offensive Stats|Fire Accuracy]]",
    ["offensiveAccuracyAffinityRating.Lightning"] = "[[Combat#Offensive Stats|Lightning Accuracy]]",
    ["offensiveAccuracyAffinityRating.Magic"] = "[[Combat#Offensive Stats|Magic Accuracy]]",
    ["toolBoost.farming"] = "[[Farming]]",
    ["toolBoost.smithing"] = "[[Smithing]]",
    ["toolBoost.runecrafting"] = "[[Runecrafting]]",
    ["toolBoost.cooking"] = "[[cooking]]",
    ["toolBoost.enchanting"] = "[[enchanting]]",
    ["toolBoost.fishing"] = "[[fishing]]",
    ["toolBoost.fishingBaitPower"] = "Bait",
    ["toolBoost.fishingRarityPower"] = "Bonus Rarity",
    ["toolBoost.fishingReelPower"] = "Reel",
    ["offensiveAccuracyAffinityRating.Chaos"] = [[Combat#Offensive Stats|Choas Accuracy]],
    ["weaponBonus.strength"] = "[[Combat#Offensive Stats|strength]]",
    ["weaponBonus.intellect"] = "[[Combat#Offensive Stats|intellect]]",
    ["weaponBonus.dexterity"] = "[[Combat#Offensive Stats|dexterity]]",
    ["offensiveAccuracyAffinityRating.Melee"] = "[[Combat#Offensive Stats|Melee Accuracy]]",
    ["offensiveCritical.chance"] = "[[Combat#Offensive Stats|Crit Chance]]",
    ["offensiveCritical.damageMultiplier"] = "[[Combat#Offensive Stats|Crit Mult]]",
    ["armorBonus.agility"] = "[[Combat#Defensive Stats|Agility]]",
    ["armorBonus.protection"] = "[[Combat#Defensive Stats|Protectino]]",
    ["armorBonus.stamina"] = "[[Combat#Defensive Stats|Stamina]]",
    ["armorBonus.resistance"] = "[[Combat#Defensive Stats|Resistance]]",
    ["offensiveAccuracyAffinityRating.Range"] = "[[Combat#Offensive Stats|Range Accuracy]]",
    ["offensiveAccuracyAffinityRating.Blunt"] = "[[Combat#Offensive Stats|Blunt Accuracy]]",
}

local pageName = mw.title.getCurrentTitle().fullText

local function pairsByKeys(t, f)
    local a = {}
    local orgi_key_type
    local orgi_key_numbered
    for n in pairs(t) do
        if tonumber(n) == nil then
            table.insert(a, n)
            orgi_key_type = "word"
        elseif type(n) == "number" then
            table.insert(a, n)
            orgi_key_type = "int"
        elseif type(n) == "string" and type(tonumber(n) == "number") then
            orgi_key_type = "number"
            table.insert(a, tonumber(n))
        end
    end
    table.sort(a, f)
    local key
    local value
    local i = 0             -- iterator variable
    local iter = function() -- iterator function
        i = i + 1
        if a[i] == nil then
            return nil
        elseif orgi_key_type == "word" or orgi_key_type == "int" then
            key = a[i]
            value = t[a[i]]
        elseif orgi_key_type == "number" then
            key = tostring(a[i])
            value = t[tostring(a[i])]
        end
        return key, value
    end
    return iter
end

local function tablelength(T)
    local count = 0
    for _ in pairs(T) do count = count + 1 end
    return count
end

---fetches an item object
---@param itemId integer|string or string
---@return table|string
local function getItem(itemId)
    local item = itemsData[tostring(itemId)]
    if item then
        return item
    else
        return "Module:Items/data out of date"
    end
end

local function genTable(name, collapse)
    local itemI = getItem(findId._findId({name, "item"}))
    local collapseS = ""
    if collapse == 1 then
        collapseS = " mw-made-collapsible mw-collapsible"
    elseif collapse == 2 then
        collapseS = " mw-made-collapsible mw-collapsible mw-collapsed"
    end
    local matchedAugs = {}
    for itemId, itemData in pairs(craftingAugmentingData) do
        if itemData.scrapping then
            for costId, _ in pairsByKeys(itemData.scrapping) do
                if tonumber(costId) == itemI.id then
                    matchedAugs[itemId] = itemData.scrapping
                end
            end
        end
    end
    local t = mw.html.create("table")
        t:addClass("wikitable sortable jquery-tablesorter" .. collapseS)
        :tag('tr')
            :tag('th')
                :attr("colspan", "2")
                :wikitext("Item")
            :done()
            :tag("th")
                :wikitext("Aug. Bonus.")
            :done()
            :tag("th")
                :wikitext("Materials")
            :done()
        :done()
    for itemId, itemData in pairs(matchedAugs) do
        local item = getItem(itemId)
        local dTr = t:tag('tr')
            :tag('td')
                :wikitext(img._img({item.name, '40'}))
            :done()
            :tag('td')
                :wikitext("[[" .. item.name .. "]]")
            :done()
        local augBonusS = ""
        if item.equipmentStats.augmentationBonus then
            local augBonusC = tablelength(item.equipmentStats.augmentationBonus)
            for _, augBonus in ipairs(item.equipmentStats.augmentationBonus) do
                augBonusS = augBonusS .. augmentationBonusNames[augBonus.stat] .. " +" .. augBonus.value
                augBonusC = augBonusC - 1
                if augBonusC > 0 then
                    augBonusS = augBonusS .. "<br>"
                end
            end
        elseif not item.equipmentStats.augmentationBonus then
            augBonusS = "-"
        end
        dTr:tag('td')
            :css('text-align', 'center')
            :wikitext(augBonusS)
        :done()
        local costCell = dTr:tag('td')
        local augCostC = tablelength(itemData)
        for costId, costQuantity in pairs(itemData) do
            local costItem = getItem(costId)
            if tonumber(costId) == itemI.id then
                costCell:tag('span'):css('display', 'inline-block')
                    :wikitext(tostring(costQuantity) .. " " .. img._img({costItem.name, '20'}))
                    :tag('span'):css('font-weight', 'bold')
                        :wikitext(costItem.name)
                    :done()
                :done()
                augCostC = augCostC - 1
                if augCostC > 0  then
                    costCell:tag('br')
                end
            else
                costCell:tag('span'):css('display', 'inline-block')
                    :wikitext(tostring(costQuantity) .. " " .. img._img({costItem.name, '20', word = 1}))
                :done()
            augCostC = augCostC - 1
            if augCostC > 0  then
                costCell:tag('br')
            end
            end
        end
        dTr:done()
    end
    return t
end

function p.augmentingUses(frame)
    return p._augmentingUses(frame:getParent().args)
end

function p._craftingUses(_args)
    local name = ""
    if tablelength(_args) == 0 then
        name = pageName
    else
        name = _args[1]
    end
    local collapse = 0
    if _args["collapse"] == "collapseable" then
        collapse = 1
    elseif _args["collapse"] == "collapsed" then
        collapse =2
    end
    local t = genTable(name, collapse)

    return t
end
return p