missionInventoryUpdate - private functions outside

This commit is contained in:
holmityd 2023-08-30 21:15:46 +04:00
parent f054db1327
commit a1c2815728
4 changed files with 53 additions and 45 deletions

View File

@ -5,7 +5,7 @@ import { Types } from "mongoose";
import { ISuitResponse } from "@/src/types/inventoryTypes/SuitTypes"; import { ISuitResponse } from "@/src/types/inventoryTypes/SuitTypes";
import { SlotType } from "@/src/types/purchaseTypes"; import { SlotType } from "@/src/types/purchaseTypes";
import { IWeaponResponse } from "@/src/types/inventoryTypes/weaponTypes"; import { IWeaponResponse } from "@/src/types/inventoryTypes/weaponTypes";
import { FlavourItem } from "@/src/types/inventoryTypes/inventoryTypes"; import { FlavourItem, IInventoryDatabaseDocument } from "@/src/types/inventoryTypes/inventoryTypes";
import { import {
MissionInventoryUpdate, MissionInventoryUpdate,
MissionInventoryUpdateCard, MissionInventoryUpdateCard,
@ -112,54 +112,56 @@ export const addCustomization = async (customizatonName: string, accountId: stri
return changedInventory.FlavourItems[flavourItemIndex].toJSON(); //mongoose bug forces as FlavourItem return changedInventory.FlavourItems[flavourItemIndex].toJSON(); //mongoose bug forces as FlavourItem
}; };
const addGearExpByCategory = (
inventory: IInventoryDatabaseDocument,
gearArray: MissionInventoryUpdateGear[] | undefined,
categoryName: "Pistols" | "LongGuns" | "Melee" | "Suits"
) => {
const category = inventory[categoryName];
gearArray?.forEach(({ ItemId, XP }) => {
const itemIndex = category.findIndex(i => i._id?.equals(ItemId.$oid));
const item = category[itemIndex];
if (itemIndex !== -1 && item.XP != undefined) {
item.XP += XP;
inventory.markModified(`${categoryName}.${itemIndex}.XP`);
}
});
};
const addItemsByCategory = (
inventory: IInventoryDatabaseDocument,
itemsArray: (MissionInventoryUpdateItem | MissionInventoryUpdateCard)[] | undefined,
categoryName: "RawUpgrades" | "MiscItems"
) => {
const category = inventory[categoryName];
itemsArray?.forEach(({ ItemCount, ItemType }) => {
const itemIndex = category.findIndex(i => i.ItemType === ItemType);
if (itemIndex !== -1) {
category[itemIndex].ItemCount += ItemCount;
inventory.markModified(`${categoryName}.${itemIndex}.ItemCount`);
} else {
category.push({ ItemCount, ItemType });
}
});
};
export const missionInventoryUpdate = async (data: MissionInventoryUpdate, accountId: string): Promise<void> => { export const missionInventoryUpdate = async (data: MissionInventoryUpdate, accountId: string): Promise<void> => {
const { RawUpgrades, MiscItems, Suits, Pistols, LongGuns, Melee, RegularCredits } = data; const { RawUpgrades, MiscItems, Suits, Pistols, LongGuns, Melee, RegularCredits } = data;
const inventory = await getInventory(accountId); const inventory = await getInventory(accountId);
// TODO - multipliers logic // TODO - multipliers logic
const addGearExpByCategory = (
gearArray: MissionInventoryUpdateGear[] | undefined,
categoryName: "Pistols" | "LongGuns" | "Melee" | "Suits"
) => {
const category = inventory[categoryName];
gearArray?.forEach(({ ItemId, XP }) => {
const itemIndex = category.findIndex(i => i._id?.equals(ItemId.$oid));
const item = category[itemIndex];
if (itemIndex !== -1 && item.XP != undefined) {
item.XP += XP;
inventory.markModified(`${categoryName}.${itemIndex}.XP`);
}
});
};
const addItemsByCategory = (
itemsArray: (MissionInventoryUpdateItem | MissionInventoryUpdateCard)[] | undefined,
categoryName: "RawUpgrades" | "MiscItems"
) => {
const category = inventory[categoryName];
itemsArray?.forEach(({ ItemCount, ItemType }) => {
const itemIndex = category.findIndex(i => i.ItemType === ItemType);
if (itemIndex !== -1) {
category[itemIndex].ItemCount += ItemCount;
inventory.markModified(`${categoryName}.${itemIndex}.ItemCount`);
} else {
category.push({ ItemCount, ItemType });
}
});
};
inventory.RegularCredits += RegularCredits || 0; inventory.RegularCredits += RegularCredits || 0;
addGearExpByCategory(Pistols, "Pistols"); addGearExpByCategory(inventory, Pistols, "Pistols");
addGearExpByCategory(LongGuns, "LongGuns"); addGearExpByCategory(inventory, LongGuns, "LongGuns");
addGearExpByCategory(Melee, "Melee"); addGearExpByCategory(inventory, Melee, "Melee");
addGearExpByCategory(Suits, "Suits"); addGearExpByCategory(inventory, Suits, "Suits");
addItemsByCategory(RawUpgrades, "RawUpgrades"); // TODO - check mods fusion level addItemsByCategory(inventory, RawUpgrades, "RawUpgrades"); // TODO - check mods fusion level
addItemsByCategory(MiscItems, "MiscItems"); addItemsByCategory(inventory, MiscItems, "MiscItems");
// TODO - save ChallengeProgress (idk where to save) // TODO - save ChallengeProgress (idk where to save)

View File

@ -1,8 +1,11 @@
import { Oid } from "@/src/types/commonTypes"; import { Oid } from "@/src/types/commonTypes";
import { AbilityOverride, Color, Polarity } from "@/src/types/inventoryTypes/commonInventoryTypes"; import { AbilityOverride, Color, Polarity } from "@/src/types/inventoryTypes/commonInventoryTypes";
import { Document } from "mongoose"; import { Document, Types } from "mongoose";
export interface ISuitDocument extends ISuitResponse, Document {} // export interface ISuitDocument extends ISuitResponse, Document {}
export interface ISuitDocument extends Document, ISuitResponse {
_id: Types.ObjectId;
}
export interface ISuitResponse extends ISuitDatabase { export interface ISuitResponse extends ISuitDatabase {
ItemId: Oid; ItemId: Oid;
@ -20,6 +23,7 @@ export interface ISuitDatabase {
ModSlotPurchases?: number; ModSlotPurchases?: number;
FocusLens?: string; FocusLens?: string;
UnlockLevel?: number; UnlockLevel?: number;
_id: Types.ObjectId;
} }
export interface SuitConfig { export interface SuitConfig {

View File

@ -929,7 +929,7 @@ export interface Progress {
export interface RawUpgrade { export interface RawUpgrade {
ItemCount: number; ItemCount: number;
LastAdded: Oid; LastAdded?: Oid;
ItemType: string; ItemType: string;
} }

View File

@ -1,5 +1,6 @@
import { Oid } from "@/src/types/commonTypes"; import { Oid } from "@/src/types/commonTypes";
import { Color, Polarity } from "@/src/types/inventoryTypes/commonInventoryTypes"; import { Color, Polarity } from "@/src/types/inventoryTypes/commonInventoryTypes";
import { Types } from "mongoose";
export interface IWeaponResponse extends IWeaponDatabase { export interface IWeaponResponse extends IWeaponDatabase {
ItemId: Oid; ItemId: Oid;
@ -20,6 +21,7 @@ export interface IWeaponDatabase {
ItemName?: string; ItemName?: string;
ModularParts?: string[]; ModularParts?: string[];
UnlockLevel?: number; UnlockLevel?: number;
_id?: Types.ObjectId;
} }
export interface WeaponConfig { export interface WeaponConfig {