Module:Profitable shops

From RuneRealm Wiki
Jump to navigation Jump to search

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

local p = {}

local prices = mw.loadJsonData('Module:GEPrices/data.json')
local limits = mw.loadJsonData('Module:GELimits/data.json')
local members = mw.loadJsonData('Module:GEMembers/data.json')
local volumes = mw.loadJsonData('Module:GEVolumes/data.json')

local coins = require( 'Module:Coins' )._amount

function createPagination(currentPage)
	local ret = mw.html.create('div')
	
	if (currentPage > 0) then
		ret:tag('span'):wikitext('First Page'):done()
		ret:tag('span'):wikitext('Previous'):done()
	end
		ret:tag('span'):wikitext(' Next'):done()
	return ret
end

function createHeader()
	local ret = mw.html.create('table'):addClass('wikitable sortable autosort=5,d sticky-header align-center-1 align-right-3 align-right-4 align-right-5 align-right-6 align-right-7 align-right-8 align-right-9 align-center-10 align-center-11')
	ret:tag('tr'):tag('th'):attr('colspan', '2'):wikitext('Item'):done()
		:tag('th'):wikitext('Shop Price'):done()
		:tag('th'):wikitext('GE Price'):done()
		:tag('th'):wikitext('Profit'):done()
		:tag('th'):wikitext('[[wikipedia:Return on investment|ROI%]]'):done()
		:tag('th'):wikitext('Shop stock'):done()
		:tag('th'):wikitext('Store price delta'):done()
		:tag('th'):wikitext('[[Grand Exchange#Trade restrictions|Limit]]'):done()
		:tag('th'):wikitext('[[Grand Exchange#Volume|Volume]]'):done()
		:tag('th'):wikitext('Members'):done()
		:tag('th'):wikitext('Shop'):done()
	return ret
end

function buildRow(rowData)
	local row = mw.html.create('tr')
	
	local membersStr = '?'
	if(rowData.isMembers) then
		membersStr = '[[File:Member icon.png|link=Members]]'
	else
		membersStr = '[[File:Free-to-play icon.png|link=Free-to-play]]'
	end

	row:tag('td'):wikitext('[[File:' .. rowData.item .. '.png|link=' .. rowData.item .. ']]'):done()
		:tag('td'):wikitext('[[' .. rowData.item  .. ']]'):attr('data-sort-value', rowData.item):done()
		:tag('td'):wikitext(coins(rowData.price)):attr('data-sort-value', rowData.price):done()
		:tag('td'):wikitext(coins(rowData.gePrice)):attr('data-sort-value', rowData.gePrice):done()
		:tag('td'):wikitext(coins(rowData.profit)):attr('data-sort-value', rowData.profit):done()
		:tag('td'):wikitext(rowData.roi):attr('data-sort-value', rowData.roi):done()
		:tag('td'):wikitext(rowData.stock):attr('data-sort-value', rowData.stock):done()
		:tag('td'):wikitext(rowData.delta):attr('data-sort-value', rowData.delta):done()
		:tag('td'):wikitext(rowData.limit):attr('data-sort-value', rowData.limit):done()
		:tag('td'):wikitext(rowData.volume):attr('data-sort-value', rowData.volume):done()
		:tag('td'):wikitext(membersStr):attr('data-sort-value', tostring(membersVal)):done()
		:tag('td'):wikitext('[[' .. rowData.store .. '|view]]'):done()
	return row
end


function getData(currentPage)
	local items = {}
	local limit = 10000
	local params = {
		"[[Store currency::Coins]]",
		"?Sold item#-",
		"?Store sell price",
		"?Sold by#-",
		"?Store stock",
		"?Store delta",
		"?Store currency",
		limit = limit,
		offset = limit * currentPage
	}
	local data = mw.smw.ask(params) or {}
	
	for _, item in ipairs(data) do
		if item ~= nil then
			table.insert(items, item)
		end
	end
	
	return items
end

function p.main(frame)
	local args = frame:getParent().args
	local currentPage = args.page or 0
	local data = getData(currentPage)
	-- local pagination = createPagination(currentPage)
	local ret = createHeader()
	
	for i, item in ipairs(data) do
		local row = {}
		row.item = item["Sold item"]
		row.store = item["Sold by"]
		row.stock = item["Store stock"]
		row.price = item["Store sell price"]
		row.delta = item["Store delta"]
		row.gePrice = prices[row.item]
		
		local shouldAcceptItem = false
		
		if type(row.stock) == 'number' then
			if (row.stock > 0) then 
				shouldAcceptItem = true
			end
		else
			if (row.stock == '∞' or row.stock == 'N/A') then
				shouldAcceptItem = true
			end
		end
		
		-- If the item is not in the Grand Exchange dataset then it should be ignored
		if row.gePrice == nil then
			shouldAcceptItem = false
		end
		
		-- Items that can only be bought have a price of 10,000,000,000 in the wiki, we should ignore those
		if row.price == 10000000000 then
			shouldAcceptItem = false
		end
		
		if shouldAcceptItem then
			row.profit = row.gePrice - row.price
			
			if row.price ~= 0 then
				row.roi = string.format('%.2f', 100 * (row.profit / (row.price))) .. '%' 
			else 
				row.roi = '∞'
			end
			
			row.isMembers = members[row.item]
			row.limit = limits[row.item]
			row.volume = volumes[row.item]
			ret:node(buildRow(row))
		end
	end
	
	return ret
end

return p