Module:Products

From Brighter Shores Wiki
Revision as of 23:12, 22 November 2024 by The Gaffer (talk | contribs)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to navigation Jump to search

Documentation for this module may be created at Module:Products/doc

local p = {}

function p._main(frame)
    local args = frame:getParent().args
    args.item = mw.title.getCurrentTitle().text
    local mw = require('mw')
    
    local products = p.getProducts(args)
    local recipeNames = p.extractRecipeNames(products)
    local prodprices = p.getShopBuyPrices(products)

    -- Generate and return a table containing the product information
    local output = p.displayProductTable(prodprices)
    return output
end

function p.extractRecipeNames(products)
    local recipeNames = {}

    for _, recipeInfo in ipairs(products) do
        local recipeLink = recipeInfo["Recipe"]
        if recipeLink then
            local displayName = recipeLink:match("%[%[.-|(.+)%]%]")
            if displayName then
                table.insert(recipeNames, displayName)
            end
        end
    end
    return recipeNames
end

function p.getProducts(args)
    local item = mw.smw.ask('[[:+]][[Uses item::' .. args.item .. ']]|?Uses item |?Profession A |?Profession Level A|?Value |mainlabel=Recipe') or 0
    return item
end

function p.getShopBuyPrices(products)
    local mw = require('mw')

    for _, product in ipairs(products) do
        local usesItems = product["Uses item"]
        if type(usesItems) == "table" then
            for _, item in ipairs(usesItems) do
                -- Extract item name from the link
                local itemName = item:match("%[%[.-|(.+)%]%]") or item:match("%[%[(.-)%]%]")
                if itemName then
                    -- Query for the shop buy price of the item
                    local shopPriceQuery = '[[:+]][[Sold item::' .. itemName .. ']]|?Shop buy price|mainlabel=' .. itemName
                    local shopPriceResult = mw.smw.ask(shopPriceQuery) or {}
                    if shopPriceResult[1] and shopPriceResult[1]["Shop buy price"] then
                        product[itemName .. "_Shop_buy_price"] = shopPriceResult[1]["Shop buy price"]
                    else
                        product[itemName .. "_Shop_buy_price"] = "N/A"
                    end
                end
            end
        elseif type(usesItems) == "string" then
            -- Handle single uses item (not a table)
            local itemName = usesItems:match("%[%[.-|(.+)%]%]") or usesItems:match("%[%[(.-)%]%]")
            if itemName then
                -- Query for the shop buy price of the item
                local shopPriceQuery = '[[:+]][[Sold item::' .. itemName .. ']]|?Shop buy price|mainlabel=' .. itemName
                local shopPriceResult = mw.smw.ask(shopPriceQuery) or {}
                if shopPriceResult[1] and shopPriceResult[1]["Shop buy price"] then
                    product[itemName .. "_Shop_buy_price"] = shopPriceResult[1]["Shop buy price"]
                else
                    product[itemName .. "_Shop_buy_price"] = "N/A"
                end
            end
        end
    end
    return products
end

function p.displayProductTable(products)
	local currency = require('Module:Currency').parse
    local out = {}
    table.insert(out, '{| class="wikitable"')
    table.insert(out, '! Recipe !! Level !! Value !! Ingredients !! Price')

    for _, product in ipairs(products) do
        local recipe = product["Recipe"] or "Unknown"
        local professionName = product["Profession A"]:match("%[%[.-|(.+)%]%]") or product["Profession A"]:match("%[%[(.-)%]%]") or product["Profession A"] or "Unknown"
        local professionLevel = "[[File:" .. professionName .. " small icon.png|30px]]  " .. (product["Profession Level A"] or "Unknown")
        local usesItems = product["Uses item"] or {}
        local value = currency(product["Value"]) or "Unknown"

        -- Concatenate uses items and their shop buy prices in unordered lists
        local usesItemStr = "<ul style='list-style:none; margin:0; padding-left:0;'>"
        local shopBuyPriceStr = "<ul style='list-style:none; margin:0; padding-left:0; text-align:right;'>"

        if type(usesItems) == "table" then
            for _, item in ipairs(usesItems) do
                local itemName = item:match("%[%[.-|(.+)%]%]") or item:match("%[%[(.-)%]%]") or item
                usesItemStr = usesItemStr .. "<li>" .. item .. "</li>"
                local shopBuyPrice = product[itemName .. "_Shop_buy_price"] or 0
                if shopBuyPrice == "N/A" then shopBuyPrice = 0 end
                shopBuyPriceStr = shopBuyPriceStr .. "<li>" .. currency(shopBuyPrice) .. "</li>"
            end
        elseif type(usesItems) == "string" then
            usesItemStr = usesItemStr .. "<li>" .. usesItems .. "</li>"
            local itemName = usesItems:match("%[%[.-|(.+)%]%]") or usesItems:match("%[%[(.-)%]%]") or usesItems
            local shopBuyPrice = product[itemName .. "_Shop_buy_price"] or 0
            if shopBuyPrice == "N/A" then shopBuyPrice = 0 end
            shopBuyPriceStr = shopBuyPriceStr .. "<li>" .. currency(shopBuyPrice) .. "</li>"
        end

        usesItemStr = usesItemStr .. "</ul>"
        shopBuyPriceStr = shopBuyPriceStr .. "</ul>"

        table.insert(out, '|-')
        table.insert(out, '| ' .. recipe .. ' || ' .. professionLevel .. ' || ' .. value .. ' || ' .. usesItemStr .. ' || ' .. shopBuyPriceStr)
    end

    table.insert(out, '|}')
    return table.concat(out, '\n')
end

return p