Module:BATTD tower changes: Difference between revisions

Created page with "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 v..."
 
mNo edit summary
 
(10 intermediate revisions by the same user not shown)
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