Module:BATTD tower changes

Revision as of 17:41, 15 January 2026 by Polavux (talk | contribs)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)

local p = {}

local function _main(frame)
	-- local functions to improve performance
	local lang = mw.language.new("en")
	local cargo = mw.ext.cargo
	local args = frame.args
	local namespace = mw.title.getCurrentTitle().namespace
	
	local tConcat = table.concat
	local sFormat = string.format
	local sSub = string.sub
	local sUpper = string.upper -- i wonder what's for dinner
	local sGmatch = string.gmatch
	
	-- template parameters
	local version = args["version"]
	local versionCode = args["version code"]
	local versionNote = args["version note"]
	local priority = args["priority"]
	local towerName = args["tower"]
	
	-- get names/links of upgrades
	local upgrades = cargo.query("battd_upgrades", "_pageName, name", {
		--where=sFormat("tower='%s' AND NOT unused", towerName)
		where=sFormat("tower='%s'", towerName)
	})

	local upgradePrereqs = cargo.query("battd_upgrades=main, battd_upgrades__previous=prev", "main.id=thisId, prev._value=prevId", {
		where=sFormat("main.tower='%s'", towerName),
		join="main._ID=prev._RowID"
	})

	-- build name:_pageName dictionary
	local pagenamesByName = {}
	for i, upgrade in ipairs(upgrades) do pagenamesByName[upgrade.name] = upgrade._pageName end
	
	local upgradePrereqsOfById = {		-- key:values table - upgrades:upgrades this is a prerequisite of
		root = {}
	}

	-- build upgradePrereqsOfById
	for i, upgrade in ipairs(upgradePrereqs) do
		-- upgrade:next table
		if upgrade.prevId then
			if upgradePrereqsOfById[upgrade.prevId] then
				upgradePrereqsOfById[upgrade.prevId][#upgradePrereqsOfById[upgrade.prevId]+1] = upgrade.thisId
			else
				upgradePrereqsOfById[upgrade.prevId] = {upgrade.thisId}
			end
		else
			-- if this upgrade is a root upgrade and is not already purchased
			upgradePrereqsOfById["root"][#upgradePrereqsOfById["root"]+1] = upgrade.thisId
		end
	end
	
	-- helper function for cargo storing
	local function store(priority, names, changes)
		if namespace == 0 or namespace == 3200 then
			frame:callParserFunction("#cargo_store:_table=battd_changes", {
				version     =version,
				version_note=versionNote,
				priority    =priority,
				names       =names,
				changes     =mw.text.killMarkers(changes)
			})
		end
	end
	
	-- helper function for cost change templates
	local function getCostListItem(before, after, liArgs)
		return sFormat("*%s", frame:expandTemplate{title = before > after and "buff" or "nerf", args = liArgs})
	end
	
	local outputTable = {"{|class='wikitable'\n!Tiers!!Changes"}
	
	for i = 1, 20 do
		local changes		= args[sFormat("upgrades %i changes", i)]
		local upgradeNames	= args[sFormat("upgrades %i", i)]
		
		local before		= tonumber(args[sFormat("upgrades %i cost before", i)])
		local after			= tonumber(args[sFormat("upgrades %i cost after", i)])
		local baseCostDiff	= 0
		-- stop if this set is missing changes/upgrades
		if upgradeNames == "" then break end
		
		local outputRow = {}
		
		for upgradeName in sGmatch(upgradeNames, "([^;]+)") do
			outputRow[#outputRow+1] = sFormat("[[%s|%s]]", upgradeName, pagenamesByName[upgradeName])
		end
		
		if before ~= 0 and after ~= 0 then
			-- list item
			changes = sFormat("%s\n%s", getCostListItem(before, after, {
				before > after and "Upgrade cost reduced" or "Upgrade cost increased",
				sFormat("$%s", lang:formatNum(before)),
				sFormat("$%s", lang:formatNum(after)),
				undoc=args[sFormat("upgrades %i cost undoc", i)],
				fix  =args[sFormat("upgrades %i cost fix", i)]
			}), changes)
		end
		
		outputTable[i+1] = sFormat("|-\n|\n%s\n|\n%s", tConcat(outputRow, ", "), changes)
	end
	
	outputTable[#outputTable+1] = "|}"
	
	return tConcat(outputTable, "\n")
end

function p.main(frame)
	return _main(frame)
end

return p