Module:Sandbox/User:Towelcat/StoreLine

From Illerai
Jump to navigation Jump to search

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

local p = {}
local lang = mw.language.getContentLanguage()
local var = mw.ext.VariablesLua
local params = require('Module:Paramtest')
local yesno = require('Module:Yesno')
local currency_image = require("Module:Currency Image")

local exchange = require('Module:Exchange')
local find_gevalue = exchange._value
local find_geprice = exchange._price

local geprices_data = mw.loadJsonData('Module:GEPrices/data.json')

local ptitle = mw.title.getCurrentTitle()
local ns = ptitle.nsText
local title = ptitle.fullText
local pgTitle = ptitle.text

local function ticktime(ticks)
	local ret = ''
	local days = math.floor(ticks/144000)
	local hours = math.floor(ticks%144000/6000)
	local minutes = math.floor(ticks%6000/100)
	local seconds = ticks%100*0.6
	if days > 0 then
		ret = ret .. days .. 'd '
	end
	if hours > 0 then
		ret = ret .. hours .. 'h '
	end
	if minutes > 0 then
		ret = ret .. minutes .. 'm '
	end
	if seconds > 0 then
		ret = ret .. seconds .. 's'
	end
	return ret
end

function p.main(frame)
	local args = frame:getParent().args
	-- Params and defaults
	local name,stock,buyvalue,sellvalue,geprice = params.defaults{
					{args.Name,''},
					{args.Stock,''},
					{args.Buy,''},
					{args.Sell,''},
					{args.geprice,''}
				}
	local itemvalue = ''
	local gemwname = params.default_to(args.gemwname,name)
	local smwname = params.default_to(args.smwname,name)
	local displayname = params.default_to(args.DisplayName,name)
	local image = 'File:' .. params.default_to(args.Image, name .. '.png')
	local gemw = yesno(args.gemw or true, true)
	local restock = params.default_to(args.Restock,-1)
	
	-- Check precached Module:GEPrices/data
	if gemw and geprice == '' then
		local cached_price = geprices_data[gemwname]
		if type(cached_price) == 'number' and cached_price > 0 then
			geprice = cached_price
		end
	end
	
	-- Lookup GE price
	if gemw and geprice == '' then
		local has_geprice, geprice_check = pcall(find_geprice,gemwname)
		if has_geprice and geprice_check > -1 then
			geprice = geprice_check
		end
	end
	
	-- Lookup GE value
	if gemw and itemvalue == '' then
		local has_gevalue, gevalue = pcall(find_gevalue,gemwname)
		if has_gevalue and gevalue > -1 then
			itemvalue = gevalue
		end
	end
	
	-- Lookup SMW value
	if itemvalue == '' then
		itemvalue = getSMWInfo(smwname)
	end
	
	-- Check precached Module:DropsLine/itemData - gets GE alch so inaccurate
	if itemvalue == '' then
		local droppeditem_data = mw.loadJsonData('Module:DropsLine/itemData.json')
		local cached_dropdata = droppeditem_data[name]
		if type(cached_dropdata) == 'table' and type(cached_dropdata[2]) == 'number' then
			itemvalue = cached_dropdata[2]/.6
		end
	end
	
	-- Check precached Module:GEHighAlch/data - gets GE alch so inaccurate
	if itemvalue == '' then
		local highalch_data = mw.loadJsonData('Module:GEHighAlchs/data.json')
		local cached_data = highalch_data[name]
		if type(cached_data) == 'number' and cached_data > -1 then
			itemvalue = cached_data/.6
		end
	end
	
	local buymultiplier = var.var('BuyMultiplier', 1000)
	local sellmultiplier = var.var('SellMultiplier', 1000)
	local currency = var.var('Currency', 'Coins')
	local namenotes = var.var('NameNotes', '')
	local nosmw = yesno(var.var('nosmw','no'), false)
	local hidege = yesno(var.var('hideGE','no'), false)
	local hidesell = yesno(var.var('hideSell','no'), false)
	local hidebuy = yesno(var.var('hideBuy','no'), false)
	local hidestock = yesno(var.var('hideStock','no'), false)
	local hiderestock = yesno(var.var('hideRestock','no'), false)
	local is_twisted = args.Twisted or var.var('is_twisted', 'No')

	local buyvalueTemp = buyvalue
	if not(buyvalue == 'N/A') then
		if not(itemvalue == '') then
			if(buyvalue == '') then
				buyvalueTemp = math.floor(math.max(itemvalue*buymultiplier/1000,itemvalue*0.1))
			end
			buyvalue = commas(buyvalueTemp)
			local currencyImage = currency_image(currency, buyvalueTemp) or ''
			if(params.has_content(currencyImage)) then
				currencyImage = string.format('[[File:%s|link=%s]] ', currencyImage, currency)
				buyvalue = currencyImage .. buyvalue
			end
		end
		buyvalueTemp = tonumber(buyvalueTemp)
	end
	local sellvalueTemp = sellvalue
	if not(sellvalue == 'N/A') then
		if not(itemvalue == '') then
			if(sellvalue == '') then
				sellvalueTemp = math.floor(math.max(itemvalue*sellmultiplier/1000,1))
			end
			sellvalue = commas(sellvalueTemp)
			local currencyImage = currency_image(currency, sellvalueTemp) or ''
			if(params.has_content(currencyImage)) then
				currencyImage = string.format('[[File:%s|link=%s]] ', currencyImage, currency)
				sellvalue = currencyImage .. sellvalue
			end
		end
		sellvalueTemp = tonumber(sellvalueTemp)
	else
		sellvalueTemp = 1e10
	end
	
	gepriceTemp = 0
	if not(gemw) then
		geprice = 'Not sold'
	elseif geprice == '' then
		geprice = 'Error: Could not find GE value.'
	else
		gepriceTemp = geprice
		geprice = commas(gepriceTemp)
		local currencyImage = currency_image('coins', gepriceTemp) or ''
		if(params.has_content(currencyImage)) then
			currencyImage = string.format('[[File:%s|link=%s]] ', currencyImage, 'coins')
			geprice = currencyImage .. geprice
		end
	end
	
	if stock=='inf' then stock='∞' end --inf is easier to type
	if stock=='∞' then restock='N/A' end --self-documenting code
	
	local ret = mw.html.create('tr')
		:css('text-align','center')
		:tag('td')
			:wikitext(mw.ustring.format('[[%s|link=%s]]', image, name))
			:done()
		:tag('td')
			:css('text-align','left')
			:wikitext(mw.ustring.format('[[%s|%s]]', name, displayname))
		:done()
	if not(hidestock) then
		if stock=='N/A' then
			ret:tag('td')
				:attr('data-sort-value', 0)
				:attr('class','table-na')
				:wikitext('<small>N/A</small>')
			:done()
		elseif stock=='∞' then
			ret:tag('td')
				:wikitext('<span style="font-size:120%;">∞</span>')
			:done()
		else
			ret:tag('td')
				:wikitext(stock)
			:done()
		end
	end
	if not(hiderestock) then
		if restock=='N/A' or stock=='N/A' or stock=='∞' then
			ret:tag('td')
				:attr('data-sort-value', 0)
				:attr('class','table-na')
				:wikitext('<small>N/A</small>')
			:done()
		elseif restock == -1 then
			ret:tag('td')
				:attr('data-sort-value', -1)
				:wikitext('<small>unknown</small>[[Category:Needs restock time]]')
			:done()
		else
			ret:tag('td')
				:attr('data-sort-value', restock)
				:wikitext(ticktime(tonumber(restock) or 0))
			:done()
		end
	else
		restock='N/A' -- Set restock to N/A if hideRestock is set to true in the header
	end
	if not(hidesell) then
		if sellvalue=='N/A' then
			ret:tag('td')
				:attr('data-sort-value', 0)
				:attr('class','table-na')
				:wikitext('<small>N/A</small>')
			:done()
		elseif sellvalueTemp==0 then
			ret:tag('td')
				:attr('data-sort-value', 0)
				:wikitext('<small>Free</small>')
			:done()
		else
			ret:tag('td')
				:attr('data-sort-value', sellvalueTemp)
				:wikitext(sellvalue)
			:done()
		end
	end
	if not(hidebuy) then
		if buyvalue=='N/A' then
			ret:tag('td')
				:attr('data-sort-value', 0)
				:attr('class','table-na')
				:wikitext('<small>N/A</small>')
			:done()
		else
			ret:tag('td')
				:attr('data-sort-value', buyvalueTemp)
				:wikitext(buyvalue)
			:done()
		end
	end
	if not(hidege) then
		ret:tag('td')
			:attr('data-sort-value', gepriceTemp)
			:wikitext(geprice)
		:done()
	end
		
	local i = 1
	while args['column' .. i] do
		if args['column' .. i]=='N/A' then
			ret:tag('td')
				:attr('data-sort-value', 0)
				:attr('class','table-na')
				:wikitext('<small>N/A</small>')
			:done()
		else
		ret:tag('td')
			:wikitext(args['column' .. i])
		:done()
		end
		i = i+1
	end
		
	
	if ns == '' and not(nosmw) then
		local smw = {}
		local smw_sub = {}
		local source = pgTitle
		smw['Sells item'] = name
		smw_sub['Sold by'] = source
		smw_sub['Sold item image'] = image
		smw_sub['Sold item'] = name
		smw_sub['Sold item text'] = displayname
		smw_sub['Store stock'] = stock
		smw_sub['Restock time'] = restock
		smw_sub['Store sell price'] = sellvalueTemp --type = number for sorting purposes
		smw_sub['Store buy price'] = buyvalueTemp
		smw_sub['Store currency'] = currency
		smw_sub['Store notes'] = namenotes
		smw_sub['Is twisted'] = is_twisted
		mw.smw.subobject(smw_sub) -- add item subobject to page
		mw.smw.set(smw) -- add data to page
	end
	return tostring(ret)
end

function getSMWInfo(item)
	local smwData = ''
	local smw = mw.smw.ask({
		'[['..item..']]',
		'?Value'
	})
	if smw and smw[1] then
		smwData = smw[1]['Value']
	end
	return smwData
end

function commas(n)
	if tonumber(n) then
		return lang:formatNum(tonumber(n))
	else
		return n
	end
end

return p