59 lines
1.5 KiB
TypeScript
59 lines
1.5 KiB
TypeScript
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 ServerChessEvent = z.discriminatedUnion("event", [
|
|
z.object({ event: z.literal('BoardUpdate'), data: BoardUpdateEvent }),
|
|
z.object({ event: z.literal('PossibleMoves'), data: PossibleMovesEvent }),
|
|
]);
|
|
|
|
export type ServerChessEvent = z.infer<typeof ServerChessEvent>;
|