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";
|
|
|
|
|
|
|
|
interface ITrainingResultsRequest {
|
|
|
|
numLevelsGained: number;
|
|
|
|
}
|
|
|
|
|
|
|
|
interface ITrainingResultsResponse {
|
|
|
|
NewTrainingDate: IMongoDate;
|
|
|
|
NewLevel: number;
|
|
|
|
InventoryChanges: any[];
|
|
|
|
}
|
|
|
|
|
|
|
|
// eslint-disable-next-line @typescript-eslint/no-misused-promises
|
|
|
|
const trainingResultController: RequestHandler = async (req, res): Promise<void> => {
|
2024-05-28 13:45:06 +02:00
|
|
|
const accountId = await getAccountIdForRequest(req);
|
2024-09-06 02:02:26 +03:00
|
|
|
const numLevelsGained = parseInt(req.query.numLevelsGained as string);
|
2023-09-11 13:20:07 +02:00
|
|
|
const inventory = await getInventory(accountId);
|
2024-09-06 02:02:26 +03:00
|
|
|
console.log(req.query);
|
2023-09-11 13:20:07 +02:00
|
|
|
inventory.TrainingDate = new Date(Date.now() + unixTimesInMs.day);
|
|
|
|
|
2024-09-06 02:02:26 +03:00
|
|
|
if (numLevelsGained == 1) {
|
2023-09-11 13:20:07 +02:00
|
|
|
inventory.PlayerLevel += 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
const changedinventory = await inventory.save();
|
|
|
|
|
|
|
|
res.json({
|
|
|
|
NewTrainingDate: {
|
|
|
|
$date: { $numberLong: changedinventory.TrainingDate.getTime().toString() }
|
|
|
|
},
|
2024-09-06 02:02:26 +03:00
|
|
|
NewLevel: numLevelsGained == 1 ? changedinventory.PlayerLevel : inventory.PlayerLevel,
|
2023-09-11 13:20:07 +02:00
|
|
|
InventoryChanges: []
|
|
|
|
} satisfies ITrainingResultsResponse);
|
|
|
|
};
|
|
|
|
|
|
|
|
export { trainingResultController };
|