Module:Sandbox/User:Gaz Lloyd/inv

From the RuneScape Wiki, the wiki for all things RuneScape
Jump to navigation Jump to search
Module documentation
This documentation is transcluded from Template:Module sandbox/doc. [edit] [history] [purge]
Module:Sandbox/User:Gaz Lloyd/inv requires Module:Coins.
Module:Sandbox/User:Gaz Lloyd/inv requires Module:ExchangeLite.
Module:Sandbox/User:Gaz Lloyd/inv loads data from Module:Disassemble/data.
Module:Sandbox/User:Gaz Lloyd/inv loads data from Module:Disassemble/mats.
Module:Sandbox/User:Gaz Lloyd/inv loads data from Module:Disassembly category calculator/data.

This module is a sandbox for Gaz Lloyd. It can be used to test changes to existing modules, prototype new modules, or just experimenting with lua features.

Invocations of this sandbox should be kept in userspace; if the module is intended for use in other namespaces, it should be moved out of the sandbox into a normal module and template.

This default documentation can be overridden by creating the /doc subpage of this module, as normal.


-- <nowiki>
-- code for [[Template:Disassembly category calculator/t]]
-- -> [[Calculator:Disassembly by category]]
local p = {}

local cats = mw.loadData('Module:Disassemble/data')
local materials = mw.loadData('Module:Disassemble/mats')
local exg = require('Module:ExchangeLite')
local coin = require('Module:Coins')._amount
local info = mw.loadData('Module:Disassembly category calculator/data')
local Title = mw.title.getCurrentTitle()
local spn = Title.subpageText


local function get_mat(m)
	local str = mw.text.trim(string.gsub(string.gsub(string.lower(m), ' parts?', ''), ' components?', ''))
 
	if materials[str] then
		return materials[str], true
	else
		return (m or ''), false
	end
end
local pagemat = get_mat(spn)

local function gep(item)
	item = info.gemwnames[item] or info.names[item] or item
	local err, val = pcall(exg.load, {args={item, 'price'}})
	if err then
		return val
	else
		return 0
	end
end

local function img(item)
	return info.imgnames[item] or info.names[item] or item
end

local function junk(lv)
	local junkpast75 = {
		[75] = 4.2, [76] = 3.8, [77] = 3.4, [78] = 3.0, [79] = 2.7,
		[80] = 2.3, [81] = 2.0, [82] = 1.7, [83] = 1.4, [84] = 1.2,
		[85] = 1.0, [86] = 0.8, [87] = 0.6, [88] = 0.4, [89] = 0.3 }
	
	lv = tonumber(lv) or 1
	if lv >= 90 then
		return 0
	elseif lv >= 75 then
		return junkpast75[lv]
	else
		return 100 - 1.1 * lv
	end
end

local function plink(page, name, img)
	return string.format('[[File:%s.png|link=%s]] [[%s|%s]]', img, page, page, name)
end

local function coins(td, am)
	if am == 0 then
		td	:wikitext("''N/A''")
			:addClass('table-na')
	else
		td	:wikitext(coin(am, false))
			
	end
	td	:attr('data-sort-value', am)
end

