Module:Sandbox/User:Alsang

From Brighter Shores Wiki
Revision as of 19:50, 17 December 2024 by Alsang (talk | contribs)
Jump to navigation Jump to search
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.
Module:Sandbox/User:Alsang requires Module:Currency.
Module:Sandbox/User:Alsang requires Module:Mw.html extension.
Module:Sandbox/User:Alsang requires Module:Paramtest.
Module:Sandbox/User:Alsang requires strict.

require('strict')
require('Module:Mw.html extension')
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 = {
		'[[Activity JSON::~*]]',
		'? = name',
		'?Activity JSON = activityJSON',
		'limit = 1500'
	}
	local results = mw.smw.ask(query)
	
	results = p.screenResults(results)

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

end

-- goes through a list of results and culls the ones which are not needed for this table
function p.screenResults(results)
	local resultsScreened = {}
	
	-- iterate through products
	for _, item in ipairs(results) do
		
		-- if theres only one activity, wrap in table
		if type(item.activityJSON)=='string' then
			item.activityJSON = { item.activityJSON }
		end
		
		for j,json in ipairs(item.activityJSON) do
		
			local activityJSON = mw.text.jsonDecode(json)
			
			if activityJSON.duration and tonumber(activityJSON.duration)==20 then
				local itemNew = {}
				itemNew.name = item.name
				itemNew.XP = activityJSON.xp
				itemNew.lvl = activityJSON.level
				itemNew.profession = activityJSON.profession
				itemNew.duration = activityJSON.duration
				itemNew.product = activityJSON.output[1].name
				itemNew.XPperHour = itemNew.XP and tonumber(itemNew.XP) and itemNew.duration and tonumber(itemNew.duration) and tonumber(itemNew.XP) * 3600 / tonumber(itemNew.duration)
				table.insert(resultsScreened,itemNew)
			end
		end
	end
	
	-- sort the results by recipe level
	table.sort(resultsScreened, function(item1, item2)
		local lvl1 = item1.lvl
		local lvl2 = item2.lvl
		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
		end
		if lvl1 == nil then
			return false  -- Both empty, equivalent
		end
		return lvl1 < lvl2 -- normal comparison
	end)

	return resultsScreened

end

-- make the table
function p.displayTable(results)
	local out = mw.html.create('table')
		:addClass('wikitable sortable')
		:tag('tr')
			:tag('th')
				:wikitext('Page')
			:done()
			:tag('th')
				:wikitext('Product')
			:done()
			:tag('th')
				:wikitext('Profession')
			:done()
			:tag('th')
				:wikitext('Level')
			:done()
			:tag('th')
				:wikitext('XP')
			:done()
			:tag('th')
				:wikitext('Duration')
			:done()
			:tag('th')
				:wikitext('XP/hr')
			:done()
		:done()

	for i, item in ipairs(results) do
		local row = out:tag('tr')
		
			--level
			:tag('td')
				:wikitext(item.name)
			:done()
			:tag('td')
				:wikitext('[[' .. item.product .. ']]')
			:done()
			:tag('td')
				:wikitext('[[' .. item.profession .. ']]')
			:done()
			:tag('td')
				:wikitext(item.lvl)
			:done()
			:tag('td')
				:wikitext(item.XP)
			:done()
			:tag('td')
				:wikitext(item.duration)
			:done()
			:tag('td')
				:wikitext(item.XPperHour)
			:done()

		:done()
	end

	return out
end

return p