Module:Mmgtable/displaysandbox

From RuneRealm Wiki
Jump to navigation Jump to search

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

-- <nowiki>
local timefunc = require('Module:Time')
local exg = require('Module:Exchange')._price
local p = {}
local lang = mw.getContentLanguage()

function gep(x)
	return exg(x, 1, nil, nil, 0)
end

local MEMBERS_ICON = {
    [false] = "[[File:Free-to-play icon.png|20px|center|link=Free-to-play]]",
    [true]  = "[[File:Member icon.png|20px|center|link=Members]]"
}

function round1k(x, f)
	if not tonumber(x) then
		return x
	end
	local _x = math.abs(x)
	_x = 1000 * math.floor(_x / 1000 + 0.5)
	if x < 0 then
		_x = _x * -1
	end
	if f then
		return lang:formatNum(_x)
	end
	return _x
end
function round1dp(x, f)
	if not tonumber(x) then
		return x
	end
	local _x = math.abs(x)
	_x = math.floor(_x * 10 + 0.5) / 10
	if x < 0 then
		_x = _x * -1
	end
	if f then
		return lang:formatNum(_x)
	end
	return _x
end

function calc_value(args, kph)
	local total = 0
	for i,v in ipairs(args) do
		local val
		if v.pricetype == 'value' then
			val = v.qty * v.value
		elseif v.pricetype == 'gemw' then
			val = gep(v.name) * v.qty
		end
		if kph>0 and not v.isph then
			val = val * kph
		end
		total = total + val
	end
	return total
end

function make_row(fullpagename, raw_data, ismulti)
	local data = mw.text.jsonDecode(mw.text.decode(raw_data))
	local tr = mw.html.create('tr')
	local pagename, pagelink
	pagelink = fullpagename..'|'..string.match(fullpagename, '/(.*)')
	if ismulti and data.version then
		pagename = '[['..pagelink..' ('..data.version..')]]'
	else
		pagename = '[['..pagelink..']]'
	end
	
	local rowcat = data.category
	if data.skillcategory then
		rowcat = rowcat .. '/' .. data.skillcategory
	end
	
	local val, c_class
	local inputval = calc_value(data.inputs, data.prices.default_kph or 0)
	local outputval = calc_value(data.outputs, data.prices.default_kph or 0)
	val = outputval - inputval
	if val > 0 then
		c_class = 'coins-pos'
	elseif val < 0 then
		c_class = 'coins-neg'
	else
		c_class = ''
	end
	
	local intensity = (data.intensity == 'High') and 3 or (data.intensity == 'Moderate') and 2 or (data.intensity == 'Low') and 1 or 0

	tr	:addClass('mmg-list-table-row')
		:tag('td')
			:wikitext(pagename)
			:css('max-width', '300px')
			:css('word-wrap', 'break-word')
		:done()
		:tag('td')
			:addClass('mmg-list-table-profit-cell')
			:tag('span')
				:addClass('coins')
				:addClass(c_class)
				:wikitext(round1k(val, true))
			:done()
		:done()
		:tag('td')
			:addClass('mmg-list-table-kph-cell')
			:wikitext(data.prices.default_kph)
			:css('max-width', '125px')
			:css('min-width', '125px')
		:done()
		:tag('td')
			:addClass('plainlist')
			:newline()
			:wikitext(data.skill)
			:css('max-width', '400px')
			:css('word-wrap', 'break-word')
		:done()
		:tag('td')
			:wikitext(rowcat)
		:done()
		:tag('td')
			:wikitext(data.intensity)
			:attr('data-sort-value', intensity)
		:done()
		:tag('td')
			:wikitext(MEMBERS_ICON[data.members])
		:done()
	return val, data.category, tr
end

function p.main(frame)
	local args = frame:getParent().args
	local query = {'[[MMG JSON::+]]', '?MMG JSON', '?=#', limit=10000}
	if args[1] then
		table.insert(query, 2, '[[Category:'..args[1]..']]')
	end
	local data = mw.smw.ask(query)
	
	local t = mw.html.create('table')
	
	t	:addClass('wikitable sortable sticky-header align-right-2 align-center-5 align-center-6 align-center-7 mmg-list-table')
		:tag('tr')
			:tag('th')
				:wikitext('Method')
				:css('word-wrap', 'break-word')
				:css('max-width', '300px')
			:done()
			:tag('th')
				:wikitext('Hourly Profit')
				:css('max-width', '100px')
			:done()
			:tag('th')
				:wikitext('Actions per hour')
				:addClass('unsortable')
			:done()
			:tag('th')
				:wikitext('Skills')
				:css('word-wrap', 'break-word')
				:css('max-width', '400px')
				:addClass('unsortable')
			:done()
			:tag('th')
				:wikitext('Category')
			:done()
			:tag('th')
				:wikitext('Intensity')
				:css('width', '65px')
			:done()
			:tag('th')
				:wikitext('Members')
				:css('width', '65px')
			:done()
			
	local methods = {}
	for i,v in ipairs(data) do
		if(string.find(v[1], 'Money making guide/')) then
			if type(v['MMG JSON']) == 'table' then
				for j,u in ipairs(v['MMG JSON']) do
					table.insert(methods, { make_row(v[1], u, true) })
				end
			else
				table.insert(methods, { make_row(v[1], v['MMG JSON'], false) })
			end
		end
	end
	table.sort(methods, function(a,b) return a[1]>b[1] end)
	for i,v in ipairs(methods) do
		if v[1] > 0 then
			t:newline():node(v[3])
		end
	end
	return t
