Module:Sandbox/User:Alsang/NodeDescriptionChecker: Difference between revisions

From Brighter Shores Wiki
Jump to navigation Jump to search
Content added Content deleted
No edit summary
(working for stonemason, need to check all data)
Line 1: Line 1:
require('strict')
require('strict')
require('Module:Mw.html extension')
require('Module:Mw.html extension')
local recipe = require('Module:Infobox Recipe') -- to make use of its extensive material searching function
local search = require('Module:RecipeTreeSearch')
local param = require( 'Module:Paramtest' )
local param = require( 'Module:Paramtest' )
local currency = require('Module:Currency')
local currency = require('Module:Currency')
local discount = require('Module:MerchantHideDiscount')
local lang = mw.getContentLanguage()
local lang = mw.getContentLanguage()


Line 13: Line 12:
-- returns only directly needed parameter needed for the row,
-- returns only directly needed parameter needed for the row,
-- other parameters are determined by subqueries of chained pages
-- other parameters are determined by subqueries of chained pages
-- this is in testing, uncomment out one line to test that type of query
-- requires pages to be made and have recipes on them or it tends to get an error
-- query types that include non-recipe passive activities behave weird and need to be fixed
local query = {
local query = {
--'[[Uses facility::One Handed Range (bonewright)||One Handed Melee Workbench (bonewright)||Two Handed Range Workbench (bonewright)||Two Handed Melee Workbench (bonewright)||Shield Vice (bonewright)]]', -- bonewright active
--'[[Uses facility::One Handed Range (bonewright)||One Handed Melee Workbench (bonewright)||Two Handed Range Workbench (bonewright)||Two Handed Melee Workbench (bonewright)||Shield Vice]', -- bonewright active
'[[Uses facility::One Handed Ranged Workbench (stonemason)||One Handed Melee Workbench (stonemason)||Two Handed Ranged Workbench (stonemason)||Two Handed Melee Workbench (stonemason)||Shield Work Rock]]', -- stonemason active
'[[Variant of::Knickknacks||Divination||Sabertooth Cat Skeleton||Wooly Mammoth Skeleton||Triceratops Skeleton||T-Rex Skeleton||Wendigo Skeleton||Bonemeal Cement]]', -- bonewright passive
--'[[Uses facility::One Handed Range (stonemason)||One Handed Melee Workbench (stonemason)||Two Handed Range Workbench (stonemason)||Two Handed Melee Workbench (stonemason)||Shield Vice (stonemason)]]', -- stonemason active
--'[[Uses facility::Goblin Forge||Gnome Forge (skill node)]]', -- blacksmith active
--'[[Uses facility::Goblin Forge||Gnome Forge (skill node)]]', -- blacksmith active
'?Variant of #- = variant',
'?Profession Level A = lvlLow',
'?Profession Level A = lvl',
'?Profession Level A High = lvlHigh',
'?Uses facility = facility',
'? #- = name',
'? #- = name',
'?Recipe JSON = recipeJSON',
'?Recipe JSON = recipeJSON',
Line 29: Line 24:
'?Activity duration = duration',
'?Activity duration = duration',
'?Value = sell',
'?Value = sell',
'?Activity coins = coins',
'sort = Activity level',
'sort = Profession Level A',
'limit = 500'
'limit = 500'
}
}
local results = mw.smw.ask(query)
local results = mw.smw.ask(query)

results = p.screenResults(results,false)
-- criterion for if an entry is a recipe, or a passive activity

for _, item in ipairs(results) do
item.passiveActivity = not(item.variant=='Knickknacks' or item.variant=='Bonemeal Cement' )
end
results = p.formatResults(results)
results = p.formatResults(results)


Line 61: Line 52:
end
end
return currency._cell(amount, { html = 'yes' })
return currency._cell(amount, { html = 'yes' })
end

-- Need to split the table into two, lines with crates are passive and lines without are not
function p.screenResults(results,isPassive)
local resultsPassive = {}
local resultsNotPassive = {}
-- iterate through products
for _, item in ipairs(results) do
local hasCrate = item.recipeJSON and string.find(item.recipeJSON,'Crate')
if hasCrate then
table.insert(resultsPassive,item)
else
table.insert(resultsNotPassive,item)
end
end
if isPassive then
return resultsPassive
else
return resultsNotPassive
end
end
end


