Module:StoreLine

From RuneScape Classic Wiki
Jump to: navigation, search

Documentation for this module may be created at Module: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 ptitle = mw.title.getCurrentTitle()
local ns = ptitle.nsText
local title = ptitle.fullText
local pgTitle = ptitle.text

function p.main(frame)
	local args = frame:getParent().args
	-- Params and defaults
	local name,stock,buyvalue,sellvalue,itemvalue = params.defaults{
					{args.Name,''},
					{args.Stock,''},
					{args.Buy,''},
					{args.Sell,''},
					{args.Value,''},
				}
	local itemvalue = tonumber(itemvalue)
	local smwname = params.default_to(args.smwname,name)
	local displayname = params.default_to(args.DisplayName,name)
	local image = params.default_to(args.Image,name .. '.png')
    
    -- Lookup SMW value
    -- if itemvalue == ''  then
    --     itemvalue = getSMWInfo(smwname)
    -- end
    
    local buymultiplier = var.var('BuyMultiplier', 100)
    local sellmultiplier = var.var('SellMultiplier', 100)
   
	local buyvalueTemp = buyvalue
    if not(buyvalue == 'N/A') then
	    if not(itemvalue == '') then
	    	if(buyvalue == '') then
	    		buyvalueTemp = math.floor(math.max(itemvalue*buymultiplier/100,itemvalue*0.1))
			end
			buyvalue = '[[File:Coins.png|link=Coins]] ' .. commas(buyvalueTemp)
		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/100,1))
			end
			sellvalue = '[[File:Coins.png|link=Coins]] ' .. commas(sellvalueTemp)
		end
		sellvalueTemp = tonumber(sellvalueTemp)
	else
		sellvalueTemp = 1e10
	end
	
	local stockTemp = stock
	if stock=='∞' then
		stock = '<span style="font-size:120%;">∞</span>'
	end
	
	local ret = mw.html.create('tr')
    		:css('text-align','center')
			:tag('td')
				:wikitext(mw.ustring.format('[[File:%s|link=%s]]', image, name))
            :done()
			:tag('td')
    			:css('text-align','left')
				:wikitext(mw.ustring.format('[[%s|%s]]', name, displayname))
            :done()
    if stock=='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(stock)
        :done()
    end
    if sellvalue=='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', sellvalueTemp)
			:wikitext(sellvalue)
	    :done()
	end
	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
        
    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'] = stockTemp
        smw_sub['Store sell price'] = sellvalueTemp --type = number for sorting purposes
        smw_sub['Store buy price'] = buyvalueTemp
        mw.smw.subobject(smw_sub) -- add item subobject to page
        mw.smw.set(smw) -- add data to page
	end
	return tostring(ret)
end

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

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

return p