This commit is contained in:
Aode (lion) 2022-01-07 22:25:01 -06:00
parent ae8974212c
commit fd23fe769a
4 changed files with 98 additions and 143 deletions

View file

@ -1,152 +1,101 @@
/* tslint:disable */
// generated by typescript-json-validator
import { inspect } from 'util';
import Ajv, { ErrorObject } from 'ajv';
import BoardResponse from './boardResponse';
import schema from 'ajv/lib/refs/json-schema-draft-06.json';
export const ajv = new Ajv({ "allErrors": true, "coerceTypes": false, "format": "fast", "nullable": true, "unicode": true, "uniqueItems": true, "useDefaults": true });
import { inspect } from "util";
import Ajv, { ErrorObject } from "ajv";
import BoardResponse from "./boardResponse";
import schema from "ajv/lib/refs/json-schema-draft-06.json";
export const ajv = new Ajv({
allErrors: true,
coerceTypes: false,
format: "fast",
nullable: true,
unicode: true,
uniqueItems: true,
useDefaults: true,
});
ajv.addMetaSchema(schema);
export const BoardResponseSchema = {
"$schema": "http://json-schema.org/draft-07/schema#",
"additionalProperties": false,
"defaultProperties": [
],
"definitions": {
"GAME_STATE": {
"enum": [
"black",
"done",
"pending",
"white"
],
"type": "string"
}
},
"properties": {
"boardState": {
"items": {
"additionalItems": {
"anyOf": [
{
"enum": [
"a",
"b",
"c",
"d",
"e",
"f",
"g",
"h"
],
"type": "string"
},
{
"enum": [
1,
2,
3,
4,
5,
6,
7,
8
],
"type": "number"
},
{
"enum": [
"bishop",
"king",
"knight",
"pawn",
"queen",
"rook"
],
"type": "string"
},
{
"enum": [
"black",
"white"
],
"type": "string"
}
]
},
"items": [
{
"enum": [
"a",
"b",
"c",
"d",
"e",
"f",
"g",
"h"
],
"type": "string"
},
{
"enum": [
1,
2,
3,
4,
5,
6,
7,
8
],
"type": "number"
},
{
"enum": [
"bishop",
"king",
"knight",
"pawn",
"queen",
"rook"
],
"type": "string"
},
{
"enum": [
"black",
"white"
],
"type": "string"
}
],
"minItems": 4,
"type": "array"
},
"type": "array"
$schema: "http://json-schema.org/draft-07/schema#",
additionalProperties: false,
defaultProperties: [],
definitions: {
GAME_STATE: {
enum: ["black", "done", "pending", "white"],
type: "string",
},
"gameState": {
"$ref": "#/definitions/GAME_STATE"
}
},
"required": [
"boardState",
"gameState"
],
"type": "object"
properties: {
boardState: {
items: {
additionalItems: {
anyOf: [
{
enum: ["a", "b", "c", "d", "e", "f", "g", "h"],
type: "string",
},
{
enum: [1, 2, 3, 4, 5, 6, 7, 8],
type: "number",
},
{
enum: ["bishop", "king", "knight", "pawn", "queen", "rook"],
type: "string",
},
{
enum: ["black", "white"],
type: "string",
},
],
},
items: [
{
enum: ["a", "b", "c", "d", "e", "f", "g", "h"],
type: "string",
},
{
enum: [1, 2, 3, 4, 5, 6, 7, 8],
type: "number",
},
{
enum: ["bishop", "king", "knight", "pawn", "queen", "rook"],
type: "string",
},
{
enum: ["black", "white"],
type: "string",
},
],
minItems: 4,
type: "array",
},
type: "array",
},
gameState: {
$ref: "#/definitions/GAME_STATE",
},
},
required: ["boardState", "gameState"],
type: "object",
};
export type ValidateFunction<T> = ((data: unknown) => data is T) & Pick<Ajv.ValidateFunction, 'errors'>
export const isBoardResponse = ajv.compile(BoardResponseSchema) as ValidateFunction<BoardResponse>;
export type ValidateFunction<T> = ((data: unknown) => data is T) &
Pick<Ajv.ValidateFunction, "errors">;
export const isBoardResponse = ajv.compile(
BoardResponseSchema
) as ValidateFunction<BoardResponse>;
export default function validate(value: unknown): BoardResponse {
if (isBoardResponse(value)) {
return value;
} else {
if (Array.isArray(isBoardResponse.errors)) {
throw new Error(
ajv.errorsText(isBoardResponse.errors.filter((e: ErrorObject) => e.keyword !== 'if'), { dataVar: 'BoardResponse' }) +
'\n\n' +
inspect(value),
ajv.errorsText(
isBoardResponse.errors.filter((e: ErrorObject) => e.keyword !== "if"),
{ dataVar: "BoardResponse" }
) +
"\n\n" +
inspect(value)
);
}

View file

@ -113,9 +113,4 @@ export const fromWireFormat = (boardWireFormat: BoardWireFormat): BoardState =>
);
export default BoardState;
export {
iconFor,
isPromoteAvailable,
opposite,
promoteCoordinates,
};
export { iconFor, isPromoteAvailable, opposite, promoteCoordinates };

View file

@ -9,7 +9,7 @@ import GAME_STATE from "./gameState";
export type ValidBoardResponse = {
boardState: BoardState;
gameState: GAME_STATE;
}
};
export type StartGame = (
playerColor: COLOR

View file

@ -1,9 +1,20 @@
import { COLOR } from "./primitives";
export type GAME_STATE = "pending" | "white" | "black" | "win" | "lose" | "draw";
export type GAME_STATE =
| "pending"
| "white"
| "black"
| "win"
| "lose"
| "draw";
const canStartGame = (gameState: GAME_STATE): boolean => {
return gameState === "pending" || gameState === "win" || gameState === "lose" || gameState === "draw";
return (
gameState === "pending" ||
gameState === "win" ||
gameState === "lose" ||
gameState === "draw"
);
};
const canMakeMove = (playerColor: COLOR, gameState: GAME_STATE): boolean => {