This repository has been archived on 2026-03-19. You can view files and clone it, but cannot push or open issues or pull requests.
Files
galvanic-corrosion/src/routes/api/players.ts
zombieb 5c69269b70
Some checks failed
Galvanic Corrosion Cross-Compile / build (push) Failing after 38s
Frostbite is gone????
* Rewrite rooms backend, "RoomFactory" and "SubroomFactory"
    - Used for modifying and fetching rooms
* Progression and reputation bulk endpoints
* Announcement endpoint temp
* OOBE is now the only initial pref key
    - Will be removed in the future when cohortnux is implemented
* Misc minor fixes and clarifications
* Simplified namegen dictionary
    - The previous one was generated with ChatGPT, hence the duplicated strings. I googled "random username generator" and borrowed a random result's generation dictionary.
* QuickPlay support with "initialRoom" in config (untested)
2025-04-15 21:15:15 -04:00

87 lines
2.9 KiB
TypeScript

/* Galvanic Corrosion - Rec Room custom server for communities.
<https://gitea.proxnet.dev/zombieb/galvanic-corrosion>
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 <https://www.gnu.org/licenses/>. */
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<NoBody, {}, ProgressionBulkBody>, 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()]);
}
},
);