返回提交历史
Modified
src/controllers/api/saveLoadoutController.ts
+1
-1
Modified
src/helpers/inventoryHelpers.ts
+0
-5
Modified
src/models/inventoryModels/loadoutModel.ts
+3
-3
Modified
src/services/importService.ts
+14
-12
Modified
src/services/saveLoadoutService.ts
+3
-1
Modified
src/types/equipmentTypes.ts
+2
-2
XFEstudio/XFESpaceNinjaServer
chore: improve loadout typings (#3575)
Closes #3550 Reviewed-on: https://onlyg.it/OpenWF/SpaceNinjaServer/pulls/3575 Co-authored-by: Sainan <63328889+Sainan@users.noreply.github.com> Co-committed-by: Sainan <63328889+Sainan@users.noreply.github.com>
af401507
代码差异
6 个文件
+23
-24
@@ -8,7 +8,7 @@ export const saveLoadoutController: RequestHandler = async (req, res) => {
8
8
const account = await getAccountForRequest(req);
9
9
10
10
const body: ISaveLoadoutRequest = getJSONfromString<ISaveLoadoutRequest>(String(req.body));
11
// console.log(util.inspect(body, { showHidden: false, depth: null, colors: true }));
11
//console.log(JSON.stringify(body, null, 2));
12
12
13
13
// eslint-disable-next-line @typescript-eslint/no-unused-vars
14
14
const { UpgradeVer, ...equipmentChanges } = body;
@@ -63,11 +63,6 @@ export const fromOid = (oid: IOidWithLegacySupport): string => {
63
63
return (oid.$oid ?? oid.$id)!;
64
64
};
65
65
66
// For oids that may have been stored incorrectly
67
export const fromDbOid = (x: Types.ObjectId | IOid): Types.ObjectId => {
68
return "$oid" in x ? new Types.ObjectId(x.$oid) : x;
69
};
70
71
66
export const toMongoDate = (date: Date): IMongoDate => {
72
67
return { $date: { $numberLong: date.getTime().toString() } };
73
68
};
@@ -1,4 +1,4 @@
1
import { fromDbOid, toOid } from "../../helpers/inventoryHelpers.ts";
1
import { toOid } from "../../helpers/inventoryHelpers.ts";
2
2
import type { IOid } from "../../types/commonTypes.ts";
3
3
import type { IEquipmentSelectionClient, IEquipmentSelectionDatabase } from "../../types/equipmentTypes.ts";
4
4
import type { ILoadoutConfigDatabase, ILoadoutDatabase } from "../../types/saveLoadoutTypes.ts";
@@ -8,7 +8,7 @@ import { Schema, model } from "mongoose";
8
8
//create a mongoose schema based on interface M
9
9
export const EquipmentSelectionSchema = new Schema<IEquipmentSelectionDatabase>(
10
10
{
11
ItemId: Schema.Types.Mixed, // should be Types.ObjectId but might be IOid because of old commits
11
ItemId: Schema.Types.Mixed, // should be Types.ObjectId but cannot be schema-validated as such because of old commits + MongoDB does not save updates as expected
12
12
mod: Number,
13
13
cus: Number,
14
14
hide: Boolean
@@ -24,7 +24,7 @@ EquipmentSelectionSchema.set("toJSON", {
24
24
const client = ret as IEquipmentSelectionClient;
25
25
26
26
if (db.ItemId) {
27
client.ItemId = toOid(fromDbOid(db.ItemId));
27
client.ItemId = toOid(db.ItemId);
28
28
}
29
29
}
30
30
});
@@ -82,7 +82,7 @@ import type {
82
82
ITailorShop,
83
83
ITailorShopDatabase
84
84
} from "../types/personalRoomsTypes.ts";
85
import { fromMongoDate } from "../helpers/inventoryHelpers.ts";
85
import { fromMongoDate, fromOid } from "../helpers/inventoryHelpers.ts";
86
86
import { getRecipe } from "./itemDataService.ts";
87
87
import { logger } from "../utils/logger.ts";
88
88
@@ -98,7 +98,7 @@ const convertEquipment = (client: IEquipmentClient): IEquipmentDatabase => {
98
98
const { ItemId, ...rest } = client;
99
99
return {
100
100
...rest,
101
_id: new Types.ObjectId(ItemId.$oid),
101
_id: new Types.ObjectId(fromOid(ItemId)),
102
102
InfestationDate: convertOptionalDate(client.InfestationDate),
103
103
Expiry: convertOptionalDate(client.Expiry),
104
104
UpgradesExpiry: convertOptionalDate(client.UpgradesExpiry),
@@ -122,7 +122,7 @@ const convertWeaponSkin = (client: IWeaponSkinClient): IWeaponSkinDatabase => {
122
122
const { ItemId, ...rest } = client;
123
123
return {
124
124
...rest,
125
_id: new Types.ObjectId(ItemId.$oid)
125
_id: new Types.ObjectId(fromOid(ItemId))
126
126
};
127
127
};
128
128
@@ -130,7 +130,7 @@ export const importUpgrade = (client: IUpgradeClient): IUpgradeDatabase => {
130
130
const { ItemId, ...rest } = client;
131
131
return {
132
132
...rest,
133
_id: new Types.ObjectId(ItemId.$oid)
133
_id: new Types.ObjectId(fromOid(ItemId))
134
134
};
135
135
};
136
136
@@ -138,7 +138,7 @@ const convertOperatorConfig = (client: IOperatorConfigClient): IOperatorConfigDa
138
138
const { ItemId, ...rest } = client;
139
139
return {
140
140
...rest,
141
_id: new Types.ObjectId(ItemId.$oid)
141
_id: new Types.ObjectId(fromOid(ItemId))
142
142
};
143
143
};
144
144
@@ -158,7 +158,7 @@ const convertEquipmentSelection = (es: IEquipmentSelectionClient): IEquipmentSel
158
158
const { ItemId, ...rest } = es;
159
159
return {
160
160
...rest,
161
ItemId: ItemId ? new Types.ObjectId(ItemId.$oid) : undefined
161
ItemId: ItemId ? new Types.ObjectId(fromOid(ItemId)) : undefined
162
162
};
163
163
};
164
164
@@ -192,7 +192,7 @@ export const importCrewShipWeapon = (weapon: ICrewShipWeaponClient): ICrewShipWe
192
192
193
193
const importCrewMemberId = (crewMemberId: ICrewShipMemberClient): ICrewShipMemberDatabase => {
194
194
if (crewMemberId.ItemId) {
195
return { ItemId: new Types.ObjectId(crewMemberId.ItemId.$oid) };
195
return { ItemId: new Types.ObjectId(fromOid(crewMemberId.ItemId)) };
196
196
}
197
197
return { NemesisFingerprint: BigInt(crewMemberId.NemesisFingerprint ?? 0) };
198
198
};
@@ -450,7 +450,7 @@ export const importInventory = (db: TInventoryDatabaseDocument, client: Partial<
450
450
db.XPInfo = client.XPInfo;
451
451
}
452
452
if (client.CurrentLoadOutIds !== undefined) {
453
db.CurrentLoadOutIds = client.CurrentLoadOutIds.map(x => new Types.ObjectId(x.$oid));
453
db.CurrentLoadOutIds = client.CurrentLoadOutIds.map(x => new Types.ObjectId(fromOid(x)));
454
454
}
455
455
if (client.Affiliations !== undefined) {
456
456
db.Affiliations = client.Affiliations;
@@ -576,7 +576,7 @@ export const importLoadOutConfig = (client: ILoadoutConfigClient): ILoadoutConfi
576
576
const { ItemId, ...rest } = client;
577
577
return {
578
578
...rest,
579
_id: new Types.ObjectId(ItemId.$oid),
579
_id: new Types.ObjectId(fromOid(ItemId)),
580
580
s: client.s ? convertEquipmentSelection(client.s) : undefined,
581
581
p: client.p ? convertEquipmentSelection(client.p) : undefined,
582
582
l: client.l ? convertEquipmentSelection(client.l) : undefined,
@@ -614,7 +614,7 @@ const convertDeco = (client: IPlacedDecosClient): IPlacedDecosDatabase => {
614
614
return {
615
615
...rest,
616
616
CustomizationInfo: client.CustomizationInfo ? convertCustomizationInfo(client.CustomizationInfo) : undefined,
617
_id: new Types.ObjectId(id.$oid)
617
_id: new Types.ObjectId(fromOid(id))
618
618
};
619
619
};
620
620
@@ -633,7 +633,9 @@ const convertShip = (client: IOrbiterClient): IOrbiterDatabase => {
633
633
Colors: typeof client.ShipInterior == "object" ? client.ShipInterior.Colors : undefined
634
634
},
635
635
Rooms: client.Rooms.map(convertRoom),
636
FavouriteLoadoutId: client.FavouriteLoadoutId ? new Types.ObjectId(client.FavouriteLoadoutId.$oid) : undefined
636
FavouriteLoadoutId: client.FavouriteLoadoutId
637
? new Types.ObjectId(fromOid(client.FavouriteLoadoutId))
638
: undefined
637
639
};
638
640
};
639
641
@@ -654,7 +656,7 @@ const convertPlanter = (client: IPlanterClient): IPlanterDatabase => {
654
656
const convertFavouriteLoadout = (client: IFavouriteLoadout): IFavouriteLoadoutDatabase => {
655
657
return {
656
658
...client,
657
LoadoutId: new Types.ObjectId(client.LoadoutId.$oid)
659
LoadoutId: new Types.ObjectId(fromOid(client.LoadoutId))
658
660
};
659
661
};
660
662
@@ -92,7 +92,6 @@ export const handleInventoryItemConfigChange = async (
92
92
93
93
break;
94
94
} else {
95
logger.debug("loadout received");
96
95
const loadout = await Loadout.findOne({ loadoutOwnerId: accountId });
97
96
if (!loadout) {
98
97
throw new Error("loadout not found");
@@ -108,6 +107,8 @@ export const handleInventoryItemConfigChange = async (
108
107
continue;
109
108
}
110
109
110
logger.debug(`${loadoutSlot} loadout received`);
111
111
112
// all non-empty entries are one loadout slot
112
113
for (const [loadoutId, loadoutConfig] of Object.entries(newLoadout)) {
113
114
if (loadoutConfig.Remove) {
@@ -118,6 +119,7 @@ export const handleInventoryItemConfigChange = async (
118
119
const oldLoadoutConfig = loadout[loadoutSlot].id(loadoutId);
119
120
120
121
const loadoutConfigDatabase = importLoadOutConfig(loadoutConfig);
122
//logger.debug(`imported loadout config:`, loadoutConfigDatabase);
121
123
122
124
// if no config with this id exists, create a new one
123
125
if (!oldLoadoutConfig) {
@@ -9,7 +9,7 @@ import type {
9
9
} from "./inventoryTypes/commonInventoryTypes.ts";
10
10
11
11
export interface IEquipmentSelectionClient {
12
ItemId?: IOid;
12
ItemId?: IOidWithLegacySupport;
13
13
mod?: number;
14
14
cus?: number;
15
15
ItemType?: string;
@@ -17,7 +17,7 @@ export interface IEquipmentSelectionClient {
17
17
}
18
18
19
19
export interface IEquipmentSelectionDatabase extends Omit<IEquipmentSelectionClient, "ItemId"> {
20
ItemId?: Types.ObjectId | IOid; // should be Types.ObjectId but might be IOid
20
ItemId?: Types.ObjectId;
21
21
}
22
22
23
23
export enum EquipmentFeatures {