Module:BATTD costs: Difference between revisions

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 id, upgrade in pairs(upgradesById) do
for i, upgrade in ipairs(upgrades) do
headerRow[#headerRow+1] = sFormat("!![[%s|%s]]", upgrade._pageName, upgrade.name)
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 id, upgrade in pairs(upgradesById) do
mw.logObject(purchasedUpgrades)
row[#row+1] = sFormat("||%s", purchasedUpgrades[upgrade.id] and Y or N)
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 = { availableUpgradeId = availableUpgrade }
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[purchasedUpgradeId] and upgradeLocksById[purchasedUpgradeId][availableUpgradeId] then
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, {}, upgradesById)
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 = {} -- key:values table - upgrades:upgrades this is a prerequisite of
local upgradePrereqsOfById = { -- key:values table - upgrades:upgrades this is a prerequisite of
local availableUpgradesById = {} -- this upgrade and all upgrades the player has to have already purchased to get it
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
availableUpgradesById[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
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
availableUpgradesById[myUpgradeId] = nil
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
-- remove future upgrades
local function getAccessibleUpgrades(myUpgradeId)
local function removeFutureUpgrades(myUpgradeId)
for _, id in ipairs(upgradePrereqsOfById[myUpgradeId]) do
for _, upgrade in ipairs(upgradePrereqsOfById[myUpgradeId]) do
availableUpgradesById[id] = nil
accessibleUpgrades[#accessibleUpgrades+1] = upgrade
if upgradePrereqsOfById[id] then removeFutureUpgrades(id) end
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
removeFutureUpgrades(upgradeId)
getAccessibleUpgrades("root")


-- build upgradeLocksById
-- build upgradeLocksById
Line 207: Line 222:
})[1].cost
})[1].cost
return GetTotalCosts(baseCost, availableUpgradesById, upgradeLocksById, upgradePrereqsOfById, Y, N)
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")
return upgrade(frame.args[1], frame.args[2], "Y", "N")
end
end


return p
return p