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>;
|
|
|
|
|
2022-11-01 10:00:17 +00:00
|
|
|
export const StartGameEvent = z.object({
|
|
|
|
side: Side,
|
|
|
|
});
|
|
|
|
|
|
|
|
export type StartGameEvent = z.infer<typeof StartGameEvent>;
|
|
|
|
|
2022-11-02 14:25:59 +00:00
|
|
|
export const GameEvent = z.object({
|
|
|
|
title: z.string(),
|
|
|
|
message: z.string(),
|
|
|
|
});
|
|
|
|
|
|
|
|
export type GameEvent = z.infer<typeof GameEvent>;
|
|
|
|
|
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 }),
|
2022-11-02 14:25:59 +00:00
|
|
|
z.object({ event: z.literal('StartGame'), data: StartGameEvent }),
|
|
|
|
z.object({ event: z.literal('GameEvent'), data: GameEvent }),
|
|
|
|
z.object({ event: z.literal('ClearGameEvent') }),
|
2022-10-31 13:25:32 +00:00
|
|
|
]);
|
|
|
|
|
|
|
|
export type ServerChessEvent = z.infer<typeof ServerChessEvent>;
|