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; const { UpgradeVer, ...equipmentChanges } = body;
await handleInventoryItemConfigChange(equipmentChanges, accountId); await handleInventoryItemConfigChange(equipmentChanges, accountId);
} catch (error) { } 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); 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 perhaps, functions that add and return inventory perhaps
//TODO: change update functions to only add and not save
/* loadouts has loadoutconfigs /* loadouts has loadoutconfigs
operatorloadouts has itemconfig, but no multiple config ids operatorloadouts has itemconfig, but no multiple config ids
itemconfig has multiple config ids itemconfig has multiple config ids
*/ */
export const handleInventoryItemConfigChange = async ( export const handleInventoryItemConfigChange = async (
equipmentChanges: ISaveLoadoutRequestNoUpgradeVer, equipmentChanges: ISaveLoadoutRequestNoUpgradeVer,
accountId: string accountId: string
@ -37,14 +35,12 @@ export const handleInventoryItemConfigChange = async (
continue; continue;
} }
// non-empty is a change in loadout(or suit...) // non-empty is a change in loadout(or suit...)
switch (equipmentName) { switch (equipmentName) {
case "OperatorLoadOuts": case "OperatorLoadOuts":
case "AdultOperatorLoadOuts": { case "AdultOperatorLoadOuts": {
const inventory = await getInventory(accountId);
const operatorConfig = equipment as IOperatorConfigEntry; const operatorConfig = equipment as IOperatorConfigEntry;
const operatorLoadout = inventory[equipmentName]; const operatorLoadout = inventory[equipmentName];
console.log("loadout received", equipmentName, operatorConfig); //console.log("loadout received", equipmentName, operatorConfig);
// all non-empty entries are one loadout slot // all non-empty entries are one loadout slot
for (const [loadoutId, loadoutConfig] of Object.entries(operatorConfig)) { for (const [loadoutId, loadoutConfig] of Object.entries(operatorConfig)) {
// console.log("loadoutId", loadoutId, "loadoutconfig", loadoutConfig); // console.log("loadoutId", loadoutId, "loadoutconfig", loadoutConfig);
@ -57,42 +53,30 @@ export const handleInventoryItemConfigChange = async (
_id: ItemId.$oid, _id: ItemId.$oid,
...loadoutConfigItemIdRemoved ...loadoutConfigItemIdRemoved
}); });
await inventory.save();
continue; continue;
} }
loadout.set(loadoutConfig); loadout.set(loadoutConfig);
//({ _id: loadoutId }, loadoutConfig);
} }
await inventory.save();
break; break;
} }
case "LoadOuts": { 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)) { for (const [_loadoutSlot, _loadout] of Object.entries(equipment)) {
const loadoutSlot = _loadoutSlot as keyof ILoadoutClient; const loadoutSlot = _loadoutSlot as keyof ILoadoutClient;
const newLoadout = _loadout as ILoadoutEntry; const newLoadout = _loadout as ILoadoutEntry;
//console.log("key", loadoutSlot, "value", loadout);
// empty loadout slot like: "NORMAL": {} // empty loadout slot like: "NORMAL": {}
if (isEmptyObject(newLoadout)) { if (isEmptyObject(newLoadout)) {
continue; 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 // all non-empty entries are one loadout slot
for (const [loadoutId, loadoutConfig] of Object.entries(newLoadout)) { 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( const oldLoadoutConfig = loadout[loadoutSlot].find(
loadout => loadout._id.toString() === loadoutId loadout => loadout._id.toString() === loadoutId
); );
@ -104,25 +88,19 @@ export const handleInventoryItemConfigChange = async (
_id: ItemId.$oid, _id: ItemId.$oid,
...loadoutConfigItemIdRemoved ...loadoutConfigItemIdRemoved
}); });
await loadout.save();
continue; continue;
} }
const loadoutIndex = loadout[loadoutSlot].indexOf(oldLoadoutConfig); const loadoutIndex = loadout[loadoutSlot].indexOf(oldLoadoutConfig);
if (loadoutIndex === undefined || loadoutIndex === -1) { if (loadoutIndex === undefined || loadoutIndex === -1) {
throw new Error("loadout index not found"); throw new Error("loadout index not found");
} }
//console.log("parent id", oldLoadoutConfig.ownerDocument()._id); //perhaps .overwrite() is better
loadout[loadoutSlot][loadoutIndex].set(loadoutConfig); loadout[loadoutSlot][loadoutIndex].set(loadoutConfig);
//loadout.NORMAL[loadoutIndex].overwrite(loadoutConfig); }
//console.log("db", loadout[loadoutSlot][loadoutIndex].schema); }
await loadout.save(); await loadout.save();
//({ _id: loadoutId }, loadoutConfig);
}
}
break; break;
} }
case "LongGuns": case "LongGuns":
@ -134,10 +112,9 @@ export const handleInventoryItemConfigChange = async (
case "DrifterMelee": case "DrifterMelee":
case "Sentinels": case "Sentinels":
case "Horses": { case "Horses": {
console.log("? ???? ?", equipmentName, equipment); //console.log("general Item config saved", equipmentName, equipment);
const itemEntries = equipment as IItemEntry; const itemEntries = equipment as IItemEntry;
const inventory = await getInventory(accountId);
for (const [itemId, itemConfigEntries] of Object.entries(itemEntries)) { for (const [itemId, itemConfigEntries] of Object.entries(itemEntries)) {
const inventoryItem = inventory[equipmentName].find(item => item._id?.toString() === itemId); const inventoryItem = inventory[equipmentName].find(item => item._id?.toString() === itemId);
@ -150,15 +127,11 @@ export const handleInventoryItemConfigChange = async (
inventoryItem.Configs[parseInt(configId)] = config; inventoryItem.Configs[parseInt(configId)] = config;
} }
} }
await inventory.save();
break; break;
} }
case "CurrentLoadOutIds": { 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 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; inventory.CurrentLoadOutIds = loadoutIds;
await inventory.save();
break; break;
} }
case "EquippedGear": { case "EquippedGear": {
@ -168,27 +141,20 @@ export const handleInventoryItemConfigChange = async (
default: { default: {
console.log("category not implemented", equipmentName, equipment); console.log("category not implemented", equipmentName, equipment);
} }
}
//case "OperatorAmps": //case "OperatorAmps":
// case "SentinelWeapons": // case "SentinelWeapons":
// case "KubrowPets": // case "KubrowPets":
// case "SpaceSuits": // case "SpaceSuits":
// case "SpaceGuns": // case "SpaceGuns":
// case "SpaceMelee": // case "SpaceMelee":
// case "SpecialItems": // case "SpecialItems":
// case "MoaPets": // case "MoaPets":
// case "Hoverboards": // case "Hoverboards":
// case "MechSuits": // case "MechSuits":
// case "CrewShipHarnesses": // case "CrewShipHarnesses":
//
// case "CrewShips": // case "CrewShips":
//case "KahlLoadOuts": not sure yet how to handle kahl: it is not sent in inventory //case "KahlLoadOuts": not sure yet how to handle kahl: it is not sent in inventory
} }
}
await inventory.save(); await inventory.save();
}; };