2025-01-19 12:33:04 +01:00
|
|
|
import { Types } from "mongoose";
|
|
|
|
import { IEquipmentClient, IEquipmentDatabase } from "../types/inventoryTypes/commonInventoryTypes";
|
|
|
|
import { IMongoDate } from "../types/commonTypes";
|
2025-01-19 17:21:02 +01:00
|
|
|
import {
|
2025-01-20 05:04:10 +01:00
|
|
|
equipmentKeys,
|
2025-01-19 17:21:02 +01:00
|
|
|
IInventoryClient,
|
2025-01-20 05:16:40 +01:00
|
|
|
ISlots,
|
2025-01-19 17:21:02 +01:00
|
|
|
IUpgradeClient,
|
|
|
|
IUpgradeDatabase,
|
|
|
|
IWeaponSkinClient,
|
|
|
|
IWeaponSkinDatabase
|
|
|
|
} from "../types/inventoryTypes/inventoryTypes";
|
2025-01-19 12:33:04 +01:00
|
|
|
import { TInventoryDatabaseDocument } from "../models/inventoryModels/inventoryModel";
|
|
|
|
|
2025-01-19 17:21:02 +01:00
|
|
|
/* eslint-disable @typescript-eslint/no-unused-vars */
|
|
|
|
|
2025-01-19 14:53:45 +01:00
|
|
|
const convertDate = (value: IMongoDate): Date => {
|
2025-01-19 12:33:04 +01:00
|
|
|
return new Date(parseInt(value.$date.$numberLong));
|
|
|
|
};
|
|
|
|
|
2025-01-19 14:53:45 +01:00
|
|
|
const convertOptionalDate = (value: IMongoDate | undefined): Date | undefined => {
|
|
|
|
return value ? convertDate(value) : undefined;
|
2025-01-19 12:33:04 +01:00
|
|
|
};
|
|
|
|
|
2025-01-19 14:53:45 +01:00
|
|
|
const convertEquipment = (client: IEquipmentClient): IEquipmentDatabase => {
|
2025-01-19 12:33:04 +01:00
|
|
|
const { ItemId, ...rest } = client;
|
|
|
|
return {
|
|
|
|
...rest,
|
|
|
|
_id: new Types.ObjectId(client.ItemId.$oid),
|
2025-01-19 14:53:45 +01:00
|
|
|
InfestationDate: convertOptionalDate(client.InfestationDate),
|
|
|
|
Expiry: convertOptionalDate(client.Expiry),
|
|
|
|
UpgradesExpiry: convertOptionalDate(client.UpgradesExpiry)
|
2025-01-19 12:33:04 +01:00
|
|
|
};
|
|
|
|
};
|
|
|
|
|
2025-01-19 17:04:38 +01:00
|
|
|
const convertWeaponSkin = (client: IWeaponSkinClient): IWeaponSkinDatabase => {
|
|
|
|
const { ItemId, ...rest } = client;
|
|
|
|
return {
|
|
|
|
...rest,
|
|
|
|
_id: new Types.ObjectId(client.ItemId.$oid)
|
|
|
|
};
|
|
|
|
};
|
|
|
|
|
2025-01-19 17:21:02 +01:00
|
|
|
const convertUpgrade = (client: IUpgradeClient): IUpgradeDatabase => {
|
|
|
|
const { ItemId, ...rest } = client;
|
|
|
|
return {
|
|
|
|
...rest,
|
|
|
|
_id: new Types.ObjectId(client.ItemId.$oid)
|
|
|
|
};
|
|
|
|
};
|
|
|
|
|
2025-01-19 17:04:38 +01:00
|
|
|
const replaceArray = <T>(arr: T[], replacement: T[]): void => {
|
|
|
|
arr.splice(0, arr.length);
|
|
|
|
replacement.forEach(x => {
|
|
|
|
arr.push(x);
|
2025-01-19 12:33:04 +01:00
|
|
|
});
|
|
|
|
};
|
2025-01-19 17:04:38 +01:00
|
|
|
|
2025-01-20 05:16:40 +01:00
|
|
|
const replaceSlots = (db: ISlots, client: ISlots): void => {
|
|
|
|
db.Extra = client.Extra;
|
|
|
|
db.Slots = client.Slots;
|
|
|
|
};
|
|
|
|
|
2025-01-20 04:45:02 +01:00
|
|
|
export const importInventory = (db: TInventoryDatabaseDocument, client: Partial<IInventoryClient>): void => {
|
2025-01-20 05:04:10 +01:00
|
|
|
for (const key of equipmentKeys) {
|
|
|
|
if (client[key]) {
|
|
|
|
replaceArray<IEquipmentDatabase>(db[key], client[key].map(convertEquipment));
|
|
|
|
}
|
2025-01-20 04:45:02 +01:00
|
|
|
}
|
|
|
|
if (client.WeaponSkins) {
|
|
|
|
replaceArray<IWeaponSkinDatabase>(db.WeaponSkins, client.WeaponSkins.map(convertWeaponSkin));
|
|
|
|
}
|
|
|
|
if (client.Upgrades) {
|
|
|
|
replaceArray<IUpgradeDatabase>(db.Upgrades, client.Upgrades.map(convertUpgrade));
|
|
|
|
}
|
2025-01-20 05:16:40 +01:00
|
|
|
for (const key of [
|
|
|
|
"SuitBin",
|
|
|
|
"WeaponBin",
|
|
|
|
"SentinelBin",
|
|
|
|
"SpaceSuitBin",
|
|
|
|
"SpaceWeaponBin",
|
|
|
|
"PvpBonusLoadoutBin",
|
|
|
|
"PveBonusLoadoutBin",
|
|
|
|
"RandomModBin",
|
|
|
|
"MechBin",
|
|
|
|
"CrewMemberBin",
|
|
|
|
"OperatorAmpBin",
|
|
|
|
"CrewShipSalvageBin"
|
|
|
|
] as const) {
|
|
|
|
if (client[key]) {
|
|
|
|
replaceSlots(db[key], client[key]);
|
|
|
|
}
|
|
|
|
}
|
2025-01-20 07:09:59 +01:00
|
|
|
for (const key of [
|
|
|
|
"RegularCredits",
|
|
|
|
"PremiumCredits",
|
|
|
|
"PremiumCreditsFree",
|
|
|
|
"FusionPoints",
|
|
|
|
"PrimeTokens"
|
|
|
|
] as const) {
|
|
|
|
if (client[key]) {
|
|
|
|
db[key] = client[key];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
for (const key of ["ThemeStyle", "ThemeBackground", "ThemeSounds", "EquippedInstrument"] as const) {
|
|
|
|
if (client[key]) {
|
|
|
|
db[key] = client[key];
|
|
|
|
}
|
|
|
|
}
|
2025-01-19 17:04:38 +01:00
|
|
|
};
|