SpaceNinjaServer/src/controllers/api/trainingResultController.ts

69 lines
2.4 KiB
TypeScript
Raw Normal View History

2024-05-28 13:45:06 +02:00
import { getAccountIdForRequest } from "@/src/services/loginService";
2023-09-11 13:20:07 +02:00
import { getJSONfromString } from "@/src/helpers/stringHelpers";
import { getInventory } from "@/src/services/inventoryService";
import { IMongoDate } from "@/src/types/commonTypes";
import { RequestHandler } from "express";
import { unixTimesInMs } from "@/src/constants/timeConstants";
import { IInventoryChanges } from "@/src/types/purchaseTypes";
import { createMessage } from "@/src/services/inboxService";
import { config } from "@/src/services/configService";
2023-09-11 13:20:07 +02:00
interface ITrainingResultsRequest {
numLevelsGained: number;
}
interface ITrainingResultsResponse {
NewTrainingDate: IMongoDate;
NewLevel: number;
InventoryChanges: IInventoryChanges;
2023-09-11 13:20:07 +02:00
}
const trainingResultController: RequestHandler = async (req, res): Promise<void> => {
2024-05-28 13:45:06 +02:00
const accountId = await getAccountIdForRequest(req);
2023-09-11 13:20:07 +02:00
const trainingResults = getJSONfromString<ITrainingResultsRequest>(String(req.body));
2023-09-11 13:20:07 +02:00
const inventory = await getInventory(accountId);
if (trainingResults.numLevelsGained == 1) {
let time = Date.now();
if (!config.noMasteryRankUpCooldown) {
time += unixTimesInMs.hour * 23;
}
inventory.TrainingDate = new Date(time);
2023-09-11 13:20:07 +02:00
inventory.PlayerLevel += 1;
await createMessage(accountId, [
{
sndr: "/Lotus/Language/Menu/Mailbox_WarframeSender",
msg: "/Lotus/Language/Inbox/MasteryRewardMsg",
arg: [
{
Key: "NEW_RANK",
Tag: inventory.PlayerLevel
}
],
att: [
`/Lotus/Types/Items/ShipDecos/MasteryTrophies/Rank${inventory.PlayerLevel.toString().padStart(2, "0")}Trophy`
],
sub: "/Lotus/Language/Inbox/MasteryRewardTitle",
icon: "/Lotus/Interface/Icons/Npcs/Lotus_d.png",
highPriority: true
}
]);
2023-09-11 13:20:07 +02:00
}
const changedinventory = await inventory.save();
res.json({
NewTrainingDate: {
$date: { $numberLong: changedinventory.TrainingDate.getTime().toString() }
},
NewLevel: trainingResults.numLevelsGained == 1 ? changedinventory.PlayerLevel : inventory.PlayerLevel,
InventoryChanges: {}
2023-09-11 13:20:07 +02:00
} satisfies ITrainingResultsResponse);
};
export { trainingResultController };