Module:Disassembly category calculator

From WIDEVERSE Wiki
Revision as of 18:08, 29 November 2020 by en>MrDew (update to fetch GE price data)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to navigation Jump to search

Documentation for this module may be created at Module:Disassembly category calculator/doc

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

local cats = mw.loadData('Module:Disassemble/data')
local exg = require('Module:ExchangeLite')
local coin = require('Module:Coins')._amount
local info = mw.loadData('Module:Disassembly category calculator/data')
local commas = require('Module:Addcommas')._add
local yesno = require('Module:Yesno')

local function gep(item)
	item = info.gemwnames[item] or info.names[item] or item
	local val = exg.price(item)
	local err2, limit = pcall(exg.load, {item = item, dataType = 'limit'})
	if val ~= nil then
		if err2 then
			return val, limit
		else
			return val, 0
		end
	else
		return 0, -1
	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
	local priceperdis = data.iqty * data.price
	local valbase = priceperdis / (data.cqty * (1 - data.junk/100))
	local matsperhour = data.cqty * (1 - data.junk/100) * 3000
	
	--round to 0dp if >100, else 2dp
	if valbase < 100 then
		valbase = string.format("%.2f", valbase)
	else
		valbase = string.format("%.0f", valbase)
	end
	
	
	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.isaugmented then
		str = str .. '<br/><span style="font-size:smaller;">(item level 9+)</span>'
	end
	
	tr	:attr({
		['data-dis-mats'] = data.cqty,
		['data-dis-junk'] = data.junk,
		['data-dis-price'] = priceperdis,
		['data-dis-raw'] = 100, -- because category
		})
		:addClass('dis-calc-row')
		: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)
			:addClass('data-dis-junkcell')
		:done()
	
	coins(tr:tag('td'), data.price)
	if tonumber(data.limit) and data.limit > 0 then
		tr	:tag('td')
				:addClass('data-dis-limitcell')
				:attr('data-sort-value', data.limit)
				:wikitext(commas(data.limit))
			:done()
	elseif tonumber(data.limit) and data.limit < 0 then
		tr	:tag('td')
				:addClass('data-dis-limitcell')
				:attr('data-sort-value', 0)
				:wikitext("''N/A''")
				:addClass('table-na')
			:done()
	else
		tr	:tag('td')
				:addClass('data-dis-limitcell')
				:attr('data-sort-value', 0)
				:wikitext("''Unknown''")
			:done()
	end
	
	local pricetd = tr:tag('td')
	pricetd:addClass('data-dis-costcell')
	
	coins(pricetd, valbase)
	
	tr	:tag('td')
			:attr('data-sort-value', matsperhour)
			:wikitext(commas(matsperhour))
			:addClass('data-dis-mphcell')
		:done()
	
	return tostring(tr)
end

-- special case: potions
local function potion(data)
	local rows = {}
	
	if data.calcvalue and data.calccomp then
		data.name = string.format('%s (%s)', data.page, data.calccomp)
		data.img = img(data.name)
		data.cqty = data.calccomp
		data.price = data.calcvalue
		data.limit = -1
		return make_row(data)
	end
	
	-- setup 6-dose
	data.name = data.page .. ' (6)'
	data.img = img(data.name)
	data.cqty = 6
	data.price, data.limit = 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, data.limit = 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.cqty = i
				data.price, data.limit = 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.cqty = i
				data.price, data.limit = gep(data.name)
				table.insert(rows, make_row(data))
			end
		end
		return table.concat(rows, '\n')
	end
end

-- main entry point
p.main = 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.isaugmented = yesno(args.augmented)
	data.iqty = tonumber(cat.itemqty) or 1
	data.cqty = tonumber(cat.compqty) or 1
	data.page = args['%PAGE%']
	if data.isaugmented then
		data.junk = 0
		data.cqty = data.cqty * 4
	else
		data.junk = junk(args.level)
	end
	
	-- defaults
	data.name = data.page
	data.img = img(data.page)
	data.calcvalue = tonumber(args.calcvalue) or nil
	
	if pot == 'yes' or pot == 'true' then
		data.calccomp = args.calccomp or nil
		return potion(data)
	end
	
	if info.versions[args['%PAGE%']] then
		for i,v in ipairs(info.versions[args['%PAGE%']]) do
			data.name = v
			if data.calcvalue then
				data.price, data.limit = data.calcvalue, -1
			else
				data.price, data.limit = gep(v)
			end
			data.img = img(v)
			table.insert(rows, make_row(data))
		end
		return table.concat(rows, '\n')
	else
		if data.calcvalue then
			data.price, data.limit = data.calcvalue, -1
		else
			data.price, data.limit = gep(data.page)
		end
		return make_row(data)
	end
end

return p