Module:Products: Difference between revisions

1,674 bytes removed ,  30 November 2024
Rewrite to use Property:Recipe JSON and mw.html; show quantity of output
m (Spaces to tabs)
(Rewrite to use Property:Recipe JSON and mw.html; show quantity of output)
Line 1:
require('Module:Mw.html extension')
local currency = require('Module:Currency').parse
local yesno = require('Module:Yesno')
 
local p = {}
Line 10 ⟶ 12:
args = args or {}
args.item = args.item or mw.title.getCurrentTitle().text
local showPrices = yesno(args.showPrices or false)
local showValues = yesno(args.showValues or false)
 
local products = p.getProducts(args)
-- Query for data
if products ~= 0 then
local smw_data = mw.smw.ask{
local recipeNames = p.extractRecipeNames(products)
'[[Uses item::' .. args.item .. ']]',
local prodprices = p.getShopBuyPrices(products)
'?Uses item',
-- Generate and return a table containing the product information
'?Recipe JSON',
local output = p.displayProductTable(prodprices, showPrices, showValues)
showValues and '?Value'
return output
}
else
if not smw_data then
return "There are no known products for item '''args.item'''"
return ('There are no known products for item [[%s]]'):format(args.item)
end
end
 
local recipes = {}
function p.extractRecipeNames(products)
for _, product in ipairs(smw_data) do
local recipeNames = {}
if type(product['Recipe JSON']) == 'string' then
for _, recipeInfo in ipairs(products) do
local product['Recipe recipeLinkJSON'] = recipeInfo{ product["'Recipe" JSON'] }
end
if recipeLink then
for _, json in ipairs(product['Recipe JSON']) do
local displayName = recipeLink:match("%[%[.-|(.+)%]%]")
json = mw.text.jsonDecode(json)
if displayName then
json.Value = product.Value
table.insert(recipeNames, displayName)
table.insert(recipes, json)
end
end
end
return recipeNames
end
 
-- Calculate shop prices
function p.getProducts(args)
if showPrices then
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 price_cache = {}
return item
for _, recipe in ipairs(recipes) do
end
for _, item in ipairs(recipe.materials) do
 
-- Extract item name and quantity from the "item,#" format
function p.getShopBuyPrices(products)
for _, product in ipairs(products) do
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
-- Query for the shop buy price of the item
local shopPriceResult = price_cache[item.name] or mw.smw.ask{
local shopPriceQuery = '[[:+]][[Sold item::' .. itemName .. ']]|?Shop buy price|mainlabel=' .. itemName
'[[Sold item::' .. item.name .. ']]',
local shopPriceResult = mw.smw.ask(shopPriceQuery) or {}
local '?Shop shopPricebuy = 0price'
} or {}
price_cache[item.name] = shopPriceResult
local shopPrice
 
if shopPriceResult[1] and shopPriceResult[1]["Shop buy price"] then
shopPrice = tonumber(shopPriceResult[1]["'Shop buy price"'] or 0) or 0
end
 
-- MultiplyUpdate the priceproduct bywith the quantitytotal price
localif totalPriceshopPrice ~= shopPrice *nil quantitythen
item.price = shopPrice * item.quantity
 
end
-- 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"] = tostring(quantity) .. " × " .. itemName
end
end
end
return products
end
 
-- Create table
function p.displayProductTable(products, showPrices, showValues)
local out = {}mw.html.create('table')
table.insert :addClass(out, '{| class="wikitable"')
:tag('tr')
:tag('th')
:attr{ colspan = '3' }
:wikitext('Recipe')
:done()
:tag('th'):wikitext('Level'):done()
:IF(showValues)
:tag('th'):wikitext('Value'):done()
:END()
:tag('th'):wikitext('Ingredients'):done()
:IF(showPrices)
:tag('th'):wikitext('Price'):done()
:END()
:done()
 
for _, recipe in ipairs(recipes) do
local headerRow = {}
local row = out:tag('tr')
table.insert(headerRow, '!colspan="2" | Recipe')
:tag('td')
table.insert(headerRow, '!! Level')
:css{ ['border-right'] = '0', ['padding-right'] = '0' }
:wikitext(recipe.output[1].quantity .. ' ×')
:done()
:tag('td')
:addClass('plinkt-image no-border')
:css{ ['border-left'] = '0' }
:wikitext('[[File:' .. recipe.output[1].name .. '.png|30px]]')
:done()
:tag('td')
:addClass('plinkt-link no-border')
:wikitext('[[' .. recipe.output[1].name .. ']]')
:done()
:tag('td')
:IF(recipe.profession)
:wikitext(('[[File:%s small icon.png|15px|link=%s]] %s'):format(recipe.profession, recipe.profession, recipe.level or 'Unknown'))
:ELSE()
:wikitext(('[[FileUnknown profession small icon.png|15px|link=Professions]] %s'):format(recipe.level or 'Unknown'))
:END()
:done()
 
if showValues then
if recipe.Value then
table.insert(headerRow, '!! Value')
row:tag('td')
end
:wikitext(currency(recipe.Value))
 
:done()
table.insert(headerRow, '!! Ingredients')
else
 
row:tag('td'):wikitext('Unknown'):done()
if showPrices then
table.insert(headerRow, '!! Price')
end
 
table.insert(out, table.concat(headerRow, ' '))
 
for _, product in ipairs(products) do
local recipeRawText = product["Recipe"]:match("%[%[.-|(.+)%]%]")
local recipeImage = "[[File:" .. recipeRawText .. ".png|30px]]"
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|15px]] " .. (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>" .. product[itemName .. "_Formatted"] .. "</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
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
if shopBuyPrice == "N/A" then shopBuyPrice = 0 end
shopBuyPriceStr = shopBuyPriceStr .. "<li>" .. currency(shopBuyPrice) .. "</li>"
end
 
local ingredients = row:tag('td')
usesItemStr = usesItemStr .. "</ul>"
:tag('ul')
shopBuyPriceStr = shopBuyPriceStr .. "</ul>"
:css{ ['list-style'] = 'none', ['margin'] = '0', ['padding-left'] = '0' }
 
for _, item in ipairs(recipe.materials) do
table.insert(out, '|-')
ingredients:tag('li')
 
:wikitext(('%s &times; [[File:%s.png|link=%s|30px]] [[%s]]'):format(item.quantity, item.name, item.name, item.name))
local valuesRow = {}
:done()
 
table.insert(valuesRow, '| ' .. recipeImage)
table.insert(valuesRow, '|| ' .. recipe)
table.insert(valuesRow, '|| ' .. professionLevel)
 
if showValues then
table.insert(valuesRow, '|| ' .. value)
end
 
if showPrices then
table.insert(valuesRow, '|| ' .. usesItemStr)
local prices = row:tag('td')
:tag('ul')
:css{ ['list-style'] = 'none', ['margin'] = '0', ['padding-left'] = '0' }
 
for _, item in ipairs(recipe.materials) do
if showPrices then
if item.price then
table.insert(valuesRow, '|| ' .. shopBuyPriceStr)
prices:tag('li'):wikitext(currency(item.price)):done()
else
prices:tag('li'):wikitext('Unknown'):done()
end
end
end
 
table.insert(out, table.concat(valuesRow, ' '))
 
 
--table.insert(out, '| ' .. recipe .. ' || ' .. professionLevel .. ' || ' .. value .. ' || ' .. usesItemStr .. ' || ' .. shopBuyPriceStr)
end
 
return out
table.insert(out, '|}')
return table.concat(out, '\n')
end
 
1,525

edits