end



function make_rec_row(fullpagename, raw_data, ismulti)
	local data = mw.text.jsonDecode(mw.text.decode(raw_data))
	local tr = mw.html.create('tr')
	local pagename, pagelink
	pagelink = fullpagename..'|'..string.match(fullpagename, '/(.*)')
	if ismulti and data.version then
		pagename = '[['..pagelink..' ('..data.version..')]]'
	else
		pagename = '[['..pagelink..']]'
	end
	
	local val = calc_value(data.outputs, 0) - calc_value(data.inputs, 0)
	local c_class
	if val > 0 then
		c_class = 'coins-pos'
	elseif val < 0 then
		c_class = 'coins-neg'
	else
		c_class = ''
	end
	tr	:tag('td')
			:wikitext(pagename)
		:done()
		:tag('td')
			:attr('data-sort-value', val)
			:tag('span')
				:addClass('coins')
				:addClass(c_class)
				:wikitext(round1k(val, true))
			:done()
		:done()
		:tag('td')
			:wikitext(timefunc._m_to_c(tostring(data.time)))
		:done()
		:tag('td')
			:attr('data-sort-value', val*60/data.time)
			:tag('span')
				:addClass('coins')
				:addClass(c_class)
				:wikitext(round1k(val*60/data.time, true))
			:done()
		:done()
		:tag('td')
			:attr('data-sort-value', timefunc._w_to_c(tostring(data.recurrence)))
			:wikitext(timefunc._w_to_c(tostring(data.recurrence)))
		:done()
		:tag('td')
			:addClass('plainlist')
			:newline()
			:wikitext(data.skill)
		:done()
		:tag('td')
			:wikitext(data.category)
		:done()
		:tag('td')
			:wikitext(MEMBERS_ICON[data.members])
		:done()
	return val, tr
end

function p.rec(frame)
	local data = mw.smw.ask({'[[MMG recurring JSON::+]]', '[[MMG value::+]]', '?MMG recurring JSON', '?=#', limit=10000})
	
	local t = mw.html.create('table')
	
	t	:addClass('wikitable sortable sticky-header align-right-2 align-right-3 align-right-4 align-right-5 align-center-7 align-center-8')
		:tag('caption')
			:wikitext('[[Money making guide|All guides]] &bull; [[Money making guide/Collecting|Collecting]] &bull; [[Money making guide/Combat|Combat]] &bull; [[Money making guide/Processing|Processing]] &bull; [[Money making guide/Skilling|Skilling]] &bull; [[Money making guide/Recurring|Recurring]] &bull; [[Money making guide/Free-to-play|Free-to-play]]')
		:done()
		:tag('tr')
			:tag('th')
				:wikitext('Method')
			:done()
			:tag('th')
				:wikitext('Profit')
			:done()
			:tag('th')
				:wikitext('Time')
			:done()
			:tag('th')
				:wikitext('Effective<br>profit')
			:done()
			:tag('th')
				:wikitext('Recurrence<br>time')
			:done()
			:tag('th')
				:wikitext('Skills')
			:done()
			:tag('th')
				:wikitext('Category')
			:done()
			:tag('th')
				:wikitext('Members')
				:css('width', '65px')
			:done()
			
	local methods = {}
	for i,v in ipairs(data) do
		if type(v['MMG recurring JSON']) == 'table' then
			for j,u in ipairs(v['MMG recurring JSON']) do
				table.insert(methods, { make_rec_row(v[1], u, true) })
			end
		else
			table.insert(methods, { make_rec_row(v[1], v['MMG recurring JSON'], false) })
		end
	end
	table.sort(methods, function(a,b) return a[1]>b[1] end)
	for i,v in ipairs(methods) do
		t:newline()
		t:node(v[2])
	end
	t:newline()
	return t
end



return p
-- </nowiki>