-- do calculations and determine strings to go in cells
-- do calculations and determine strings to go in cells
function p.formatResults(results)
function p.formatResults(results)

-- iterate through products
-- iterate through products
for _, item in ipairs(results) do
for _, item in ipairs(results) do
-- recipe in a workable format
-- New module for recipe searching
local fullRecipe = search.main(item.name)
if item.passiveActivity then
item.materials = fullRecipe.materials
item.outputQuantity = fullRecipe.output[1].quantity
-- if it is a passive activity that is not a recipe
item.buy = fullRecipe.buyPrice
-- some fields are easy to work out
item.outputQuantity = 1
item.XP = fullRecipe.xp
item.materials = {}
item.duration = fullRecipe.duration
item.intermediates = {}
-- specific to weapon crafting, where properties are a bit different
item.buy = 0
item.sell = item.coins
item.lvl = fullRecipe.level
-- iterate through materials, keep track of how many materials are needed from each shop
item.poleShopTrips = 0
item.stoneShopTrips = 0
for _, material in ipairs(item.materials) do
-- query which shop sold it, add to running total
local shopNameQuery = '[[:+]][[Sold item::' .. material.name .. ']]|?Sold by #-|mainlabel=' .. material.name
local shopNameResult = mw.smw.ask(shopNameQuery) or {}
local shopName = ''


if shopNameResult[1] and shopNameResult[1]["Sold by"] then
else
shopName = shopNameResult[1]["Sold by"] or ''
local unpackJSON = mw.text.jsonDecode(item.recipeJSON)
item.outputQuantity = unpackJSON.output[1].quantity
-- call the module:infobox recipe to extensively search for
-- all raw materials (to add to prices)
-- all intermediate materials (for intermediate XP and duration)
local Materials = recipe._getTrueRawMaterials(unpackJSON.materials)
if next(Materials) ~= nil then
item.materials = Materials.rawMaterials
item.intermediates = Materials.intermediateMaterials
end
end
if shopName=="Timber Merchant Shop" then
-- iterate through materials, adding buy price to running total (individuals not needed)
item.poleShopTrips = item.poleShopTrips + material.quantity
item.buy = 0
elseif shopName=="Poffit's Interesting Rocks" then
for _, material in ipairs(item.materials) do
item.stoneShopTrips = item.stoneShopTrips + material.quantity
--shamelessley lifted from Module:Products
local shopPriceQuery = '[[:+]][[Sold item::' .. material.name .. ']]|?Shop buy price|mainlabel=' .. material.name
local shopPriceResult = mw.smw.ask(shopPriceQuery) or {}
local shopPrice = 0
if shopPriceResult[1] and shopPriceResult[1]["Shop buy price"] then
shopPrice = tonumber(shopPriceResult[1]["Shop buy price"]) or 0
else
item.buy = nil
end
item.buy = item.buy and item.buy + shopPrice * material.quantity
end
end

