Module:Sandbox/User:Alsang: Difference between revisions

copy of module:Products
No edit summary
(copy of module:Products)
Line 1:
require('Module:Mw.html extension')
p = {}
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()
 
function p.main(frame)
local weaponsStone = {
return p._main(frame:getParent().args)
'Hammerfist',
'Throwing Stones',
'Stone Slab Shield',
'Stone Mace',
'Stone Discs',
'War Hammer',
'Bolas',
'Stone Chunk Shield',
'Double Headed Hammer',
'Throwing Clubs',
'Great Stone Mace',
' Polished Stone Shield',
'Throwing Hammers',
'Great Hammer',
'Sling'}
local weaponsMetal = {
'Rapier ',
'Throwing Twinblades',
'Metal Kite Shield',
'Flanged Mace',
'Throwing Rings',
'Shortsword',
'Metal Javelins',
'Metal Heater Shield',
'Longsword',
'Throwing Knives',
'Broadsword',
'Metal Buckler',
'Throwing Axes',
'Battleaxe',
'Metal Bow'}
local weaponsBone = {
'Truncheon',
'Blowpipe',
'Wooden Round Shield',
'Cudgel',
'Javelins',
'Club',
'Light Crossbow',
'Wooden Square Shield',
'Quarterstaff',
'Recurve Bow',
'Spear',
'Wooden Hexagon Shield',
'Longbow',
'Poleaxe',
'Heavy Crossbow'}
local out = mw.html.create('table')
:addClass('wikitable sortable')
:tag('tr')
:tag('th')
:wikitext('Stone')
:done()
:tag('th')
:wikitext('Metal')
:done()
:tag('th')
:wikitext('Bone')
:done()
:done()
for i,item in ipairs(weaponsStone) do
local row = out:tag('tr')
:tag('td')
:wikitext('[[' .. weaponsStone[i] .. ']]')
:done()
:tag('td')
:wikitext('[[' .. weaponsMetal[i] .. ']]')
:done()
:tag('td')
:wikitext('[[' .. weaponsBone[i] .. ']]')
:done()
:done()
end
return out
end
 
local function p.intermediatesrecipe_sort(recipe_a, recipe_b)
-- Sort unknown levels to the end
if (recipe_a.level == nil) ~= (recipe_b.level == nil) then
local allRecipes = mw.smw.ask('[[Recipe JSON::~*]]|? #- = name|?Uses facility = facility|?-Sold item.Sold by #- = seller|?Variant of = variant|limit=1200')
return recipe_b.level == nil
end
local allFacilities = {'Standard Potion Station',
 
'Potent Potion Station',
if recipe_a.level ~= nil then
'Passive Potion Station',
return recipe_a.level < recipe_b.level
'Workbench',
end
'One Handed Ranged Workbench (stonemason)',
 
'One Handed Melee Workbench (stonemason)',
-- Sort by name if same level
'Two Handed Ranged Workbench (stonemason)',
return recipe_a.output[1].name < recipe_b.output[1].name
'Two Handed Melee Workbench (stonemason)',
end
'Shield Work Rock',
 
'Stoneware Bench',
function p._main(args)
'One Handed Ranged Workbench (bonewright)',
args = args or {}
'One Handed Melee Workbench (bonewright)',
local item = args[1] or mw.title.getCurrentTitle().text
'Two Handed Ranged Workbench (bonewright)',
local showPrices = yesno(args.showPrices)
'Two Handed Melee Workbench (bonewright)',
local showValues = yesno(args.showValues)
'Shield Vice',
local limit = tonumber(args.limit or 0) or 0
'Knickknacks Workbench',
if limit <= 0 then
'Bone Grinder',
limit = 500
'Goblin Forge',
end
'Gnome Forge',
 
'Jewelry Bench'}
-- Query for data
local smw_data = mw.smw.ask{
local allShops = {'Head Chef (shop)','Timber Merchant Shop','Leather Stall','Leather Goods','Waiter'}
'[[Uses item::' .. item .. ']]',
'?Uses item',
local intermediateRecipes = {}
'?Recipe JSON',
showValues and '?Value',
for _,item in ipairs(allRecipes) do
intermediatelimit = truelimit
}
if not smw_data then
-- check facility
return ":''No products found. To force an update, click "
for _,facility in ipairs(allFacilities) do
..purge('dml-'..mw.uri.anchorEncode(item), 'here', 'span')
if item.facility==facility then
..".''[[Category:Empty products lists]]"
intermediate = false
end
 
-- Create a list of all recipes, grouped by output item (to keep them together in the sort)
-- check shops
local produced_items = {}
for _,shop in ipairs(allShops) do
for _, product in ipairs(smw_data) do
if item.seller==shop then
local jsons = product['Recipe JSON']
intermediate = false
if type(jsons) == 'string' then
end
jsons = { jsons }
if type(item.seller)=='table' then
end
for _,seller in ipairs(item.seller) do
local parsed = {}
if seller==shop then
for _, json in ipairs(jsons) do
intermediate = false
local json = mw.text.jsonDecode(json)
end
json.Value = product.Value
end
-- 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 intermediatefirst1 then== nil then
return false -- Both empty, equivalent
table.insert(intermediateRecipes,item)
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')
:wikitext(attr{ colspan = 'Page3') }
:donewikitext('Recipe')
:tag('th')
:wikitext('Variant')
: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 i_,item recipe in ipairs(intermediateRecipesrecipes) do
local row = out:tag('tr')
:tag('td')
:css{ ['border-right'] = '0', ['padding-right'] = '0' }
:wikitext('[[' .. item.name .. ']]')
:attr{ ['data-sort-value'] = recipe.output[1].name }
:wikitext(recipe.output[1].quantity .. ' &times;')
: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()
:tag('td')else
row:wikitexttag(item.variant'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 &times; [[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
 
19,555

edits