返回提交历史
Modified
src/services/inventoryService.ts
+23
-56
Modified
src/types/inventoryTypes/inventoryTypes.ts
+7
-12
XFEstudio/XFESpaceNinjaServer
chore: remove consumables, recipes, etc. from array when their ItemCount becomes 0 (#1216)
Reviewed-on: https://onlyg.it/OpenWF/SpaceNinjaServer/pulls/1216
1d091e3c
代码差异
2 个文件
+30
-68
@@ -8,7 +8,6 @@ import { HydratedDocument, Types } from "mongoose";
8
8
import { SlotNames, IInventoryChanges, IBinChanges, slotNames } from "@/src/types/purchaseTypes";
9
9
import {
10
10
IChallengeProgress,
11
IConsumable,
12
11
IFlavourItem,
13
12
IMiscItem,
14
13
IMission,
@@ -378,7 +377,7 @@ export const addItem = async (
378
377
{
379
378
ItemType: typeName,
380
379
ItemCount: quantity
381
} satisfies IConsumable
380
} satisfies ITypeCount
382
381
];
383
382
addConsumables(inventory, consumablesChanges);
384
383
return {
@@ -1102,74 +1101,42 @@ export const addMiscItems = (inventory: TInventoryDatabaseDocument, itemsArray:
1102
1101
});
1103
1102
};
1104
1103
1105
export const addShipDecorations = (inventory: TInventoryDatabaseDocument, itemsArray: IConsumable[]): void => {
1106
const { ShipDecorations } = inventory;
1107
1108
itemsArray.forEach(({ ItemCount, ItemType }) => {
1109
const itemIndex = ShipDecorations.findIndex(miscItem => miscItem.ItemType === ItemType);
1104
const applyArrayChanges = (arr: ITypeCount[], changes: ITypeCount[]): void => {
1105
for (const change of changes) {
1106
if (change.ItemCount != 0) {
1107
let itemIndex = arr.findIndex(x => x.ItemType === change.ItemType);
1108
if (itemIndex == -1) {
1109
itemIndex = arr.push({ ItemType: change.ItemType, ItemCount: 0 }) - 1;
1110
}
1110
1111
1111
if (itemIndex !== -1) {
1112
ShipDecorations[itemIndex].ItemCount += ItemCount;
1113
} else {
1114
ShipDecorations.push({ ItemCount, ItemType });
1112
arr[itemIndex].ItemCount += change.ItemCount;
1113
if (arr[itemIndex].ItemCount == 0) {
1114
arr.splice(itemIndex, 1);
1115
} else if (arr[itemIndex].ItemCount <= 0) {
1116
logger.warn(`account now owns a negative amount of ${change.ItemType}`);
1117
}
1115
1118
}
1116
});
1119
}
1117
1120
};
1118
1121
1119
export const addConsumables = (inventory: TInventoryDatabaseDocument, itemsArray: IConsumable[]): void => {
1120
const { Consumables } = inventory;
1121
1122
itemsArray.forEach(({ ItemCount, ItemType }) => {
1123
const itemIndex = Consumables.findIndex(i => i.ItemType === ItemType);
1122
export const addShipDecorations = (inventory: TInventoryDatabaseDocument, itemsArray: ITypeCount[]): void => {
1123
applyArrayChanges(inventory.ShipDecorations, itemsArray);
1124
};
1124
1125
1125
if (itemIndex !== -1) {
1126
Consumables[itemIndex].ItemCount += ItemCount;
1127
} else {
1128
Consumables.push({ ItemCount, ItemType });
1129
}
1130
});
1126
export const addConsumables = (inventory: TInventoryDatabaseDocument, itemsArray: ITypeCount[]): void => {
1127
applyArrayChanges(inventory.Consumables, itemsArray);
1131
1128
};
1132
1129
1133
1130
export const addCrewShipRawSalvage = (inventory: TInventoryDatabaseDocument, itemsArray: ITypeCount[]): void => {
1134
const { CrewShipRawSalvage } = inventory;
1135
1136
itemsArray.forEach(({ ItemCount, ItemType }) => {
1137
const itemIndex = CrewShipRawSalvage.findIndex(i => i.ItemType === ItemType);
1138
1139
if (itemIndex !== -1) {
1140
CrewShipRawSalvage[itemIndex].ItemCount += ItemCount;
1141
} else {
1142
CrewShipRawSalvage.push({ ItemCount, ItemType });
1143
}
1144
});
1131
applyArrayChanges(inventory.CrewShipRawSalvage, itemsArray);
1145
1132
};
1146
1133
1147
1134
export const addCrewShipAmmo = (inventory: TInventoryDatabaseDocument, itemsArray: ITypeCount[]): void => {
1148
const { CrewShipAmmo } = inventory;
1149
1150
itemsArray.forEach(({ ItemCount, ItemType }) => {
1151
const itemIndex = CrewShipAmmo.findIndex(i => i.ItemType === ItemType);
1152
1153
if (itemIndex !== -1) {
1154
CrewShipAmmo[itemIndex].ItemCount += ItemCount;
1155
} else {
1156
CrewShipAmmo.push({ ItemCount, ItemType });
1157
}
1158
});
1135
applyArrayChanges(inventory.CrewShipAmmo, itemsArray);
1159
1136
};
1160
1137
1161
1138
export const addRecipes = (inventory: TInventoryDatabaseDocument, itemsArray: ITypeCount[]): void => {
1162
const { Recipes } = inventory;
1163
1164
itemsArray.forEach(({ ItemCount, ItemType }) => {
1165
const itemIndex = Recipes.findIndex(i => i.ItemType === ItemType);
1166
1167
if (itemIndex !== -1) {
1168
Recipes[itemIndex].ItemCount += ItemCount;
1169
} else {
1170
Recipes.push({ ItemCount, ItemType });
1171
}
1172
});
1139
applyArrayChanges(inventory.Recipes, itemsArray);
1173
1140
};
1174
1141
1175
1142
export const addMods = (inventory: TInventoryDatabaseDocument, itemsArray: IRawUpgrade[]): void => {
@@ -236,8 +236,8 @@ export interface IInventoryClient extends IDailyAffiliations, InventoryClientEqu
236
236
FusionTreasures: IFusionTreasure[];
237
237
WebFlags: IWebFlags;
238
238
CompletedAlerts: string[];
239
Consumables: IConsumable[];
240
LevelKeys: IConsumable[];
239
Consumables: ITypeCount[];
240
LevelKeys: ITypeCount[];
241
241
TauntHistory?: ITaunt[];
242
242
StoryModeChoice: string;
243
243
PeriodicMissionCompletions: IPeriodicMissionCompletionDatabase[];
@@ -265,7 +265,7 @@ export interface IInventoryClient extends IDailyAffiliations, InventoryClientEqu
265
265
Drones: IDroneClient[];
266
266
StepSequencers: IStepSequencer[];
267
267
ActiveAvatarImageType: string;
268
ShipDecorations: IConsumable[];
268
ShipDecorations: ITypeCount[];
269
269
DiscoveredMarkers: IDiscoveredMarker[];
270
270
CompletedJobs: ICompletedJob[];
271
271
FocusAbility?: string;
@@ -293,7 +293,7 @@ export interface IInventoryClient extends IDailyAffiliations, InventoryClientEqu
293
293
Settings: ISettings;
294
294
PersonalTechProjects: IPersonalTechProject[];
295
295
PlayerSkills: IPlayerSkills;
296
CrewShipAmmo: IConsumable[];
296
CrewShipAmmo: ITypeCount[];
297
297
CrewShipSalvagedWeaponSkins: IUpgradeClient[];
298
298
CrewShipWeapons: ICrewShipWeaponClient[];
299
299
CrewShipSalvagedWeapons: IEquipmentClient[];
@@ -303,7 +303,7 @@ export interface IInventoryClient extends IDailyAffiliations, InventoryClientEqu
303
303
SubscribedToEmailsPersonalized: number;
304
304
InfestedFoundry?: IInfestedFoundryClient;
305
305
BlessingCooldown?: IMongoDate;
306
CrewShipRawSalvage: IConsumable[];
306
CrewShipRawSalvage: ITypeCount[];
307
307
CrewMembers: ICrewMember[];
308
308
LotusCustomization: ILotusCustomization;
309
309
UseAdultOperatorLoadout?: boolean;
@@ -417,11 +417,6 @@ export interface ICompletedJob {
417
417
StageCompletions: number[];
418
418
}
419
419
420
export interface IConsumable {
421
ItemCount: number;
422
ItemType: string;
423
}
424
425
420
export interface ICrewMember {
426
421
ItemType: string;
427
422
NemesisFingerprint: number;
@@ -891,7 +886,7 @@ export enum GettingSlotOrderInfo {
891
886
}
892
887
893
888
export interface IGiving {
894
RawUpgrades: IConsumable[];
889
RawUpgrades: ITypeCount[];
895
890
_SlotOrderInfo: GivingSlotOrderInfo[];
896
891
}
897
892
@@ -924,7 +919,7 @@ export interface IPersonalTechProject {
924
919
State: number;
925
920
ReqCredits: number;
926
921
ItemType: string;
927
ReqItems: IConsumable[];
922
ReqItems: ITypeCount[];
928
923
CompletionDate?: IMongoDate;
929
924
ItemId: IOid;
930
925
ProductCategory?: string;