-- iterate through intermediaries to add to XP and duration parameters
for _, intermediate in ipairs(item.intermediates) do
-- look up activity XP and add it to running total
local XPQuery = '[[Recipe output::' .. intermediate.name .. ']]|?Activity XP|mainlabel=' .. intermediate.name
local XPResult = mw.smw.ask(XPQuery) or {}
local XPIncrease = 0
if XPResult[1] and XPResult[1]["Activity XP"] then
XPIncrease = tonumber(XPResult[1]["Activity XP"]) or 0
else
XPIncrease = nil
end
-- need to look up both the amount of the item used in this recipe and created in its own recipe to know how much of this xp to use
local QTYused = intermediate.quantity
local QTYQuery = '[[Recipe output::' .. intermediate.name .. ']]|?Recipe JSON|mainlabel=' .. intermediate.name
local QTYResult = mw.smw.ask(QTYQuery) or {}
local QTYmade = 0
if QTYResult[1] and QTYResult[1]["Recipe JSON"] then
QTYmade = mw.text.jsonDecode(QTYResult[1]["Recipe JSON"]) or {}
end
QTYmade = QTYmade.output[1].quantity
item.XP = item.XP and XPIncrease and item.XP + XPIncrease * QTYused / QTYmade;
-- look up activity duration and add it to running total
local durationQuery = '[[Recipe output::' .. intermediate.name .. ']]|?Activity duration|mainlabel=' .. intermediate.name
local durationResult = mw.smw.ask(durationQuery) or {}
local durationIncrease = 0
if durationResult[1] and durationResult[1]["Activity duration"] then
durationIncrease = tonumber(durationResult[1]["Activity duration"]) or 0
else
durationIncrease = nil
end
item.duration = item.duration and durationIncrease and item.duration + durationIncrease * intermediate.quantity
end
end
end
-- direct values
-- direct values
item.sell = item.sell and item.outputQuantity and item.sell * item.outputQuantity
item.sell = item.sell and item.outputQuantity and item.sell * item.outputQuantity
item.profit = item.sell and item.buy and item.sell - item.buy
item.costPerXP = item.buy and item.XP and math.floor(item.buy / item.XP * 100) / 100
item.profitPerXP = item.profit and item.XP and math.floor(item.profit / item.XP * 100) / 100
item.XPPerStone = item.XP and not(item.stoneShopTrips==0) and math.floor(item.XP / item.stoneShopTrips * 100) / 100
item.XPPerSpace = item.XPPerStone and item.poleShopTrips and math.floor(item.XP / (item.stoneShopTrips + item.poleShopTrips) * 100) / 100


-- there will be extra time spent buying and selling items, and moving between stations
-- a lot of downtime in leatherworker
-- assume make enough trips to buy 24 of each material from each shop, per batch of 24 weapons
-- 1min 40s to go from hide stall to tannery, run round the tannery, run to leather stall, then back to hide stall
local batchSize = 24
local batchSize = 24
-- 120 seconds moving between stations and selling items per 24 made
local downtime = 100
-- 40 seconds per trip to the pole shop
-- 40 seconds per trip to the stone shop
-- are these results optimal? no. are they approximate? yes
local downtime = 120 + item.poleShopTrips * 40 + item.stoneShopTrips * 40
item.duration = item.duration and item.duration + downtime/batchSize
item.duration = item.duration and item.duration + downtime/batchSize
item.productPerHour = item.duration and 1 / item.duration * 3600
item.productPerHour = item.duration and 1 / item.duration * 3600
Line 168: Line 136:
-- properties per hour
-- properties per hour
item.XPPerHour = item.XP and item.productPerHour and math.floor(item.XP * item.productPerHour)
item.XPPerHour = item.XP and item.productPerHour and math.floor(item.XP * item.productPerHour)
item.profitPerHour = item.profit and item.productPerHour and math.floor(item.profit * item.productPerHour)
end
end


Line 181: Line 148:
:tag('th')
:tag('th')
:wikitext('[[File:Stonemason small icon.png|15px]] Level')
:wikitext('[[File:Stonemason small icon.png|15px]] Level')
:done()
:tag('th')
:wikitext('[[File:Hammermage small icon.png|15px]] Levels')
:done()
:done()
:tag('th')
:tag('th')
:attr{ colspan = '3' }
:attr{ colspan = '3' }
:wikitext('Product')
:wikitext('Products')
:done()
:done()
:tag('th')
:tag('th')
Line 194: Line 164:
:done()
:done()
:tag('th')
:tag('th')
:wikitext('XP<br>(per item)')
:attr{ colspan = '10' }
:wikitext('Sell Value')
:done()
:done()
:tag('th')
:tag('th')
:wikitext('XP<br>(per stome)')
:attr{ colspan = '10' }
:wikitext('Profit')
:done()
:done()
:tag('th')
:tag('th')
:wikitext('XP<br>(per space)')
:attr{ colspan = '10' }
:wikitext('Profit/hr')
:done()
:tag('th')
:wikitext('XP')
:done()
:done()
:tag('th')
:tag('th')
Line 228: Line 192:
:css{ ['text-align'] = 'center' }
:css{ ['text-align'] = 'center' }
:wikitext(item.lvl)
:wikitext(item.lvl)
:done()
:ELSE()
:node(unknown_value_cell)
:END()
:IF(item.lvlHigh and item.lvlLow)
:tag('td')
:css{ ['text-align'] = 'center' }
:wikitext(item.lvlLow)
:wikitext(' – ')
:wikitext(item.lvlHigh)
:done()
:done()
:ELSE()
:ELSE()
Line 247: Line 221:
:wikitext('[[' .. item.name .. ']]')
:wikitext('[[' .. item.name .. ']]')
:done()
:done()
:IF(not(item.passiveActivity))


