Module:BTD6 stats: Difference between revisions

From Blooncyclopedia, the independent Bloons knowledge base
Jump to navigation Jump to search
No edit summary
Tag: Reverted
Pymonkibot (talk | contribs)
mNo edit summary
Tag: Reverted
Line 31: Line 31:
-- search through tables
-- search through tables
for k, v in pairs(new_data) do
for k, v in pairs(new_data) do
if type(v) == "table" and ssub(k, 1, 1) ~= "_" then
if type(v) == "table" then
template[k] = {}
if ssub(k, 1, 1) ~= "_" then
template[k] = {}
if prev_data[k] == nil then prev_data[k] = {} end
-- iterate through attacks/abilities/subtowers
for kk, vv in pairs(v) do
if k == "subtowers" then tinsert(template[k], sformat(header, kk)) end
if prev_data[k][kk] == nil then prev_data[k][kk] = {} end
-- parse sub-table of table
tinsert(template[k], parse_stats(frame, kk, vv, prev_data[k][kk], template_names[k]))
end
if prev_data[k] == nil then prev_data[k] = {} end
template[k] = tconcat(template[k])
else
-- iterate through attacks/abilities/subtowers
for kk, vv in pairs(v) do
if k == "subtowers" then tinsert(template[k], sformat(header, kk)) end
if prev_data[k][kk] == nil then prev_data[k][kk] = {} end
-- parse sub-table of table
tinsert(template[k], parse_stats(frame, kk, vv, prev_data[k][kk], template_names[k]))
end
end
template[k] = tconcat(template[k])
elseif type(v) ~= "table" then
else
-- if previous value exists, make it a comparison
-- if previous value exists, make it a comparison
if prev_data[k] ~= nil then
if prev_data[k] ~= nil then
Line 73: Line 77:
-- search through tables
-- search through tables
for k, v in pairs(new_data) do
for k, v in pairs(new_data) do
if type(v) == "table" and ssub(k, 1, 1) ~= "_" then
if type(v) == "table" then
template[k] = {}
if ssub(k, 1, 1) ~= "_" then
template[k] = {}
if prev_data[k] == nil then prev_data[k] = {} end
-- iterate through attacks/abilities/subtowers
for kk, vv in pairs(v) do
if prev_data[k][kk] == nil then prev_data[k][kk] = {} end
-- parse sub-table of table
tinsert(template[k], parse_stats(frame, kk, vv, prev_data[k][kk], template_names[k]))
end
if prev_data[k] == nil then prev_data[k] = {} end
template[k] = tconcat(template[k])
else
-- iterate through attacks/abilities/subtowers
for kk, vv in pairs(v) do
if prev_data[k][kk] == nil then prev_data[k][kk] = {} end
-- parse sub-table of table
tinsert(template[k], parse_stats(frame, kk, vv, prev_data[k][kk], template_names[k]))
end
end
template[k] = tconcat(template[k])
else
else
Line 115: Line 123:
-- update prev_data first
-- update prev_data first
for k, v in pairs(new_data) do
for k, v in pairs(new_data) do
if type(v) == "table" and ssub(k, 1, 1) ~= "_" then
if type(v) == "table" then
if prev_data[k] == nil then prev_data[k] = {} end
if ssub(k, 1, 1) ~= "_" then
if prev_data[k] == nil then prev_data[k] = {} end
-- iterate through attacks/abilities/subtowers
for kk, vv in pairs(v) do
-- iterate through attacks/abilities/subtowers
if k == "subtowers" then tinsert(template[k], sformat(header, kk)) end
for kk, vv in pairs(v) do
if prev_data[k][kk] == nil then prev_data[k][kk] = {} end
if k == "subtowers" then tinsert(template[k], sformat(header, kk)) end
-- parse sub-table of table
if prev_data[k][kk] == nil then prev_data[k][kk] = {} end
parse_stats_full(frame, kk, vv, prev_data[k][kk], template_names[k])
-- parse sub-table of table
parse_stats_full(frame, kk, vv, prev_data[k][kk], template_names[k])
end
else
end
end
elseif type(v) ~= "table" then
else
-- update previous value
-- update previous value
prev_data[k] = v
prev_data[k] = v
Line 164: Line 176:
if new_data ~= nil then
if new_data ~= nil then
for k, v in pairs(new_data) do
for k, v in pairs(new_data) do
if type(v) == "table" and ssub(k, 1, 1) ~= "_" then
if type(v) == "table" then
mw.logObject(k)
if ssub(k, 1, 1) ~= "_" then
if prev_data[k] == nil then prev_data[k] = {} end
mw.logObject(k)
if prev_data[k] == nil then prev_data[k] = {} end
-- iterate through attacks/abilities/subtowers
for kk, vv in pairs(v) do
-- iterate through attacks/abilities/subtowers
if prev_data[k][kk] == nil then prev_data[k][kk] = {} end
for kk, vv in pairs(v) do
-- parse sub-table of table
if prev_data[k][kk] == nil then prev_data[k][kk] = {} end
parse_stats_full(frame, kk, vv, prev_data[k][kk], template_names[k])
-- parse sub-table of table
parse_stats_full(frame, kk, vv, prev_data[k][kk], template_names[k])
end
else
end
end
else
else
prev_data[k] = v
prev_data[k] = v
Line 226: Line 241:
local sformat = string.format
local sformat = string.format
tinsert(ret, sformat("<div class='hatnote'>Last updated: [[Bloons TD 6 v%g|version %g]]</div>", data["_last_updated"], data["_last_updated"]))
tinsert(ret, sformat("<div class='hatnote'>Last updated: [[Bloons TD 6 v%s|version %s]]</div>", data["_last_updated"], data["_last_updated"]))
tinsert(ret, parse_tower(frame, data["_" .. frame.args[2]], my_data, "<h3>%s</h3>"))
tinsert(ret, parse_tower(frame, data["_" .. frame.args[2]], my_data, "<h3>%s</h3>"))
-- mw.logObject(data["_" .. frame.args[2]])
return table.concat(ret, "\n")
return table.concat(ret, "\n")
end
end

