45 lines
1.6 KiB
TypeScript
Raw Normal View History

2023-08-31 14:29:09 +04:00
import { RequestHandler } from "express";
2024-05-28 13:45:06 +02:00
import { getAccountIdForRequest } from "@/src/services/loginService";
2024-05-08 22:53:06 +02:00
import { Inventory } from "@/src/models/inventoryModels/inventoryModel";
2024-05-03 21:04:01 +02:00
import { IStatsView } from "@/src/types/statTypes";
2024-05-15 21:55:59 +02:00
import { config } from "@/src/services/configService";
2024-05-03 21:04:01 +02:00
import allScans from "@/static/fixed_responses/allScans.json";
2024-07-29 14:03:27 +02:00
import Stat from "@/src/models/statsModel";
2023-08-31 14:29:09 +04:00
// eslint-disable-next-line @typescript-eslint/no-misused-promises
2024-05-08 22:53:06 +02:00
const viewController: RequestHandler = async (req, res) => {
2024-05-28 13:45:06 +02:00
const accountId = await getAccountIdForRequest(req);
const inventory = await Inventory.findOne({ accountOwnerId: accountId });
2024-07-29 14:03:27 +02:00
let playerStats = await Stat.findOne({ accountOwnerId: accountId });
2024-05-08 22:53:06 +02:00
if (!inventory) {
res.status(400).json({ error: "inventory was undefined" });
return;
}
2024-07-29 14:03:27 +02:00
if (!playerStats) {
playerStats = new Stat({ accountOwnerId: accountId });
await playerStats.save();
}
const responseJson: IStatsView = playerStats;
responseJson.Weapons ??= [];
2024-05-08 22:53:06 +02:00
for (const item of inventory.XPInfo) {
2024-07-29 14:03:27 +02:00
const weaponIndex = responseJson.Weapons.findIndex(element => element.type == item.ItemType);
if (weaponIndex !== -1) {
responseJson.Weapons[weaponIndex].xp == item.XP;
} else {
responseJson.Weapons.push({
type: item.ItemType,
xp: item.XP
});
}
2024-05-08 22:53:06 +02:00
}
2024-05-03 21:04:01 +02:00
if (config.unlockAllScans) {
responseJson.Scans = allScans;
}
res.json(responseJson);
2023-08-31 14:29:09 +04:00
};
export { viewController };