Module:Sandbox/User:Alsang: Difference between revisions

Jump to navigation Jump to search
Content added Content deleted
No edit summary
No edit summary
Line 1: Line 1:
require('strict')
require('Module:Mw.html extension')
require('Module:Mw.html extension')
local Array = require('Module:Array')
local param = require( 'Module:Paramtest' )
local currency = require('Module:Currency').parse
local currency = require('Module:Currency')
local yesno = require('Module:Yesno')
local lang = mw.getContentLanguage()
local purge = require('Module:Purge')._purge


local p = {}
local p = {}


-- non dynamic module, no inputs
function p.main(frame)
function p.main()
return p._main(frame:getParent().args)
end
-- returns only directly needed parameter needed for the row,
-- other parameters are determined by subqueries of chained pages
local query = {
'[[Rotten Tree Stump||Rotting Log (Hopeport)||Rock Pool||Scrap Metal Bin]]',
'?Activity JSON = activityJSON',
'?#- = name',
'limit = 500'
}
local results = mw.smw.ask(query)
results = p.screenResults(results)


results = p.formatResults(results)
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


return p.displayTable(results)
if recipe_a.level ~= nil then
return recipe_a.level < recipe_b.level
--for debugging
end
--return '<pre>'..mw.text.jsonEncode(results, mw.text.JSON_PRETTY)..'</pre>'


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


-- makes the html for the cells containing currency directly
function p._main(args)
-- Replaces nil with an "unknown" cell
args = args or {}
local function currency_cell(amount)
local item = args[1] or mw.title.getCurrentTitle().text
if not amount then
local showPrices = yesno(args.showPrices)
return mw.html.create('td')
local showValues = yesno(args.showValues)
:addClass('table-bg-gray')
local limit = tonumber(args.limit or 0) or 0
:css{ ['text-align'] = 'center' }
if limit <= 0 then
:attr{ colspan = '10' }
limit = 500
:wikitext("''unknown''")
:done()
end
end
return currency._cell(amount, { html = 'yes' })
end

