Module:Crafting Uses

From Idlescape Wiki
Revision as of 14:21, 1 July 2024 by HASH (talk | contribs)
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 function tablelength(T)
	local count = 0
	for _ in pairs(T) do count = count + 1 end
	return count
end

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 getItem(itemId)
    local item = itemsData[tostring(itemId)]
    if item then
        return item
    else
        return "Module:Items/data out of date"
    end
end

function p.itemCraftingUses(frame)
    return p._itemCraftingUses(frame:getParent().args)
end

function p._itemCraftingUses(_args)
    local pageId = findId._findId({pageName, "item"})
    local t = mw.html.create("table")
        t:addClass("wikitable")
        t:tag("tr")
            :tag("th")
                :attr("colspan", "2")
                :wikitext("Item Created")
            :done()
            :tag("th")
                :wikitext("Category")
            :done()
            :tag("th")
                :wikitext("Lvl.</br>Req.")
            :done()
            :tag("th")
                :wikitext("XP")
            :done()
            :tag("th")
                :wikitext("Materials")
            :done()

    for itemId, iData in pairsByKeys(craftingAugmentingData) do
        local matchedRecipes ={}
        if iData.crafting then
            for _, r in ipairs(iData.crafting) do
                for mId, _ in pairsByKeys(r.recipe) do
                    if tonumber(mId) == pageId then
                        mw.log=(1)
                        table.insert(matchedRecipes, r.recipe)
                    end
                end
            end
            if tablelength(matchedRecipes) == 1 then
                local item = getItem(itemId)
                local nameS = ""
                if item.craftingStats.multiplier then
                    nameS = tostring(item.craftingStats.multiplier) .. "x "
                end
                local matS = ""
                for mId, mQuantity in pairsByKeys(matchedRecipes[1]) do
                    local mat = getItem(mId)
                    matS = matS .. tostring(mQuantity) .. img._img({mat.name, '20', word=1}) .. "</br>"
                end
                t:tag('tr')
                    :tag('td')
                        :wikitext(img._img({item.name, "40"}))
                    :done()
                    :tag('td')
                        :wikitext(nameS .. "[["..item.name.."]]")
                    :done()
                    :tag('td')
                        :wikitext(item.craftingStats.category)
                    :done()
                    :tag('td')
                        :wikitext(tostring(item.craftingStats.level))
                    :done()
                    :tag('td')
                        :wikitext(tostring(item.craftingStats.experience))
                    :done()
                    :tag('td')
                        :addClass('plainlist')
                        :wikitext(matS)
                :allDone()
            elseif tablelength(matchedRecipes) > 1 then
                local recipeCount = tablelength(matchedRecipes)
                local item = getItem(itemId)
                local nameS = ""
                if item.craftingStats.multiplier then
                    nameS = tostring(item.craftingStats.multiplier) .. "x "
                end
                local matS = ""
                for mId, mQuantity in pairsByKeys(table.remove(matchedRecipes, 1)) do
                    local mat = getItem(mId)
                    matS = matS .. tostring(mQuantity) .. img._img({mat.name, '20', word=1}) .. "</br>"
                end
                t:tag('tr')
                    :tag('td')
                        :attr("rowspan", tostring(recipeCount))
                        :wikitext(img._img({item.name, "40"}))
                    :done()
                    :tag('td')
                        :attr("rowspan", tostring(recipeCount))
                        :wikitext(nameS .. "[["..item.name.."]]")
                    :done()
                    :tag('td')
                        :attr("rowspan", tostring(recipeCount))
                        :wikitext(item.craftingStats.category)
                    :done()
                    :tag('td')
                        :attr("rowspan", tostring(recipeCount))
                        :wikitext(tostring(item.craftingStats.level))
                    :done()
                    :tag('td')
                        :attr("rowspan", tostring(recipeCount))
                        :wikitext(tostring(item.craftingStats.experience))
                    :done()
                    :tag('td')
                        :wikitext(matS)
                    :done()
                for _, recipe in ipairs(matchedRecipes) do
                    matS = ""
                    for mId, mQuantity in pairsByKeys(recipe) do
                        local mat = getItem(mId)
                        matS = matS .. tostring(mQuantity) .. img._img({mat.name, '20', word=1}) .. "</br>"
                    end
                    t:tag('tr')
                        :tag('td')
                            :wikitext(matS)
                    :allDone()
                end
            end
        end
    end
    return t
end

return p