Revision as of 01:35, 18 October 2024

Documentation for this module may be created at Module:BTD6 stats/doc

-- This code is copyrighted and licensed under the Creative Commons Attribution-NonCommercial-ShareAlike International license, version 4.0. You may reuse and adapt this code for other purposes if:
-- * You give appropriate credit
-- * You are not using this material for commercial purposes
-- * You are releasing it under the same license
-- The subpages of this page are adapted from internal data in Bloons TD 6, which is copyrighted by Ninja Kiwi Limited. Its usage on Blooncyclopedia is believed to be fair use.
-- Further information: https://www.bloonswiki.com/Blooncyclopedia:Copyrights
-- By editing this code, you agree to release your changes under the same terms.

local p = {}
local my_data = {}

local template_names = {
	attacks = "BTD6 attack",
	projectiles = "BTD6 projectile",
	abilities = "BTD6 ability",
	effects = "BTD6 effect",
	zones = "BTD6 zone",
	subtowers = "BTD6 tower",
	buffs = "BTD6 buff"
}

function parse_tower(frame, new_data, prev_data, header)
	local template = {}
	
	-- local functions to improve performance
	local tinsert = table.insert
	local tconcat = table.concat
	local sformat = string.format
	local ssub = string.sub
	
	-- search through tables
	for k, v in pairs(new_data) do
		if type(v) == "table" then
			if ssub(k, 1, 1) ~= "_" then
				template[k] = {}
				
				if prev_data[k] == nil then prev_data[k] = {} end
				
				-- iterate through attacks/abilities/subtowers
				for kk, vv in pairs(v) do
					if k == "subtowers" then tinsert(template[k], sformat(header, kk)) end
					if prev_data[k][kk] == nil then prev_data[k][kk] = {} end
					-- parse sub-table of table
					tinsert(template[k], parse_stats(frame, kk, vv, prev_data[k][kk], template_names[k]))
				end
			
				template[k] = tconcat(template[k])
			else
				
			end
		
		else
			-- if previous value exists, make it a comparison
			if prev_data[k] ~= nil then
				template[k] = prev_data[k]
				template[k .. " after"] = v
			else
				template[k] = v
			end
			
			-- update previous value
			prev_data[k] = v
		end
	end
	
	return frame:expandTemplate{title = "BTD6 tower", args = template}
end

function parse_stats(frame, name, new_data, prev_data, template_name)
	local template = {name = name}
	
	-- local functions to improve performance
	local tinsert = table.insert
	local tconcat = table.concat
	local ssub = string.sub
	
	-- search through tables
	for k, v in pairs(new_data) do
		if type(v) == "table" then
			if ssub(k, 1, 1) ~= "_" then
				template[k] = {}
				
				if prev_data[k] == nil then prev_data[k] = {} end
				
				-- iterate through attacks/abilities/subtowers
				for kk, vv in pairs(v) do
					if prev_data[k][kk] == nil then prev_data[k][kk] = {} end
					-- parse sub-table of table
					tinsert(template[k], parse_stats(frame, kk, vv, prev_data[k][kk], template_names[k]))
				end
			
				template[k] = tconcat(template[k])
			else
				
			end
		
		else
			-- if previous value exists, make it a comparison
			if prev_data[k] ~= nil then
				template[k] = prev_data[k]
				template[k .. " after"] = v
			else
				template[k] = v
			end
			
			-- update previous value
			prev_data[k] = v
		end
	end
	
	return frame:expandTemplate{title = template_name, args = template}
end

