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: cleanup inventory types (#691)

e6432b50
Sainan <sainan@calamity.inc>
提交于

代码差异

7 个文件 +42 -45
Modified src/controllers/api/infestedFoundryController.ts +3 -8
@@ -3,15 +3,10 @@ import { getAccountIdForRequest } from "@/src/services/loginService";
3 3 import { getJSONfromString } from "@/src/helpers/stringHelpers";
4 4 import { getInventory, addMiscItems, updateCurrency, addRecipes } from "@/src/services/inventoryService";
5 5 import { IOid } from "@/src/types/commonTypes";
6 import {
7 IConsumedSuit,
8 IInfestedFoundry,
9 IInventoryDatabaseDocument,
10 IMiscItem,
11 ITypeCount
12 } from "@/src/types/inventoryTypes/inventoryTypes";
6 import { IConsumedSuit, IInfestedFoundry, IMiscItem, ITypeCount } from "@/src/types/inventoryTypes/inventoryTypes";
13 7 import { ExportMisc, ExportRecipes } from "warframe-public-export-plus";
14 8 import { getRecipe } from "@/src/services/itemDataService";
9 import { TInventoryDatabaseDocument } from "@/src/models/inventoryModels/inventoryModel";
15 10
16 11 export const infestedFoundryController: RequestHandler = async (req, res) => {
17 12 const accountId = await getAccountIdForRequest(req);
@@ -239,7 +234,7 @@ interface IHelminthSubsumeRequest {
239 234 Recipe: string;
240 235 }
241 236
242 export const handleSubsumeCompletion = (inventory: IInventoryDatabaseDocument): ITypeCount[] => {
237 export const handleSubsumeCompletion = (inventory: TInventoryDatabaseDocument): ITypeCount[] => {
243 238 const [recipeType] = Object.entries(ExportRecipes).find(
244 239 ([_recipeType, recipe]) =>
245 240 recipe.secretIngredientAction == "SIA_WARFRAME_ABILITY" &&
Modified src/controllers/api/inventoryController.ts +10 -10
@@ -1,11 +1,10 @@
1 1 import { RequestHandler } from "express";
2 2 import { getAccountForRequest } from "@/src/services/loginService";
3 import { toInventoryResponse } from "@/src/helpers/inventoryHelpers";
4 3 import { Inventory } from "@/src/models/inventoryModels/inventoryModel";
5 4 import { config } from "@/src/services/configService";
6 5 import allDialogue from "@/static/fixed_responses/allDialogue.json";
7 6 import { ILoadoutDatabase } from "@/src/types/saveLoadoutTypes";
8 import { IInventoryDatabaseDocument, IShipInventory, equipmentKeys } from "@/src/types/inventoryTypes/inventoryTypes";
7 import { IInventoryResponse, IShipInventory, equipmentKeys } from "@/src/types/inventoryTypes/inventoryTypes";
9 8 import { IPolarity, ArtifactPolarity } from "@/src/types/inventoryTypes/commonInventoryTypes";
10 9 import {
11 10 ExportCustoms,
@@ -26,9 +25,7 @@ export const inventoryController: RequestHandler = async (request, response) =>
26 25 return;
27 26 }
28 27
29 const inventory = await Inventory.findOne({ accountOwnerId: account._id.toString() })
30 .populate<{ LoadOutPresets: ILoadoutDatabase }>("LoadOutPresets")
31 .populate<{ Ships: IShipInventory }>("Ships");
28 const inventory = await Inventory.findOne({ accountOwnerId: account._id.toString() });
32 29
33 30 if (!inventory) {
34 31 response.status(400).json({ error: "inventory was undefined" });
@@ -63,14 +60,17 @@ export const inventoryController: RequestHandler = async (request, response) =>
63 60 inventory.InfestedFoundry.AbilityOverrideUnlockCooldown &&
64 61 new Date() >= inventory.InfestedFoundry.AbilityOverrideUnlockCooldown
65 62 ) {
66 handleSubsumeCompletion(inventory as unknown as IInventoryDatabaseDocument);
63 handleSubsumeCompletion(inventory);
67 64 await inventory.save();
68 65 }
69 66
70 //TODO: make a function that converts from database representation to client
71 const inventoryJSON = inventory.toJSON();
72
73 const inventoryResponse = toInventoryResponse(inventoryJSON);
67 const inventoryWithLoadOutPresets = await inventory.populate<{ LoadOutPresets: ILoadoutDatabase }>(
68 "LoadOutPresets"
69 );
70 const inventoryWithLoadOutPresetsAndShips = await inventoryWithLoadOutPresets.populate<{ Ships: IShipInventory }>(
71 "Ships"
72 );
73 const inventoryResponse = inventoryWithLoadOutPresetsAndShips.toJSON<IInventoryResponse>();
74 74
75 75 if (config.infiniteCredits) {
76 76 inventoryResponse.RegularCredits = 999999999;
Modified src/helpers/inventoryHelpers.ts +0 -8
@@ -1,14 +1,6 @@
1 1 import { IMongoDate, IOid } from "@/src/types/commonTypes";
2 import { IInventoryResponse } from "@/src/types/inventoryTypes/inventoryTypes";
3 2 import { Types } from "mongoose";
4 3
5 //TODO: this needs to be addressed: a schema's toJSON is responsible for changing Oid and Date to their corresponding Response versions __id to "ItemId":{"$oid":"6450f720bc562ebf030222d4"}, and a Date to "date":{"$date":{"$numberLong":"unix timestamp"})
6 export const toInventoryResponse = (inventoryDatabase: { accountOwnerId: Types.ObjectId }): IInventoryResponse => {
7 // eslint-disable-next-line @typescript-eslint/no-unused-vars
8 const { accountOwnerId, ...inventoryResponse } = inventoryDatabase;
9 return inventoryResponse as unknown as IInventoryResponse;
10 };
11
12 4 export const toOid = (objectId: Types.ObjectId): IOid => {
13 5 return { $oid: objectId.toString() } satisfies IOid;
14 6 };
Modified src/models/inventoryModels/inventoryModel.ts +14 -1
@@ -1,4 +1,4 @@
1 import { Model, Schema, Types, model } from "mongoose";
1 import { Document, Model, Schema, Types, model } from "mongoose";
2 2 import {
3 3 IFlavourItem,
4 4 IRawUpgrade,
@@ -1061,6 +1061,7 @@ inventorySchema.set("toJSON", {
1061 1061 transform(_document, returnedObject) {
1062 1062 delete returnedObject._id;
1063 1063 delete returnedObject.__v;
1064 delete returnedObject.accountOwnerId;
1064 1065
1065 1066 const inventoryDatabase = returnedObject as IInventoryDatabase;
1066 1067 const inventoryResponse = returnedObject as IInventoryResponse;
@@ -1114,3 +1115,15 @@ type InventoryDocumentProps = {
1114 1115 type InventoryModelType = Model<IInventoryDatabase, {}, InventoryDocumentProps>;
1115 1116
1116 1117 export const Inventory = model<IInventoryDatabase, InventoryModelType>("Inventory", inventorySchema);
1118
1119 // eslint-disable-next-line @typescript-eslint/ban-types
1120 export type TInventoryDatabaseDocument = Document<unknown, {}, IInventoryDatabase> &
1121 Omit<
1122 IInventoryDatabase & {
1123 _id: Types.ObjectId;
1124 } & {
1125 __v: number;
1126 },
1127 keyof InventoryDocumentProps
1128 > &
1129 InventoryDocumentProps;
Modified src/services/guildService.ts +2 -2
@@ -2,7 +2,7 @@ import { Request } from "express";
2 2 import { getAccountIdForRequest } from "@/src/services/loginService";
3 3 import { getInventory } from "@/src/services/inventoryService";
4 4 import { Guild } from "@/src/models/guildModel";
5 import { IInventoryDatabaseDocument } from "../types/inventoryTypes/inventoryTypes";
5 import { TInventoryDatabaseDocument } from "@/src/models/inventoryModels/inventoryModel";
6 6
7 7 export const getGuildForRequest = async (req: Request) => {
8 8 const accountId = await getAccountIdForRequest(req);
@@ -10,7 +10,7 @@ export const getGuildForRequest = async (req: Request) => {
10 10 return await getGuildForRequestEx(req, inventory);
11 11 };
12 12
13 export const getGuildForRequestEx = async (req: Request, inventory: IInventoryDatabaseDocument) => {
13 export const getGuildForRequestEx = async (req: Request, inventory: TInventoryDatabaseDocument) => {
14 14 const guildId = req.query.guildId as string;
15 15 if (!inventory.GuildId || inventory.GuildId.toString() != guildId) {
16 16 throw new Error("Account is not in the guild that it has sent a request for");
Modified src/services/inventoryService.ts +13 -14
@@ -1,4 +1,4 @@
1 import { Inventory } from "@/src/models/inventoryModels/inventoryModel";
1 import { Inventory, TInventoryDatabaseDocument } 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";
@@ -7,7 +7,6 @@ import {
7 7 IChallengeProgress,
8 8 IConsumable,
9 9 IFlavourItem,
10 IInventoryDatabaseDocument,
11 10 IMiscItem,
12 11 IMission,
13 12 IRawUpgrade,
@@ -95,7 +94,7 @@ export const combineInventoryChanges = (InventoryChanges: IInventoryChanges, del
95 94 }
96 95 };
97 96
98 export const getInventory = async (accountOwnerId: string) => {
97 export const getInventory = async (accountOwnerId: string): Promise<TInventoryDatabaseDocument> => {
99 98 const inventory = await Inventory.findOne({ accountOwnerId: accountOwnerId });
100 99
101 100 if (!inventory) {
@@ -485,7 +484,7 @@ const isCurrencyTracked = (usePremium: boolean): boolean => {
485 484 };
486 485
487 486 export const updateCurrency = (
488 inventory: IInventoryDatabaseDocument,
487 inventory: TInventoryDatabaseDocument,
489 488 price: number,
490 489 usePremium: boolean
491 490 ): ICurrencyChanges => {
@@ -590,7 +589,7 @@ const addCrewShip = async (typeName: string, accountId: string) => {
590 589 };
591 590
592 591 const addGearExpByCategory = (
593 inventory: IInventoryDatabaseDocument,
592 inventory: TInventoryDatabaseDocument,
594 593 gearArray: IEquipmentClient[] | undefined,
595 594 categoryName: TEquipmentKey
596 595 ): void => {
@@ -622,7 +621,7 @@ const addGearExpByCategory = (
622 621 });
623 622 };
624 623
625 export const addMiscItems = (inventory: IInventoryDatabaseDocument, itemsArray: IMiscItem[] | undefined): void => {
624 export const addMiscItems = (inventory: TInventoryDatabaseDocument, itemsArray: IMiscItem[] | undefined): void => {
626 625 const { MiscItems } = inventory;
627 626
628 627 itemsArray?.forEach(({ ItemCount, ItemType }) => {
@@ -638,7 +637,7 @@ export const addMiscItems = (inventory: IInventoryDatabaseDocument, itemsArray:
638 637 };
639 638
640 639 export const addShipDecorations = (
641 inventory: IInventoryDatabaseDocument,
640 inventory: TInventoryDatabaseDocument,
642 641 itemsArray: IConsumable[] | undefined
643 642 ): void => {
644 643 const { ShipDecorations } = inventory;
@@ -655,7 +654,7 @@ export const addShipDecorations = (
655 654 });
656 655 };
657 656
658 export const addConsumables = (inventory: IInventoryDatabaseDocument, itemsArray: IConsumable[] | undefined): void => {
657 export const addConsumables = (inventory: TInventoryDatabaseDocument, itemsArray: IConsumable[] | undefined): void => {
659 658 const { Consumables } = inventory;
660 659
661 660 itemsArray?.forEach(({ ItemCount, ItemType }) => {
@@ -670,7 +669,7 @@ export const addConsumables = (inventory: IInventoryDatabaseDocument, itemsArray
670 669 });
671 670 };
672 671
673 export const addRecipes = (inventory: IInventoryDatabaseDocument, itemsArray: ITypeCount[] | undefined): void => {
672 export const addRecipes = (inventory: TInventoryDatabaseDocument, itemsArray: ITypeCount[] | undefined): void => {
674 673 const { Recipes } = inventory;
675 674
676 675 itemsArray?.forEach(({ ItemCount, ItemType }) => {
@@ -685,7 +684,7 @@ export const addRecipes = (inventory: IInventoryDatabaseDocument, itemsArray: IT
685 684 });
686 685 };
687 686
688 export const addMods = (inventory: IInventoryDatabaseDocument, itemsArray: IRawUpgrade[] | undefined): void => {
687 export const addMods = (inventory: TInventoryDatabaseDocument, itemsArray: IRawUpgrade[] | undefined): void => {
689 688 const { RawUpgrades } = inventory;
690 689 itemsArray?.forEach(({ ItemType, ItemCount }) => {
691 690 const itemIndex = RawUpgrades.findIndex(i => i.ItemType === ItemType);
@@ -700,7 +699,7 @@ export const addMods = (inventory: IInventoryDatabaseDocument, itemsArray: IRawU
700 699 };
701 700
702 701 export const addFusionTreasures = (
703 inventory: IInventoryDatabaseDocument,
702 inventory: TInventoryDatabaseDocument,
704 703 itemsArray: IFusionTreasure[] | undefined
705 704 ): void => {
706 705 const { FusionTreasures } = inventory;
@@ -729,7 +728,7 @@ export const updateChallengeProgress = async (
729 728 };
730 729
731 730 export const addSeasonalChallengeHistory = (
732 inventory: IInventoryDatabaseDocument,
731 inventory: TInventoryDatabaseDocument,
733 732 itemsArray: ISeasonChallenge[] | undefined
734 733 ): void => {
735 734 const category = inventory.SeasonChallengeHistory;
@@ -746,7 +745,7 @@ export const addSeasonalChallengeHistory = (
746 745 };
747 746
748 747 export const addChallenges = (
749 inventory: IInventoryDatabaseDocument,
748 inventory: TInventoryDatabaseDocument,
750 749 itemsArray: IChallengeProgress[] | undefined
751 750 ): void => {
752 751 const category = inventory.ChallengeProgress;
@@ -763,7 +762,7 @@ export const addChallenges = (
763 762 });
764 763 };
765 764
766 const addMissionComplete = (inventory: IInventoryDatabaseDocument, { Tag, Completes }: IMission): void => {
765 const addMissionComplete = (inventory: TInventoryDatabaseDocument, { Tag, Completes }: IMission): void => {
767 766 const { Missions } = inventory;
768 767 const itemIndex = Missions.findIndex(item => item.Tag === Tag);
769 768
Modified src/types/inventoryTypes/inventoryTypes.ts +0 -2
@@ -10,8 +10,6 @@ import {
10 10 IEquipmentDatabase
11 11 } from "@/src/types/inventoryTypes/commonInventoryTypes";
12 12
13 //Document extends will be deleted soon. TODO: delete and migrate uses to ...
14 export interface IInventoryDatabaseDocument extends IInventoryDatabase, Document {}
15 13 export interface IInventoryDatabase
16 14 extends Omit<
17 15 IInventoryResponse,