Module:Sandbox/User:Alsang: Difference between revisions

(starting to work)
No edit summary
 
(16 intermediate revisions by the same user not shown)
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.main(frame)
function p.gathererPages1()
local queryString = '[[Variant of::Globeplant||Dandelion||Apple||Haleberries||Chestnut||Catkin||Clover||Snake Scale||Orchid||Dock Leaf||Sage||Thistle]]'
return p._main(frame:getParent().args)
return p.main(queryString)
end
end


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


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

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


function p._main(args)
function p.main(queryString)

args = args or {}
local query = {
local item = args[1] or mw.title.getCurrentTitle().text
queryString,
local showPrices = yesno(args.showPrices)
'? #- = name',
local showValues = yesno(args.showValues)
'?Profession Level A = level',
local limit = tonumber(args.limit or 0) or 0
'?Variant of #-= variant',
if limit <= 0 then
'sort = Variant of,Profession Level A',
limit = 500
'limit = 500'
end
}
local results = mw.smw.ask(query)
for _,page in ipairs(results) do
-- Query for data
if string.find(page.name,'Mine') then
local smw_data = mw.smw.ask{
page.node = page.name
'[[Uses item::' .. item .. ']] OR [[Activity input::' .. item .. ']] OR [[Activity container::' .. item .. ']]',
page.nodevar = page.variant
'?Recipe JSON',
else
'?Activity JSON',
page.node = page.name .. ' (skill node)'
showValues and '?Value',
page.nodevar = page.variant .. ' (skill node)'
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 result = mw.smw.ask('[['..page.node..']]|?Activity JSON = data')
local parsed = {}
page.JSON = (result and result[1] and result[1].data) or ''
for _, json in ipairs(jsons) do
if type(page.JSON)=='table' then
local json = mw.text.jsonDecode(json)
page.JSON = table.concat(page.JSON,'<br>')
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
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 align-right-1 sortable')
:addClass('wikitable sortable')
:tag('tr')
:tag('tr')
:tag('th')
:tag('th')
:attr{ colspan = '3' }
:wikitext('Variant')
:wikitext('Recipe')
:done()
: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 _, recipe in ipairs(recipes) do
for i, item in ipairs(results) do
local row = out:tag('tr')
local row = out:tag('tr')
--level
:tag('td')
:tag('td')
:wikitext('[['..item.variant..']]')
:css{ ['border-right'] = '0', ['padding-right'] = '0' }
:attr{ ['data-sort-value'] = recipe.output[1].name }
:wikitext(recipe.output[1].quantity .. ' &times;')
:done()
:done()
:tag('td')
:tag('td')
:addClass('plinkt-image no-border')
:wikitext('[['..item.nodevar..']]')
: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')
:wikitext(item.level)
:addClass('plinkt-link no-border')
:wikitext('[[' .. recipe.output[1].name .. ']]')
:done()
:done()
:tag('td')
:tag('td')
:IF(recipe.profession)
:wikitext('[['..item.name..']]')
: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')

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

:done()
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
return out


return out
end
end



Latest revision as of 16:27, 23 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.

local p = {}

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

function p.gathererPages2()
	local queryString = '[[Variant of::Bone Spike||Femur Shard||Goat Horn||Rams Horn]]'
	return p.main(queryString)
end

function p.gathererPages3()
	local queryString = '[[Variant of::Leek||Bitterfruit||Tangfruit]]'
	return p.main(queryString)
end

function p.main(queryString)

	local query = {
		queryString,
		'? #- = name',
		'?Profession Level A = level',
		'?Variant of #-= variant',
		'sort = Variant of,Profession Level A',
		'limit = 500'
	}
	local results = mw.smw.ask(query)
	
	for _,page in ipairs(results) do
		if string.find(page.name,'Mine') then
			page.node = page.name
			page.nodevar = page.variant
		else
			page.node = page.name .. ' (skill node)'
			page.nodevar = page.variant .. ' (skill node)'
		end
		local result = mw.smw.ask('[['..page.node..']]|?Activity JSON = data')
		page.JSON = (result and result[1] and result[1].data) or ''
		if type(page.JSON)=='table' then
			page.JSON = table.concat(page.JSON,'<br>')
		end
	end
	
	local out = mw.html.create('table')
		:addClass('wikitable sortable')
		:tag('tr')
			:tag('th')
				:wikitext('Variant')
			:done()
			: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()

	for i, item in ipairs(results) do
		local row = out:tag('tr')
		
			--level
			:tag('td')
				:wikitext('[['..item.variant..']]')
			:done()
			:tag('td')
				:wikitext('[['..item.nodevar..']]')
			:done()
			:tag('td')
				:wikitext(item.level)
			:done()
			:tag('td')
				:wikitext('[['..item.name..']]')
			:done()
			:tag('td')
				:wikitext('[['..item.node..']]')
			:done()
			:tag('td')
				:wikitext(item.JSON)
			:done()
			
	end
	
	return out

end

return p