function parse_tower_full(frame, new_data, prev_data, header)
	local template = {}
	
	-- local functions to improve performance
	local tinsert = table.insert
	local tconcat = table.concat
	local sformat = string.format
	local ssub = string.sub
	
	-- update prev_data first
	for k, v in pairs(new_data) do
		if type(v) == "table" then
			if ssub(k, 1, 1) ~= "_" then
				if prev_data[k] == nil then prev_data[k] = {} end
				
				-- iterate through attacks/abilities/subtowers
				for kk, vv in pairs(v) do
					if k == "subtowers" then tinsert(template[k], sformat(header, kk)) end
					if prev_data[k][kk] == nil then prev_data[k][kk] = {} end
					-- parse sub-table of table
					 parse_stats_full(frame, kk, vv, prev_data[k][kk], template_names[k])
				end
			else
				
			end
		
		else
			-- update previous value
			prev_data[k] = v
		end
	end
	
	-- update prev_data first
	for k, v in pairs(prev_data) do
		if k == "attacks" or k == "abilities" or k == "subtowers" or k == "zones" then
			template[k] = {}
			
			-- iterate through attacks/abilities/subtowers
			for kk, vv in pairs(v) do
				 tinsert(template[k], parse_stats_full(frame, kk, nil, vv, template_names[k]))
			end
			
			template[k] = tconcat(template[k])
		
		elseif type(v) ~= "table" then
			-- update previous value
			template[k] = v
		end
	end
	
	return frame:expandTemplate{title = "BTD6 tower", args = template}
end

function parse_stats_full(frame, name, new_data, prev_data, template_name)
	local template = {name = name}
	
	-- local functions to improve performance
	local tinsert = table.insert
	local tconcat = table.concat
	local ssub = string.sub
	
	-- update prev_data first
	if new_data ~= nil then
		for k, v in pairs(new_data) do
			if type(v) == "table" then
				if ssub(k, 1, 1) ~= "_" then
					mw.logObject(k)
					if prev_data[k] == nil then prev_data[k] = {} end
					
					-- iterate through attacks/abilities/subtowers
					for kk, vv in pairs(v) do
						if prev_data[k][kk] == nil then prev_data[k][kk] = {} end
						-- parse sub-table of table
						parse_stats_full(frame, kk, vv, prev_data[k][kk], template_names[k])
					end
				else
					
				end
			else
				prev_data[k] = v
			end
		end
	end
	
	for k, v in pairs(prev_data) do
		if type(v) == "table" then
			template[k] = {}
			
			-- iterate through attacks/abilities/subtowers
			for kk, vv in pairs(v) do
				if prev_data[k][kk] == nil then prev_data[k][kk] = {} end
				-- parse sub-table of table
				tinsert(template[k], parse_stats_full(frame, kk, nil, vv, template_names[k]))
			end
			
			template[k] = tconcat(template[k])
		
		else
			template[k] = v
		end
	end
	
	return frame:expandTemplate{title = template_name, args = template}
end

-- stats of a single tower
function p.base_stats(frame)
	local data = mw.loadJsonData(string.format("Module:BTD6 stats/%s", frame.args[1]))
	
	return parse_tower(frame, data, my_data, "<h3>%s</h3>")
end

-- stats of a paragon
function p.paragon(frame)
	local data = mw.loadJsonData(string.format("Module:BTD6 stats/%s", frame.args[1]))
	
	return parse_tower(frame, data, my_data, "<h3>%s</h3>")
end

-- stats of a specific upgrade
function p.tower_upgrade_stats(frame)
	local data = mw.loadJsonData(string.format("Module:BTD6 stats/%s", frame.args[1]))
	
	local ret = {}
	
	-- local functions to improve performance
	local tinsert = table.insert
	local sformat = string.format
	
	tinsert(ret, sformat("<div class='hatnote'>Last updated: [[Bloons TD 6 v%s|version %s]]</div>", data["_last_updated"], data["_last_updated"]))
	
	tinsert(ret, parse_tower(frame, data["_" .. frame.args[2]], my_data, "<h3>%s</h3>"))
	return table.concat(ret, "\n")
end

-- stat changes of a hero
function p.hero_level_changes(frame)
	local data = mw.loadJsonData(string.format("Module:BTD6 stats/%s", frame.args[1]))
	
	local ret = {}
	
	-- local functions to improve performance
	local tinsert = table.insert
	local sformat = string.format
	
	-- calculate base tower stats for later comparison
	parse_tower(frame, data, my_data, "<h3>%s</h3>")
	
	for i = 2, 20 do
		tinsert(ret, sformat("===Level %i===", i))
		tinsert(ret, parse_tower(frame, data[i], my_data, "<h4>%s</h4>"))
	end
	
	return table.concat(ret, "\n")
end

-- stat changes of a hero
function p.hero_level_stats(frame)
	local data = mw.loadJsonData(string.format("Module:BTD6 stats/%s", frame.args[1]))
	
	local ret = {}
	
	-- local functions to improve performance
	local tinsert = table.insert
	local sformat = string.format
	
	-- calculate base tower stats for later comparison
	parse_tower(frame, data, my_data, "<h3>%s</h3>")
	
	for i = 2, 20 do
		tinsert(ret, sformat("===Level %i===", i))
		tinsert(ret, parse_tower_full(frame, data[i], my_data, "<h4>%s</h4>"))
	end
	
	return table.concat(ret, "\n")
end

return p