local function make_row(data)
	local tr = mw.html.create('tr')
	local str
	
	if data.iqty > 1 then
		str = string.format('%s %s %s', data.iqty, '×', plink(data.page, data.name, data.img))
	else
		str = plink(data.page, data.name, data.img)
	end
	
	if data.isspecial then
		local spcstring = '{{okay}}'
		if not data.spchance then
			spcstring = '{{not okay}}'
		end
		local val = data.price * data.iqty / data.cqty -- TODO: chances
		if val < 100 then
			val = string.format("%.2f", val)
		else
			val = string.format("%.0f", val)
		end
		tr	:tag('td')
				:css('text-align','left')
				:wikitext(str)
				:attr('data-sort-value', data.name)
			:done()
			:tag('td')
				:wikitext(data.cqty)
				:attr('data-sort-value', data.cqty)
			:done()
			:tag('td')
				:wikitext(spcstring)
				:attr('data-sort-value', tostring(data.spchance))
			:done()
			:tag('td')
				:wikitext(string.format('%.1f%%', data.junk))
				:attr('data-sort-value', data.junk)
			:done()
		coins(tr:tag('td'), val)
		return tostring(tr)
	end
	
	-- layover for non-special
	--[=[
	
	local valbase = data.price * data.iqty / (data.cqty * (1 - data.junk/100))
	local valjcr5 = data.price * data.iqty / (data.cqty * (1 - math.ceil(10*0.91*data.junk)/1000))
	local valjcr9 = data.price * data.iqty / (data.cqty * (1 - math.ceil(10*0.8*data.junk)/1000))
	
	--round to 0dp if >100, else 2dp
	if valbase < 100 then
		valbase = string.format("%.2f", valbase)
	else
		valbase = string.format("%.0f", valbase)
	end
	if valjcr5 < 100 then
		valjcr5 = string.format("%.2f", valjcr5)
	else
		valjcr5 = string.format("%.0f", valjcr5)
	end
	if valjcr9 < 100 then
		valjcr9 = string.format("%.2f", valjcr9)
	else
		valjcr9 = string.format("%.0f", valjcr9)
	end
	
	tr	:tag('td')
			:css('text-align','left')
			:wikitext(str)
			:attr('data-sort-value', data.name)
		:done()
		:tag('td')
			:wikitext(data.cqty)
			:attr('data-sort-value', data.cqty)
		:done()
		:tag('td')
			:wikitext(string.format('%.1f%%', data.junk))
			:attr('data-sort-value', data.junk)
		:done()
	coins(tr:tag('td'), valbase)
	coins(tr:tag('td'), valjcr5)
	coins(tr:tag('td'), valjcr9)
	return tostring(tr)
	--]=]
end

-- special case: potions
-- i don't think any potions actually give special comps
-- but just in case
-- cqty does not vary, it might actually idk
local function potion(data)
	local rows = {}
	
	-- setup 6-dose
	data.name = data.page .. ' (6)'
	data.img = img(data.name)
	data.price = gep(data.name)
	if data.price > 0 then
		-- if 6-dose has a price, then this is a flask; only show 6 dose (5 and lower not GE-able)
		return make_row(data)
	else
		data.name = data.page .. ' (4)'
		data.price = gep(data.name)
		if data.price > 0 then
		-- if 4 dose has a price, this is a 4-dose vial
			for i = 4, 1, -1 do
				data.name = string.format('%s (%s)',data.page,i)
				data.img = img(data.name)
				data.price = gep(data.name)
				table.insert(rows, make_row(data))
			end
		else
			-- if 4 dose does not have a price, this is a 2-dose mix
			for i = 2, 1, -1 do
				data.name = string.format('%s (%s)',data.page,i)
				data.img = img(data.name)
				data.price = gep(data.name)
				table.insert(rows, make_row(data))
			end
		end
		return table.concat(rows, '\n')
	end
end

-- special components are different to other components
p.special = function(frame)
	local args = frame:getParent().args
	local data = {}
	local cat = cats[string.lower(args.category)]
	local rows = {}
	local pot = args.potion or cat.potion or false
	pot = string.lower(tostring(pot))
	
	data.isspecial = true
	
	local _special = mw.text.split(args.special or '','%s*,%s')
 	
 	local numsp = 1
 	
	for _, v in ipairs(_special) do
		v = string.gsub(v, '%[%[(.+)%]%]', '%1')
		local _,_,n,l = string.find(v,'(.+) ?%[(%d+)%]')
		if not n then
			n,l = v
		end
		if not tonumber(l) then
			l = 1
		end
		n = mw.text.trim(n)
		if v:find('%S') then
			local mat,matb = get_mat(n) 
			if matb and mat == pagemat then
				numsp = l
				break
			end
		end
	end
	
	if string.lower(args.alwaysgivesaspecialmaterial or '') == 'no' then
		data.spchance = false
	else
		data.spchance = true
	end
	
	data.iqty = tonumber(cat.itemqty) or 1
	data.cqty = numsp
	data.page = args['%PAGE%']
	data.junk = junk(args.level)
	
	-- defaults
	data.name = data.page
	data.img = img(data.page)
	
	if pot == 'yes' or pot == 'true' then
		return potion(data)
	end
	
	if info.versions[args['%PAGE%']] then
		for i,v in ipairs(info.versions[args['%PAGE%']]) do
			data.name = v
			data.price = gep(v)
			data.img = img(v)
			table.insert(rows, make_row(data))
		end
		return table.concat(rows, '\n')
	else
		data.price = gep(data.page)
		return make_row(data)
	end
end

-- main entry point
p.main = p.special

return p