local materialCell = row:tag('td')
local materialCell = row:tag('td')

for i, _ in ipairs(item.materials) do
for i, _ in ipairs(item.materials) do
materialCell:wikitext(item.materials[i].quantity .. '&times; [[File:' .. item.materials[i].name .. '.png|30px|link=' .. item.materials[i].name .. ']]' .. '[[' .. item.materials[i].name .. ']]<br>')
materialCell:wikitext(item.materials[i].quantity .. '&times; [[File:' .. item.materials[i].name .. '.png|18px|link=' .. item.materials[i].name .. ']] [[' .. item.materials[i].name .. ']]<br>')
end
end
row
:node(currency_cell(item.buy))
:node(currency_cell(item.sell))
:ELSE()
:tag('td')
:addClass('table-na')
:attr{ colspan = '21' }
:wikitext('N/A')
:done()
:END()
row
:node(currency_cell(item.profit))
:node(currency_cell(item.profitPerHour))
:node(currency_cell(item.buy))

:IF(item.XP)
:IF(item.XP)
:tag('td')
:tag('td')
:wikitext(item.XP and lang:formatNum(tonumber(item.XP)))
:wikitext(item.XP and lang:formatNum(tonumber(item.XP)))
:done()
:ELSE()
:node(unknown_value_cell)
:END()

:IF(item.XPPerStone)
:tag('td')
:wikitext(item.XPPerStone and lang:formatNum(tonumber(item.XPPerStone)))
:done()
:ELSE()
:node(unknown_value_cell)
:END()

:IF(item.XPPerSpace)
:tag('td')
:wikitext(item.XPPerSpace and lang:formatNum(tonumber(item.XPPerSpace)))
:done()
:done()
:ELSE()
:ELSE()
Line 288: Line 263:
:END()
:END()


:node(currency_cell(item.profitPerXP))
:node(currency_cell(item.costPerXP))


:done()
:done()

Revision as of 18:56, 7 December 2024

Module documentation
This documentation is transcluded from Module:Sandbox/User:Alsang/NodeDescriptionChecker/doc. [edit] [history] [purge]
This module does not have any documentation. Please consider adding documentation at Module:Sandbox/User:Alsang/NodeDescriptionChecker/doc. [edit]
Module:Sandbox/User:Alsang/NodeDescriptionChecker's function main is invoked by Template:Sandbox/User:Alsang/NodeDescriptionChecker.
Module:Sandbox/User:Alsang/NodeDescriptionChecker requires Module:Currency.
Module:Sandbox/User:Alsang/NodeDescriptionChecker requires Module:Mw.html extension.
Module:Sandbox/User:Alsang/NodeDescriptionChecker requires Module:Paramtest.
Module:Sandbox/User:Alsang/NodeDescriptionChecker requires Module:RecipeTreeSearch.
Module:Sandbox/User:Alsang/NodeDescriptionChecker requires strict.

require('strict')
require('Module:Mw.html extension')
local search = require('Module:RecipeTreeSearch')
local param = require( 'Module:Paramtest' )
local currency = require('Module:Currency')
local lang = mw.getContentLanguage()

local p = {}

