Module:Mmgsection: Difference between revisions

From Roat Pkz
Jump to navigation Jump to search
Bosses>Shayani
(list mmgs are that obsolete in maintenance category + friendlier error for nonexistent mmgs)
 
m (1 revision imported)
 
(No difference)

Latest revision as of 22:16, 30 March 2024

Documentation for this module may be created at Module:Mmgsection/doc

require('strict')

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

-- funcs from [[Module:Mmgtable]]
local function sigfig(x, p)
	local x_sign = x < 0 and '-1' or '1'
	local x = math.abs(x)
	if x == 0 then
		return 0
	end
	local n = math.floor(math.log10(x)) + 1 - p
	return tonumber(x_sign) * math.pow(10,n) * round(x/math.pow(10, n), 0)
end

local function autoround(x, f)
	x = tonumber(x) or 0
	local _x
	if x < 0.1 and x > -0.1 then
		_x = sigfig(x,2)
	elseif x >= 100 or x <= -100 then
		_x = round(x, 0)
	else
		_x = round(x, 2)
	end
	if f then
		return lang:formatNum(_x)
	end
	return _x
end

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()
	
	-- redo query for recurring
	if not smwData[1].json then
		query = {
			'[[Money making guide/' .. page .. ']]',
			'?MMG recurring JSON = json'
		}
		t1 = os.clock()
		smwData = mw.smw.ask(query)
		t2 = os.clock()
	end
	
	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

local function addtblrow(row, page, price, skill)
	row:tag('tr')
		:tag('td')
			:wikitext('[[Money making guide/' .. page .. '|' .. page .. ']]')
			:done()
		:tag('td')
			:tag('span')
				:addClass('coins')
				:wikitext( _coins( autoround(price) , 'nocoins') )
				:done()
			:done()
		:tag('td')
			:addClass('plainlist')
			:wikitext('\n' .. skill)
			:done()
		:done()
end

function p._main(args)
	local tbl = mw.html.create('table'):addClass('wikitable align-right-2')
	local cats = ''
	local strplural, normalmmg, recurringmmg = false, false, false
	local normalrows, recurringrows = mw.html.create(''), mw.html.create('')
	
	for i,v in ipairs(args) do
		local mmgpagecontent = mw.title.new('Money making guide/' .. args[i]):getContent()
		
		assert(mmgpagecontent, '"Money making guide/' .. args[i] .. '" does not exist')
		local data = mw.text.jsonDecode(mw.text.decode( loadData(args[i]) ))
		
		if data.prices.default_value then
			if not normalmmg then
				normalmmg = true
			end
			
			addtblrow(normalrows, args[i], data.prices.default_value, data.skill)
			
			if string.find( mmgpagecontent, 'Category:Obsolete money making guide' ) or
				data.prices.default_value <= 100000 and data.members or
				data.prices.default_value <= 20000 then
				cats = cats .. '[[Category:Pages with obsolete money making guides listed]]'
			end
			
		elseif data.prices.value then
			if not recurringmmg then
				recurringmmg = true
			end
			
			addtblrow(recurringrows, args[i], data.prices.value, data.skill)
			
			if data.prices.value <= 0 then
				cats = cats .. '[[Category:Pages with obsolete money making guides listed]]'
			end
		end
		
		if i == 2 then
			strplural = true
		end
	end
	
	if normalmmg then
		tbl:tag('tr')
			:tag('th')
				:wikitext('Method')
			:done()
			:tag('th')
				:wikitext('Hourly profit')
			:done()
			:tag('th')
				:wikitext('Skills')
			:done()
		:done()
		:node(normalrows)
	end
	
	if recurringmmg then
		tbl:tag('tr')
			:tag('th')
				:wikitext('Method')
			:done()
			:tag('th')
				:wikitext('Profit per instance')
			:done()
			:tag('th')
				:wikitext('Skills')
			:done()
		:done()
		:node(recurringrows)
	end
	
	local str = string.format('<p>The following [[money making guide]]%s %s available for %s:</p>',
		(strplural and 's' or ''),
		(strplural and 'are' or 'is'),
		(args.mobname or mw.title.getCurrentTitle().text or 'where\'s da page name ???')
		)
	
	return str .. tostring( tbl ) .. cats
end

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

return p