/* Galvanic Corrosion - Rec Room custom server for communities. Copyright (C) 2025 @zombieb (Discord / proxnet Gitea) This program is free software: you can redistribute it and/or modify it under the terms of the GNU Affero General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version. This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Affero General Public License for more details. You should have received a copy of the GNU Affero General Public License along with this program. If not, see . */ import Logging from "@proxnet/undead-logging"; import { APIUtils, NoBody } from "../../apiutils.ts"; import express from "express"; import UnifiedProfile from "../../data/profiles.ts"; import { AuthType } from "../../data/users.ts"; import { z } from "zod"; const log = new Logging("ProgressionRoute"); const rateLimit = new APIUtils.RateLimiter(); export const route = APIUtils.createRouter("/players"); route.router.get('/v1/progression/:id', rateLimit.middle(), async (rq: express.Request<{ id: string }>, rs) => { const unparsedPlayerId = rq.params.id; const parsedPlayerId = parseInt(unparsedPlayerId); if (isNaN(parsedPlayerId)) { rs.json(APIUtils.genericResponseFormat(true, 'The player ID was invalid.')); return; } const profile = UnifiedProfile.get(parsedPlayerId); const res = { PlayerId: profile.getId(), Level: await profile.Progression.getLevel(), XP: await profile.Progression.getXp() }; log.d(`prog res: ${JSON.stringify(res)}`); rs.json(res); } ); interface ProgressionBulkBody { Ids: string[] | string } const progressionBulkSchema = z.object({ Ids: z.union([ z.array(z.string()), z.string() ]) }); route.router.post('/v1/progression/bulk', APIUtils.Authentication, APIUtils.AuthenticationType(AuthType.Game), express.urlencoded({ extended: true }), APIUtils.validateRequestBody(progressionBulkSchema), async (rq: express.Request, rs: express.Response) => { if (typeof rq.body.Ids == 'object') { const progressions = rq.body.Ids .map(id => parseInt(id)).filter(id => !isNaN(id)) // filter out non-numbers .map(id => UnifiedProfile.get(id).Progression.export()); // get all progressions rs.json(await Promise.all(progressions)); } else { const id = parseInt(rq.body.Ids); if (isNaN(id)) { rs.sendStatus(400); return; } rs.json([await UnifiedProfile.get(id).Progression.export()]); } }, );