Editing Module:Products
Jump to navigation
Jump to search
The edit can be undone. Please check the comparison below to verify that this is what you want to do, and then publish the changes below to finish undoing the edit.
Latest revision | Your text | ||
Line 1: | Line 1: | ||
local p = {} |
|||
require('Module:Mw.html extension') |
|||
local Array = require('Module:Array') |
|||
local currency = require('Module:Currency').parse |
|||
local yesno = require('Module:Yesno') |
|||
local purge = require('Module:Purge')._purge |
local purge = require('Module:Purge')._purge |
||
local p = {} |
|||
function p.main(frame) |
function p.main(frame) |
||
return p._main(frame:getParent().args) |
return p._main(frame:getParent().args) |
||
end |
|||
local function recipe_sort(recipe_a, recipe_b) |
|||
--if one is nil but not both, put nil levels after known levels |
|||
if (recipe_a.level == nil) ~= (recipe_b.level == nil) then |
|||
return recipe_b.level == nil |
|||
end |
|||
-- if both are nil, sort by name |
|||
if recipe_a.level == nil then |
|||
return recipe_a.output[1].name < recipe_b.output[1].name |
|||
end |
|||
-- if neither is nil, sort first by level |
|||
if recipe_a.level ~= recipe_b.level then |
|||
return recipe_a.level < recipe_b.level |
|||
end |
|||
-- if neither is nil and levels are the same, sort by name |
|||
return recipe_a.output[1].name < recipe_b.output[1].name |
|||
end |
end |
||
function p._main(args) |
function p._main(args) |
||
args = args or {} |
args = args or {} |
||
args.item = args.item or mw.title.getCurrentTitle().text |
|||
local showPrices = yesno(args.showPrices) |
|||
local products = p.getProducts(args) |
|||
if products ~= 0 then |
|||
local limit = tonumber(args.limit or 0) or 0 |
|||
local recipeNames = p.extractRecipeNames(products) |
|||
if limit <= 0 then |
|||
local prodprices = p.getShopBuyPrices(products) |
|||
limit = 500 |
|||
-- Generate and return a table containing the product information |
|||
end |
|||
local output = p.displayProductTable(prodprices) |
|||
return output |
|||
-- Query for data |
|||
else |
|||
local smw_data = mw.smw.ask{ |
|||
return ":''No products found. To force an update, click " |
|||
'[[Uses item::' .. item .. ']] OR [[Activity input::' .. item .. ']] OR [[Activity container::' .. item .. ']]', |
|||
..purge('dml-'..mw.uri.anchorEncode(args.item), 'here', 'span') |
|||
'?Recipe JSON', |
|||
'?Activity JSON', |
|||
showValues and '?Value', |
|||
limit = limit |
|||
} |
|||
if not smw_data then |
|||
return ":''No products found. To force an update, click " |
|||
..purge('dml-'..mw.uri.anchorEncode(item), 'here', 'span') |
|||
..".''[[Category:Empty products lists]]" |
..".''[[Category:Empty products lists]]" |
||
end |
|||
end |
|||
function p.extractRecipeNames(products) |
|||
-- Create a list of all recipes that use this item |
|||
local recipeNames = {} |
|||
for _, product in ipairs(smw_data) do |
|||
-- this part will need to be addressed if a page has both a recipe and an activity |
|||
local jsons = product['Recipe JSON'] or product['Activity JSON'] |
|||
if type(jsons) == 'string' then |
|||
jsons = { jsons } |
|||
end |
|||
for _, json in ipairs(jsons) do |
|||
local json = mw.text.jsonDecode(json) |
|||
json.Value = product.Value |
|||
-- Filter out when this item isn't actually used (Will happen on pages with multiple recipes) |
|||
if Array.any(json.materials, function(mat) |
|||
return mat.name == item |
|||
end) then |
|||
table.insert(recipes, json) |
|||
end |
|||
end |
|||
end |
|||
for _, recipeInfo in ipairs(products) do |
|||
-- Sort list of recipes by the level of the recipe (cross-profession) |
|||
local recipeLink = recipeInfo["Recipe"] |
|||
table.sort(recipes, recipe_sort) |
|||
if recipeLink then |
|||
local displayName = recipeLink:match("%[%[.-|(.+)%]%]") |
|||
if displayName then |
|||
table.insert(recipeNames, displayName) |
|||
end |
|||
end |
|||
end |
|||
return recipeNames |
|||
end |
|||
function p.getProducts(args) |
|||
-- if recipe is passive, want per hour |
|||
local item = mw.smw.ask('[[:+]][[Uses item::' .. args.item .. ']]|?Uses item |?Profession A |?Profession Level A|?Value |mainlabel=Recipe') or 0 |
|||
for _, recipe in ipairs(recipes) do |
|||
return item |
|||
recipe.recipePerHour = recipe.duration and math.floor(3600/recipe.duration) |
|||
end |
|||
-- replace quantities with per-hour quantities |
|||
-- if recipe.passive then |
|||
for _,item in ipairs(recipe.output) do |
|||
item.displayQuantity = (item.quantity and recipe.recipePerHour and item.quantity * recipe.recipePerHour) or '' |
|||
end |
|||
for _,item in ipairs(recipe.materials) do |
|||
item.displayQuantity = (item.quantity and recipe.recipePerHour and item.quantity * recipe.recipePerHour) or '' |
|||
end |
|||
-- end |
|||
end |
|||
-- Calculate shop prices |
|||
if showPrices then |
|||
local price_cache = {} |
|||
for _, recipe in ipairs(recipes) do |
|||
for _, item in ipairs(recipe.materials) do |
|||
-- Extract item name and quantity from the "item,#" format |
|||
function p.getShopBuyPrices(products) |
|||
-- Query for the shop buy price of the item |
|||
local mw = require('mw') |
|||
local shopPriceResult = price_cache[item.name] or mw.smw.ask{ |
|||
'[[Sold item::' .. item.name .. ']]', |
|||
'?Shop buy price' |
|||
} or {} |
|||
price_cache[item.name] = shopPriceResult |
|||
local shopPrice |
|||
for _, product in ipairs(products) do |
|||
if shopPriceResult[1] then |
|||
local usesItems = product["Uses item"] |
|||
shopPrice = tonumber(shopPriceResult[1]['Shop buy price'] or 0) or 0 |
|||
if type(usesItems) == "table" then |
|||
end |
|||
for _, item in ipairs(usesItems) do |
|||
-- Extract item name from the link |
|||
-- Update the product with the total price |
|||
local itemName = item:match("%[%[.-|(.+)%]%]") or item:match("%[%[(.-)%]%]") |
|||
if shopPrice ~= nil then |
|||
if itemName then |
|||
item.price = shopPrice * item.quantity |
|||
-- Query for the shop buy price of the item |
|||
end |
|||
local shopPriceQuery = '[[:+]][[Sold item::' .. itemName .. ']]|?Shop buy price|mainlabel=' .. itemName |
|||
end |
|||
local shopPriceResult = mw.smw.ask(shopPriceQuery) or {} |
|||
end |
|||
if shopPriceResult[1] and shopPriceResult[1]["Shop buy price"] then |
|||
end |
|||
product[itemName .. "_Shop_buy_price"] = shopPriceResult[1]["Shop buy price"] |
|||
else |
|||
-- Create table |
|||
product[itemName .. "_Shop_buy_price"] = "N/A" |
|||
local out = mw.html.create('table') |
|||
end |
|||
:addClass('wikitable align-right-1 sortable') |
|||
end |
|||
:tag('tr') |
|||
end |
|||
:tag('th') |
|||
elseif type(usesItems) == "string" then |
|||
:attr{ colspan = '3' } |
|||
-- Handle single uses item (not a table) |
|||
:wikitext('Product') |
|||
local itemName = usesItems:match("%[%[.-|(.+)%]%]") or usesItems:match("%[%[(.-)%]%]") |
|||
:done() |
|||
if itemName then |
|||
:tag('th'):wikitext('Level'):done() |
|||
-- Query for the shop buy price of the item |
|||
:IF(showValues) |
|||
local shopPriceQuery = '[[:+]][[Sold item::' .. itemName .. ']]|?Shop buy price|mainlabel=' .. itemName |
|||
:tag('th'):wikitext('Value'):done() |
|||
local shopPriceResult = mw.smw.ask(shopPriceQuery) or {} |
|||
:END() |
|||
if shopPriceResult[1] and shopPriceResult[1]["Shop buy price"] then |
|||
:tag('th'):wikitext('Inputs'):done() |
|||
product[itemName .. "_Shop_buy_price"] = shopPriceResult[1]["Shop buy price"] |
|||
:IF(showPrices) |
|||
else |
|||
:tag('th'):wikitext('Price'):done() |
|||
product[itemName .. "_Shop_buy_price"] = "N/A" |
|||
:END() |
|||
end |
|||
:done() |
|||
end |
|||
end |
|||
for _, recipe in ipairs(recipes) do |
|||
end |
|||
local row = out:tag('tr') |
|||
return products |
|||
:tag('td') |
|||
end |
|||
:css{ ['border-right'] = '0', ['padding-right'] = '0' } |
|||
:attr{ ['data-sort-value'] = recipe.output[1].name } |
|||
:IF(recipe.passive) |
|||
:wikitext(recipe.output[1].displayQuantity .. ' (/hr) ×') |
|||
:ELSE() |
|||
:wikitext(recipe.output[1].quantity .. ' ×') |
|||
:END() |
|||
:done() |
|||
:tag('td') |
|||
:addClass('plinkt-image no-border') |
|||
:css{ ['border-left'] = '0', ['padding-left'] = '0' } |
|||
:wikitext('[[File:' .. recipe.output[1].name .. '.png|link=' .. recipe.output[1].name .. '|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() |
|||
function p.displayProductTable(products) |
|||
if showValues then |
|||
local currency = require('Module:Currency').parse |
|||
if recipe.Value then |
|||
local out = {} |
|||
row:tag('td') |
|||
table.insert(out, '{| class="wikitable"') |
|||
:wikitext(currency(recipe.Value)) |
|||
table.insert(out, '! Recipe !! Level !! Value !! Ingredients !! Price') |
|||
:done() |
|||
else |
|||
row:tag('td') |
|||
:attr{ ['data-sort-value'] = '' } |
|||
:wikitext('Unknown') |
|||
:done() |
|||
end |
|||
end |
|||
for _, product in ipairs(products) do |
|||
local ingredients = row:tag('td') |
|||
local recipe = product["Recipe"] or "Unknown" |
|||
:attr{ ['data-sort-value'] = table.concat(Array.map(recipe.materials, function(item) return item.name end), '\0') } |
|||
local professionName = product["Profession A"]:match("%[%[.-|(.+)%]%]") or product["Profession A"]:match("%[%[(.-)%]%]") or product["Profession A"] or "Unknown" |
|||
:tag('ul') |
|||
local professionLevel = "[[File:" .. professionName .. " small icon.png|x30px]] " .. (product["Profession Level A"] or "Unknown") |
|||
:css{ ['list-style'] = 'none', ['margin'] = '0', ['padding-left'] = '0' } |
|||
local usesItems = product["Uses item"] or {} |
|||
local value = currency(product["Value"]) or "Unknown" |
|||
-- Concatenate uses items and their shop buy prices in unordered lists |
|||
for _, item in ipairs(recipe.materials) do |
|||
local usesItemStr = "<ul style='list-style:none; margin:0; padding-left:0;'>" |
|||
ingredients:tag('li') |
|||
local shopBuyPriceStr = "<ul style='list-style:none; margin:0; padding-left:0; text-align:right;'>" |
|||
:IF(recipe.passive) |
|||
:wikitext(('%s (/hr) × [[File:%s.png|link=%s|18px]] [[%s]]'):format(item.displayQuantity, item.name, item.name, item.name)) |
|||
:ELSE() |
|||
:wikitext(('%s × [[File:%s.png|link=%s|18px]] [[%s]]'):format(item.quantity, item.name, item.name, item.name)) |
|||
:END() |
|||
:done() |
|||
end |
|||
if type(usesItems) == "table" then |
|||
if showPrices then |
|||
for _, item in ipairs(usesItems) do |
|||
local prices = row:tag('td') |
|||
local itemName = item:match("%[%[.-|(.+)%]%]") or item:match("%[%[(.-)%]%]") or item |
|||
:tag('ul') |
|||
usesItemStr = usesItemStr .. "<li>" .. item .. "</li>" |
|||
:css{ ['list-style'] = 'none', ['margin'] = '0', ['padding-left'] = '0' } |
|||
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>" |
|||
for _, item in ipairs(recipe.materials) do |
|||
shopBuyPriceStr = shopBuyPriceStr .. "</ul>" |
|||
if item.price then |
|||
prices:tag('li'):wikitext(currency(item.price)):done() |
|||
else |
|||
prices:tag('li'):wikitext('Unknown'):done() |
|||
end |
|||
end |
|||
end |
|||
table.insert(out, '|-') |
|||
end |
|||
table.insert(out, '| ' .. recipe .. ' || ' .. professionLevel .. ' || ' .. value .. ' || ' .. usesItemStr .. ' || ' .. shopBuyPriceStr) |
|||
end |
|||
table.insert(out, '|}') |
|||
return out |
|||
return table.concat(out, '\n') |
|||
end |
end |
||