SpaceNinjaServer/src/controllers/api/inventoryController.ts

120 lines
4.4 KiB
TypeScript
Raw Normal View History

/* eslint-disable @typescript-eslint/no-misused-promises */
2024-05-28 13:45:06 +02:00
import { getAccountIdForRequest } from "@/src/services/loginService";
import { toInventoryResponse } from "@/src/helpers/inventoryHelpers";
import { Inventory } from "@/src/models/inventoryModels/inventoryModel";
2023-05-23 20:53:26 -04:00
import { Request, RequestHandler, Response } from "express";
2024-05-15 21:55:59 +02:00
import { config } from "@/src/services/configService";
2024-02-12 14:31:28 +01:00
import allMissions from "@/static/fixed_responses/allMissions.json";
import allQuestKeys from "@/static/fixed_responses/allQuestKeys.json";
2024-05-03 21:10:05 +02:00
import allShipDecorations from "@/static/fixed_responses/allShipDecorations.json";
import allFlavourItems from "@/static/fixed_responses/allFlavourItems.json";
2024-05-29 22:08:41 +02:00
import allSkins from "@/static/fixed_responses/allSkins.json";
import { ILoadoutDatabase } from "@/src/types/saveLoadoutTypes";
2024-05-03 21:10:05 +02:00
import { IShipInventory, IFlavourItem } from "@/src/types/inventoryTypes/inventoryTypes";
2023-05-19 15:22:48 -03:00
const inventoryController: RequestHandler = async (request: Request, response: Response) => {
2024-05-28 13:45:06 +02:00
let accountId;
try {
accountId = await getAccountIdForRequest(request);
} catch (e) {
response.status(400).send("Log-in expired");
return;
}
const inventory = await Inventory.findOne({ accountOwnerId: accountId })
.populate<{
LoadOutPresets: ILoadoutDatabase;
}>("LoadOutPresets")
.populate<{ Ships: IShipInventory }>("Ships", "-ShipInteriorColors");
if (!inventory) {
response.status(400).json({ error: "inventory was undefined" });
return;
}
2024-01-25 14:49:45 +01:00
//TODO: make a function that converts from database representation to client
const inventoryJSON = inventory.toJSON();
console.log(inventoryJSON.Ships);
2023-09-11 13:20:07 +02:00
const inventoryResponse = toInventoryResponse(inventoryJSON);
if (config.infiniteResources) {
inventoryResponse.RegularCredits = 999999999;
inventoryResponse.TradesRemaining = 999999999;
inventoryResponse.PremiumCreditsFree = 999999999;
inventoryResponse.PremiumCredits = 999999999;
}
if (config.unlockAllMissions) {
inventoryResponse.Missions = allMissions;
inventoryResponse.NodeIntrosCompleted.push("TeshinHardModeUnlocked");
}
if (config.unlockAllQuests) {
for (const questKey of allQuestKeys) {
if (!inventoryResponse.QuestKeys.find(quest => quest.ItemType == questKey)) {
inventoryResponse.QuestKeys.push({ ItemType: questKey });
}
}
}
if (config.completeAllQuests) {
for (const quest of inventoryResponse.QuestKeys) {
quest.Completed = true;
quest.Progress = [
{
c: 0,
i: false,
m: false,
b: []
}
];
}
2024-06-16 23:57:30 +02:00
inventoryResponse.ArchwingEnabled = true;
// Skip "Watch The Maker"
inventoryResponse.NodeIntrosCompleted.push("/Lotus/Levels/Cinematics/NewWarIntro/NewWarStageTwo.level");
}
2024-05-03 21:10:05 +02:00
if (config.unlockAllShipDecorations) inventoryResponse.ShipDecorations = allShipDecorations;
if (config.unlockAllFlavourItems) inventoryResponse.FlavourItems = allFlavourItems satisfies IFlavourItem[];
2024-05-29 22:08:41 +02:00
if (config.unlockAllSkins) {
inventoryResponse.WeaponSkins = [];
for (const skin of allSkins) {
2024-05-29 22:08:41 +02:00
inventoryResponse.WeaponSkins.push({
ItemId: {
$oid: "000000000000000000000000"
},
ItemType: skin
});
}
}
if (typeof config.spoofMasteryRank === "number" && config.spoofMasteryRank >= 0) {
inventoryResponse.PlayerLevel = config.spoofMasteryRank;
if (!("xpBasedLevelCapDisabled" in request.query)) {
// This client has not been patched to accept any mastery rank, need to fake the XP.
inventoryResponse.XPInfo = [];
let numFrames = getExpRequiredForMr(Math.min(config.spoofMasteryRank, 5030)) / 6000;
while (numFrames-- > 0) {
inventoryResponse.XPInfo.push({
ItemType: "/Lotus/Powersuits/Mag/Mag",
XP: 1_600_000
});
}
}
}
2023-09-11 13:20:07 +02:00
response.json(inventoryResponse);
2023-05-19 15:22:48 -03:00
};
const getExpRequiredForMr = (rank: number): number => {
if (rank <= 30) {
return 2500 * rank * rank;
}
return 2_250_000 + 147_500 * (rank - 30);
};
2023-06-02 00:20:49 -03:00
export { inventoryController };