Module:Augmenting Uses

From Idlescape Wiki
Revision as of 15:18, 3 July 2024 by HASH (talk | contribs) (more bugs)
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 pageName = mw.title.getCurrentTitle().fullText
local scrappingBaseXp = 20
local scrappingXpPower = 2


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|Protection]]",
    ["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 tierToDust = {
    [1] = "basic",
    [2] = "basic",
    [3] = "unusual",
    [4] = "singular",
    [5] = "prime",
    [6] = "mythical",
    [7] = "mythical",
    [7.5] = "mythical"
}

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

local function getItemTier(itemData)
    local highestRequiredLevel
    if itemData.requiredLevel then
        -- Highest level of the required skills
        highestRequiredLevel = 0
        for _, level in pairs(itemData.requiredLevel) do
            if level > highestRequiredLevel then
                highestRequiredLevel = level
            end
        end
        highestRequiredLevel = tonumber(string.format("%.1f", highestRequiredLevel / 10))
    end
    local itemTier = itemData.overrideItemTier or highestRequiredLevel or itemData.enchantmentTier or 1
    return itemTier
end

local function getScrapExperience(itemData)
    local item_tier = getItemTier(itemData)
    local xp = scrappingBaseXp * (item_tier ^ scrappingXpPower)
    return math.floor(xp)
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
---gets the augments that have the itemId in thier augmenting cost and returns an array of them
---@param itemId number
---@return table
local function getMatchedAugs(itemId)
    local matchedAugs = {}
    for id, data in pairs(craftingAugmentingData) do
        if data.scrapping then
            for costId, _ in pairsByKeys(data.scrapping) do
                if tonumber(costId) == itemId then
                    matchedAugs[id] = data.scrapping
                end
            end
        end
    end
    return matchedAugs
end
---decides the collase state based on the given string
---@param collapse string
---@return string
local function decideCollapseState(collapse)
    if collapse == "collapsible" then
        collapse = " mw-made-collapsible mw-collapsible"
    elseif collapse == "collapsed" then
        collapse = " mw-made-collapsible mw-collapsible mw-collapsed"
    else
        collapse = ""
    end
    return collapse
end

local function getItemDust(tier)
    if tier <= 2 then
        return "Basic Runic Dust"
    elseif tier <= 3 then
        return "Unusual Runic Dust"
    elseif tier <= 4 then
        return "Singular Runic Dust"
    elseif tier <= 5 then
        return "Prime Runic Dust"
    elseif tier > 5 then
        return "Mythical Runic Dust"
    end
end

local function getItemScrap(item)
    if item.rarity == "common" then
        return "Common Gear Scraps"
    elseif item.rarity == "uncommon" then
        return "Uncommon Gear Scraps"
    elseif item.rarity == "rare" then
        return "Rare Gear Scraps"
    elseif item.rarity == "epic" then
        return "Epic Gear Scraps"
    elseif item.rarity == "legendary" then
        return "Legendary Gear Scraps"
    end
end

local function createImgCell(item)
    local imgCell = mw.html.create('td')
    :wikitext(img._img({item.name, '50'}))
    :done()
    return imgCell
end

local function createNameCell(item)
    local nameCell = mw.html.create('td')
    :wikitext("[[" .. item.name .. "]]")
    :done()
    return nameCell
end

local function createXpCell(item)
    local xpCell = mw.html.create('td')
    :css('text-align', 'center')
    :wikitext(tostring(getScrapExperience(item)))
    :done()
    return xpCell
end

local function createAugBonusCell(item)
    local augBonusCell = mw.html.create('td')
    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

    augBonusCell
    :css('text-align', 'center')
    :wikitext(augBonusS)
    :done()

    return augBonusCell
end

local function createCostCell(mItemData, item)
    local costCell = mw.html.create('td')
    local augCostC = tablelength(mItemData)
    for costId, costQuantity in pairsByKeys(mItemData) do
        local costItem = getItem(costId)
        if tonumber(costId) == item.id then
            costCell:tag('span'):css('display', 'inline-block')
                :wikitext(tostring(costQuantity) .. " " .. img._img({costItem.name, '25'}))
                :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, '25', word = 1}))
            :done()
            augCostC = augCostC - 1
            if augCostC > 0  then
                costCell:tag('br')
            end
        end
    end
    return costCell
end

local function createDustCell(item)
    local itemTier = getItemTier(item)
    local dustCell = mw.html.create('td')
    :wikitext(img._img({getItemDust(itemTier), '25', word = 1}))
    :done()
    return dustCell
end

local function createScrapCell(item)
    local scrapCell = mw.html.create('td')
    :wikitext(img._img({getItemScrap(item), '25', word = 1}))
    :done()
    return scrapCell
end

local function genTable(name, collapse)
    local item = getItem(findId._findId({name, "item"}))
    local matchedAugs = getMatchedAugs(item.id)
    local t = mw.html.create("table")
        :addClass("wikitable sortable jquery-tablesorter" .. collapse)
        :tag('tr')
            :tag('th')
                :attr("colspan", "2")
                :wikitext("Item")
            :done()
            :tag('th')
                :wikitext("XP")
            :done()
            :tag('th')
                :wikitext("Dust")
            :done()
            :tag('th')
                :wikitext("Scrap")
            :done()
            :tag("th")
                :wikitext("Aug. Bonus")
            :done()
            :tag("th")
                :wikitext("Cost")
            :done()
        :done()
    for mItemId, mItemData in pairs(matchedAugs) do
        local mItem = getItem(mItemId)
        t:tag('tr')
            :node(createImgCell(mItem))
            :node(createNameCell(mItem))
            :node(createXpCell(mItem))
            :node(createDustCell(mItem))
            :node(createScrapCell(mItem))
            :node(createAugBonusCell(mItem))
            :node(createCostCell(mItemData, item))
        :done()
    end
    return t
end

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

function p._augmentingUses(_args)
    local name = ""
    if tablelength(_args) == 0 then
        name = pageName
    elseif _args["collapse"] and not _args[1] then
        name = pageName
    elseif _args["collapse"] and _args[1] then
        name = _args[2]
    else
        name = _args[1]
    end
    local t = genTable(name, decideCollapseState(_args["collapse"]))
    return t
end
return p