-- non dynamic module, no inputs
function p.main()
	-- returns only directly needed parameter needed for the row,
	-- other parameters are determined by subqueries of chained pages
	local query = {
		--'[[Uses facility::One Handed Range (bonewright)||One Handed Melee Workbench (bonewright)||Two Handed Range Workbench (bonewright)||Two Handed Melee Workbench (bonewright)||Shield Vice]', -- bonewright active
		'[[Uses facility::One Handed Ranged Workbench (stonemason)||One Handed Melee Workbench (stonemason)||Two Handed Ranged Workbench (stonemason)||Two Handed Melee Workbench (stonemason)||Shield Work Rock]]', -- stonemason active 
		--'[[Uses facility::Goblin Forge||Gnome Forge (skill node)]]', -- blacksmith active
		'?Profession Level A = lvlLow',
		'?Profession Level A High = lvlHigh',
		'?Uses facility = facility',
		'? #- = name',
		'?Recipe JSON = recipeJSON',
		'?Activity XP = XP',
		'?Activity duration = duration',
		'?Value = sell',
		'sort = Activity level',
		'limit = 500'
	}
	local results = mw.smw.ask(query)

	results = p.screenResults(results,false)

	results = p.formatResults(results)

	return p.displayTable(results)
	
	--for debugging
	--return '<pre>'..mw.text.jsonEncode(results, mw.text.JSON_PRETTY)..'</pre>'

end

-- makes the html for the cells containing currency directly
-- Replaces nil with an "unknown" cell
local function currency_cell(amount)
	if not amount then
		return mw.html.create('td')
			:addClass('table-bg-gray')
			:css{ ['text-align'] = 'center' }
			:attr{ colspan = '10' }
			:wikitext("''unknown''")
		:done()
	end
	return currency._cell(amount, { html = 'yes' })
end

-- Need to split the table into two, lines with crates are passive and lines without are not
function p.screenResults(results,isPassive)
	local resultsPassive = {}
	local resultsNotPassive = {}
	
	-- iterate through products
	for _, item in ipairs(results) do
		local hasCrate = item.recipeJSON and string.find(item.recipeJSON,'Crate')
		
		if hasCrate then
			table.insert(resultsPassive,item)
		else
			table.insert(resultsNotPassive,item)
		end
		
	end
	
	if isPassive then
		return resultsPassive
	else
		return resultsNotPassive
	end
end

-- do calculations and determine strings to go in cells
function p.formatResults(results)

	-- iterate through products
	for _, item in ipairs(results) do
		
		-- New module for recipe searching
		local fullRecipe = search.main(item.name)
		item.materials = fullRecipe.materials
		item.outputQuantity = fullRecipe.output[1].quantity
		item.buy = fullRecipe.buyPrice
		item.XP = fullRecipe.xp
		item.duration = fullRecipe.duration
		
		-- specific to weapon crafting, where properties are a bit different
		item.lvl = fullRecipe.level
		
		-- iterate through materials, keep track of how many materials are needed from each shop
		item.poleShopTrips = 0
		item.stoneShopTrips = 0
		for _, material in ipairs(item.materials) do
		
			-- query which shop sold it, add to running total
			local shopNameQuery = '[[:+]][[Sold item::' .. material.name .. ']]|?Sold by #-|mainlabel=' .. material.name
			local shopNameResult = mw.smw.ask(shopNameQuery) or {}
			local shopName = ''

			if shopNameResult[1] and shopNameResult[1]["Sold by"] then
				shopName = shopNameResult[1]["Sold by"] or ''
			end
			
			if shopName=="Timber Merchant Shop" then
				item.poleShopTrips = item.poleShopTrips + material.quantity
			elseif shopName=="Poffit's Interesting Rocks" then
				item.stoneShopTrips = item.stoneShopTrips + material.quantity
			end

		end
		
		-- direct values
		item.sell = item.sell and item.outputQuantity and item.sell * item.outputQuantity
		item.costPerXP = item.buy and item.XP and math.floor(item.buy / item.XP * 100) / 100
		item.XPPerStone = item.XP and not(item.stoneShopTrips==0) and math.floor(item.XP / item.stoneShopTrips * 100) / 100
		item.XPPerSpace = item.XPPerStone and item.poleShopTrips and math.floor(item.XP / (item.stoneShopTrips + item.poleShopTrips) * 100) / 100

		-- there will be extra time spent buying and selling items, and moving between stations
		-- assume make enough trips to buy 24 of each material from each shop, per batch of 24 weapons
		local batchSize = 24
		-- 120 seconds moving between stations and selling items per 24 made
		-- 40 seconds per trip to the pole shop
		-- 40 seconds per trip to the stone shop
		-- are these results optimal? no. are they approximate? yes
		local downtime = 120 + item.poleShopTrips * 40 + item.stoneShopTrips * 40
		item.duration = item.duration and item.duration + downtime/batchSize
		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)
	end

	return results
