cleanup a bit

This commit is contained in:
Ordis 2023-12-14 17:21:03 +01:00
parent ee6f9cb985
commit 647fd85bab
2 changed files with 25 additions and 59 deletions

View File

@ -17,7 +17,7 @@ const saveLoadoutController: RequestHandler = async (req, res) => {
const { UpgradeVer, ...equipmentChanges } = body;
await handleInventoryItemConfigChange(equipmentChanges, accountId);
} catch (error) {
res.status(200).end();
res.status(400).json({ error: error.message });
}
};

View File

@ -13,16 +13,14 @@ export const isEmptyObject = (obj: unknown): boolean => {
return Boolean(obj && Object.keys(obj).length === 0 && obj.constructor === Object);
};
//setup default items on account creation or like originally in giveStartingItems.php
//TODO: setup default items on account creation or like originally in giveStartingItems.php
//TODO: avoid multiple saves for less db calls
//TODO: change update functions to only add and not save
//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
@ -37,14 +35,12 @@ export const handleInventoryItemConfigChange = async (
continue;
}
// non-empty is a change in loadout(or suit...)
switch (equipmentName) {
case "OperatorLoadOuts":
case "AdultOperatorLoadOuts": {
const inventory = await getInventory(accountId);
const operatorConfig = equipment as IOperatorConfigEntry;
const operatorLoadout = inventory[equipmentName];
console.log("loadout received", equipmentName, operatorConfig);
//console.log("loadout received", equipmentName, operatorConfig);
// all non-empty entries are one loadout slot
for (const [loadoutId, loadoutConfig] of Object.entries(operatorConfig)) {
// console.log("loadoutId", loadoutId, "loadoutconfig", loadoutConfig);
@ -57,42 +53,30 @@ export const handleInventoryItemConfigChange = async (
_id: ItemId.$oid,
...loadoutConfigItemIdRemoved
});
await inventory.save();
continue;
}
loadout.set(loadoutConfig);
//({ _id: loadoutId }, loadoutConfig);
}
await inventory.save();
break;
}
case "LoadOuts": {
console.log("loadout received");
//console.log("loadout received");
const loadout = await LoadoutModel.findOne({ loadoutOwnerId: accountId });
if (!loadout) {
throw new Error("loadout not found");
}
for (const [_loadoutSlot, _loadout] of Object.entries(equipment)) {
const loadoutSlot = _loadoutSlot as keyof ILoadoutClient;
const newLoadout = _loadout as ILoadoutEntry;
//console.log("key", loadoutSlot, "value", loadout);
// empty loadout slot like: "NORMAL": {}
if (isEmptyObject(newLoadout)) {
continue;
}
const loadout = await LoadoutModel.findOne({ loadoutOwnerId: accountId });
//const {, ...loadout } = loadoutWithLoadoutOwnerId;
if (!loadout) {
throw new Error("loadout not found");
}
// all non-empty entries are one loadout slot
for (const [loadoutId, loadoutConfig] of Object.entries(newLoadout)) {
// eslint-disable-next-line @typescript-eslint/no-unused-vars
//const { loadoutOwnerId, ...loadout } = loadoutWithLoadoutOwnerId;
// console.log("loadoutId", loadoutId, "loadoutconfig", loadoutConfig);
const oldLoadoutConfig = loadout[loadoutSlot].find(
loadout => loadout._id.toString() === loadoutId
);
@ -104,25 +88,19 @@ export const handleInventoryItemConfigChange = async (
_id: ItemId.$oid,
...loadoutConfigItemIdRemoved
});
await loadout.save();
continue;
}
const loadoutIndex = loadout[loadoutSlot].indexOf(oldLoadoutConfig);
if (loadoutIndex === undefined || loadoutIndex === -1) {
throw new Error("loadout index not found");
}
//console.log("parent id", oldLoadoutConfig.ownerDocument()._id);
//perhaps .overwrite() is better
loadout[loadoutSlot][loadoutIndex].set(loadoutConfig);
//loadout.NORMAL[loadoutIndex].overwrite(loadoutConfig);
//console.log("db", loadout[loadoutSlot][loadoutIndex].schema);
}
}
await loadout.save();
//({ _id: loadoutId }, loadoutConfig);
}
}
break;
}
case "LongGuns":
@ -134,10 +112,9 @@ export const handleInventoryItemConfigChange = async (
case "DrifterMelee":
case "Sentinels":
case "Horses": {
console.log("? ???? ?", equipmentName, equipment);
//console.log("general Item config saved", equipmentName, equipment);
const itemEntries = equipment as IItemEntry;
const inventory = await getInventory(accountId);
for (const [itemId, itemConfigEntries] of Object.entries(itemEntries)) {
const inventoryItem = inventory[equipmentName].find(item => item._id?.toString() === itemId);
@ -150,15 +127,11 @@ export const handleInventoryItemConfigChange = async (
inventoryItem.Configs[parseInt(configId)] = config;
}
}
await inventory.save();
break;
}
case "CurrentLoadOutIds": {
//TODO: remove duplicate getInventory after finding out when currentloadOutId is sent
const loadoutIds = equipment as IOid[]; // TODO: Check for more than just an array of oids, I think i remember one instance
const inventory = await getInventory(accountId);
inventory.CurrentLoadOutIds = loadoutIds;
await inventory.save();
break;
}
case "EquippedGear": {
@ -168,27 +141,20 @@ export const handleInventoryItemConfigChange = async (
default: {
console.log("category not implemented", equipmentName, equipment);
}
}
//case "OperatorAmps":
// case "SentinelWeapons":
// case "KubrowPets":
// case "SpaceSuits":
// case "SpaceGuns":
// case "SpaceMelee":
// case "SpecialItems":
// case "MoaPets":
// case "Hoverboards":
// case "MechSuits":
// case "CrewShipHarnesses":
//
// case "CrewShips":
//case "KahlLoadOuts": not sure yet how to handle kahl: it is not sent in inventory
}
}
await inventory.save();
};