Module:BATTD costs: Difference between revisions
Jump to navigation
Jump to search
mNo edit summary |
saving progress |
||
| Line 3: | Line 3: | ||
-- LOCAL FUNCTIONS | -- LOCAL FUNCTIONS | ||
local function GetTotalCosts(baseCost, accessibleUpgrades, upgradesById, upgradeLocksById, upgradePrereqsOfById, Y, N) | local function GetTotalCosts(baseCost, initialPurchasedUpgrades, accessibleUpgrades, upgradesById, upgradeLocksById, upgradePrereqsById, upgradePrereqsOfById, Y, N) | ||
local sFormat = string.format | local sFormat = string.format | ||
local tConcat = table.concat | local tConcat = table.concat | ||
| Line 10: | Line 10: | ||
local headerRow = {"{|class=\"wikitable sortable\" style=\"text-align:center\"\n!Total cost!!Sell value"} | local headerRow = {"{|class=\"wikitable sortable\" style=\"text-align:center\"\n!Total cost!!Sell value"} | ||
for | mw.logObject(accessibleUpgrades) | ||
headerRow[ | |||
for upgradeId, _ in ipairs(accessibleUpgrades) do | |||
headerRow[#headerRow+1] = sFormat("!![[%s|%s]]", upgradesById[upgradeId]._pageName, upgradesById[upgradeId].name) | |||
end | end | ||
| Line 27: | Line 29: | ||
return formatted | return formatted | ||
end | |||
local function GetPurchasedAndAvailableUpgrades(purchasedUpgrades, availableUpgradeId) | |||
-- skip this upgrade if it is a leaf | |||
if not upgradePrereqsOfById[availableUpgradeId] then return nil, nil end | |||
-- skip this upgrade if we don't have all the prereqs | |||
if upgradePrereqsById[availableUpgradeId] then | |||
for _, prereqId in ipairs(upgradePrereqsById[availableUpgradeId]) do | |||
if not purchasedUpgrades[prereqId] then | |||
return nil, nil | |||
end | |||
end | |||
end | |||
-- shallow copy purchasedUpgrades and availableUpgrades | |||
local purchasedUpgradesNext = {} | |||
local availableUpgradesNext = {} | |||
for purchasedUpgradeId, _ in pairs(purchasedUpgrades) do | |||
if upgradeLocksById[purchasedUpgradeId] and upgradeLocksById[purchasedUpgradeId][availableUpgradeId] then | |||
return nil, nil | |||
end | |||
purchasedUpgradesNext[purchasedUpgradeId] = true | |||
for nextUpgradeId, _ in pairs(upgradePrereqsOfById[purchasedUpgradeId]) do | |||
if not upgradeLocksById[purchasedUpgradeId] or not upgradeLocksById[purchasedUpgradeId][nextUpgradeId] then | |||
availableUpgradesNext[nextUpgradeId] = true | |||
end | |||
end | |||
end | |||
purchasedUpgradesNext[availableUpgradeId] = true | |||
return purchasedUpgradesNext, availableUpgradesNext | |||
end | end | ||
| Line 32: | Line 70: | ||
row = {sFormat("\n|-\n|$%s||$%s", FormatNum(currentCost), FormatNum(currentCost*0.7))} | row = {sFormat("\n|-\n|$%s||$%s", FormatNum(currentCost), FormatNum(currentCost*0.7))} | ||
mw.logObject(purchasedUpgrades) | --mw.logObject(purchasedUpgrades) | ||
mw.logObject(availableUpgrades) | --mw.logObject(availableUpgrades) | ||
for | for upgradeId, _ in pairs(accessibleUpgrades) do | ||
row[ | row[#row+1] = sFormat("||%s", purchasedUpgrades[upgradeId] and Y or N) | ||
end | end | ||
outputTable[#outputTable+1] = tConcat(row, "") | outputTable[#outputTable+1] = tConcat(row, "") | ||
for _ | for availableUpgradeId, _ in pairs(availableUpgrades) do | ||
local purchasedUpgradesNext, availableUpgradesNext = GetPurchasedAndAvailableUpgrades(purchasedUpgrades, availableUpgradeId) | |||
local purchasedUpgradesNext | |||
if purchasedUpgradesNext and availableUpgradesNext then | |||
purchasedUpgradesNext[availableUpgradeId] = true | |||
--RecursiveGetTotalCosts(currentCost + upgradesById[availableUpgradeId].cost, purchasedUpgradesNext, availableUpgradesNext) | |||
purchasedUpgradesNext[ | |||
RecursiveGetTotalCosts(currentCost + upgradesById[availableUpgradeId].cost, purchasedUpgradesNext, availableUpgradesNext) | RecursiveGetTotalCosts(currentCost + upgradesById[availableUpgradeId].cost, purchasedUpgradesNext, availableUpgradesNext) | ||
end | end | ||
end | end | ||
end | end | ||
local rootUpgrades = {} | |||
for _, upgradeId in ipairs(upgradePrereqsOfById.root) do | |||
rootUpgrades[upgradeId] = true | |||
end | |||
RecursiveGetTotalCosts(baseCost, initialPurchasedUpgrades, rootUpgrades) | |||
outputTable[#outputTable+1] = "\n|}" | outputTable[#outputTable+1] = "\n|}" | ||
| Line 74: | Line 101: | ||
return tConcat(outputTable, "") | return tConcat(outputTable, "") | ||
end | end | ||
local function | local function Upgrade(towerName, upgradeName, Y, N) | ||
local sFormat = string.format | local sFormat = string.format | ||
local query = mw.ext.cargo.query | local query = mw.ext.cargo.query | ||
| Line 164: | Line 149: | ||
end | end | ||
else | else | ||
-- if this upgrade is a root upgrade | -- if this upgrade is a root upgrade | ||
upgradePrereqsOfById["root"][#upgradePrereqsOfById["root"]+1] = upgrade.thisId | upgradePrereqsOfById["root"][#upgradePrereqsOfById["root"]+1] = upgrade.thisId | ||
upgradePrereqsById[upgrade.thisId] = {"root"} | |||
end | end | ||
| Line 188: | Line 174: | ||
-- build alreadyPurchasedUpgrades | -- build alreadyPurchasedUpgrades | ||
local function | local function GetPurchasedUpgrades(myUpgradeId) | ||
local cost = upgradesById[myUpgradeId].cost | local cost = upgradesById[myUpgradeId].cost | ||
purchasedUpgrades[myUpgradeId] = true | purchasedUpgrades[myUpgradeId] = true | ||
for | for _, id in ipairs(upgradePrereqsById[myUpgradeId]) do | ||
cost = cost + | if not id == "root" then cost = cost + GetPurchasedUpgrades(id) end | ||
end | end | ||
return cost | return cost | ||
| Line 199: | Line 185: | ||
-- tower cost + total cost of this upgrade and all its prereqs | -- tower cost + total cost of this upgrade and all its prereqs | ||
local baseCost = | local baseCost = GetPurchasedUpgrades(upgradeId) + query("battd_characters", "cost", { | ||
where=sFormat("name='%s'", towerName) | where=sFormat("name='%s'", towerName) | ||
})[1].cost | })[1].cost | ||
local function | local function GetAccessibleUpgrades(myUpgradeId) | ||
for _, upgrade in ipairs(upgradePrereqsOfById[myUpgradeId]) do | for _, upgrade in ipairs(upgradePrereqsOfById[myUpgradeId]) do | ||
| Line 213: | Line 199: | ||
end | end | ||
accessibleUpgrades[ | -- upgrades with multiple prereqs are only accessible if we can reach all prereqs | ||
for _, prereqId in ipairs(upgradePrereqsById[upgrade]) do | |||
if not accessibleUpgrades[prereqId] then return end | |||
end | |||
accessibleUpgrades[myUpgradeId] = true | |||
if upgradePrereqsOfById[upgrade] then | if upgradePrereqsOfById[upgrade] then | ||
GetAccessibleUpgrades(upgrade) | |||
end | end | ||
end | end | ||
end | end | ||
end | end | ||
GetAccessibleUpgrades("root") | |||
--return "" | --return "" | ||
return GetTotalCosts(baseCost, accessibleUpgrades, upgradesById, upgradeLocksById, upgradePrereqsOfById, Y, N) | return GetTotalCosts(baseCost, purchasedUpgrades, accessibleUpgrades, upgradesById, upgradeLocksById, upgradePrereqsById, upgradePrereqsOfById, Y, N) | ||
end | end | ||
-- GLOBAL FUNCTIONS | -- GLOBAL FUNCTIONS | ||
p["tower"] = function(frame) | |||
return "" | return "" | ||
end | end | ||
p["upgrade"] = function(frame) | |||
--return upgrade(frame.args[1], frame.args[2], frame:expandTemplate{title = "Y", args = {}}, frame:expandTemplate{title = "N", args = {}}) | --return upgrade(frame.args[1], frame.args[2], frame:expandTemplate{title = "Y", args = {}}, frame:expandTemplate{title = "N", args = {}}) | ||
return | return Upgrade(frame.args[1], frame.args[2], "Y", "N") | ||
end | end | ||
return p | return p | ||
Revision as of 02:42, 17 June 2026
Documentation for this module may be created at Module:BATTD costs/doc
local p = {}
-- LOCAL FUNCTIONS
local function GetTotalCosts(baseCost, initialPurchasedUpgrades, accessibleUpgrades, upgradesById, upgradeLocksById, upgradePrereqsById, upgradePrereqsOfById, Y, N)
local sFormat = string.format
local tConcat = table.concat
local sGsub = string.gsub
local headerRow = {"{|class=\"wikitable sortable\" style=\"text-align:center\"\n!Total cost!!Sell value"}
mw.logObject(accessibleUpgrades)
for upgradeId, _ in ipairs(accessibleUpgrades) do
headerRow[#headerRow+1] = sFormat("!![[%s|%s]]", upgradesById[upgradeId]._pageName, upgradesById[upgradeId].name)
end
local outputTable = {tConcat(headerRow, "")}
-- english-only alternative to FormatNum that takes up less time
local function FormatNum(num)
local formatted = tostring(num)
-- skip if less than 1000
local k = (num > 999 and 1 or 0)
while k ~= 0 do
formatted, k = sGsub(formatted, "^(-?%d+)(%d%d%d)", "%1,%2")
end
return formatted
end
local function GetPurchasedAndAvailableUpgrades(purchasedUpgrades, availableUpgradeId)
-- skip this upgrade if it is a leaf
if not upgradePrereqsOfById[availableUpgradeId] then return nil, nil end
-- skip this upgrade if we don't have all the prereqs
if upgradePrereqsById[availableUpgradeId] then
for _, prereqId in ipairs(upgradePrereqsById[availableUpgradeId]) do
if not purchasedUpgrades[prereqId] then
return nil, nil
end
end
end
-- shallow copy purchasedUpgrades and availableUpgrades
local purchasedUpgradesNext = {}
local availableUpgradesNext = {}
for purchasedUpgradeId, _ in pairs(purchasedUpgrades) do
if upgradeLocksById[purchasedUpgradeId] and upgradeLocksById[purchasedUpgradeId][availableUpgradeId] then
return nil, nil
end
purchasedUpgradesNext[purchasedUpgradeId] = true
for nextUpgradeId, _ in pairs(upgradePrereqsOfById[purchasedUpgradeId]) do
if not upgradeLocksById[purchasedUpgradeId] or not upgradeLocksById[purchasedUpgradeId][nextUpgradeId] then
availableUpgradesNext[nextUpgradeId] = true
end
end
end
purchasedUpgradesNext[availableUpgradeId] = true
return purchasedUpgradesNext, availableUpgradesNext
end
local function RecursiveGetTotalCosts(currentCost, purchasedUpgrades, availableUpgrades)
row = {sFormat("\n|-\n|$%s||$%s", FormatNum(currentCost), FormatNum(currentCost*0.7))}
--mw.logObject(purchasedUpgrades)
--mw.logObject(availableUpgrades)
for upgradeId, _ in pairs(accessibleUpgrades) do
row[#row+1] = sFormat("||%s", purchasedUpgrades[upgradeId] and Y or N)
end
outputTable[#outputTable+1] = tConcat(row, "")
for availableUpgradeId, _ in pairs(availableUpgrades) do
local purchasedUpgradesNext, availableUpgradesNext = GetPurchasedAndAvailableUpgrades(purchasedUpgrades, availableUpgradeId)
if purchasedUpgradesNext and availableUpgradesNext then
purchasedUpgradesNext[availableUpgradeId] = true
--RecursiveGetTotalCosts(currentCost + upgradesById[availableUpgradeId].cost, purchasedUpgradesNext, availableUpgradesNext)
RecursiveGetTotalCosts(currentCost + upgradesById[availableUpgradeId].cost, purchasedUpgradesNext, availableUpgradesNext)
end
end
end
local rootUpgrades = {}
for _, upgradeId in ipairs(upgradePrereqsOfById.root) do
rootUpgrades[upgradeId] = true
end
RecursiveGetTotalCosts(baseCost, initialPurchasedUpgrades, rootUpgrades)
outputTable[#outputTable+1] = "\n|}"
return tConcat(outputTable, "")
end
local function Upgrade(towerName, upgradeName, Y, N)
local sFormat = string.format
local query = mw.ext.cargo.query
local outputTable = {}
local upgradeId = ""
local upgradesById = {} -- key:value table - upgrade:upgrade data
local upgradeLocksById = {} -- key:values table - upgrades:upgrades that this upgrade locks
local upgradePrereqsById = {} -- key:values table - upgrade:prerequisites to purchase this upgrade
local upgradePrereqsOfById = { -- key:values table - upgrades:upgrades this is a prerequisite of
root = {}
}
local purchasedUpgrades = {}
local accessibleUpgrades = {} -- list of upgrades that we can get from this upgrade
-- cargo queries for data
local upgrades, upgradePrereqs, upgradeLocks =
query("battd_upgrades", "_pageName, id, name, tower, cost", {
where=sFormat("tower='%s'", towerName),
orderBy="cost"
}),
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"
}),
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"
})
-- build upgradesById
for i, upgrade in ipairs(upgrades) do
upgradesById[upgrade.id] = upgrade
if upgrade.name == upgradeName then upgradeId = upgrade.id end
end
-- build upgradePrereqsById, 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
upgradePrereqsOfById["root"][#upgradePrereqsOfById["root"]+1] = upgrade.thisId
upgradePrereqsById[upgrade.thisId] = {"root"}
end
-- upgrade:prev table
if upgradePrereqsById[upgrade.thisId] then
upgradePrereqsById[upgrade.thisId][#upgradePrereqsById[upgrade.thisId]+1] = upgrade.prevId
else
upgradePrereqsById[upgrade.thisId] = {upgrade.prevId}
end
end
-- build upgradeLocksById
for i, upgrade in ipairs(upgradeLocks) do
if upgrade.lockId then
if upgradeLocksById[upgrade.thisId] then
upgradeLocksById[upgrade.thisId][upgrade.lockId] = true
else
upgradeLocksById[upgrade.thisId] = {[upgrade.lockId] = true}
end
end
end
-- build alreadyPurchasedUpgrades
local function GetPurchasedUpgrades(myUpgradeId)
local cost = upgradesById[myUpgradeId].cost
purchasedUpgrades[myUpgradeId] = true
for _, id in ipairs(upgradePrereqsById[myUpgradeId]) do
if not id == "root" then cost = cost + GetPurchasedUpgrades(id) end
end
return cost
end
-- tower cost + total cost of this upgrade and all its prereqs
local baseCost = GetPurchasedUpgrades(upgradeId) + query("battd_characters", "cost", {
where=sFormat("name='%s'", towerName)
})[1].cost
local function GetAccessibleUpgrades(myUpgradeId)
for _, upgrade in ipairs(upgradePrereqsOfById[myUpgradeId]) do
if not purchasedUpgrades[upgrade] then
if upgradeLocksById[upgrade] then
for _, upgradeLock in ipairs(upgradeLocksById[upgrade]) do
if purchasedUpgrades[upgradeLock] then return end
end
end
-- upgrades with multiple prereqs are only accessible if we can reach all prereqs
for _, prereqId in ipairs(upgradePrereqsById[upgrade]) do
if not accessibleUpgrades[prereqId] then return end
end
accessibleUpgrades[myUpgradeId] = true
if upgradePrereqsOfById[upgrade] then
GetAccessibleUpgrades(upgrade)
end
end
end
end
GetAccessibleUpgrades("root")
--return ""
return GetTotalCosts(baseCost, purchasedUpgrades, accessibleUpgrades, upgradesById, upgradeLocksById, upgradePrereqsById, upgradePrereqsOfById, Y, N)
end
-- GLOBAL FUNCTIONS
p["tower"] = function(frame)
return ""
end
p["upgrade"] = function(frame)
--return upgrade(frame.args[1], frame.args[2], frame:expandTemplate{title = "Y", args = {}}, frame:expandTemplate{title = "N", args = {}})
return Upgrade(frame.args[1], frame.args[2], "Y", "N")
end
return p