75 lines
2 KiB
TypeScript
75 lines
2 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 StartGameEvent = z.object({
|
|
side: Side,
|
|
});
|
|
|
|
export type StartGameEvent = z.infer<typeof StartGameEvent>;
|
|
|
|
export const GameEvent = z.object({
|
|
title: z.string(),
|
|
message: z.string(),
|
|
});
|
|
|
|
export type GameEvent = z.infer<typeof GameEvent>;
|
|
|
|
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 }),
|
|
z.object({ event: z.literal('GameEvent'), data: GameEvent }),
|
|
z.object({ event: z.literal('ClearGameEvent') }),
|
|
]);
|
|
|
|
export type ServerChessEvent = z.infer<typeof ServerChessEvent>;
|