end

-- make the table
function p.displayTable(results)
	local out = mw.html.create('table')
		:addClass('wikitable sortable')
		:tag('tr')
			:tag('th')
				:wikitext('[[File:Stonemason small icon.png|15px]]  Level')
			:done()
			:tag('th')
				:wikitext('[[File:Hammermage small icon.png|15px]] Levels')
			:done()
			:tag('th')
				:attr{ colspan = '3' }
				:wikitext('Products')
			:done()
			:tag('th')
				:wikitext('Materials')
			:done()
			:tag('th')
				:attr{ colspan = '10' }
				:wikitext('Buy Value')
			:done()
			:tag('th')
				:wikitext('XP<br>(per item)')
			:done()
			:tag('th')
				:wikitext('XP<br>(per stome)')
			:done()
			:tag('th')
				:wikitext('XP<br>(per space)')
			:done()
			:tag('th')
				:wikitext('XP/hr')
			:done()
			:tag('th')
				:attr{ colspan = '10' }
				:wikitext('Coins/XP')
			:done()
		:done()

	local unknown_value_cell = mw.html.create('td')
		:addClass('table-bg-gray')
		:css{ ['text-align'] = 'center' }
		:wikitext("''unknown''")

	for i, item in ipairs(results) do
		local row = out:tag('tr')
			:IF(item.lvl)
				:tag('td')
					:css{ ['text-align'] = 'center' }
					:wikitext(item.lvl)
				:done()
			:ELSE()
				:node(unknown_value_cell)
			:END()
			:IF(item.lvlHigh and item.lvlLow)
				:tag('td')
					:css{ ['text-align'] = 'center' }
					:wikitext(item.lvlLow)
					:wikitext(' – ')
					:wikitext(item.lvlHigh)
				:done()
			:ELSE()
				:node(unknown_value_cell)
			:END()

			:tag('td')
				:css{ ['border-right'] = '0', ['padding-right'] = '0', ['text-align'] = 'right' }
				:attr{ ['data-sort-value'] = item.name }
				:wikitext(item.outputQuantity .. ' &times;')
			:done()
			:tag('td')
				:addClass('plinkt-image no-border')
				:css{ ['border-left'] = '0', ['padding-left'] = '0' }
				:wikitext('[[File:' .. item.name .. '.png|link=' .. item.name .. '|30px]]')
			:done()
			:tag('td')
				:addClass('plinkt-link no-border')
				:wikitext('[[' .. item.name .. ']]')
			:done()

		local materialCell = row:tag('td')

		for i, _ in ipairs(item.materials) do
			materialCell:wikitext(item.materials[i].quantity .. '&times; [[File:' .. item.materials[i].name .. '.png|18px|link=' .. item.materials[i].name .. ']] [[' .. item.materials[i].name .. ']]<br>')
		end
		
		row
			:node(currency_cell(item.buy))

			:IF(item.XP)
				:tag('td')
					:wikitext(item.XP and lang:formatNum(tonumber(item.XP)))
				:done()
			:ELSE()
				:node(unknown_value_cell)
			:END()

			:IF(item.XPPerStone)
				:tag('td')
					:wikitext(item.XPPerStone and lang:formatNum(tonumber(item.XPPerStone)))
				:done()
			:ELSE()
				:node(unknown_value_cell)
			:END()

			:IF(item.XPPerSpace)
				:tag('td')
					:wikitext(item.XPPerSpace and lang:formatNum(tonumber(item.XPPerSpace)))
				:done()
			:ELSE()
				:node(unknown_value_cell)
			:END()

			:IF(item.XPPerHour)
				:tag('td')
					:wikitext(item.XPPerHour and lang:formatNum(tonumber(item.XPPerHour)))
				:done()
			:ELSE()
				:node(unknown_value_cell)
			:END()

			:node(currency_cell(item.costPerXP))

		:done()
	end

	return out
end

return p