Module:GETable

From the RuneScape Wiki, the wiki for all things RuneScape
Jump to navigation Jump to search
Module documentation
This documentation is transcluded from Module:GETable/doc. [edit] [history] [purge]
Module:GETable's function lastUpdated is invoked by Template:GEHeader.
Module:GETable's function row is invoked by Template:GEItem.
Module:GETable requires Module:Addcommas.
Module:GETable requires Module:ChangePerDay.
Module:GETable requires Module:Exchange.
Module:GETable requires Module:Yesno.
Module:GETable loads data from Module:Exchange/< ... >.
Module:GETable transcludes Template:GEHeader using frame:preprocess() or frame:expandTemplate().
Module:GETable transcludes Template:GETableEnd using frame:preprocess() or frame:expandTemplate().
Module:GETable is required by Module:GEMW/Most expensive.
Module:GETable is required by Module:GEMW/Most traded.
Function list
L 14 — p.row
L 31 — p._row
L 173 — p.lastUpdated
L 188 — p.table
L 192 — p._table

Module to format a GE table. Use with {{GEItem}}.


-- <nowiki>
local exchange = require('Module:Exchange')
local addcommas = require('Module:Addcommas')._add
local changeperday = require('Module:ChangePerDay')._change
local yesno = require('Module:Yesno')

local p = {}

--[[
{{GEItem}}

@example {{GEItem|bones}}
]]--
function p.row(frame)
	local args = frame:getParent().args
	local item = args[1]

	if item then
		return p._row(item)
	end

	error('"item" argument not specified')
end

--[[
{{GEItem}} internal method

@param item {string} Item to get data for
@return {string}
]]--
function p._row(item)
	-- load data
	local item = exchange.checkTitle(mw.text.trim(item))
	local data = mw.loadData('Module:Exchange/' .. item)
	local bulkData = {
		price = exchange.loadBulkData(item, 'price'),
		date = exchange.loadBulkData('%LAST_UPDATE_F%', 'price'),
		last = exchange.loadBulkData(item, 'lastPrice'),
		lastDate = exchange.loadBulkData('%LAST_UPDATE_F%', 'lastPrice'),
		volume = exchange.loadBulkData(item, 'volume'),
	}

	-- set variables here to make the row building easier to follow
	local div = '<i>Unknown</i>'
	local limit, limitSortKey
	if data.limit then
		limit = addcommas(data.limit)
		limitSortKey = data.limit
	else
		limit = '<i>Unknown</i>'
		limitSortKey = -1
	end
	local isBond = item == 'Bond'
	local volume, volumeSortKey
	if bulkData.volume then
		volume = addcommas(bulkData.volume)
		volumeSortKey = bulkData.volume
	else
		volume = isBond and 'N/A' or '<i>Unknown</i>'
		volumeSortKey = -1
	end
	local members = '<i>Unknown</i>'
	local changeSortKey = 1

	if bulkData.last then
		local link = 'https://secure.runescape.com/m=itemdb_rs/viewitem.ws?obj=' .. data.itemId
		changeSortKey = bulkData.price / bulkData.last
		local change = changeperday({ bulkData.price, bulkData.last, bulkData.date, bulkData.lastDate })
		local changeMagnitude = math.abs(change)

		local arrow
		if bulkData.price > bulkData.last then
			arrow = '[[File:Up.svg|17px|link=' .. link .. ']]'
		elseif bulkData.price < bulkData.last then
			arrow = '[[File:Down.svg|17px|link=' .. link .. ']]'
		else
			arrow = '[[File:Unchanged.svg|22px|link=' .. link .. ']]'
		end

		if changeMagnitude >= 0.04 then
			arrow = arrow  .. arrow .. arrow
		elseif changeMagnitude >= 0.02 then
			arrow = arrow .. arrow
		end
		local changeHover = ('%+.1f%%'):format(change * 100)
		if bulkData.price < bulkData.last and changeHover == '+0.0%' then
			-- Rounded to zero
			changeHover = '-0.0%'
		end

		div = mw.html.create('div')
			:css('white-space', 'nowrap')
			:wikitext(arrow)
			:attr('title', changeHover)

		div = tostring(div)
	end

	if data.members == true then
		members = '[[File:P2P icon.png|30px|link=Members|Members]]'
	elseif data.members == false then
		members = '[[File:F2P icon.png|30px|link=Free-to-play|Free-to-play]]'
	end

	-- build table row
	local icon = data.icon or (item .. '.png')
	local tr = mw.html.create('tr')
		:attr('data-rowid', item)
		:tag('td')
			:addClass('inventory-image')
			:wikitext('[[File:' .. icon .. '|link=' .. item .. ']]')
		:done()
		:tag('td')
			:css({
				['width'] = '15%',
				['text-align'] = 'left'
			})
			:wikitext('[[' .. item .. ']]')
		:done()
		:tag('td')
			:wikitext(addcommas(bulkData.price))
		:done()
		:tag('td')
			:attr('data-sort-value', changeSortKey)
			:wikitext(div)
		:done()

	if data.alchable == nil or yesno(data.alchable) then
		local low, high = '<i>Unknown</i>', '<i>Unknown</i>'

		if data.value then
			low = addcommas(exchange._lowalch(item))
			high = addcommas(exchange._highalch(item))
		end

		tr
			:tag('td')
				:wikitext(low)
			:done()
			:tag('td')
				:wikitext(high)
			:done()
	else
		tr
			:tag('td')
				:attr('colspan', '2')
				:addClass('table-na')
				:wikitext('<i>Cannot be alchemised</i>')
			:done()
	end

	tr
		:tag('td')
			:attr('data-sort-value', limitSortKey)
			:wikitext(limit)
		:done()
		:tag('td')
			:attr('data-sort-value', volumeSortKey)
			:addClass(isBond and 'table-na' or nil)
			:wikitext(volume)
		:done()
		:tag('td')
			:wikitext(members)
		:done()
		:tag('td')
			:css('white-space', 'nowrap')
			:wikitext('[[Exchange:' .. item .. '|view]]')
		:done()

	return tr
end

function p.lastUpdated()
	local timestamp = exchange.loadBulkData('%LAST_UPDATE%', 'price')
	return mw.html.create('time')
		:attr('datetime', os.date('!%Y-%m-%dT%T.000Z', timestamp))
		:addClass('localcomments explain')
		-- The following text is shown if [[MediaWiki:Gadget-relativetime.js]] is not turned on ('3 Feb at 04:05 UTC')
		:wikitext(os.date('%d %b at %H:%M UTC', timestamp))
	:done()
end

--[[
{{#invoke:GETable|table|item1|item2|item3|...}}
is equivalent to
{{GEHeader}}{{GEItem|item1}}{{GEItem|item2}}{{GEItem|item3}}{{GEItem|...}}{{GETableEnd}}
]]--
function p.table(frame)
	return p._table(frame.args, frame)
end

function p._table(items, frame)
	frame = frame or mw.getCurrentFrame()
	local result = mw.html.create()
		:wikitext(frame:expandTemplate{ title = 'GEHeader', args = {} })

	for _, item in ipairs(items) do
		result
			:node(p._row(item))
	end

	result
		:wikitext(frame:expandTemplate{ title = 'GETableEnd', args = {} })
	return result
end

return p
-- </nowiki>