Module:BATTD costs: Difference between revisions
Jump to navigation
Jump to search
mNo edit summary |
yes i know this is broken i will fix it l8r |
||
| Line 3: | Line 3: | ||
-- LOCAL FUNCTIONS | -- LOCAL FUNCTIONS | ||
local function GetTotalCosts(baseCost, upgradesById, upgradeLocksById, upgradePrereqsOfById, Y, N) | local function GetTotalCosts(baseCost, accessibleUpgrades, upgradesById, upgradeLocksById, 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 | for i, upgrade in ipairs(upgrades) do | ||
headerRow[ | headerRow[i+1] = sFormat("!![[%s|%s]]", upgrade._pageName, upgrade.name) | ||
end | end | ||
| Line 32: | Line 32: | ||
row = {sFormat("\n|-\n|$%s||$%s", FormatNum(currentCost), FormatNum(currentCost*0.7))} | row = {sFormat("\n|-\n|$%s||$%s", FormatNum(currentCost), FormatNum(currentCost*0.7))} | ||
for | mw.logObject(purchasedUpgrades) | ||
row[ | mw.logObject(availableUpgrades) | ||
for i, upgrade in ipairs(upgrades) do | |||
row[i+1] = sFormat("||%s", purchasedUpgrades[upgrade.id] and Y or N) | |||
end | end | ||
| Line 40: | Line 43: | ||
for availableUpgradeId, availableUpgrade in pairs(availableUpgrades) do | for availableUpgradeId, availableUpgrade in pairs(availableUpgrades) do | ||
-- shallow copy purchasedUpgrades and availableUpgrades | -- shallow copy purchasedUpgrades and availableUpgrades | ||
local purchasedUpgradesNext = { | local purchasedUpgradesNext = {} | ||
local availableUpgradesNext = {} | local availableUpgradesNext = {} | ||
local doNext = true | local doNext = true | ||
for purchasedUpgradeId, purchasedUpgrade in pairs(purchasedUpgrades) do | for purchasedUpgradeId, purchasedUpgrade in pairs(purchasedUpgrades) do | ||
if upgradeLocksById[ | if upgradeLocksById[availableUpgradeId] and upgradeLocksById[purchasedUpgradeId][purchasedUpgradeId] then | ||
doNext = false | doNext = false | ||
break | break | ||
| Line 60: | Line 63: | ||
if doNext then | if doNext then | ||
purchasedUpgradesNext[availableUpgradeId] = availableUpgrade | |||
RecursiveGetTotalCosts(currentCost + availableUpgrade.cost, purchasedUpgradesNext, availableUpgradesNext) | RecursiveGetTotalCosts(currentCost + availableUpgrade.cost, purchasedUpgradesNext, availableUpgradesNext) | ||
end | end | ||
end | end | ||
end | end | ||
RecursiveGetTotalCosts(baseCost, {}, | RecursiveGetTotalCosts(baseCost, {}, upgradePrereqsOfById.root) | ||
outputTable[#outputTable+1] = "\n|}" | outputTable[#outputTable+1] = "\n|}" | ||
| Line 124: | Line 128: | ||
local upgradeLocksById = {} -- key:values table - upgrades:upgrades that this upgrade locks | local upgradeLocksById = {} -- key:values table - upgrades:upgrades that this upgrade locks | ||
local upgradePrereqsById = {} -- key:values table - upgrade:prerequisites to purchase this upgrade | local upgradePrereqsById = {} -- key:values table - upgrade:prerequisites to purchase this upgrade | ||
local upgradePrereqsOfById = { | local upgradePrereqsOfById = { -- key:values table - upgrades:upgrades this is a prerequisite of | ||
local | root = {} | ||
} | |||
local purchasedUpgrades = {} | |||
local accessibleUpgrades = {} -- list of upgrades that we can get from this upgrade | |||
-- cargo queries for data | -- cargo queries for data | ||
| Line 145: | Line 152: | ||
for i, upgrade in ipairs(upgrades) do | for i, upgrade in ipairs(upgrades) do | ||
upgradesById[upgrade.id] = upgrade | upgradesById[upgrade.id] = upgrade | ||
if upgrade.name == upgradeName then upgradeId = upgrade.id end | if upgrade.name == upgradeName then upgradeId = upgrade.id end | ||
end | end | ||
| Line 160: | Line 166: | ||
else | else | ||
-- if this upgrade is a root upgrade and is not already purchased | -- if this upgrade is a root upgrade and is not already purchased | ||
upgradePrereqsOfById["root"][#upgradePrereqsOfById["root"]+1] = upgrade.thisId | |||
end | end | ||
| Line 174: | Line 180: | ||
local function getAlreadyPurchasedUpgrades(myUpgradeId) | local function getAlreadyPurchasedUpgrades(myUpgradeId) | ||
local cost = upgradesById[myUpgradeId].cost | local cost = upgradesById[myUpgradeId].cost | ||
purchasedUpgrades[myUpgradeId] = true | |||
for i, id in ipairs(upgradePrereqsById[myUpgradeId]) do | for i, id in ipairs(upgradePrereqsById[myUpgradeId]) do | ||
| Line 182: | Line 188: | ||
end | end | ||
local function getAccessibleUpgrades(myUpgradeId) | |||
local function | |||
for _, | for _, upgrade in ipairs(upgradePrereqsOfById[myUpgradeId]) do | ||
accessibleUpgrades[#accessibleUpgrades+1] = upgrade | |||
if upgradePrereqsOfById[ | |||
if upgradePrereqsOfById[upgrade] then | |||
if upgradeLocksById[upgrade] then | |||
for _, upgradeLock in ipairs(upgradeLocksById[upgrade]) do | |||
if purchasedUpgrades[upgradeLock] then return end | |||
end | |||
end | |||
getAccessibleUpgrades(upgrade) | |||
end | |||
end | end | ||
end | end | ||
getAccessibleUpgrades("root") | |||
-- build upgradeLocksById | -- build upgradeLocksById | ||
| Line 207: | Line 222: | ||
})[1].cost | })[1].cost | ||
return GetTotalCosts(baseCost, | return GetTotalCosts(baseCost, availableUpgrades, upgradesById, upgradeLocksById, upgradePrereqsOfById, Y, N) | ||
end | end | ||
| Line 217: | Line 232: | ||
function p.upgrade(frame) | function p.upgrade(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 upgrade(frame.args[1], frame.args[2], "Y", "N") | |||
end | end | ||
return p | return p | ||
Revision as of 19:16, 16 June 2026
Documentation for this module may be created at Module:BATTD costs/doc
local p = {}
-- LOCAL FUNCTIONS
local function GetTotalCosts(baseCost, accessibleUpgrades, upgradesById, upgradeLocksById, 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"}
for i, upgrade in ipairs(upgrades) do
headerRow[i+1] = sFormat("!![[%s|%s]]", upgrade._pageName, upgrade.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 RecursiveGetTotalCosts(currentCost, purchasedUpgrades, availableUpgrades)
row = {sFormat("\n|-\n|$%s||$%s", FormatNum(currentCost), FormatNum(currentCost*0.7))}
mw.logObject(purchasedUpgrades)
mw.logObject(availableUpgrades)
for i, upgrade in ipairs(upgrades) do
row[i+1] = sFormat("||%s", purchasedUpgrades[upgrade.id] and Y or N)
end
outputTable[#outputTable+1] = tConcat(row, "")
for availableUpgradeId, availableUpgrade in pairs(availableUpgrades) do
-- shallow copy purchasedUpgrades and availableUpgrades
local purchasedUpgradesNext = {}
local availableUpgradesNext = {}
local doNext = true
for purchasedUpgradeId, purchasedUpgrade in pairs(purchasedUpgrades) do
if upgradeLocksById[availableUpgradeId] and upgradeLocksById[purchasedUpgradeId][purchasedUpgradeId] then
doNext = false
break
end
purchasedUpgradesNext[purchasedUpgradeId] = purchasedUpgrade
for _, nextUpgrade in ipairs(upgradePrereqsOfById[purchasedUpgradeId]) do
if not upgradeLocksById[purchasedUpgradeId] and upgradeLocksById[purchasedUpgradeId][availableUpgradeId] then
availableUpgradesNext[nextUpgrade.id] = nextUpgrade
end
end
end
if doNext then
purchasedUpgradesNext[availableUpgradeId] = availableUpgrade
RecursiveGetTotalCosts(currentCost + availableUpgrade.cost, purchasedUpgradesNext, availableUpgradesNext)
end
end
end
RecursiveGetTotalCosts(baseCost, {}, upgradePrereqsOfById.root)
outputTable[#outputTable+1] = "\n|}"
return tConcat(outputTable, "")
end
local a = [[-- add this combination's row to outputTable
row = {sFormat("\n|-\n|$%s||$%s", lang:formatNum(currentCost), lang:formatNum(currentCost*0.7))}
for i, upgrade in ipairs(upgrades) do
if not alreadyPurchasedUpgrades[upgrade.id] and not upgradeLocksById[upgradeId][upgrade.id] then
row[#row+1] = sFormat("||%s", purchasedUpgrades[upgrade.id] and Y or N)
end
end
outputTable[#outputTable+1] = tConcat(row, "\n")
for thisUpgradeId, _ in pairs(purchasedUpgrades) do
-- skip if this upgrade is a leaf
if upgradePrereqsOfById[thisUpgradeId] then
for _, nextUpgradeId in pairs(upgradePrereqsOfById[thisUpgradeId]) do
if not purchasedUpgrades[nextUpgradeId] then
-- shallow copy purchasedUpgrades and check if nextUpgradeId is locked by another upgrade
local purchasedUpgradesNext = {}
local doNext = true
for purchasedUpgrade, _ in pairs(purchasedUpgrades) do
-- if nextUpgradeId is locked by a purchasedUpgrade then don't proceed
if upgradeLocksById[purchasedUpgrade] and upgradeLocksById[purchasedUpgrade][nextUpgradeId] then
doNext = false
break
end
purchasedUpgradesNext[purchasedUpgrade] = true
end
if doNext then
purchasedUpgradesNext[nextUpgradeId] = true
recursiveGetTotalCosts(currentCost + upgradesById[nextUpgradeId].cost, purchasedUpgradesNext)
-- lock this combination so it isn't repeated on the graph again
if not upgradeLocksById[thisUpgradeId] then upgradeLocksById[thisUpgradeId] = {} end
upgradeLocksById[thisUpgradeId][nextUpgradeId] = true
if not upgradeLocksById[nextUpgradeId] then upgradeLocksById[nextUpgradeId] = {} end
upgradeLocksById[nextUpgradeId][thisUpgradeId] = true
end
end
end
end
end]]
local function upgrade(towerName, upgradeName, Y, N)
local sFormat = string.format
local query = mw.ext.cargo.query
local lang = mw.language.new("en")
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 and is not already purchased
upgradePrereqsOfById["root"][#upgradePrereqsOfById["root"]+1] = upgrade.thisId
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 alreadyPurchasedUpgrades
local function getAlreadyPurchasedUpgrades(myUpgradeId)
local cost = upgradesById[myUpgradeId].cost
purchasedUpgrades[myUpgradeId] = true
for i, id in ipairs(upgradePrereqsById[myUpgradeId]) do
cost = cost + getAlreadyPurchasedUpgrades(id)
end
return cost
end
local function getAccessibleUpgrades(myUpgradeId)
for _, upgrade in ipairs(upgradePrereqsOfById[myUpgradeId]) do
accessibleUpgrades[#accessibleUpgrades+1] = upgrade
if upgradePrereqsOfById[upgrade] then
if upgradeLocksById[upgrade] then
for _, upgradeLock in ipairs(upgradeLocksById[upgrade]) do
if purchasedUpgrades[upgradeLock] then return end
end
end
getAccessibleUpgrades(upgrade)
end
end
end
getAccessibleUpgrades("root")
-- 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
-- tower cost + total cost of this upgrade and all its prereqs
local baseCost = getAlreadyPurchasedUpgrades(upgradeId) + query("battd_characters", "cost", {
where=sFormat("name='%s'", towerName)
})[1].cost
return GetTotalCosts(baseCost, availableUpgrades, upgradesById, upgradeLocksById, upgradePrereqsOfById, Y, N)
end
-- GLOBAL FUNCTIONS
function p.tower(frame)
return ""
end
function p.upgrade(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