Module:BATTD tower changes: Difference between revisions

From Blooncyclopedia, the independent Bloons knowledge base
Jump to navigation Jump to search
mNo edit summary
mNo edit summary
 
(9 intermediate revisions by the same user not shown)
Line 1: Line 1:
local p = {}
local p = {}


function _main(frame)
local function _main(frame)
-- local functions to improve performance
-- local functions to improve performance
local lang = mw.language.new("en")
local lang = mw.language.new("en")
Line 23: Line 23:
-- get names/links of upgrades
-- get names/links of upgrades
local upgrades = cargo.query("battd_upgrades", "_pageName, name", {
local upgrades = cargo.query("battd_upgrades", "_pageName, name", {
where=sFormat("tower='%s' AND NOT unused", towerName)
--where=sFormat("tower='%s' AND NOT unused", towerName)
where=sFormat("tower='%s'", towerName)
})
})


-- create name:_pageName dictionary
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 = {}
local pagenamesByName = {}
for i, upgrade in ipairs(upgrades) do pagenamesByName[upgrade.name] = upgrade._pageName end
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
-- helper function for cargo storing
Line 48: Line 73:
end
end
local outputTable = {"{|class='wikitable'\n!Upgrades!!Changes"}
local outputTable = {"{|class='wikitable'\n!Tiers!!Changes"}
for i = 1, 20 do
for i = 1, 20 do
local changes = args[sFormat("changes %i", i)]
local changes = args[sFormat("upgrades %i changes", i)]
local upgradeNames = args[sFormat("changes %i upgrades", i)]
local upgradeNames = args[sFormat("upgrades %i", i)]
local costBefore = args[sFormat("changes %i cost before", i)]
local costAfter = args[sFormat("changes %i cost after", 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
-- stop if this set is missing changes/upgrades
if changes == "" or (upgradeNames == "" and costBefore == "" and costAfter == "") then break end
if upgradeNames == "" then break end
local outputRow = {}
local outputRow = {}
for upgradeName in sGmatch(sUpper(upgradeNames), "([^;]+)") do
for upgradeName in sGmatch(upgradeNames, "([^;]+)") do
outputRow[#outputRow+1] = sFormat("[[%s|%s]]", upgradeName, pagenamesByName[upgradeName])
outputRow[#outputRow+1] = sFormat("[[%s|%s]]", upgradeName, pagenamesByName[upgradeName])
end
end
outputTable[i+1] = sFormat("|-\n|%s||%s", tConcat(outputRow, "\n"), changes)
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
end
Line 75: Line 113:
return _main(frame)
return _main(frame)
end
end
return p

Latest revision as of 17:41, 15 January 2026


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