chs/src-web/events.ts

67 lines
1.7 KiB
TypeScript
Raw Normal View History

2022-10-31 13:25:32 +00:00
import { z } from 'zod';
export const Coordinate = z.object({
rank: z.number(),
file: z.number(),
});
export type Coordinate = z.infer<typeof Coordinate>;
export const Side = z.enum(['white', 'black']);
export type Side = z.infer<typeof Side>;
export const PieceType = z.enum(['king', 'queen', 'rook', 'bishop', 'knight', 'pawn']);
export type PieceType = z.infer<typeof PieceType>;
export const Piece = z.object({
side: Side,
ty: PieceType,
});
export type Piece = z.infer<typeof Piece>;
export interface Move {
from: Coordinate;
to: Coordinate;
set_en_passant: Coordinate | null;
other: Move | null;
promotions: Piece[] | null;
}
export const Move: z.ZodType<Move> = z.lazy(() => z.object({
from: Coordinate,
to: Coordinate,
set_en_passant: z.nullable(Coordinate),
other: z.nullable(Move),
promotions: z.nullable(z.array(Piece)),
}));
export const Board = z.array(z.nullable(Piece));
export type Board = z.infer<typeof Board>;
export const BoardUpdateEvent = z.object({
board: Board,
});
export type BoardUpdateEvent = z.infer<typeof BoardUpdateEvent>;
export const PossibleMovesEvent = z.object({
moves: z.array(Move),
});
export type PossibleMovesEvent = z.infer<typeof PossibleMovesEvent>;
export const StartGameEvent = z.object({
side: Side,
});
export type StartGameEvent = z.infer<typeof StartGameEvent>;
2022-10-31 13:25:32 +00:00
export const ServerChessEvent = z.discriminatedUnion("event", [
z.object({ event: z.literal('BoardUpdate'), data: BoardUpdateEvent }),
z.object({ event: z.literal('PossibleMoves'), data: PossibleMovesEvent }),
z.object({ event: z.literal('StartGame'), data: StartGameEvent })
2022-10-31 13:25:32 +00:00
]);
export type ServerChessEvent = z.infer<typeof ServerChessEvent>;