Editing Module:Sandbox/User:Alsang

Jump to navigation Jump to search
Warning: You are not logged in. Your IP address will be publicly visible if you make any edits. If you log in or create an account, your edits will be attributed to your username, along with other benefits.

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')
local Array = require('Module:Array')
local currency = require('Module:Currency').parse
local yesno = require('Module:Yesno')
local purge = require('Module:Purge')._purge


local p = {}
local p = {}


function p.gathererPages1()
function p.main(frame)
return p._main(frame:getParent().args)
local queryString = '[[Variant of::Globeplant||Dandelion||Apple||Haleberries||Chestnut||Catkin||Clover||Snake Scale||Orchid||Dock Leaf||Sage||Thistle]]'
return p.main(queryString)
end
end


function p.gathererPages2()
local function recipe_sort(recipe_a, recipe_b)
-- Sort unknown levels to the end
local queryString = '[[Variant of::Bone Spike||Femur Shard||Goat Horn||Rams Horn]]'
if (recipe_a.level == nil) ~= (recipe_b.level == nil) then
return p.main(queryString)
return recipe_b.level == nil
end
end


if recipe_a.level ~= nil then
function p.gathererPages3()
return recipe_a.level < recipe_b.level
local queryString = '[[Variant of::Leek||Bitterfruit||Tangfruit]]'
end
return p.main(queryString)

-- Sort by name if same level
return recipe_a.output[1].name < recipe_b.output[1].name
end
end


function p.main(queryString)
function p._main(args)
args = args or {}

local item = args[1] or mw.title.getCurrentTitle().text
local query = {
local showPrices = yesno(args.showPrices)
queryString,
local showValues = yesno(args.showValues)
'? #- = name',
local limit = tonumber(args.limit or 0) or 0
'?Profession Level A = level',
if limit <= 0 then
'?Variant of #-= variant',
limit = 500
'sort = Variant of,Profession Level A',
end
'limit = 500'
}
local results = mw.smw.ask(query)
-- Query for data
for _,page in ipairs(results) do
local smw_data = mw.smw.ask{
if string.find(page.name,'Mine') then
'[[Uses item::' .. item .. ']] OR [[Activity input::' .. item .. ']] OR [[Activity container::' .. item .. ']]',
page.node = page.name
'?Recipe JSON',
page.nodevar = page.variant
'?Activity JSON',
else
showValues and '?Value',
page.node = page.name .. ' (skill node)'
limit = limit
page.nodevar = page.variant .. ' (skill node)'
}
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'] or product['Activity JSON']
if type(jsons) == 'string' then
jsons = { jsons }
end
end
local parsed = {}
local result = mw.smw.ask('[['..page.node..']]|?Activity JSON = data')
for _, json in ipairs(jsons) do
page.JSON = (result and result[1] and result[1].data) or ''
local json = mw.text.jsonDecode(json)
if type(page.JSON)=='table' then
json.Value = product.Value
page.JSON = table.concat(page.JSON,'<br>')
-- 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
end
table.sort(parsed, recipe_sort)
table.insert(produced_items, parsed)
end
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')
local out = mw.html.create('table')
:addClass('wikitable sortable')
:addClass('wikitable align-right-1 sortable')
:tag('tr')
:tag('tr')
:tag('th')
:tag('th')
:wikitext('Variant')
:attr{ colspan = '3' }
:done()
:wikitext('Recipe')
:tag('th')
:wikitext('Node Variant')
:done()
:tag('th')
:wikitext('Level')
:done()
:tag('th')
:wikitext('Page')
:done()
:tag('th')
:wikitext('Node')
:done()
:tag('th')
:wikitext('Node JSON')
: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()


for i, item in ipairs(results) do
for _, recipe in ipairs(recipes) do
local row = out:tag('tr')
local row = out:tag('tr')
--level
:tag('td')
:tag('td')
:css{ ['border-right'] = '0', ['padding-right'] = '0' }
:wikitext('[['..item.variant..']]')
:attr{ ['data-sort-value'] = recipe.output[1].name }
:wikitext(recipe.output[1].quantity .. ' &times;')
:done()
:done()
:tag('td')
:tag('td')
:wikitext('[['..item.nodevar..']]')
: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()
:done()
:tag('td')
:tag('td')
:addClass('plinkt-link no-border')
:wikitext(item.level)
:wikitext('[[' .. recipe.output[1].name .. ']]')
:done()
:done()
:tag('td')
:tag('td')
:wikitext('[['..item.name..']]')
: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()
:done()

:tag('td')
if showValues then
:wikitext('[['..item.node..']]')
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 &times; [[File:%s.png|link=%s|18px]] [[%s]]'):format(item.quantity, item.name, item.name, item.name))
:done()
:done()
end
:tag('td')

:wikitext(item.JSON)
if showPrices then
:done()
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
return out


return out
end
end


Please note that all contributions to Brighter Shores Wiki are considered to be released under the CC BY-NC-SA 3.0 (see Brighter Shores:Copyrights for details). If you do not want your writing to be edited mercilessly and redistributed at will, then do not submit it here.
You are also promising us that you wrote this yourself, or copied it from a public domain or similar free resource. Do not submit copyrighted work without permission!
Cancel Editing help (opens in new window)
Preview page with this template

This page is a member of a hidden category: