返回提交历史
Modified
src/services/inventoryService.ts
+21
-12
XFEstudio/SpaceNinjaServer
fix: validate non-starter platinum balance suffices for trading, etc. (#4175)
Reviewed-on: https://onlyg.it/OpenWF/SpaceNinjaServer/pulls/4175 Co-authored-by: Sainan <63328889+Sainan@users.noreply.github.com> Co-committed-by: Sainan <63328889+Sainan@users.noreply.github.com>
54358adb
代码差异
1 个文件
+21
-12
@@ -1730,7 +1730,7 @@ export const updateCredits = (
1730
1730
inventoryChanges: IInventoryChanges = {}
1731
1731
): IInventoryChanges => {
1732
1732
if (price != 0 && !inventory.infiniteCredits) {
1733
if (inventory.RegularCredits - price < 0) {
1733
if (price > inventory.RegularCredits) {
1734
1734
throw new Error(`Cannot subtract ${price} credits, would be left with ${inventory.RegularCredits - price}`);
1735
1735
}
1736
1736
inventoryChanges.RegularCredits ??= 0;
@@ -1748,17 +1748,26 @@ export const updatePlatinum = (
1748
1748
inventoryChanges: IInventoryChanges = {}
1749
1749
): IInventoryChanges => {
1750
1750
if (price != 0 && !inventory.infinitePlatinum) {
1751
if (price > 0 && inventory.PremiumCreditsFree > 0 && !mustBePaidPlatinum) {
1752
const premiumCreditsFreeDelta = Math.min(price, inventory.PremiumCreditsFree) * -1;
1753
inventoryChanges.PremiumCreditsFree ??= 0;
1754
inventoryChanges.PremiumCreditsFree += premiumCreditsFreeDelta;
1755
inventory.PremiumCreditsFree += premiumCreditsFreeDelta;
1756
logger.debug(`spending ${-premiumCreditsFreeDelta} starter plat`);
1757
}
1758
if (inventory.PremiumCredits - price < 0) {
1759
throw new Error(
1760
`Cannot subtract ${price} platinum, would be left with ${inventory.PremiumCredits - price}`
1761
);
1751
if (price > 0) {
1752
if (mustBePaidPlatinum) {
1753
const paidPlatinumBalance = inventory.PremiumCredits - inventory.PremiumCreditsFree;
1754
if (price > paidPlatinumBalance) {
1755
throw new Error(
1756
`Cannot subtract ${price} paid platinum, would be left with ${paidPlatinumBalance - price}`
1757
);
1758
}
1759
} else if (inventory.PremiumCreditsFree > 0) {
1760
const premiumCreditsFreeDelta = Math.min(price, inventory.PremiumCreditsFree) * -1;
1761
inventoryChanges.PremiumCreditsFree ??= 0;
1762
inventoryChanges.PremiumCreditsFree += premiumCreditsFreeDelta;
1763
inventory.PremiumCreditsFree += premiumCreditsFreeDelta;
1764
logger.debug(`spending ${-premiumCreditsFreeDelta} starter plat`);
1765
}
1766
if (price > inventory.PremiumCredits) {
1767
throw new Error(
1768
`Cannot subtract ${price} platinum, would be left with ${inventory.PremiumCredits - price}`
1769
);
1770
}
1762
1771
}
1763
1772
inventoryChanges.PremiumCredits ??= 0;
1764
1773
inventoryChanges.PremiumCredits -= price;