Spaces:
Sleeping
Sleeping
File size: 1,143 Bytes
90cbf22 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 |
import { v } from 'convex/values';
const IdShortCodes = { agents: 'a', conversations: 'c', players: 'p', operations: 'o' };
export type IdTypes = keyof typeof IdShortCodes;
export type GameId<T extends IdTypes> = string & { __type: T };
export function parseGameId<T extends IdTypes>(idType: T, gameId: string): GameId<T> {
const type = gameId[0];
const match = Object.entries(IdShortCodes).find(([_, value]) => value === type);
if (!match || match[0] !== idType) {
throw new Error(`Invalid game ID type: ${type}`);
}
const number = parseInt(gameId.slice(2), 10);
if (isNaN(number) || !Number.isInteger(number) || number < 0) {
throw new Error(`Invalid game ID number: ${gameId}`);
}
return gameId as GameId<T>;
}
export function allocGameId<T extends IdTypes>(idType: T, idNumber: number): GameId<T> {
const type = IdShortCodes[idType];
if (!type) {
throw new Error(`Invalid game ID type: ${idType}`);
}
return `${type}:${idNumber}` as GameId<T>;
}
export const conversationId = v.string();
export const playerId = v.string();
export const agentId = v.string();
export const operationId = v.string();
|