feat: Mod upgrade
This commit is contained in:
parent
0309e182c2
commit
fe69170e60
@ -64,7 +64,6 @@ const missionInventoryUpdateController: RequestHandler = async (req, res) => {
|
|||||||
const parsedData = JSON.parse(data) as MissionInventoryUpdate;
|
const parsedData = JSON.parse(data) as MissionInventoryUpdate;
|
||||||
if (typeof parsedData !== "object" || parsedData === null) throw new Error("Invalid data format");
|
if (typeof parsedData !== "object" || parsedData === null) throw new Error("Invalid data format");
|
||||||
// eslint-disable-next-line @typescript-eslint/no-unused-vars
|
// eslint-disable-next-line @typescript-eslint/no-unused-vars
|
||||||
const InventoryJson = JSON.stringify(await missionInventoryUpdate(parsedData, id));
|
|
||||||
|
|
||||||
const missionCredits = parsedData.RegularCredits || 0;
|
const missionCredits = parsedData.RegularCredits || 0;
|
||||||
const creditsBonus = 0;
|
const creditsBonus = 0;
|
||||||
@ -76,6 +75,8 @@ const missionInventoryUpdateController: RequestHandler = async (req, res) => {
|
|||||||
|
|
||||||
console.log(getRewards(parsedData.RewardInfo));
|
console.log(getRewards(parsedData.RewardInfo));
|
||||||
|
|
||||||
|
// eslint-disable-next-line @typescript-eslint/no-unused-vars
|
||||||
|
const InventoryJson = JSON.stringify(await missionInventoryUpdate(parsedData, id));
|
||||||
res.json({
|
res.json({
|
||||||
// InventoryJson, // this part will reset game data and missions will be locked
|
// InventoryJson, // this part will reset game data and missions will be locked
|
||||||
TotalCredits,
|
TotalCredits,
|
||||||
|
@ -1,4 +1,4 @@
|
|||||||
import mongoose, { Model, Schema, Types, model } from "mongoose";
|
import { Model, Schema, Types, model } from "mongoose";
|
||||||
import { FlavourItem, RawUpgrade, MiscItem, IInventoryDatabase, Booster } from "../types/inventoryTypes/inventoryTypes";
|
import { FlavourItem, RawUpgrade, MiscItem, IInventoryDatabase, Booster } from "../types/inventoryTypes/inventoryTypes";
|
||||||
import { Oid } from "../types/commonTypes";
|
import { Oid } from "../types/commonTypes";
|
||||||
import { ISuitDatabase } from "@/src/types/inventoryTypes/SuitTypes";
|
import { ISuitDatabase } from "@/src/types/inventoryTypes/SuitTypes";
|
||||||
@ -73,21 +73,11 @@ const BoosterSchema = new Schema({
|
|||||||
|
|
||||||
const RawUpgrades = new Schema({
|
const RawUpgrades = new Schema({
|
||||||
ItemType: String,
|
ItemType: String,
|
||||||
UpgradeFingerprint: String,
|
ItemCount: Number
|
||||||
PendingRerollFingerprint: String,
|
|
||||||
ItemCount: Number,
|
|
||||||
ItemId: {
|
|
||||||
$oid: mongoose.Schema.Types.ObjectId
|
|
||||||
},
|
|
||||||
LastAdded: {
|
|
||||||
$oid: mongoose.Schema.Types.ObjectId
|
|
||||||
}
|
|
||||||
});
|
});
|
||||||
|
|
||||||
RawUpgrades.set("toJSON", {
|
RawUpgrades.set("toJSON", {
|
||||||
transform(_document, returnedObject) {
|
transform(_document, returnedObject) {
|
||||||
// eslint-disable-next-line @typescript-eslint/no-unsafe-assignment, @typescript-eslint/no-unsafe-call
|
|
||||||
returnedObject.ItemId = { $oid: returnedObject._id.toString() } satisfies Oid;
|
|
||||||
// eslint-disable-next-line @typescript-eslint/no-unsafe-assignment, @typescript-eslint/no-unsafe-call
|
// eslint-disable-next-line @typescript-eslint/no-unsafe-assignment, @typescript-eslint/no-unsafe-call
|
||||||
returnedObject.LastAdded = { $oid: returnedObject._id.toString() } satisfies Oid;
|
returnedObject.LastAdded = { $oid: returnedObject._id.toString() } satisfies Oid;
|
||||||
delete returnedObject._id;
|
delete returnedObject._id;
|
||||||
@ -95,6 +85,20 @@ RawUpgrades.set("toJSON", {
|
|||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
|
const Upgrade = new Schema({
|
||||||
|
UpgradeFingerprint: String,
|
||||||
|
ItemType: String
|
||||||
|
});
|
||||||
|
|
||||||
|
Upgrade.set("toJSON", {
|
||||||
|
transform(_document, returnedObject) {
|
||||||
|
// eslint-disable-next-line @typescript-eslint/no-unsafe-assignment, @typescript-eslint/no-unsafe-call
|
||||||
|
returnedObject.ItemId = { $oid: returnedObject._id.toString() } satisfies Oid;
|
||||||
|
delete returnedObject._id;
|
||||||
|
delete returnedObject.__v;
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
WeaponSchema.set("toJSON", {
|
WeaponSchema.set("toJSON", {
|
||||||
transform(_document, returnedObject) {
|
transform(_document, returnedObject) {
|
||||||
// eslint-disable-next-line @typescript-eslint/no-unsafe-assignment, @typescript-eslint/no-unsafe-call
|
// eslint-disable-next-line @typescript-eslint/no-unsafe-assignment, @typescript-eslint/no-unsafe-call
|
||||||
@ -227,7 +231,7 @@ const inventorySchema = new Schema<IInventoryDatabase, InventoryDocumentProps>({
|
|||||||
PendingRecipes: [Schema.Types.Mixed],
|
PendingRecipes: [Schema.Types.Mixed],
|
||||||
TrainingDate: Schema.Types.Mixed,
|
TrainingDate: Schema.Types.Mixed,
|
||||||
PlayerLevel: Number,
|
PlayerLevel: Number,
|
||||||
Upgrades: [Schema.Types.Mixed],
|
Upgrades: [Upgrade],
|
||||||
EquippedGear: [String],
|
EquippedGear: [String],
|
||||||
DeathMarks: [String],
|
DeathMarks: [String],
|
||||||
FusionTreasures: [Schema.Types.Mixed],
|
FusionTreasures: [Schema.Types.Mixed],
|
||||||
|
@ -7,6 +7,7 @@ import { SlotType } from "@/src/types/purchaseTypes";
|
|||||||
import { IWeaponResponse } from "@/src/types/inventoryTypes/weaponTypes";
|
import { IWeaponResponse } from "@/src/types/inventoryTypes/weaponTypes";
|
||||||
import {
|
import {
|
||||||
ChallengeProgress,
|
ChallengeProgress,
|
||||||
|
CrewShipSalvagedWeaponSkin,
|
||||||
FlavourItem,
|
FlavourItem,
|
||||||
IInventoryDatabaseDocument,
|
IInventoryDatabaseDocument,
|
||||||
MiscItem,
|
MiscItem,
|
||||||
@ -148,18 +149,14 @@ const addMiscItems = (inventory: IInventoryDatabaseDocument, itemsArray: MiscIte
|
|||||||
|
|
||||||
const addMods = (inventory: IInventoryDatabaseDocument, itemsArray: RawUpgrade[] | undefined) => {
|
const addMods = (inventory: IInventoryDatabaseDocument, itemsArray: RawUpgrade[] | undefined) => {
|
||||||
const { RawUpgrades } = inventory;
|
const { RawUpgrades } = inventory;
|
||||||
itemsArray?.forEach(({ ItemType, ItemCount, UpgradeFingerprint }) => {
|
itemsArray?.forEach(({ ItemType, ItemCount }) => {
|
||||||
const itemIndex = RawUpgrades.findIndex(
|
const itemIndex = RawUpgrades.findIndex(i => i.ItemType === ItemType);
|
||||||
i =>
|
|
||||||
i.ItemType === ItemType &&
|
|
||||||
(i.UpgradeFingerprint === UpgradeFingerprint || (!i.UpgradeFingerprint && !UpgradeFingerprint))
|
|
||||||
);
|
|
||||||
|
|
||||||
if (itemIndex !== -1) {
|
if (itemIndex !== -1) {
|
||||||
RawUpgrades[itemIndex].ItemCount += ItemCount;
|
RawUpgrades[itemIndex].ItemCount += ItemCount;
|
||||||
inventory.markModified(`RawUpgrades.${itemIndex}.ItemCount`);
|
inventory.markModified(`RawUpgrades.${itemIndex}.ItemCount`);
|
||||||
} else {
|
} else {
|
||||||
RawUpgrades.push({ ItemCount, ItemType, UpgradeFingerprint });
|
RawUpgrades.push({ ItemCount, ItemType });
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
};
|
};
|
||||||
@ -228,66 +225,56 @@ export const upgradeMod = async (
|
|||||||
LevelDiff,
|
LevelDiff,
|
||||||
Cost,
|
Cost,
|
||||||
FusionPointCost
|
FusionPointCost
|
||||||
}: { Upgrade: RawUpgrade; LevelDiff: number; Cost: number; FusionPointCost: number },
|
}: { Upgrade: CrewShipSalvagedWeaponSkin; LevelDiff: number; Cost: number; FusionPointCost: number },
|
||||||
accountId: string
|
accountId: string
|
||||||
): Promise<string | undefined> => {
|
): Promise<string | undefined> => {
|
||||||
const inventory = await getInventory(accountId);
|
try {
|
||||||
const { RawUpgrades } = inventory;
|
const inventory = await getInventory(accountId);
|
||||||
const { ItemCount, ItemType, UpgradeFingerprint } = Upgrade;
|
const { Upgrades, RawUpgrades } = inventory;
|
||||||
const itemIndex = RawUpgrades.findIndex(
|
const { ItemType, UpgradeFingerprint, ItemId } = Upgrade;
|
||||||
i =>
|
|
||||||
i.ItemType === ItemType &&
|
|
||||||
(i.UpgradeFingerprint === UpgradeFingerprint || (!i.UpgradeFingerprint && !UpgradeFingerprint))
|
|
||||||
);
|
|
||||||
|
|
||||||
console.log(itemIndex, ItemType, UpgradeFingerprint);
|
const safeUpgradeFingerprint = UpgradeFingerprint || '{"lvl":0}';
|
||||||
|
// eslint-disable-next-line @typescript-eslint/no-unsafe-assignment
|
||||||
|
const parsedUpgradeFingerprint = JSON.parse(safeUpgradeFingerprint);
|
||||||
|
parsedUpgradeFingerprint.lvl += LevelDiff;
|
||||||
|
const stringifiedUpgradeFingerprint = JSON.stringify(parsedUpgradeFingerprint);
|
||||||
|
|
||||||
const safeUpgradeFingerprint = UpgradeFingerprint || '{"lvl":0}';
|
let itemIndex = Upgrades.findIndex(i => i._id?.equals(ItemId!.$oid));
|
||||||
// eslint-disable-next-line @typescript-eslint/no-unsafe-assignment
|
|
||||||
const parsedUpgradeFingerprint = JSON.parse(safeUpgradeFingerprint);
|
|
||||||
parsedUpgradeFingerprint.lvl += LevelDiff;
|
|
||||||
|
|
||||||
if (!ItemCount || itemIndex === -1) return;
|
if (itemIndex !== -1) {
|
||||||
|
Upgrades[itemIndex].UpgradeFingerprint = stringifiedUpgradeFingerprint;
|
||||||
|
inventory.markModified(`Upgrades.${itemIndex}.UpgradeFingerprint`);
|
||||||
|
} else {
|
||||||
|
itemIndex =
|
||||||
|
Upgrades.push({
|
||||||
|
UpgradeFingerprint: stringifiedUpgradeFingerprint,
|
||||||
|
ItemType
|
||||||
|
}) - 1;
|
||||||
|
|
||||||
RawUpgrades[itemIndex].UpgradeFingerprint = JSON.stringify(parsedUpgradeFingerprint);
|
const rawItemIndex = RawUpgrades.findIndex(i => i.ItemType === ItemType);
|
||||||
|
RawUpgrades[rawItemIndex].ItemCount--;
|
||||||
|
if (RawUpgrades[rawItemIndex].ItemCount > 0) {
|
||||||
|
inventory.markModified(`RawUpgrades.${rawItemIndex}.UpgradeFingerprint`);
|
||||||
|
} else {
|
||||||
|
RawUpgrades.splice(rawItemIndex, 1);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
// RawUpgrades[itemIndex].ItemCount--;
|
inventory.RegularCredits -= Cost;
|
||||||
|
inventory.FusionPoints -= FusionPointCost;
|
||||||
|
|
||||||
// if (RawUpgrades[itemIndex].ItemCount > 0) {
|
const changedInventory = await inventory.save();
|
||||||
// inventory.markModified(`RawUpgrades.${itemIndex}.ItemCount`);
|
const itemId = changedInventory.toJSON().Upgrades[itemIndex]?.ItemId?.$oid;
|
||||||
// } else {
|
|
||||||
// RawUpgrades.splice(itemIndex, 1);
|
|
||||||
// inventory.markModified(`RawUpgrades`);
|
|
||||||
// }
|
|
||||||
|
|
||||||
// addMods(inventory, [{ ItemType, ItemCount: 1, UpgradeFingerprint: JSON.stringify(parsedUpgradeFingerprint) }]);
|
if (!itemId) {
|
||||||
|
throw new Error("Item Id not found in upgradeMod");
|
||||||
|
}
|
||||||
|
|
||||||
inventory.RegularCredits -= Cost;
|
return itemId;
|
||||||
inventory.FusionPoints -= FusionPointCost;
|
} catch (error) {
|
||||||
|
console.error("Error in upgradeMod:", error);
|
||||||
const changedInventory = await inventory.save();
|
throw error;
|
||||||
// eslint-disable-next-line @typescript-eslint/no-unsafe-return
|
}
|
||||||
return changedInventory.RawUpgrades[itemIndex]?.toJSON().ItemId.$oid;
|
|
||||||
|
|
||||||
// await inventory.save();
|
|
||||||
// {
|
|
||||||
// "Upgrade": {
|
|
||||||
// ItemType: "/Lotus/Upgrades/Mods/Warframe/Beginner/AvatarShieldMaxModBeginner",
|
|
||||||
// ItemId: {
|
|
||||||
// $oid: ""
|
|
||||||
// },
|
|
||||||
// UpgradeFingerprint: '{"lvl":1}',
|
|
||||||
// PendingRerollFingerprint: "",
|
|
||||||
// ItemCount: 2,
|
|
||||||
// LastAdded: {
|
|
||||||
// $oid: "64f01ab0c4dfa3a8ef090043"
|
|
||||||
// }
|
|
||||||
// }
|
|
||||||
// "LevelDiff": 1,
|
|
||||||
// "Consumed": [],
|
|
||||||
// "Cost": 483,
|
|
||||||
// "FusionPointCost": 10
|
|
||||||
// }
|
|
||||||
};
|
};
|
||||||
|
|
||||||
export { createInventory, addPowerSuit };
|
export { createInventory, addPowerSuit };
|
||||||
|
@ -321,7 +321,8 @@ export interface CrewShipSalvageBinClass {
|
|||||||
export interface CrewShipSalvagedWeaponSkin {
|
export interface CrewShipSalvagedWeaponSkin {
|
||||||
ItemType: string;
|
ItemType: string;
|
||||||
UpgradeFingerprint?: string;
|
UpgradeFingerprint?: string;
|
||||||
ItemId: Oid;
|
ItemId?: Oid;
|
||||||
|
_id?: Types.ObjectId;
|
||||||
}
|
}
|
||||||
|
|
||||||
export interface CrewShipWeapon {
|
export interface CrewShipWeapon {
|
||||||
@ -925,10 +926,7 @@ export interface Progress {
|
|||||||
export interface RawUpgrade {
|
export interface RawUpgrade {
|
||||||
ItemType: string;
|
ItemType: string;
|
||||||
ItemCount: number;
|
ItemCount: number;
|
||||||
ItemId?: Oid;
|
|
||||||
LastAdded?: Oid;
|
LastAdded?: Oid;
|
||||||
UpgradeFingerprint?: string;
|
|
||||||
PendingRerollFingerprint?: string;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
export interface Scoop {
|
export interface Scoop {
|
||||||
|
Loading…
x
Reference in New Issue
Block a user