Module:BATTD costs

From Blooncyclopedia, the independent Bloons knowledge base
Revision as of 05:42, 20 August 2025 by Polavux (talk | contribs) (saving progress)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to navigation Jump to search

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

local p = {}

local function upgrade(upgradeName)
	-- build key:values table of upgrades where id=key
	local upgradesById = {}
	local upgradeId = ""
	local upgrades = mw.ext.cargo.query("battd_upgrades", "id, name, tower, cost", {
		where="_pageName LIKE CONCAT(name, '%')", -- filter ally upgrades (upgrades where the page name does not match the upgrade name)
		limit=250
	})

	for i, upgrade in ipairs(upgrades) do
		if upgrade.name == upgradeName then upgradeId = upgrade.id end
		upgradesById[upgrade.id] = upgrade
	end
	
	-- build key:values table of upgrade prereqs where id=current and values=previous
	local upgradePrereqsById = {}
	local upgradePrereqs = mw.ext.cargo.query("battd_upgrades=main, battd_upgrades__previous=prev", "main.id=prevId, prev._value=nextId", {
		where="_pageName LIKE CONCAT(name, '%')", -- filter ally upgrades (upgrades where the page name does not match the upgrade name)
		join="main._ID=prev._RowID",
		limit=600
	})

	for i, upgrade in ipairs(upgradePrereqs) do
		if upgradePrereqsById[upgrade.prevId] then upgradePrereqsById[upgrade.prevId][#upgradePrereqsById[upgrade.prevId]+1] = upgrade.nextId end
		upgradePrereqsById[upgrade.prevId] = {upgrade.nextId}
	end
	
	-- build key:values table of upgrade prereqs where id=current and values=previous
	local upgradeLocksById = {}
	local upgradeLocks = mw.ext.cargo.query("battd_upgrades=main, battd_upgrades__locked_by_upgrades=lock", "main.id=thisId, lock._value=lockId", {
		where="_pageName LIKE CONCAT(name, '%')", -- filter ally upgrades (upgrades where the page name does not match the upgrade name)
		join="main._ID=lock._RowID",
		limit=100
	})

	for i, upgrade in ipairs(upgradeLocks) do
		if upgradeLocksById[upgrade.thisId] then upgradePrereqsById[upgrade.thisId][#upgradePrereqsById[upgrade.thisId]+1] = upgrade.lockId end
		upgradeLocksById[upgrade.thisId] = {upgrade.lockId}
	end
	
	local function recursiveGetCost(id)
		local cost = upgradesById[id].cost
		for i, v in ipairs(upgradePrereqsById[id]) do
			cost = cost + recursiveGetCost(v)
		end
		return cost
	end
	
	return recursiveGetCost(upgradeId)
end

function p.upgrade(frame)
	return upgrade(frame.args[1])
end

return p