Skip to content

Instantly share code, notes, and snippets.

@olamedia
Last active May 10, 2022
Embed
What would you like to do?
const getCost = (base, rank, step) => {
return Math.round(rank * base * (1 + step / 10));
};
const getTotalCost = (base, rankNumber, startingStep) => {
let totalCost = 0;
const costPerRank = [];
for (let stepShift = 0; stepShift < rankNumber; stepShift += 1) {
const cost = getCost(base, 1 + stepShift, startingStep + stepShift);
costPerRank.push(cost);
totalCost += cost;
}
const avgCost = totalCost / rankNumber;
return { totalCost, costPerRank, avgCost };
};
const dump = (base, rankNumber) => {
const displaySteps = 53;
for (let step = 0; step < displaySteps; step += 1) {
console.log(`${step}\t${getCost(base, 2, step)}\t${getTotalCost(base, rankNumber, step)}`);
}
};
const allUpgrades = Object.entries({
Banish: [100, 4],
Skip: [100, 3],
MaxHealth: [200, 3],
Might: [200, 5],
Recovery: [200, 5],
Greed: [200, 5],
Area: [300, 2],
Speed: [300, 2],
Duration: [300, 2],
MoveSpeed: [300, 2],
Magnet: [300, 2],
Armor: [600, 3],
Luck: [600, 3],
Cooldown: [900, 2],
Growth: [900, 5],
Curse: [1666, 5],
Reroll: [1000, 4],
Amount: [5000, 1],
Revival: [10000, 1],
}).map(([key, [base, rankNumber]]) => ({
id: key,
name: key,
base,
rankNumber,
}));
const upgradeAtStep = (upgrade, step) => {
const totalCostInfo = getTotalCost(upgrade.base, upgrade.rankNumber, step);
return {
...upgrade,
cost: totalCostInfo.totalCost,
costPerRank: totalCostInfo.costPerRank,
avgCost: totalCostInfo.avgCost,
};
};
const findBest = (upgradesLeft, step) => {
// console.log('---------');
const dump = (bestUpgrade) => {
console.log(
`?${step + 1}\t${total}\t${bestUpgrade.cost}\t${bestUpgrade.name}\t${bestUpgrade.base}->${
bestUpgrade.rankNumber
}\t${bestUpgrade.costPerRank.join(',')} `,
);
};
const upgradesWithCost = upgradesLeft.map((upgrade) => {
return upgradeAtStep(upgrade, step);
});
// upgradesWithCost.forEach((upgrade) => dump(upgrade));
upgradesWithCost.sort((a, b) => {
// console.log(a.name);
const currentACost = getTotalCost(a.base, a.rankNumber, step).totalCost;
const currentBCost = getTotalCost(b.base, b.rankNumber, step).totalCost;
const delayedACost = getTotalCost(a.base, a.rankNumber, step + b.rankNumber).totalCost;
const delayedBCost = getTotalCost(b.base, b.rankNumber, step + a.rankNumber).totalCost;
return currentACost + delayedBCost - (currentBCost + delayedACost);
});
// upgradesWithCost.forEach((upgrade) => dump(upgrade));
// console.log('=============');
// upgradesWithCost.forEach((upgrade) => dump(upgrade));
const bestUpgrade = upgradesWithCost[0];
// const minCost = Math.max(...upgradesWithCost.map((upgrade) => upgrade.avgCost));
// const bestUpgrade = upgradesWithCost.find((upgrade) => upgrade.avgCost === minCost);
const upgradesToReturn = upgradesLeft.filter((upgrade) => upgrade.name !== bestUpgrade.name);
return { bestUpgrade, upgradesLeft: upgradesToReturn };
};
const partialUpgrades = [];
allUpgrades.forEach((upgrade) => {
for (let rank = 1; rank <= upgrade.rankNumber; rank += 1) {
const rankNumber = upgrade.rankNumber;
let rankName = `${rank}/${rankNumber}`;
if (rank === rankNumber) {
rankName = `${rank}/${rankNumber} max`;
}
const partialUpgrade = {
...upgrade,
name: `${upgrade.name} ${rankName}`,
rankNumber: rank,
actualRank: rankNumber,
};
// console.log(partialUpgrade.name);
partialUpgrades.push(partialUpgrade);
}
});
const Mayaek = [
'Revival',
'Reroll',
'Curse',
'Amount',
'Growth',
'Cooldown',
'Armor',
'Luck',
'Skip',
'Banish',
'Might',
'Greed',
'Recovery',
];
const useOrderOrFindBest = (order, upgrades, step) => {
if (order.length) {
const bestUpgrade = upgrades.find((upgrade) => upgrade.id === order[0]);
if (bestUpgrade) {
const orderLeft = order.slice(1);
const upgradesLeft = upgrades.filter((upgrade) => upgrade.id !== bestUpgrade.id);
return { orderLeft, bestUpgrade: upgradeAtStep(bestUpgrade, step), upgradesLeft };
}
}
const { bestUpgrade, upgradesLeft } = findBest(upgrades, step);
return { orderLeft: order, bestUpgrade, upgradesLeft };
};
const dumpOrder = (upgrades, short = false) => {
let step = 0;
let total = 0;
// const order = Mayaek;
while (upgrades.length > 0 && step < 1000) {
const { bestUpgrade, upgradesLeft } = findBest(upgrades, step);
// const { orderLeft, bestUpgrade, upgradesLeft } = useOrderOrFindBest(order, upgrades, step);
// console.log({ orderLeft, bestUpgrade, upgradesLeft });
upgrades = upgradesLeft;
// order = orderLeft;
total += bestUpgrade.cost;
// console.log(upgradesLeft.map((upgrade) => `${upgrade.name}`).join(', '));
if (short) {
console.log(`${bestUpgrade.name}`);
} else {
console.log(
`${step + 1}\t${0 + total}\t${bestUpgrade.cost}\t${bestUpgrade.name.padEnd(15, ' ')}\t${bestUpgrade.costPerRank.join(
',',
)} `,
);
}
// \t${bestUpgrade.base}->${
// bestUpgrade.rankNumber
// }
step += bestUpgrade.rankNumber;
}
};
// LIMITS
// upgrades.find((upgrade) => upgrade.name === 'Growth').rankNumber = 1;
// 0.5.0e
console.log('-------------------- ALL UPGRADES -------------------------------------');
dumpOrder(allUpgrades);
console.log('-------------------- Lucky -------------------------------------');
dumpOrder(
allUpgrades.filter((upgrade) => {
return !['Curse', 'Growth', 'Revival', 'Reroll', 'Skip', 'Banish', 'Armor', 'Recovery', 'MaxHealth', 'MoveSpeed'].includes(
upgrade.name,
);
}),
);
console.log('-------------------- Lucky + defence -------------------------------------');
dumpOrder(allUpgrades.filter((upgrade) => !['Curse', 'Growth', 'Revival', 'Reroll', 'Skip', 'Banish'].includes(upgrade.name)));
console.log('-------------------- Basic -------------------------------------');
dumpOrder(allUpgrades.filter((upgrade) => ['Luck', 'Greed', 'Magnet'].includes(upgrade.name)));
console.log('-------------------- Basic + Amount -------------------------------------');
dumpOrder(allUpgrades.filter((upgrade) => ['Luck', 'Greed', 'Amount', 'Magnet'].includes(upgrade.name)));
console.log('-------------------- Late without Curse, Growth, Revival, MaxHealth, Armor -------------------------------------');
dumpOrder(allUpgrades.filter((upgrade) => !['Curse', 'Growth', 'Revival', 'MaxHealth', 'Armor'].includes(upgrade.name)));
console.log('-------------------- Late without Curse, Growth -------------------------------------');
dumpOrder(allUpgrades.filter((upgrade) => !['Curse', 'Growth'].includes(upgrade.name)));
console.log('-------------------- Partial power ups -------------------------------------');
dumpOrder(partialUpgrades, true);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment