Module:Sandbox/User:Alsang: Difference between revisions
Jump to navigation
Jump to search
Content added Content deleted
Tag: Undo |
No edit summary Tag: Manual revert |
||
Line 1: | Line 1: | ||
require('strict') |
|||
require('Module:Mw.html extension') |
require('Module:Mw.html extension') |
||
local |
local Array = require('Module:Array') |
||
local currency = require('Module:Currency').parse |
|||
local edit = editbutton("'''?''' (edit)") |
|||
local |
local yesno = require('Module:Yesno') |
||
local |
local purge = require('Module:Purge')._purge |
||
local qty = require('Module:Quantity box')._main |
|||
local pcp = require('Module:Profession clickpic')._main |
|||
local currency = require('Module:Currency') |
|||
local album_xp_data = mw.loadData('Module:Experience/data').album |
|||
local lang = mw.language.getContentLanguage() |
|||
local tooltip = require('Module:Tooltip') |
|||
local p = {} |
local p = {} |
||
function p.main(frame) |
function p.main(frame) |
||
return p._main(frame:getParent().args) |
|||
end |
|||
local albumxp = album_xp_data[tonumber(args.level)] |
|||
local function recipe_sort(recipe_a, recipe_b) |
|||
if albumxp then |
|||
-- Sort unknown levels to the end |
|||
albumxp = lang:formatNum(albumxp)..' xp' |
|||
if (recipe_a.level == nil) ~= (recipe_b.level == nil) then |
|||
return recipe_b.level == nil |
|||
end |
end |
||
if recipe_a.level ~= nil then |
|||
--Checks if the recipe has been flagged as passive, defaults to no. |
|||
return recipe_a.level < recipe_b.level |
|||
local passive = yn(args.passive or 'no', false) |
|||
local quant = 1 |
|||
if passive then |
|||
quant = 0.005 |
|||
end |
end |
||
-- Sort by name if same level |
|||
return recipe_a.output[1].name < recipe_b.output[1].name |
|||
local function exparg(xp) |
|||
end |
|||
if hc(xp) then |
|||
return qty(xp) |
|||
function p._main(args) |
|||
else |
|||
args = args or {} |
|||
return qty(0)..edit..'[[Category:Needs experience info]]' |
|||
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 ":''No products found. To force an update, click " |
|||
..purge('dml-'..mw.uri.anchorEncode(item), 'here', 'span') |
|||
..".''[[Category:Empty products lists]]" |
|||
end |
|||
-- Create a list of all recipes, grouped by output item (to keep them together in the sort) |
|||
local produced_items = {} |
|||
for _, product in ipairs(smw_data) do |
|||
local jsons = product['Recipe JSON'] |
|||
if type(jsons) == 'string' then |
|||
jsons = { jsons } |
|||
end |
end |
||
local parsed = {} |
|||
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(parsed, json) |
|||
end |
|||
end |
|||
table.sort(parsed, recipe_sort) |
|||
table.insert(produced_items, parsed) |
|||
end |
end |
||
-- Sort by the smallest recipe in the group |
|||
local function levelarg(profession, level) |
|||
table.sort(produced_items, function(item1, item2) |
|||
if hc(profession) and hc(level) then |
|||
local first1 = item1[1] |
|||
return pcp(profession, level) |
|||
local first2 = item2[1] |
|||
else |
|||
if (first1 == nil) ~= (first2 == nil) then |
|||
return edit..'[[Category:Needs level info]]' |
|||
return first2 == nil |
|||
end |
|||
if first1 == nil then |
|||
return false -- Both empty, equivalent |
|||
end |
|||
return recipe_sort(first1, first2) |
|||
end) |
|||
-- Flatten into a single list |
|||
local recipes = {} |
|||
for _, product in ipairs(produced_items) do |
|||
for _, json in ipairs(product) do |
|||
table.insert(recipes, json) |
|||
end |
end |
||
end |
end |
||
-- Calculate shop prices |
|||
local function durationarg(duration) |
|||
if showPrices then |
|||
local price_cache = {} |
|||
return duration..' seconds' |
|||
for _, recipe in ipairs(recipes) do |
|||
else |
|||
for _, item in ipairs(recipe.materials) do |
|||
return edit..'[[Category:Needs duration info]]' |
|||
-- 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 |
||
end |
end |
||
-- Create table |
|||
local smw_properties = { |
|||
['Skill node name'] = args.name, |
|||
['Uses profession'] = args.profession, |
|||
['Profession level'] = args.level, |
|||
['Activity XP'] = args.xp, |
|||
['Activity album XP'] = album_xp_data[tonumber(args.level)], |
|||
['Activity KP'] = args.kp, |
|||
['Activity duration'] = args.duration, |
|||
['Activity coins'] = args.coins, |
|||
['Activity input'] = args.input, |
|||
['Activity container'] = args.container, |
|||
-- straight copy from Infobox Recipe, its good to have a standard format |
|||
['Activity JSON'] = mw.text.jsonEncode({ |
|||
xp = args.xp and tonumber(args.xp), |
|||
kp = args.kp and tonumber(args.kp), |
|||
duration = args.duration, |
|||
tool = args.tool, |
|||
materials = { |
|||
{ name = args.input, quantity = quant }, |
|||
{ name = args.container, quantity = quant }, |
|||
}, |
|||
profession = args.profession, |
|||
level = args.level and tonumber(args.level), |
|||
-- Make sure to update this when multiple outputs are supported |
|||
output = { |
|||
{ name = args.name, quantity = quant }, |
|||
} |
|||
}), |
|||
} |
|||
mw.smw.set(smw_properties) |
|||
local out = mw.html.create('table') |
local out = mw.html.create('table') |
||
:addClass('wikitable') |
:addClass('wikitable align-right-1 sortable') |
||
:tag('tr') |
:tag('tr') |
||
:tag('th') |
:tag('th') |
||
:attr{ colspan = ' |
:attr{ colspan = '3' } |
||
:wikitext( |
:wikitext('Recipe') |
||
:done() |
: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() |
:done() |
||
:tag('tr') |
|||
for _, recipe in ipairs(recipes) do |
|||
:tag('th') |
|||
local row = out:tag('tr') |
|||
:wikitext('Level required') |
|||
:done() |
|||
:tag('td') |
:tag('td') |
||
:css{ ['border-right'] = '0', ['padding-right'] = '0' } |
|||
:wikitext(levelarg(args.profession, args.level)) |
|||
:attr{ ['data-sort-value'] = recipe.output[1].name } |
|||
:done() |
|||
:wikitext(recipe.output[1].quantity .. ' ×') |
|||
:done() |
|||
:tag('tr') |
|||
:tag('th') |
|||
:IF(passive) |
|||
:wikitext('[[File:Passive small icon.png|20x20px|link=Passive Activity]] [['..args.profession..'|'..args.profession..' XP]] ') |
|||
:node(tooltip._span{ 'passive' }) |
|||
:node(tooltip._div{ name = 'passive', content = 'Passive activities give reduced experience when a players level is high enough to perform a new passive activity for that profession. The number shown here is the full experience.' }) |
|||
:ELSE() |
|||
:wikitext('[['..args.profession..'|'..args.profession..' XP]]') |
|||
:END() |
|||
:done() |
:done() |
||
:tag('td') |
:tag('td') |
||
:addClass('plinkt-image no-border') |
|||
:wikitext(exparg(args.xp)) |
|||
:css{ ['border-left'] = '0', ['padding-left'] = '0' } |
|||
:done() |
|||
:wikitext('[[File:' .. recipe.output[1].name .. '.png|link=' .. recipe.output[1].name .. '|30px]]') |
|||
:done() |
|||
:tag('tr') |
|||
:tag('th') |
|||
:wikitext('[[Album|Album XP]]') |
|||
:done() |
:done() |
||
:tag('td') |
:tag('td') |
||
: |
:addClass('plinkt-link no-border') |
||
:wikitext('[[' .. recipe.output[1].name .. ']]') |
|||
:done() |
|||
:done() |
|||
:tag('tr') |
|||
:tag('th') |
|||
:wikitext('Duration') |
|||
:done() |
:done() |
||
:tag('td') |
:tag('td') |
||
:IF(recipe.profession) |
|||
:wikitext(durationarg(args.duration)) |
|||
: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() |
:done() |
||
:done() |
|||
if showValues then |
|||
if recipe.Value then |
|||
row:tag('td') |
|||
out |
|||
:wikitext(currency(recipe.Value)) |
|||
:tag('tr') |
|||
:tag('th') |
|||
:wikitext('Respawn time') |
|||
:done() |
:done() |
||
else |
|||
row:tag('td') |
|||
:wikitext(durationarg(args.respawn)) |
|||
:attr{ ['data-sort-value'] = '' } |
|||
:wikitext('Unknown') |
|||
:done() |
:done() |
||
end |
|||
end |
|||
local ingredients = row:tag('td') |
|||
:attr{ ['data-sort-value'] = table.concat(Array.map(recipe.materials, function(item) return item.name end), '\0') } |
|||
: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() |
: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 |
end |
||
if hc(args.tool) then |
|||
out |
|||
:tag('tr') |
|||
:tag('th') |
|||
:wikitext('Required tool') |
|||
:done() |
|||
:tag('td') |
|||
:wikitext(args.tool) |
|||
:done() |
|||
:done() |
|||
end |
|||
if hc(args.input) then |
|||
out |
|||
:tag('tr') |
|||
:tag('th') |
|||
:wikitext('Required input') |
|||
:done() |
|||
:tag('td') |
|||
:wikitext(args.input) |
|||
:done() |
|||
:done() |
|||
end |
|||
if hc(args.container) then |
|||
out |
|||
:tag('tr') |
|||
:tag('th') |
|||
:wikitext('Required container') |
|||
:done() |
|||
:tag('td') |
|||
:wikitext(args.container) |
|||
:done() |
|||
:done() |
|||
end |
|||
if hc(args.coins) then |
|||
out |
|||
:tag('tr') |
|||
:tag('th') |
|||
:wikitext('Coins') |
|||
:done() |
|||
:tag('td') |
|||
:wikitext(currency.parse(args.coins)) |
|||
:done() |
|||
:done() |
|||
end |
|||
return out |
return out |
||
end |
end |
Revision as of 00:52, 16 December 2024
Module documentation
This documentation is transcluded from Module:Sandbox/User:Alsang/doc. [edit] [history] [purge]
This module does not have any documentation. Please consider adding documentation at Module:Sandbox/User:Alsang/doc. [edit]
Module:Sandbox/User:Alsang's function main is invoked by Template:Sandbox/User:Alsang.
Module:Sandbox/User:Alsang requires Module:Array.
Module:Sandbox/User:Alsang requires Module:Currency.
Module:Sandbox/User:Alsang requires Module:Mw.html extension.
Module:Sandbox/User:Alsang requires Module:Purge.
Module:Sandbox/User:Alsang requires Module:Yesno.
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 p = {}
function p.main(frame)
return p._main(frame:getParent().args)
end
local function recipe_sort(recipe_a, recipe_b)
-- Sort unknown levels to the end
if (recipe_a.level == nil) ~= (recipe_b.level == nil) then
return recipe_b.level == nil
end
if recipe_a.level ~= nil then
return recipe_a.level < recipe_b.level
end
-- Sort by name if same level
return recipe_a.output[1].name < recipe_b.output[1].name
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 ":''No products found. To force an update, click "
..purge('dml-'..mw.uri.anchorEncode(item), 'here', 'span')
..".''[[Category:Empty products lists]]"
end
-- Create a list of all recipes, grouped by output item (to keep them together in the sort)
local produced_items = {}
for _, product in ipairs(smw_data) do
local jsons = product['Recipe JSON']
if type(jsons) == 'string' then
jsons = { jsons }
end
local parsed = {}
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(parsed, json)
end
end
table.sort(parsed, recipe_sort)
table.insert(produced_items, parsed)
end
-- Sort by the smallest recipe in the group
table.sort(produced_items, function(item1, item2)
local first1 = item1[1]
local first2 = item2[1]
if (first1 == nil) ~= (first2 == nil) then
return first2 == nil
end
if first1 == nil then
return false -- Both empty, equivalent
end
return recipe_sort(first1, first2)
end)
-- Flatten into a single list
local recipes = {}
for _, product in ipairs(produced_items) do
for _, json in ipairs(product) do
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 sortable')
: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' }
:attr{ ['data-sort-value'] = recipe.output[1].name }
: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')
:attr{ ['data-sort-value'] = '' }
:wikitext('Unknown')
:done()
end
end
local ingredients = row:tag('td')
:attr{ ['data-sort-value'] = table.concat(Array.map(recipe.materials, function(item) return item.name end), '\0') }
: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