2023-12-14 17:34:15 +01:00
|
|
|
import {
|
|
|
|
IItemEntry,
|
|
|
|
ILoadoutClient,
|
|
|
|
ILoadoutEntry,
|
2024-05-07 20:38:44 +02:00
|
|
|
ILoadoutConfigDatabase,
|
2023-12-14 17:34:15 +01:00
|
|
|
IOperatorConfigEntry,
|
|
|
|
ISaveLoadoutRequestNoUpgradeVer
|
|
|
|
} from "@/src/types/saveLoadoutTypes";
|
2024-02-18 13:58:43 +01:00
|
|
|
import { Loadout } from "@/src/models/inventoryModels/loadoutModel";
|
2023-12-14 17:34:15 +01:00
|
|
|
import { getInventory } from "@/src/services/inventoryService";
|
|
|
|
import { IOid } from "@/src/types/commonTypes";
|
2023-12-28 16:24:52 +01:00
|
|
|
import { Types } from "mongoose";
|
|
|
|
import { isEmptyObject } from "@/src/helpers/general";
|
2024-01-06 16:26:58 +01:00
|
|
|
import { logger } from "@/src/utils/logger";
|
2023-12-14 17:34:15 +01:00
|
|
|
|
|
|
|
//TODO: setup default items on account creation or like originally in giveStartingItems.php
|
|
|
|
|
|
|
|
//TODO: change update functions to only add and not save perhaps, functions that add and return inventory perhaps
|
|
|
|
|
|
|
|
/* loadouts has loadoutconfigs
|
|
|
|
operatorloadouts has itemconfig, but no multiple config ids
|
|
|
|
itemconfig has multiple config ids
|
|
|
|
*/
|
|
|
|
export const handleInventoryItemConfigChange = async (
|
|
|
|
equipmentChanges: ISaveLoadoutRequestNoUpgradeVer,
|
|
|
|
accountId: string
|
2023-12-28 16:24:52 +01:00
|
|
|
): Promise<string | void> => {
|
2023-12-14 17:34:15 +01:00
|
|
|
const inventory = await getInventory(accountId);
|
|
|
|
|
|
|
|
for (const [_equipmentName, _equipment] of Object.entries(equipmentChanges)) {
|
|
|
|
const equipment = _equipment as ISaveLoadoutRequestNoUpgradeVer[keyof ISaveLoadoutRequestNoUpgradeVer];
|
|
|
|
const equipmentName = _equipmentName as keyof ISaveLoadoutRequestNoUpgradeVer;
|
|
|
|
|
|
|
|
if (isEmptyObject(equipment)) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
// non-empty is a change in loadout(or suit...)
|
|
|
|
switch (equipmentName) {
|
|
|
|
case "OperatorLoadOuts":
|
|
|
|
case "AdultOperatorLoadOuts": {
|
|
|
|
const operatorConfig = equipment as IOperatorConfigEntry;
|
|
|
|
const operatorLoadout = inventory[equipmentName];
|
2024-01-06 16:26:58 +01:00
|
|
|
logger.debug(`operator loadout received ${equipmentName} `, operatorConfig);
|
2023-12-14 17:34:15 +01:00
|
|
|
// all non-empty entries are one loadout slot
|
|
|
|
for (const [loadoutId, loadoutConfig] of Object.entries(operatorConfig)) {
|
2024-01-06 16:26:58 +01:00
|
|
|
logger.debug(`loadoutId ${loadoutId} loadoutConfig`, { config: loadoutConfig });
|
2023-12-14 17:34:15 +01:00
|
|
|
const loadout = operatorLoadout.find(loadout => loadout._id?.toString() === loadoutId);
|
|
|
|
|
|
|
|
// if no config with this id exists, create a new one
|
|
|
|
if (!loadout) {
|
|
|
|
const { ItemId, ...loadoutConfigItemIdRemoved } = loadoutConfig;
|
|
|
|
operatorLoadout.push({
|
|
|
|
_id: ItemId.$oid,
|
|
|
|
...loadoutConfigItemIdRemoved
|
|
|
|
});
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
loadout.set(loadoutConfig);
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case "LoadOuts": {
|
2024-01-06 16:26:58 +01:00
|
|
|
logger.debug("loadout received");
|
2024-02-18 13:58:43 +01:00
|
|
|
const loadout = await Loadout.findOne({ loadoutOwnerId: accountId });
|
2023-12-14 17:34:15 +01:00
|
|
|
if (!loadout) {
|
|
|
|
throw new Error("loadout not found");
|
|
|
|
}
|
|
|
|
|
2023-12-28 16:24:52 +01:00
|
|
|
let newLoadoutId: Types.ObjectId | undefined;
|
2023-12-14 17:34:15 +01:00
|
|
|
for (const [_loadoutSlot, _loadout] of Object.entries(equipment)) {
|
|
|
|
const loadoutSlot = _loadoutSlot as keyof ILoadoutClient;
|
|
|
|
const newLoadout = _loadout as ILoadoutEntry;
|
|
|
|
|
|
|
|
// empty loadout slot like: "NORMAL": {}
|
|
|
|
if (isEmptyObject(newLoadout)) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
// all non-empty entries are one loadout slot
|
|
|
|
for (const [loadoutId, loadoutConfig] of Object.entries(newLoadout)) {
|
2024-05-04 23:11:55 +02:00
|
|
|
if (loadoutConfig.Remove) {
|
|
|
|
loadout[loadoutSlot].pull({ _id: loadoutId });
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2023-12-14 17:34:15 +01:00
|
|
|
const oldLoadoutConfig = loadout[loadoutSlot].find(
|
|
|
|
loadout => loadout._id.toString() === loadoutId
|
|
|
|
);
|
|
|
|
|
2024-05-07 20:38:44 +02:00
|
|
|
const { ItemId, ...loadoutConfigItemIdRemoved } = loadoutConfig;
|
|
|
|
const loadoutConfigDatabase: ILoadoutConfigDatabase = {
|
|
|
|
_id: new Types.ObjectId(ItemId.$oid),
|
|
|
|
...loadoutConfigItemIdRemoved
|
|
|
|
};
|
|
|
|
|
2023-12-14 17:34:15 +01:00
|
|
|
// if no config with this id exists, create a new one
|
|
|
|
if (!oldLoadoutConfig) {
|
2023-12-28 16:24:52 +01:00
|
|
|
//save the new object id and assign it for every ffff return at the end
|
|
|
|
if (ItemId.$oid === "ffffffffffffffffffffffff") {
|
|
|
|
if (!newLoadoutId) {
|
|
|
|
newLoadoutId = new Types.ObjectId();
|
|
|
|
}
|
|
|
|
loadout[loadoutSlot].push({ _id: newLoadoutId, ...loadoutConfigItemIdRemoved });
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2024-05-07 20:38:44 +02:00
|
|
|
loadout[loadoutSlot].push(loadoutConfigDatabase);
|
2023-12-14 17:34:15 +01:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
const loadoutIndex = loadout[loadoutSlot].indexOf(oldLoadoutConfig);
|
|
|
|
if (loadoutIndex === undefined || loadoutIndex === -1) {
|
|
|
|
throw new Error("loadout index not found");
|
|
|
|
}
|
|
|
|
|
2024-05-07 20:38:44 +02:00
|
|
|
loadout[loadoutSlot][loadoutIndex].overwrite(loadoutConfigDatabase);
|
2023-12-14 17:34:15 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
await loadout.save();
|
2023-12-28 16:24:52 +01:00
|
|
|
|
|
|
|
//only return an id if a new loadout was added
|
|
|
|
if (newLoadoutId) {
|
|
|
|
return newLoadoutId.toString();
|
|
|
|
}
|
2023-12-14 17:34:15 +01:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
case "LongGuns":
|
|
|
|
case "Pistols":
|
|
|
|
case "Suits":
|
|
|
|
case "Melee":
|
|
|
|
case "Scoops":
|
|
|
|
case "DataKnives":
|
|
|
|
case "DrifterMelee":
|
|
|
|
case "Sentinels":
|
2024-06-16 17:56:51 +02:00
|
|
|
case "Horses":
|
|
|
|
case "OperatorAmps":
|
|
|
|
case "SentinelWeapons":
|
|
|
|
case "KubrowPets":
|
|
|
|
case "SpaceSuits":
|
|
|
|
case "SpaceGuns":
|
|
|
|
case "SpaceMelee":
|
|
|
|
case "SpecialItems":
|
|
|
|
case "MoaPets":
|
|
|
|
case "Hoverboards":
|
|
|
|
case "MechSuits":
|
|
|
|
case "CrewShipHarnesses":
|
|
|
|
case "CrewShips": {
|
2024-01-06 16:26:58 +01:00
|
|
|
logger.debug(`general Item config saved of type ${equipmentName}`, { config: equipment });
|
2023-12-14 17:34:15 +01:00
|
|
|
|
|
|
|
const itemEntries = equipment as IItemEntry;
|
|
|
|
for (const [itemId, itemConfigEntries] of Object.entries(itemEntries)) {
|
|
|
|
const inventoryItem = inventory[equipmentName].find(item => item._id?.toString() === itemId);
|
|
|
|
|
|
|
|
if (!inventoryItem) {
|
|
|
|
throw new Error(`inventory item ${equipmentName} not found with id ${itemId}`);
|
|
|
|
}
|
|
|
|
|
|
|
|
//config ids are 0,1,2 can there be a 3?
|
|
|
|
for (const [configId, config] of Object.entries(itemConfigEntries)) {
|
|
|
|
inventoryItem.Configs[parseInt(configId)] = config;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case "CurrentLoadOutIds": {
|
|
|
|
const loadoutIds = equipment as IOid[]; // TODO: Check for more than just an array of oids, I think i remember one instance
|
|
|
|
inventory.CurrentLoadOutIds = loadoutIds;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case "EquippedGear": {
|
|
|
|
inventory.EquippedGear = equipment as string[];
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
default: {
|
2024-01-06 16:26:58 +01:00
|
|
|
logger.error(`category not implemented: ${equipmentName}`, { config: equipment });
|
2023-12-14 17:34:15 +01:00
|
|
|
}
|
|
|
|
//case "KahlLoadOuts": not sure yet how to handle kahl: it is not sent in inventory
|
|
|
|
}
|
|
|
|
}
|
|
|
|
await inventory.save();
|
|
|
|
};
|