XFE Git
XFE Studio Git
Git 首页 全局搜索
XFE 主站 文档 NuGet

SpaceNinjaServer

A simple server for a small space ninja game

公开
关注 0 Fork 1 Star 0
返回提交历史

XFEstudio/SpaceNinjaServer

chore: updateCurrency with existing inventory instance (#674)

16d98636
Sainan <sainan@calamity.inc>
提交于

代码差异

9 个文件 +63 -60
Modified src/controllers/api/claimCompletedRecipeController.ts +10 -4
@@ -7,7 +7,14 @@ import { getRecipe } from "@/src/services/itemDataService";
7 7 import { IOid } from "@/src/types/commonTypes";
8 8 import { getJSONfromString } from "@/src/helpers/stringHelpers";
9 9 import { getAccountIdForRequest } from "@/src/services/loginService";
10 import { getInventory, updateCurrency, addItem, addMiscItems, addRecipes } from "@/src/services/inventoryService";
10 import {
11 getInventory,
12 updateCurrency,
13 addItem,
14 addMiscItems,
15 addRecipes,
16 updateCurrencyByAccountId
17 } from "@/src/services/inventoryService";
11 18
12 19 export interface IClaimCompletedRecipeRequest {
13 20 RecipeIds: IOid[];
@@ -43,9 +50,8 @@ export const claimCompletedRecipeController: RequestHandler = async (req, res) =
43 50 }
44 51
45 52 if (req.query.cancel) {
46 const currencyChanges = await updateCurrency(recipe.buildPrice * -1, false, accountId);
47
48 53 const inventory = await getInventory(accountId);
54 const currencyChanges = updateCurrency(inventory, recipe.buildPrice * -1, false);
49 55 addMiscItems(inventory, recipe.ingredients);
50 56 await inventory.save();
51 57
@@ -74,7 +80,7 @@ export const claimCompletedRecipeController: RequestHandler = async (req, res) =
74 80 if (req.query.rush) {
75 81 InventoryChanges = {
76 82 ...InventoryChanges,
77 ...(await updateCurrency(recipe.skipBuildTimePrice, true, accountId))
83 ...(await updateCurrencyByAccountId(recipe.skipBuildTimePrice, true, accountId))
78 84 };
79 85 }
80 86 res.json({
Modified src/controllers/api/inventorySlotsController.ts +2 -2
@@ -1,5 +1,5 @@
1 1 import { getAccountIdForRequest } from "@/src/services/loginService";
2 import { updateCurrency } from "@/src/services/inventoryService";
2 import { updateCurrencyByAccountId } from "@/src/services/inventoryService";
3 3 import { RequestHandler } from "express";
4 4 import { updateSlots } from "@/src/services/inventoryService";
5 5 import { InventorySlot } from "@/src/types/inventoryTypes/inventoryTypes";
@@ -26,7 +26,7 @@ export const inventorySlotsController: RequestHandler = async (req, res) => {
26 26
27 27 //TODO: check which slot was purchased because pvpBonus is also possible
28 28
29 const currencyChanges = await updateCurrency(20, true, accountId);
29 const currencyChanges = await updateCurrencyByAccountId(20, true, accountId);
30 30 await updateSlots(accountId, InventorySlot.PVE_LOADOUTS, 1, 1);
31 31
32 32 //console.log({ InventoryChanges: currencyChanges }, " added loadout changes:");
Modified src/controllers/api/modularWeaponCraftingController.ts +6 -8
@@ -34,14 +34,7 @@ export const modularWeaponCraftingController: RequestHandler = async (req, res)
34 34 // Give weapon
35 35 const weapon = await addEquipment(category, data.WeaponType, accountId, data.Parts);
36 36
37 // Remove credits
38 const currencyChanges = await updateCurrency(
39 category == "Hoverboards" || category == "MoaPets" ? 5000 : 4000,
40 false,
41 accountId
42 );
43
44 // Remove parts
37 // Remove credits & parts
45 38 const miscItemChanges = [];
46 39 for (const part of data.Parts) {
47 40 miscItemChanges.push({
@@ -50,6 +43,11 @@ export const modularWeaponCraftingController: RequestHandler = async (req, res)
50 43 });
51 44 }
52 45 const inventory = await getInventory(accountId);
46 const currencyChanges = updateCurrency(
47 inventory,
48 category == "Hoverboards" || category == "MoaPets" ? 5000 : 4000,
49 false
50 );
53 51 addMiscItems(inventory, miscItemChanges);
54 52 await inventory.save();
55 53
Modified src/controllers/api/nameWeaponController.ts +2 -1
@@ -20,8 +20,9 @@ export const nameWeaponController: RequestHandler = async (req, res) => {
20 20 } else {
21 21 item.ItemName = undefined;
22 22 }
23 const currencyChanges = updateCurrency(inventory, "webui" in req.query ? 0 : 15, true);
23 24 await inventory.save();
24 25 res.json({
25 InventoryChanges: await updateCurrency("webui" in req.query ? 0 : 15, true, accountId)
26 InventoryChanges: currencyChanges
26 27 });
27 28 };
Modified src/controllers/api/startRecipeController.ts +1 -2
@@ -26,14 +26,13 @@ export const startRecipeController: RequestHandler = async (req, res) => {
26 26 throw new Error(`unknown recipe ${recipeName}`);
27 27 }
28 28
29 await updateCurrency(recipe.buildPrice, false, accountId);
30
31 29 const ingredientsInverse = recipe.ingredients.map(component => ({
32 30 ItemType: component.ItemType,
33 31 ItemCount: component.ItemCount * -1
34 32 }));
35 33
36 34 const inventory = await getInventory(accountId);
35 updateCurrency(inventory, recipe.buildPrice, false);
37 36 addMiscItems(inventory, ingredientsInverse);
38 37
39 38 //buildtime is in seconds
Modified src/controllers/api/upgradesController.ts +1 -1
@@ -19,7 +19,7 @@ export const upgradesController: RequestHandler = async (req, res) => {
19 19 operation.UpgradeRequirement == "/Lotus/Types/Items/MiscItems/ModSlotUnlocker" ||
20 20 operation.UpgradeRequirement == "/Lotus/Types/Items/MiscItems/CustomizationSlotUnlocker"
21 21 ) {
22 await updateCurrency(10, true, accountId);
22 updateCurrency(inventory, 10, true);
23 23 } else {
24 24 addMiscItems(inventory, [
25 25 {
Modified src/services/inventoryService.ts +32 -36
@@ -2,7 +2,7 @@ import { Inventory } from "@/src/models/inventoryModels/inventoryModel";
2 2 import postTutorialInventory from "@/static/fixed_responses/postTutorialInventory.json";
3 3 import { config } from "@/src/services/configService";
4 4 import { Types } from "mongoose";
5 import { SlotNames, IInventoryChanges, IBinChanges } from "@/src/types/purchaseTypes";
5 import { SlotNames, IInventoryChanges, IBinChanges, ICurrencyChanges } from "@/src/types/purchaseTypes";
6 6 import {
7 7 IChallengeProgress,
8 8 IConsumable,
@@ -483,47 +483,43 @@ export const updateSlots = async (
483 483 await inventory.save();
484 484 };
485 485
486 export const updateCurrency = async (
486 const isCurrencyTracked = (usePremium: boolean): boolean => {
487 return usePremium ? !config.infinitePlatinum : !config.infiniteCredits;
488 };
489
490 export const updateCurrency = (
491 inventory: IInventoryDatabaseDocument,
492 price: number,
493 usePremium: boolean
494 ): ICurrencyChanges => {
495 const currencyChanges: ICurrencyChanges = {};
496 if (price != 0 && isCurrencyTracked(usePremium)) {
497 if (usePremium) {
498 if (inventory.PremiumCreditsFree > 0) {
499 currencyChanges.PremiumCreditsFree = Math.min(price, inventory.PremiumCreditsFree) * -1;
500 inventory.PremiumCreditsFree += currencyChanges.PremiumCreditsFree;
501 }
502 currencyChanges.PremiumCredits = -price;
503 inventory.PremiumCredits += currencyChanges.PremiumCredits;
504 } else {
505 currencyChanges.RegularCredits = -price;
506 inventory.RegularCredits += currencyChanges.RegularCredits;
507 }
508 logger.debug(`currency changes `, currencyChanges);
509 }
510 return currencyChanges;
511 };
512
513 export const updateCurrencyByAccountId = async (
487 514 price: number,
488 515 usePremium: boolean,
489 516 accountId: string
490 ): Promise<IInventoryChanges> => {
491 if (usePremium ? config.infinitePlatinum : config.infiniteCredits) {
517 ): Promise<ICurrencyChanges> => {
518 if (!isCurrencyTracked(usePremium)) {
492 519 return {};
493 520 }
494
495 521 const inventory = await getInventory(accountId);
496
497 if (usePremium) {
498 if (inventory.PremiumCreditsFree > 0) {
499 inventory.PremiumCreditsFree -= Math.min(price, inventory.PremiumCreditsFree);
500 }
501 inventory.PremiumCredits -= price;
502 } else {
503 inventory.RegularCredits -= price;
504 }
505
506 const modifiedPaths = inventory.modifiedPaths();
507
508 type currencyKeys = "RegularCredits" | "PremiumCredits" | "PremiumCreditsFree";
509
510 const currencyChanges = {} as Record<currencyKeys, number>;
511 modifiedPaths.forEach(path => {
512 currencyChanges[path as currencyKeys] = -price;
513 });
514
515 logger.debug(`currency changes `, { currencyChanges });
516
517 //let changes = {} as keyof currencyKeys;
518
519 // const obj2 = modifiedPaths.reduce(
520 // (obj, key) => {
521 // obj[key as keyof currencyKeys] = price;
522 // return obj;
523 // },
524 // {} as Record<keyof currencyKeys, number>
525 // );
526
522 const currencyChanges = updateCurrency(inventory, price, usePremium);
527 523 await inventory.save();
528 524 return currencyChanges;
529 525 };
Modified src/services/purchaseService.ts +2 -2
@@ -6,7 +6,7 @@ import {
6 6 addMiscItems,
7 7 combineInventoryChanges,
8 8 getInventory,
9 updateCurrency,
9 updateCurrencyByAccountId,
10 10 updateSlots
11 11 } from "@/src/services/inventoryService";
12 12 import { getRandomWeightedReward } from "@/src/services/rngService";
@@ -66,7 +66,7 @@ export const handlePurchase = async (
66 66
67 67 if (!purchaseResponse) throw new Error("purchase response was undefined");
68 68
69 const currencyChanges = await updateCurrency(
69 const currencyChanges = await updateCurrencyByAccountId(
70 70 purchaseRequest.PurchaseParams.ExpectedPrice,
71 71 purchaseRequest.PurchaseParams.UsePremium,
72 72 accountId
Modified src/types/purchaseTypes.ts +7 -4
@@ -17,13 +17,16 @@ export interface IPurchaseParams {
17 17 UseFreeFavor?: boolean; // for Source 2
18 18 }
19 19
20 export type IInventoryChanges = {
21 [_ in SlotNames]?: IBinChanges;
22 } & {
20 export interface ICurrencyChanges {
23 21 RegularCredits?: number;
24 22 PremiumCredits?: number;
25 23 PremiumCreditsFree?: number;
26 } & Record<string, IBinChanges | number | object[]>;
24 }
25
26 export type IInventoryChanges = {
27 [_ in SlotNames]?: IBinChanges;
28 } & ICurrencyChanges &
29 Record<string, IBinChanges | number | object[]>;
27 30
28 31 export interface IPurchaseResponse {
29 32 InventoryChanges: IInventoryChanges;