-- goes through a list of results and culls the ones which are not needed for this table
function p.screenResults(results)
local resultsScreened = {}
-- Query for data
-- iterate through products
for _, item in ipairs(results) do
local smw_data = mw.smw.ask{
'[[Uses item::' .. item .. ']] OR [[Activity input::' .. item .. ']] OR [[Activity container::' .. item .. ']]',
-- if theres only one activity, wrap in table
'?Recipe JSON',
if type(item.activityJSON)=='string' then
'?Activity JSON',
item.activityJSON = { item.activityJSON }
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'] or product['Activity JSON']
if type(jsons) == 'string' then
jsons = { jsons }
end
end
local parsed = {}
for _, json in ipairs(jsons) do
for j,json in ipairs(item.activityJSON) do
local json = mw.text.jsonDecode(json)
-- if string.find(json,'Branches') or string.find(json,'Split') then
json.Value = product.Value
-- Filter out when this item isn't actually used (Will happen on pages with multiple recipes)
local activityJSON = mw.text.jsonDecode(json)
if Array.any(json.materials, function(mat)
return mat.name == item
local itemNew = {}
itemNew.name = item.name
end) then
itemNew.XP = activityJSON.xp
table.insert(parsed, json)
itemNew.lvl = activityJSON.level
end
itemNew.duration = activityJSON.duration and tonumber(activityJSON.duration)
itemNew.product = activityJSON.output[1].name
itemNew.activityJSON = json
table.insert(resultsScreened,itemNew)
-- end
end
end
table.sort(parsed, recipe_sort)
table.insert(produced_items, parsed)
end
end

-- Sort by the smallest recipe in the group
-- sort the results by recipe level
table.sort(produced_items, function(item1, item2)
table.sort(resultsScreened, function(item1, item2)
local first1 = item1[1]
local lvl1 = item1.lvl
local first2 = item2[1]
local lvl2 = item2.lvl
if (first1 == nil) ~= (first2 == nil) then
if (lvl1 == nil) ~= (lvl2 == nil) then --one of two are empty
return lvl2 == nil -- true if lvl2 is nil but not lvl1, false if lvl1 is nil but not lvl2
return first2 == nil
end
end
if first1 == nil then
if lvl1 == nil then
return false -- Both empty, equivalent
return false -- Both empty, equivalent
end
end
return lvl1 < lvl2 -- normal comparison

return recipe_sort(first1, first2)
end)
end)


return resultsScreened
-- 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
-- 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


-- do calculations and determine strings to go in cells
-- Query for the shop buy price of the item
function p.formatResults(results)
local shopPriceResult = price_cache[item.name] or mw.smw.ask{
'[[Sold item::' .. item.name .. ']]',
-- iterate through products
'?Shop buy price'
for _, item in ipairs(results) do
} or {}
price_cache[item.name] = shopPriceResult
item.givesCoins = string.find(item.product,'Split')
local shopPrice


-- profit from selling the items
if shopPriceResult[1] then
if not item.givesCoins then
shopPrice = tonumber(shopPriceResult[1]['Shop buy price'] or 0) or 0
end
-- protection against missing parameters

-- Update the product with the total price
item.product = item.product or ''
if shopPrice ~= nil then
-- query is: there is a shop object page that has this item AND has buy price cannot be "N/A"
item.price = shopPrice * item.quantity
local query = mw.smw.ask('[[Sold item::' .. item.product .. ']][[Shop sell price::!~N/A]]|?Shop sell price=data|mainlabel=-')
end
if type(query)=='table' then
-- price is returned as a number
item.coins = tonumber(query[1].data)*0.005 or 0
else
-- will return nil if the item is not in a shop, or if it is in a shop but can only be sold to the shop
item.coins = 0
end
end
end
end
-- no downtime for passives
item.productPerHour = item.duration and 1 / item.duration * 3600

-- properties per hour
item.XPPerHour = item.XP and item.productPerHour and math.floor(item.XP * item.productPerHour)
item.coinsPerHour = item.coins and item.productPerHour and math.floor(item.coins * item.productPerHour)
end
end


return results
-- Create table
end

-- make the table
function p.displayTable(results)
local out = mw.html.create('table')
local out = mw.html.create('table')
:addClass('wikitable align-right-1 sortable')
:addClass('wikitable sortable')
:tag('tr')
:tag('tr')
:tag('th')
:wikitext('[[File:Forager small icon.png|15px]] Level')
:done()
:tag('th')
:attr{ colspan = '2' }
:wikitext('Forager node')
:done()
:tag('th')
:tag('th')
:attr{ colspan = '3' }
:attr{ colspan = '3' }
:wikitext('Recipe')
:wikitext('Product')
:done()
:tag('th')
:wikitext('Quantity/hr')
:done()
:tag('th')
:attr{ colspan = '10' }
:wikitext('Profit/hr')
:done()
:tag('th')
:wikitext('XP')
:done()
:tag('th')
:wikitext('XP/hr')
: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()


local unknown_value_cell = mw.html.create('td')
for _, recipe in ipairs(recipes) do
:addClass('table-bg-gray')
:css{ ['text-align'] = 'center' }
:wikitext("''unknown''")

for i, item in ipairs(results) do
local row = out:tag('tr')
local row = out:tag('tr')
--level
:IF(item.lvl)
:tag('td')
:css{ ['text-align'] = 'center' }
:wikitext(item.lvl)
:done()
:ELSE()
:node(unknown_value_cell)
:END()
-- activity
:tag('td')
:tag('td')
:css{ ['border-right'] = '0', ['padding-right'] = '0' }
:css{ ['border-right'] = '0', ['text-align'] = 'right', ['max-width'] = '100px' }
:attr{ ['data-sort-value'] = recipe.output[1].name }
:attr{ ['data-sort-value'] = item.name }
:wikitext(recipe.output[1].quantity .. ' &times;')
:wikitext(' [[File:' .. item.name .. '.png|link=' .. item.name .. '|30x30px]]')
:done()
:done()
:tag('td')
:tag('td')
:addClass('plinkt-image no-border')
:addClass('plinkt-link no-border')
:wikitext('[[' .. item.name .. ']]')
:css{ ['border-left'] = '0', ['padding-left'] = '0' }
:done()
:wikitext('[[File:' .. recipe.output[1].name .. '.png|link=' .. recipe.output[1].name .. '|30px]]')

:tag('td')
:css{ ['border-right'] = '0', ['text-align'] = 'right' }
:attr{ ['data-sort-value'] = item.product }
:wikitext('0.005 &times; ')
:done()
:done()
:tag('td')
:tag('td')
:css{ ['border-right'] = '0', ['text-align'] = 'center', ['max-width'] = '100px' }
:addClass('plinkt-link no-border')
:addClass('plinkt-link no-border')
:wikitext('[[' .. recipe.output[1].name .. ']]')
:wikitext('[[File:' .. item.product .. '.png|link=' .. item.product .. '|30x30px]]')
:done()
:done()
:tag('td')
:tag('td')
:addClass('plinkt-link no-border')
:IF(recipe.profession)
:wikitext(('[[File:%s small icon.png|15px|link=%s]] %s'):format(recipe.profession, recipe.profession, recipe.level or 'Unknown'))
:wikitext('[[' .. item.product .. ']]')
:ELSE()
:wikitext(('[[FileUnknown profession small icon.png|15px|link=Professions]] %s'):format(recipe.level or 'Unknown'))
:END()
:done()
:done()


if showValues then
-- products per hour (items)
if recipe.Value then
row:tag('td')
:tag('td')
:css{ ['text-align'] = 'right' }
:wikitext(currency(recipe.Value))
:wikitext('0.9 &times; [[File:' .. item.product .. '.png|link=' .. item.product .. '|30x30px]]')
: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


-- profit per hour (coins)
if showPrices then
:node(currency_cell(item.coinsPerHour))
local prices = row:tag('td')
:tag('ul')
:css{ ['list-style'] = 'none', ['margin'] = '0', ['padding-left'] = '0' }


-- XP
for _, item in ipairs(recipe.materials) do
if item.price then
:IF(item.XP)
prices:tag('li'):wikitext(currency(item.price)):done()
:tag('td')
:wikitext(item.XP and lang:formatNum(tonumber(item.XP)))
else
prices:tag('li'):wikitext('Unknown'):done()
:done()
end
:ELSE()
:node(unknown_value_cell)
end
end
:END()
-- XP per hour
:IF(item.XPPerHour)
:tag('td')
:wikitext(item.XPPerHour and lang:formatNum(tonumber(item.XPPerHour)))
:done()
:ELSE()
:node(unknown_value_cell)
:END()


:done()
end
end