chore: move mod upgrading logic into artifactsController (#800)
This commit is contained in:
parent
734ca84557
commit
f1c3dcbefc
@ -1,19 +1,66 @@
|
|||||||
import { getJSONfromString } from "@/src/helpers/stringHelpers";
|
import { getJSONfromString } from "@/src/helpers/stringHelpers";
|
||||||
import { getAccountIdForRequest } from "@/src/services/loginService";
|
import { getAccountIdForRequest } from "@/src/services/loginService";
|
||||||
import { upgradeMod } from "@/src/services/inventoryService";
|
|
||||||
import { IArtifactsRequest } from "@/src/types/requestTypes";
|
|
||||||
import { RequestHandler } from "express";
|
import { RequestHandler } from "express";
|
||||||
|
import { ICrewShipSalvagedWeaponSkin } from "@/src/types/inventoryTypes/inventoryTypes";
|
||||||
|
import { getInventory } from "@/src/services/inventoryService";
|
||||||
|
import { config } from "@/src/services/configService";
|
||||||
|
|
||||||
const artifactsController: RequestHandler = async (req, res) => {
|
export const artifactsController: RequestHandler = async (req, res) => {
|
||||||
const accountId = await getAccountIdForRequest(req);
|
const accountId = await getAccountIdForRequest(req);
|
||||||
|
const artifactsData = getJSONfromString(String(req.body)) as IArtifactsRequest;
|
||||||
|
|
||||||
try {
|
const { Upgrade, LevelDiff, Cost, FusionPointCost } = artifactsData;
|
||||||
const artifactsData = getJSONfromString(String(req.body)) as IArtifactsRequest;
|
|
||||||
const upgradeModId = await upgradeMod(artifactsData, accountId);
|
const inventory = await getInventory(accountId);
|
||||||
res.send(upgradeModId);
|
const { Upgrades, RawUpgrades } = inventory;
|
||||||
} catch (err) {
|
const { ItemType, UpgradeFingerprint, ItemId } = Upgrade;
|
||||||
console.error("Error parsing JSON data:", err);
|
|
||||||
|
const safeUpgradeFingerprint = UpgradeFingerprint || '{"lvl":0}';
|
||||||
|
const parsedUpgradeFingerprint = JSON.parse(safeUpgradeFingerprint) as { lvl: number };
|
||||||
|
parsedUpgradeFingerprint.lvl += LevelDiff;
|
||||||
|
const stringifiedUpgradeFingerprint = JSON.stringify(parsedUpgradeFingerprint);
|
||||||
|
|
||||||
|
let itemIndex = Upgrades.findIndex(upgrade => upgrade._id?.equals(ItemId!.$oid));
|
||||||
|
|
||||||
|
if (itemIndex !== -1) {
|
||||||
|
Upgrades[itemIndex].UpgradeFingerprint = stringifiedUpgradeFingerprint;
|
||||||
|
inventory.markModified(`Upgrades.${itemIndex}.UpgradeFingerprint`);
|
||||||
|
} else {
|
||||||
|
itemIndex =
|
||||||
|
Upgrades.push({
|
||||||
|
UpgradeFingerprint: stringifiedUpgradeFingerprint,
|
||||||
|
ItemType
|
||||||
|
}) - 1;
|
||||||
|
|
||||||
|
const rawItemIndex = RawUpgrades.findIndex(rawUpgrade => rawUpgrade.ItemType === ItemType);
|
||||||
|
RawUpgrades[rawItemIndex].ItemCount--;
|
||||||
|
if (RawUpgrades[rawItemIndex].ItemCount > 0) {
|
||||||
|
inventory.markModified(`RawUpgrades.${rawItemIndex}.UpgradeFingerprint`);
|
||||||
|
} else {
|
||||||
|
RawUpgrades.splice(rawItemIndex, 1);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (!config.infiniteCredits) {
|
||||||
|
inventory.RegularCredits -= Cost;
|
||||||
|
}
|
||||||
|
if (!config.infiniteEndo) {
|
||||||
|
inventory.FusionPoints -= FusionPointCost;
|
||||||
|
}
|
||||||
|
|
||||||
|
const changedInventory = await inventory.save();
|
||||||
|
const itemId = changedInventory.toJSON().Upgrades[itemIndex]?.ItemId?.$oid;
|
||||||
|
|
||||||
|
if (!itemId) {
|
||||||
|
throw new Error("Item Id not found in upgradeMod");
|
||||||
|
}
|
||||||
|
|
||||||
|
res.send(itemId);
|
||||||
};
|
};
|
||||||
|
|
||||||
export { artifactsController };
|
interface IArtifactsRequest {
|
||||||
|
Upgrade: ICrewShipSalvagedWeaponSkin;
|
||||||
|
LevelDiff: number;
|
||||||
|
Cost: number;
|
||||||
|
FusionPointCost: number;
|
||||||
|
}
|
||||||
|
@ -21,7 +21,6 @@ import {
|
|||||||
} from "@/src/types/inventoryTypes/inventoryTypes";
|
} from "@/src/types/inventoryTypes/inventoryTypes";
|
||||||
import { IGenericUpdate } from "../types/genericUpdate";
|
import { IGenericUpdate } from "../types/genericUpdate";
|
||||||
import {
|
import {
|
||||||
IArtifactsRequest,
|
|
||||||
IMissionInventoryUpdateRequest,
|
IMissionInventoryUpdateRequest,
|
||||||
IThemeUpdateRequest,
|
IThemeUpdateRequest,
|
||||||
IUpdateChallengeProgressRequest
|
IUpdateChallengeProgressRequest
|
||||||
@ -899,57 +898,3 @@ export const addBooster = async (ItemType: string, time: number, accountId: stri
|
|||||||
|
|
||||||
await inventory.save();
|
await inventory.save();
|
||||||
};
|
};
|
||||||
|
|
||||||
export const upgradeMod = async (artifactsData: IArtifactsRequest, accountId: string): Promise<string | undefined> => {
|
|
||||||
const { Upgrade, LevelDiff, Cost, FusionPointCost } = artifactsData;
|
|
||||||
try {
|
|
||||||
const inventory = await getInventory(accountId);
|
|
||||||
const { Upgrades, RawUpgrades } = inventory;
|
|
||||||
const { ItemType, UpgradeFingerprint, ItemId } = Upgrade;
|
|
||||||
|
|
||||||
const safeUpgradeFingerprint = UpgradeFingerprint || '{"lvl":0}';
|
|
||||||
const parsedUpgradeFingerprint = JSON.parse(safeUpgradeFingerprint) as { lvl: number };
|
|
||||||
parsedUpgradeFingerprint.lvl += LevelDiff;
|
|
||||||
const stringifiedUpgradeFingerprint = JSON.stringify(parsedUpgradeFingerprint);
|
|
||||||
|
|
||||||
let itemIndex = Upgrades.findIndex(upgrade => upgrade._id?.equals(ItemId!.$oid));
|
|
||||||
|
|
||||||
if (itemIndex !== -1) {
|
|
||||||
Upgrades[itemIndex].UpgradeFingerprint = stringifiedUpgradeFingerprint;
|
|
||||||
inventory.markModified(`Upgrades.${itemIndex}.UpgradeFingerprint`);
|
|
||||||
} else {
|
|
||||||
itemIndex =
|
|
||||||
Upgrades.push({
|
|
||||||
UpgradeFingerprint: stringifiedUpgradeFingerprint,
|
|
||||||
ItemType
|
|
||||||
}) - 1;
|
|
||||||
|
|
||||||
const rawItemIndex = RawUpgrades.findIndex(rawUpgrade => rawUpgrade.ItemType === ItemType);
|
|
||||||
RawUpgrades[rawItemIndex].ItemCount--;
|
|
||||||
if (RawUpgrades[rawItemIndex].ItemCount > 0) {
|
|
||||||
inventory.markModified(`RawUpgrades.${rawItemIndex}.UpgradeFingerprint`);
|
|
||||||
} else {
|
|
||||||
RawUpgrades.splice(rawItemIndex, 1);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
if (!config.infiniteCredits) {
|
|
||||||
inventory.RegularCredits -= Cost;
|
|
||||||
}
|
|
||||||
if (!config.infiniteEndo) {
|
|
||||||
inventory.FusionPoints -= FusionPointCost;
|
|
||||||
}
|
|
||||||
|
|
||||||
const changedInventory = await inventory.save();
|
|
||||||
const itemId = changedInventory.toJSON().Upgrades[itemIndex]?.ItemId?.$oid;
|
|
||||||
|
|
||||||
if (!itemId) {
|
|
||||||
throw new Error("Item Id not found in upgradeMod");
|
|
||||||
}
|
|
||||||
|
|
||||||
return itemId;
|
|
||||||
} catch (error) {
|
|
||||||
console.error("Error in upgradeMod:", error);
|
|
||||||
throw error;
|
|
||||||
}
|
|
||||||
};
|
|
||||||
|
@ -4,7 +4,6 @@ import {
|
|||||||
IBooster,
|
IBooster,
|
||||||
IChallengeProgress,
|
IChallengeProgress,
|
||||||
IConsumable,
|
IConsumable,
|
||||||
ICrewShipSalvagedWeaponSkin,
|
|
||||||
IEvolutionProgress,
|
IEvolutionProgress,
|
||||||
IMiscItem,
|
IMiscItem,
|
||||||
ITypeCount,
|
ITypeCount,
|
||||||
@ -16,13 +15,6 @@ import {
|
|||||||
IFusionTreasure
|
IFusionTreasure
|
||||||
} from "./inventoryTypes/inventoryTypes";
|
} from "./inventoryTypes/inventoryTypes";
|
||||||
|
|
||||||
export interface IArtifactsRequest {
|
|
||||||
Upgrade: ICrewShipSalvagedWeaponSkin;
|
|
||||||
LevelDiff: number;
|
|
||||||
Cost: number;
|
|
||||||
FusionPointCost: number;
|
|
||||||
}
|
|
||||||
|
|
||||||
export interface IThemeUpdateRequest {
|
export interface IThemeUpdateRequest {
|
||||||
Style?: string;
|
Style?: string;
|
||||||
Background?: string;
|
Background?: string;
|
||||||
|
Loading…
x
Reference in New Issue
Block a user