Module:Products: Difference between revisions

Jump to navigation Jump to search
Content added Content deleted
No edit summary
No edit summary
Line 1: Line 1:
local p = {}
local p = {}
local purge = require('Module:Purge')._purge


function p.main(frame)
function p.main(frame)
Line 9: Line 8:
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 showValues = args.showValues or false
local products = p.getProducts(args)
local products = p.getProducts(args)
if products ~= 0 then
if products ~= 0 then
Line 15: Line 15:
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)
local output = p.displayProductTable(prodprices, showPrices, showValues)
return output
return output
else
else
return ":''No products found. To force an update, click "
return "There are no known products for item '''args.item'''"
..purge('dml-'..mw.uri.anchorEncode(args.item), 'here', 'span')
..".''[[Category:Empty products lists]]"
end
end
end
end
Line 26: Line 24:
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"]
Line 40: Line 37:


function p.getProducts(args)
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
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
Line 48: Line 45:


for _, product in ipairs(products) do
for _, product in ipairs(products) do
local usesItems = product["Uses item"]
local usesItems = product["Uses item and quantity"]
if type(usesItems) == "table" then
if type(usesItems) == "table" then
for _, item in ipairs(usesItems) do
for _, item in ipairs(usesItems) do
-- Extract item name from the link
-- Extract item name and quantity from the "item,#" format
local itemName = item:match("%[%[.-|(.+)%]%]") or item:match("%[%[(.-)%]%]")
local itemName, quantity = item:match("([^,]+),(%d+)")
quantity = tonumber(quantity) or 1 -- Default to 1 if quantity is missing

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

if shopPriceResult[1] and shopPriceResult[1]["Shop buy price"] then
if shopPriceResult[1] and shopPriceResult[1]["Shop buy price"] then
product[itemName .. "_Shop_buy_price"] = shopPriceResult[1]["Shop buy price"]
shopPrice = tonumber(shopPriceResult[1]["Shop buy price"]) or 0
else
product[itemName .. "_Shop_buy_price"] = "N/A"
end
end

-- Multiply the price by the quantity
local totalPrice = shopPrice * quantity

-- Update the product with the formatted string and total price
product[itemName .. "_Shop_buy_price"] = totalPrice > 0 and totalPrice or "N/A"
product[itemName .. "_Formatted"] = quantity .. " x " .. itemName
end
end
end
end
elseif type(usesItems) == "string" then
elseif type(usesItems) == "string" then
-- Handle single uses item (not a table)
-- Handle single uses item_and_quantity (not a table)
local itemName = usesItems:match("%[%[.-|(.+)%]%]") or usesItems:match("%[%[(.-)%]%]")
local itemName, quantity = usesItems:match("([^,]+),(%d+)")
quantity = tonumber(quantity) or 1 -- Default to 1 if quantity is missing

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

if shopPriceResult[1] and shopPriceResult[1]["Shop buy price"] then
if shopPriceResult[1] and shopPriceResult[1]["Shop buy price"] then
product[itemName .. "_Shop_buy_price"] = shopPriceResult[1]["Shop buy price"]
shopPrice = tonumber(shopPriceResult[1]["Shop buy price"]) or 0
else
product[itemName .. "_Shop_buy_price"] = "N/A"
end
end

-- Multiply the price by the quantity
local totalPrice = shopPrice * quantity

-- Update the product with the formatted string and total price
product[itemName .. "_Shop_buy_price"] = totalPrice > 0 and totalPrice or "N/A"
product[itemName .. "_Formatted"] = quantity .. " x " .. itemName
end
end
end
end
Line 82: Line 97:
end
end


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

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

table.insert(headerRow, '!! Ingredients')

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

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


for _, product in ipairs(products) do
for _, product in ipairs(products) do
local recipeRawText = product["Recipe"]:match("%[%[.-|(.+)%]%]")
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|x30px]] " .. (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"
Line 102: Line 134:
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>" .. item .. "</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
Line 108: Line 140:
end
end
elseif type(usesItems) == "string" then
elseif type(usesItems) == "string" then
usesItemStr = usesItemStr .. "<li>" .. usesItems .. "</li>"
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>"
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
Line 119: Line 151:


table.insert(out, '|-')
table.insert(out, '|-')
table.insert(out, '| ' .. recipe .. ' || ' .. professionLevel .. ' || ' .. value .. ' || ' .. usesItemStr .. ' || ' .. shopBuyPriceStr)
local valuesRow = {}
table.insert(valuesRow, '| ' .. recipeImage)
table.insert(valuesRow, '|| ' .. recipe)
table.insert(valuesRow, '|| ' .. professionLevel)

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

table.insert(valuesRow, '|| ' .. usesItemStr)

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

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