Module:Sandbox/User:Shayani: Difference between revisions

From Illerai
Jump to navigation Jump to search
illerai>Shayani
mNo edit summary
 
m 1 revision imported
 
(No difference)

Latest revision as of 22:26, 2 November 2024

Documentation for this module may be created at Module:Sandbox/User:Shayani/doc

require('strict')

local p = {}
local _coins = require('Module:Currency')._amount

local function loadData(page)
	local query = {
		'[[Money making guide/' .. page .. ']]',
		'?MMG JSON = json'
	}
	local t1 = os.clock()
	local smwData = mw.smw.ask(query)
	local t2 = os.clock()
	assert(smwData ~= nil and #smwData > 0, 'SMW query failed')
	mw.log(string.format('HasMMG table SMW: entries %d, time elapsed: %.3f ms.', #smwData, (t2 - t1) * 1000))
	
	return smwData[1].json
end

function p._main(args)
	local tbl = mw.html.create('table'):addClass('wikitable align-right-2')
		:tag('tr')
			:tag('th')
				:wikitext('Method')
			:done()
			:tag('th')
				:wikitext('Hourly profit')
			:done()
			:tag('th')
				:wikitext('Skills')
			:done()
		:done()

	local strplural = false
	mw.log(strplural)
	for i,v in ipairs(args) do
		local data = mw.text.jsonDecode(mw.text.decode( loadData(args[i]) ))
		
		tbl:tag('tr')
			:tag('td')
				:wikitext('[[Money making guide/' .. args[i] .. '|' .. args[i] .. ']]')
				:done()
			:tag('td')
--				:attr('data-sort-value', val)
				:tag('span')
					:addClass('coins')
					:wikitext( _coins(data.prices.default_value, 'nocoins') )
					:done()
				:done()
			:tag('td')
				:addClass('plainlist')
				:wikitext('\n' .. data.skill)
				:done()
			:done()
		
		if i == 2 then
			strplural = true
		end
	end
	mw.log(strplural)
	
	local str = string.format('<p>The following page%s %s available as [[money making guide]]%s for %s:</p>',
		(strplural and 's' or ''),
		(strplural and 'are' or 'is'),
		(strplural and 's' or ''),
		(args.mobname or mw.title.getCurrentTitle().text or 'where\'s da page name ???')
		)
	
	return str .. tostring( tbl )
end

function p.main(frame)
	local args = frame:getParent().args
	return p._main(args)
end

return p