Module:Products: Difference between revisions

Jump to navigation Jump to search
Content added Content deleted
(x -> × simplify single value / table of values implementation; allow quantity to have a decimal point (like when it's "0.05"))
m (Spaces to tabs)
Line 1: Line 1:
local currency = require('Module:Currency').parse

local p = {}
local p = {}


Line 7: Line 9:
function p._main(args)
function p._main(args)
args = args or {}
args = args or {}
args.item = args.item or mw.title.getCurrentTitle().text
args.item = args.item or mw.title.getCurrentTitle().text
local showPrices = args.showPrices or false
local showPrices = args.showPrices or false
local showValues = args.showValues or false
local showValues = args.showValues or false
local products = p.getProducts(args)
local products = p.getProducts(args)
if products ~= 0 then
if products ~= 0 then
local recipeNames = p.extractRecipeNames(products)
local recipeNames = p.extractRecipeNames(products)
local prodprices = p.getShopBuyPrices(products)
local prodprices = p.getShopBuyPrices(products)
-- Generate and return a table containing the product information
-- Generate and return a table containing the product information
local output = p.displayProductTable(prodprices, showPrices, showValues)
local output = p.displayProductTable(prodprices, showPrices, showValues)
return output
return output
else
else
return "There are no known products for item '''args.item'''"
return "There are no known products for item '''args.item'''"
end
end
end
end


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


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


function p.getShopBuyPrices(products)
function p.getShopBuyPrices(products)
for _, product in ipairs(products) do
local mw = require('mw')
local usesItems = product["Uses item and quantity"]
if type(usesItems) == "string" then
-- Single value
usesItems = { usesItems }
end
for _, item in ipairs(usesItems) do
-- Extract item name and quantity from the "item,#" format
local itemName, quantity = item:match("^([^,]+),([0-9.]+)$")
quantity = tonumber(quantity) or 1 -- Default to 1 if quantity is missing


if itemName then
for _, product in ipairs(products) do
-- Query for the shop buy price of the item
local usesItems = product["Uses item and quantity"]
local shopPriceQuery = '[[:+]][[Sold item::' .. itemName .. ']]|?Shop buy price|mainlabel=' .. itemName
if type(usesItems) == "string" then
local shopPriceResult = mw.smw.ask(shopPriceQuery) or {}
-- Single value
local shopPrice = 0
usesItems = { usesItems }
end
for _, item in ipairs(usesItems) do
-- Extract item name and quantity from the "item,#" format
local itemName, quantity = item:match("^([^,]+),([0-9.]+)$")
quantity = tonumber(quantity) or 1 -- Default to 1 if quantity is missing


if shopPriceResult[1] and shopPriceResult[1]["Shop buy price"] then
if itemName then
shopPrice = tonumber(shopPriceResult[1]["Shop buy price"]) or 0
-- Query for the shop buy price of the item
end
local shopPriceQuery = '[[:+]][[Sold item::' .. itemName .. ']]|?Shop buy price|mainlabel=' .. itemName
local shopPriceResult = mw.smw.ask(shopPriceQuery) or {}
local shopPrice = 0


-- Multiply the price by the quantity
if shopPriceResult[1] and shopPriceResult[1]["Shop buy price"] then
local totalPrice = shopPrice * quantity
shopPrice = tonumber(shopPriceResult[1]["Shop buy price"]) or 0
end


-- Update the product with the formatted string and total price
-- Multiply the price by the quantity
product[itemName .. "_Shop_buy_price"] = totalPrice > 0 and totalPrice or "N/A"
local totalPrice = shopPrice * quantity
product[itemName .. "_Formatted"] = tostring(quantity) .. " × " .. itemName

end
-- Update the product with the formatted string and total price
end
product[itemName .. "_Shop_buy_price"] = totalPrice > 0 and totalPrice or "N/A"
end
product[itemName .. "_Formatted"] = tostring(quantity) .. " × " .. itemName
return products
end
end
end
return products
end
end


function p.displayProductTable(products, showPrices, showValues)
function p.displayProductTable(products, showPrices, showValues)
local out = {}
local currency = require('Module:Currency').parse
table.insert(out, '{| class="wikitable"')
local out = {}

table.insert(out, '{| class="wikitable"')
local headerRow = {}
local headerRow = {}
table.insert(headerRow, '!colspan="2" | Recipe')
table.insert(headerRow, '!colspan="2" | Recipe')
table.insert(headerRow, '!! Level')
table.insert(headerRow, '!! Level')


if showValues then
if showValues then
table.insert(headerRow, '!! Value')
table.insert(headerRow, '!! Value')
end
end


Line 93: Line 92:


if showPrices then
if showPrices then
table.insert(headerRow, '!! Price')
table.insert(headerRow, '!! Price')
end
end


table.insert(out, table.concat(headerRow, ' '))
table.insert(out, table.concat(headerRow, ' '))


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


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


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


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


table.insert(out, '|-')
table.insert(out, '|-')

local valuesRow = {}
local valuesRow = {}

table.insert(valuesRow, '| ' .. recipeImage)
table.insert(valuesRow, '| ' .. recipeImage)
table.insert(valuesRow, '|| ' .. recipe)
table.insert(valuesRow, '|| ' .. recipe)
table.insert(valuesRow, '|| ' .. professionLevel)
table.insert(valuesRow, '|| ' .. professionLevel)


if showValues then
if showValues then
table.insert(valuesRow, '|| ' .. value)
table.insert(valuesRow, '|| ' .. value)
end
end


Line 145: Line 144:


if showPrices then
if showPrices then
table.insert(valuesRow, '|| ' .. shopBuyPriceStr)
table.insert(valuesRow, '|| ' .. shopBuyPriceStr)
end
end


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



table.insert(out, '|}')
--table.insert(out, '| ' .. recipe .. ' || ' .. professionLevel .. ' || ' .. value .. ' || ' .. usesItemStr .. ' || ' .. shopBuyPriceStr)
return table.concat(out, '\n')
end

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