Mod upgrade - take 2

This commit is contained in:
holmityd 2023-09-02 13:43:17 +04:00
parent 20658043dd
commit 0309e182c2
4 changed files with 38 additions and 19 deletions

View File

@ -13,8 +13,8 @@ const artifactsController: RequestHandler = async (req, res) => {
const parsedData = JSON.parse(data);
// eslint-disable-next-line @typescript-eslint/no-unsafe-argument
await upgradeMod(parsedData, id);
res.json({});
const upgradeModId = await upgradeMod(parsedData, id);
res.send(upgradeModId);
} catch (err) {
console.error("Error parsing JSON data:", err);
}

View File

@ -1,4 +1,4 @@
import { Model, Schema, Types, model } from "mongoose";
import mongoose, { Model, Schema, Types, model } from "mongoose";
import { FlavourItem, RawUpgrade, MiscItem, IInventoryDatabase, Booster } from "../types/inventoryTypes/inventoryTypes";
import { Oid } from "../types/commonTypes";
import { ISuitDatabase } from "@/src/types/inventoryTypes/SuitTypes";
@ -75,7 +75,13 @@ const RawUpgrades = new Schema({
ItemType: String,
UpgradeFingerprint: String,
PendingRerollFingerprint: String,
ItemCount: Number
ItemCount: Number,
ItemId: {
$oid: mongoose.Schema.Types.ObjectId
},
LastAdded: {
$oid: mongoose.Schema.Types.ObjectId
}
});
RawUpgrades.set("toJSON", {

View File

@ -150,14 +150,16 @@ const addMods = (inventory: IInventoryDatabaseDocument, itemsArray: RawUpgrade[]
const { RawUpgrades } = inventory;
itemsArray?.forEach(({ ItemType, ItemCount, UpgradeFingerprint }) => {
const itemIndex = RawUpgrades.findIndex(
i => i.ItemType === ItemType && i.UpgradeFingerprint === UpgradeFingerprint
i =>
i.ItemType === ItemType &&
(i.UpgradeFingerprint === UpgradeFingerprint || (!i.UpgradeFingerprint && !UpgradeFingerprint))
);
if (itemIndex !== -1) {
RawUpgrades[itemIndex].ItemCount += ItemCount;
inventory.markModified(`RawUpgrades.${itemIndex}.ItemCount`);
} else {
RawUpgrades.push({ ItemCount, ItemType });
RawUpgrades.push({ ItemCount, ItemType, UpgradeFingerprint });
}
});
};
@ -228,36 +230,46 @@ export const upgradeMod = async (
FusionPointCost
}: { Upgrade: RawUpgrade; LevelDiff: number; Cost: number; FusionPointCost: number },
accountId: string
): Promise<void> => {
): Promise<string | undefined> => {
const inventory = await getInventory(accountId);
const { RawUpgrades } = inventory;
const { ItemCount, ItemType, UpgradeFingerprint } = Upgrade;
const itemIndex = RawUpgrades.findIndex(
i => i.ItemType === ItemType && i.UpgradeFingerprint === UpgradeFingerprint
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;
if (!ItemCount) return;
if (!ItemCount || itemIndex === -1) return;
RawUpgrades[itemIndex].ItemCount--;
RawUpgrades[itemIndex].UpgradeFingerprint = JSON.stringify(parsedUpgradeFingerprint);
if (RawUpgrades[itemIndex].ItemCount > 0) {
inventory.markModified(`RawUpgrades.${itemIndex}.ItemCount`);
} else {
RawUpgrades.splice(itemIndex, 1);
inventory.markModified(`RawUpgrades`);
}
// RawUpgrades[itemIndex].ItemCount--;
addMods(inventory, [{ ItemType, ItemCount: 1, UpgradeFingerprint: JSON.stringify(parsedUpgradeFingerprint) }]);
// if (RawUpgrades[itemIndex].ItemCount > 0) {
// inventory.markModified(`RawUpgrades.${itemIndex}.ItemCount`);
// } else {
// RawUpgrades.splice(itemIndex, 1);
// inventory.markModified(`RawUpgrades`);
// }
// addMods(inventory, [{ ItemType, ItemCount: 1, UpgradeFingerprint: JSON.stringify(parsedUpgradeFingerprint) }]);
inventory.RegularCredits -= Cost;
inventory.FusionPoints -= FusionPointCost;
await inventory.save();
const changedInventory = await inventory.save();
// 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",

View File

@ -925,9 +925,10 @@ export interface Progress {
export interface RawUpgrade {
ItemType: string;
ItemCount: number;
ItemId?: Oid;
LastAdded?: Oid;
UpgradeFingerprint?: string;
PendingRerollFingerprint?: string;
LastAdded?: Oid;
}
export interface Scoop {