Module:BTD5 costs

From Blooncyclopedia, the independent Bloons knowledge base
Jump to navigation Jump to search

Documentation for this module may be created at Module:BTD5 costs/doc

-- This code is copyrighted and licensed under the Creative Commons Attribution-NonCommercial-ShareAlike International license, version 4.0. You may use and adapt this code if:
-- * You give appropriate credit
-- * You are not using this material for commercial purposes
-- * You are releasing it under the same license
-- Further information: https://www.bloonswiki.com/Blooncyclopedia:Copyrights
-- By editing this code, you agree to release your changes under the same terms.

local p = {}

function _upgrade(name, p, t)
	-- local functions to improve performance
	local lang = mw.language.new("en")
	local cg = mw.ext.cargo
	local tnum = tonumber
	local tinsert = table.insert
	local sformat = string.format
	local ip = ipairs
	local mceil = math.ceil
	
	-- generate table
	local ret = {
		-- header
		'{|class="wikitable sortable" style="text-align:center"\n!class="unsortable"|Tiers!![[Easy]]!![[Medium]]!![[Hard]]!![[Impoppable (BTD5)|Impoppable]]'
	}
	
	local function round_to_5(value)
		return mceil((value) / 5) * 5
	end
	
	-- query base tower to get its base cost
	local tower_cost = cg.query("btd5_towers", "name, cost_M=cost", {
		where = "name='" .. name .. "'"
	})[1]["cost"]

	local upgrade_query = tnum(t) < 3 and
	"tower = '%s' AND unused = 0 AND ((NOT path = %s) OR (PATH = %s AND NOT tier > %s))" or
	"tower = '%s' AND unused = 0 AND ((NOT path = %s AND NOT tier > 2) OR (PATH = %s AND NOT tier > %s))"
	
	-- query upgrades
	local upgrades = cg.query("btd5_upgrades", "path, tier, cost_M=cost", {
		where = sformat(upgrade_query,
			name, p, p, t),
		orderBy = "path, tier"
	})
	
	local total_costs = {
		round_to_5(tower_cost * 0.85),
		tnum(tower_cost),
		round_to_5(tower_cost * 1.08),
		round_to_5(tower_cost * 1.2)
	}
	
	-- calculate total cost of non-crosspath row
	for i, v in ip(upgrades) do
		if v["path"] == p then
			total_costs[1] = total_costs[1] + round_to_5(v["cost"] * 0.85)
			total_costs[2] = total_costs[2] + v["cost"]
			total_costs[3] = total_costs[3] + round_to_5(v["cost"] * 1.08)
			total_costs[4] = total_costs[4] + round_to_5(v["cost"] * 1.2)
		end
	end
	
	tinsert(ret, sformat("|-\n!%i-%i\n|$%s ($%s)||$%s ($%s)||$%s ($%s)||$%s ($%s)",
		p == "1" and t or 0,
		p == "2" and t or 0,
		lang:formatNum(total_costs[1]), lang:formatNum(total_costs[1] * 0.8),
		lang:formatNum(total_costs[2]), lang:formatNum(total_costs[2] * 0.8),
		lang:formatNum(total_costs[3]), lang:formatNum(total_costs[3] * 0.8),
		lang:formatNum(total_costs[4]), lang:formatNum(total_costs[4] * 0.8)
	))
	
	-- calculate total cost of crosspath row
	for i, v in ip(upgrades) do
		if v["path"] ~= p then
			total_costs[1] = total_costs[1] + round_to_5(v["cost"] * 0.85)
			total_costs[2] = total_costs[2] + v["cost"]
			total_costs[3] = total_costs[3] + round_to_5(v["cost"] * 1.08)
			total_costs[4] = total_costs[4] + round_to_5(v["cost"] * 1.2)

			tinsert(ret, sformat("|-\n!%i-%i\n|$%s ($%s)||$%s ($%s)||$%s ($%s)||$%s ($%s)",
				p == "2" and v["tier"] or t,
				p == "1" and v["tier"] or t,
				lang:formatNum(total_costs[1]), lang:formatNum(total_costs[1] * 0.8),
				lang:formatNum(total_costs[2]), lang:formatNum(total_costs[2] * 0.8),
				lang:formatNum(total_costs[3]), lang:formatNum(total_costs[3] * 0.8),
				lang:formatNum(total_costs[4]), lang:formatNum(total_costs[4] * 0.8)
			))
		end
	end
	
	tinsert(ret, "|-class='sortbottom'\n|colspan='5'|''Sell values are in parentheses.''\n|}")
	
	return table.concat(ret, "\n")
end

function p.upgrade(frame)
	return _upgrade(frame.args[1], frame.args[2], frame.args[3])
end

return p