Module:BATTD costs: Difference between revisions

From Blooncyclopedia, the independent Bloons knowledge base
Jump to navigation Jump to search
saving progress
 
No edit summary
Line 1: Line 1:
local p = {}
local p = {}


local function upgrade(upgradeName)
local function upgrade(towerName, upgradeName)
local sFormat = string.format
local query = mw.ext.cargo.query
-- build key:values table of upgrades where id=key
-- build key:values table of upgrades where id=key
local upgradesById = {}
local upgradesById = {}
local upgradeId = ""
local upgradeId = ""
local upgrades = mw.ext.cargo.query("battd_upgrades", "id, name, tower, cost", {
local upgrades = 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)
where=sFormat("tower='%s'", towerName)
limit=250
})
})


Line 17: Line 19:
-- build key:values table of upgrade prereqs where id=current and values=previous
-- build key:values table of upgrade prereqs where id=current and values=previous
local upgradePrereqsById = {}
local upgradePrereqsById = {}
local upgradePrereqs = mw.ext.cargo.query("battd_upgrades=main, battd_upgrades__previous=prev", "main.id=prevId, prev._value=nextId", {
local upgradePrereqs = 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)
where=sFormat("main.tower='%s'", towerName),
join="main._ID=prev._RowID",
join="main._ID=prev._RowID",
limit=600
})
})


Line 30: Line 31:
-- build key:values table of upgrade prereqs where id=current and values=previous
-- build key:values table of upgrade prereqs where id=current and values=previous
local upgradeLocksById = {}
local upgradeLocksById = {}
local upgradeLocks = mw.ext.cargo.query("battd_upgrades=main, battd_upgrades__locked_by_upgrades=lock", "main.id=thisId, lock._value=lockId", {
local upgradeLocks = 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)
where=sFormat("main.tower='%s'", towerName),
join="main._ID=lock._RowID",
join="main._ID=lock._RowID",
limit=100
})
})


Line 53: Line 53:


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


return p
return p

Revision as of 22:07, 9 January 2026

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

local p = {}

local function upgrade(towerName, upgradeName)
	local sFormat = string.format
	local query = mw.ext.cargo.query
	
	-- build key:values table of upgrades where id=key
	local upgradesById = {}
	local upgradeId = ""
	local upgrades = query("battd_upgrades", "id, name, tower, cost", {
		where=sFormat("tower='%s'", towerName)
	})

	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 = query("battd_upgrades=main, battd_upgrades__previous=prev", "main.id=prevId, prev._value=nextId", {
		where=sFormat("main.tower='%s'", towerName),
		join="main._ID=prev._RowID",
	})

	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 = query("battd_upgrades=main, battd_upgrades__locked_by_upgrades=lock", "main.id=thisId, lock._value=lockId", {
		where=sFormat("main.tower='%s'", towerName),
		join="main._ID=lock._RowID",
	})

	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], frame.args[2])
end

return p