Files
aaaa/src/routes/api/routes/playerReputation.ts

34 lines
1.2 KiB
TypeScript

import z from "zod";
import { createHonoRoute } from "../../../util/import.ts";
import { typedZValidator } from "../../../util/validators.ts";
import { authenticate } from "../../../util/api.ts";
import Server from "../../../server/server.ts";
export const route = createHonoRoute("/playerReputation");
const getRepIdParamSchema = z.object({
id: z.coerce.number().min(0).max(2_147_483_647)
})
route.app.get('/v1/:id', authenticate, typedZValidator('param', getRepIdParamSchema), authenticate, async c => {
const { id } = c.req.valid('param');
const prof = await Server.Profiles.get(id);
if (!prof) return c.status(404);
else return c.json(await prof.Reputation.export());
});
const getRepBulkBodySchema = z.object({
Ids: z.array(z.coerce.number())
});
route.app.post('/v1/bulk', authenticate, typedZValidator('form', getRepBulkBodySchema), async c => {
const ids = c.req.valid('form').Ids;
if (typeof ids == 'object') {
const profs = await Server.Profiles.getMany(...ids);
return c.json(await Promise.all(profs.map(prof => prof.Reputation.export())));
} else {
const prof = await Server.Profiles.get(ids);
if (!prof) return c.json([]);
return c.json([await prof.Reputation.export()]);
}
});