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: | ||
require('Module:Mw.html extension') |
require('Module:Mw.html extension') |
||
local Array = require('Module:Array') |
|||
local currency = require('Module:Currency').parse |
local currency = require('Module:Currency').parse |
||
local yesno = require('Module:Yesno') |
local yesno = require('Module:Yesno') |
||
local purge = require('Module:Purge')._purge |
|||
local p = {} |
local p = {} |
||
Line 9: | Line 7: | ||
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 showPrices = yesno(args.showPrices) |
||
local showValues = yesno(args.showValues) |
local showValues = yesno(args.showValues) |
||
local limit = tonumber(args.limit or 0) or 0 |
|||
if limit <= 0 then |
|||
limit = 500 |
|||
end |
|||
-- Query for data |
-- Query for data |
||
local smw_data = mw.smw.ask{ |
local smw_data = mw.smw.ask{ |
||
'[[Uses item::' .. |
'[[Uses item::' .. args.item .. ']]', |
||
'?Uses item', |
|||
'?Recipe JSON', |
'?Recipe JSON', |
||
' |
'limit=500', |
||
showValues and '?Value' |
showValues and '?Value' |
||
limit = limit |
|||
} |
} |
||
if not smw_data then |
if not smw_data then |
||
return ('There are no known products for item [[%s]]'):format(args.item) |
|||
return ":''No products found. To force an update, click " |
|||
..purge('dml-'..mw.uri.anchorEncode(item), 'here', 'span') |
|||
..".''[[Category:Empty products lists]]" |
|||
end |
end |
||
-- Create a list of all recipes that use this item |
|||
local recipes = {} |
local recipes = {} |
||
for _, product in ipairs(smw_data) do |
for _, product in ipairs(smw_data) do |
||
⚫ | |||
-- this part will need to be addressed if a page has both a recipe and an activity |
|||
product['Recipe JSON'] = { product['Recipe JSON'] } |
|||
⚫ | |||
jsons = { jsons } |
|||
end |
end |
||
for _, json in ipairs( |
for _, json in ipairs(product['Recipe JSON']) do |
||
json = mw.text.jsonDecode(json) |
|||
json.Value = product.Value |
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 |
|||
⚫ | |||
end |
|||
end |
end |
||
end |
end |
||
-- Sort list of recipes by the level of the recipe (cross-profession) |
|||
table.sort(recipes, recipe_sort) |
|||
-- if recipe is passive, want per hour |
|||
for _, recipe in ipairs(recipes) do |
|||
recipe.recipePerHour = recipe.duration and math.floor(3600/recipe.duration) |
|||
-- 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 |
-- Calculate shop prices |
||
if showPrices then |
if showPrices then |
||
Line 121: | Line 68: | ||
-- Create table |
-- Create table |
||
local out = mw.html.create('table') |
local out = mw.html.create('table') |
||
:addClass('wikitable |
:addClass('wikitable') |
||
:tag('tr') |
:tag('tr') |
||
:tag('th') |
:tag('th') |
||
:attr{ colspan = '3' } |
:attr{ colspan = '3' } |
||
:wikitext(' |
:wikitext('Recipe') |
||
:done() |
:done() |
||
:tag('th'):wikitext('Level'):done() |
:tag('th'):wikitext('Level'):done() |
||
Line 131: | Line 78: | ||
:tag('th'):wikitext('Value'):done() |
:tag('th'):wikitext('Value'):done() |
||
:END() |
:END() |
||
:tag('th'):wikitext(' |
:tag('th'):wikitext('Ingredients'):done() |
||
:IF(showPrices) |
:IF(showPrices) |
||
:tag('th'):wikitext('Price'):done() |
:tag('th'):wikitext('Price'):done() |
||
Line 141: | Line 88: | ||
:tag('td') |
:tag('td') |
||
:css{ ['border-right'] = '0', ['padding-right'] = '0' } |
:css{ ['border-right'] = '0', ['padding-right'] = '0' } |
||
: |
:wikitext(recipe.output[1].quantity .. ' ×') |
||
:IF(recipe.passive) |
|||
:wikitext(recipe.output[1].displayQuantity .. ' (/hr) ×') |
|||
:ELSE() |
|||
:wikitext(recipe.output[1].quantity .. ' ×') |
|||
:END() |
|||
:done() |
:done() |
||
:tag('td') |
:tag('td') |
||
Line 171: | Line 113: | ||
:done() |
:done() |
||
else |
else |
||
row:tag('td') |
row:tag('td'):wikitext('Unknown'):done() |
||
:attr{ ['data-sort-value'] = '' } |
|||
:wikitext('Unknown') |
|||
:done() |
|||
end |
end |
||
end |
end |
||
local ingredients = row:tag('td') |
local ingredients = row:tag('td') |
||
:attr{ ['data-sort-value'] = table.concat(Array.map(recipe.materials, function(item) return item.name end), '\0') } |
|||
:tag('ul') |
:tag('ul') |
||
:css{ ['list-style'] = 'none', ['margin'] = '0', ['padding-left'] = '0' } |
:css{ ['list-style'] = 'none', ['margin'] = '0', ['padding-left'] = '0' } |
||
Line 185: | Line 123: | ||
for _, item in ipairs(recipe.materials) do |
for _, item in ipairs(recipe.materials) do |
||
ingredients:tag('li') |
ingredients:tag('li') |
||
⚫ | |||
:IF(recipe.passive) |
|||
:wikitext(('%s (/hr) × [[File:%s.png|link=%s|18px]] [[%s]]'):format(item.displayQuantity, item.name, item.name, item.name)) |
|||
:ELSE() |
|||
⚫ | |||
:END() |
|||
:done() |
:done() |
||
end |
end |