Module:Products: Difference between revisions
Jump to navigation
Jump to search
Content added Content deleted
(Rename "item" parameter to "1", since many pages are using it like that anyways) |
(Make limit a parameter; Make used item images smaller) |
||
Line 14: | Line 14: | ||
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 |
|||
⚫ | |||
end |
|||
-- Query for data |
-- Query for data |
||
Line 20: | Line 24: | ||
'?Uses item', |
'?Uses item', |
||
'?Recipe JSON', |
'?Recipe JSON', |
||
⚫ | |||
⚫ | |||
limit = limit |
|||
⚫ | |||
} |
} |
||
if not smw_data then |
if not smw_data then |
||
Line 68: | Line 72: | ||
-- Create table |
-- Create table |
||
local out = mw.html.create('table') |
local out = mw.html.create('table') |
||
:addClass('wikitable') |
:addClass('wikitable align-right-1') |
||
:tag('tr') |
:tag('tr') |
||
:tag('th') |
:tag('th') |
||
Line 123: | Line 127: | ||
for _, item in ipairs(recipe.materials) do |
for _, item in ipairs(recipe.materials) do |
||
ingredients:tag('li') |
ingredients:tag('li') |
||
:wikitext(('%s × [[File:%s.png|link=%s| |
:wikitext(('%s × [[File:%s.png|link=%s|18px]] [[%s]]'):format(item.quantity, item.name, item.name, item.name)) |
||
:done() |
:done() |
||
end |
end |
Revision as of 12:10, 30 November 2024
Module documentation
This documentation is transcluded from Module:Products/doc. [edit] [history] [purge]
This module does not have any documentation. Please consider adding documentation at Module:Products/doc. [edit]
Module:Products's function main is invoked by Template:Products.
Module:Products requires Module:Currency.
Module:Products requires Module:Mw.html extension.
Module:Products requires Module:Yesno.
require('Module:Mw.html extension')
local currency = require('Module:Currency').parse
local yesno = require('Module:Yesno')
local p = {}
function p.main(frame)
return p._main(frame:getParent().args)
end
function p._main(args)
args = args or {}
local item = args[1] or mw.title.getCurrentTitle().text
local showPrices = yesno(args.showPrices)
local showValues = yesno(args.showValues)
local limit = tonumber(args.limit or 0) or 0
if limit <= 0 then
limit = 500
end
-- Query for data
local smw_data = mw.smw.ask{
'[[Uses item::' .. item .. ']]',
'?Uses item',
'?Recipe JSON',
showValues and '?Value',
limit = limit
}
if not smw_data then
return ('There are no known products for item [[%s]]'):format(item)
end
local recipes = {}
for _, product in ipairs(smw_data) do
if type(product['Recipe JSON']) == 'string' then
product['Recipe JSON'] = { product['Recipe JSON'] }
end
for _, json in ipairs(product['Recipe JSON']) do
json = mw.text.jsonDecode(json)
json.Value = product.Value
table.insert(recipes, json)
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
-- Query for the shop buy price of the item
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
if shopPriceResult[1] then
shopPrice = tonumber(shopPriceResult[1]['Shop buy price'] or 0) or 0
end
-- Update the product with the total price
if shopPrice ~= nil then
item.price = shopPrice * item.quantity
end
end
end
end
-- Create table
local out = mw.html.create('table')
:addClass('wikitable align-right-1')
: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 row = out:tag('tr')
:tag('td')
:css{ ['border-right'] = '0', ['padding-right'] = '0' }
:wikitext(recipe.output[1].quantity .. ' ×')
: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()
if showValues then
if recipe.Value then
row:tag('td')
:wikitext(currency(recipe.Value))
:done()
else
row:tag('td'):wikitext('Unknown'):done()
end
end
local ingredients = row:tag('td')
:tag('ul')
:css{ ['list-style'] = 'none', ['margin'] = '0', ['padding-left'] = '0' }
for _, item in ipairs(recipe.materials) do
ingredients:tag('li')
:wikitext(('%s × [[File:%s.png|link=%s|18px]] [[%s]]'):format(item.quantity, item.name, item.name, item.name))
:done()
end
if showPrices then
local prices = row:tag('td')
:tag('ul')
:css{ ['list-style'] = 'none', ['margin'] = '0', ['padding-left'] = '0' }
for _, item in ipairs(recipe.materials) do
if item.price then
prices:tag('li'):wikitext(currency(item.price)):done()
else
prices:tag('li'):wikitext('Unknown'):done()
end
end
end
end
return out
end
return p