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 */ /* tslint:disable */
// generated by typescript-json-validator // generated by typescript-json-validator
import { inspect } from 'util'; import { inspect } from "util";
import Ajv, { ErrorObject } from 'ajv'; import Ajv, { ErrorObject } from "ajv";
import BoardResponse from './boardResponse'; import BoardResponse from "./boardResponse";
import schema from 'ajv/lib/refs/json-schema-draft-06.json'; 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 }); export const ajv = new Ajv({
allErrors: true,
coerceTypes: false,
format: "fast",
nullable: true,
unicode: true,
uniqueItems: true,
useDefaults: true,
});
ajv.addMetaSchema(schema); ajv.addMetaSchema(schema);
export const BoardResponseSchema = { export const BoardResponseSchema = {
"$schema": "http://json-schema.org/draft-07/schema#", $schema: "http://json-schema.org/draft-07/schema#",
"additionalProperties": false, additionalProperties: false,
"defaultProperties": [ defaultProperties: [],
], definitions: {
"definitions": { GAME_STATE: {
"GAME_STATE": { enum: ["black", "done", "pending", "white"],
"enum": [ type: "string",
"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"
}, },
"gameState": {
"$ref": "#/definitions/GAME_STATE"
}
}, },
"required": [ properties: {
"boardState", boardState: {
"gameState" items: {
], additionalItems: {
"type": "object" 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 type ValidateFunction<T> = ((data: unknown) => data is T) &
export const isBoardResponse = ajv.compile(BoardResponseSchema) as ValidateFunction<BoardResponse>; Pick<Ajv.ValidateFunction, "errors">;
export const isBoardResponse = ajv.compile(
BoardResponseSchema
) as ValidateFunction<BoardResponse>;
export default function validate(value: unknown): BoardResponse { export default function validate(value: unknown): BoardResponse {
if (isBoardResponse(value)) { if (isBoardResponse(value)) {
return value; return value;
} else { } else {
if (Array.isArray(isBoardResponse.errors)) { if (Array.isArray(isBoardResponse.errors)) {
throw new Error( throw new Error(
ajv.errorsText(isBoardResponse.errors.filter((e: ErrorObject) => e.keyword !== 'if'), { dataVar: 'BoardResponse' }) + ajv.errorsText(
'\n\n' + isBoardResponse.errors.filter((e: ErrorObject) => e.keyword !== "if"),
inspect(value), { dataVar: "BoardResponse" }
) +
"\n\n" +
inspect(value)
); );
} }

View file

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

View file

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

View file

@ -1,9 +1,20 @@
import { COLOR } from "./primitives"; 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 => { 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 => { const canMakeMove = (playerColor: COLOR, gameState: GAME_STATE): boolean => {