From 04fcf35079d91ee72d50be755441d9afc109ae37 Mon Sep 17 00:00:00 2001 From: Naviary Alt Date: Tue, 31 Dec 2024 17:46:06 -0700 Subject: [PATCH 1/4] Converted coordutil.js to typescript (still need to update imports, and have scripts import the Coords type definition) --- .../scripts/esm/chess/util/coordutil.js | 92 -------------- .../scripts/esm/chess/util/coordutil.ts | 114 ++++++++++++++++++ 2 files changed, 114 insertions(+), 92 deletions(-) delete mode 100644 src/client/scripts/esm/chess/util/coordutil.js create mode 100644 src/client/scripts/esm/chess/util/coordutil.ts diff --git a/src/client/scripts/esm/chess/util/coordutil.js b/src/client/scripts/esm/chess/util/coordutil.js deleted file mode 100644 index b194eea2..00000000 --- a/src/client/scripts/esm/chess/util/coordutil.js +++ /dev/null @@ -1,92 +0,0 @@ - -/** - * This script contains utility methods for working with coordinates [x,y]. - * - * ZERO dependancies. - */ - -/** - * Checks if both the x-coordinate and the y-coordinate of a point are integers. - * @param {number} x - The x-coordinate of the point. - * @param {number} y - The y-coordinate of the point. - * @returns {boolean} - Returns true if both coordinates are integers, otherwise false. - */ -function areCoordsIntegers(coords) { - return Number.isInteger(coords[0]) && Number.isInteger(coords[1]); -} - -/** - * Returns the key string of the coordinates: [x,y] => 'x,y' - * @param {number[]} coords - The coordinates - * @returns {string} The key - */ -function getKeyFromCoords(coords) { - return `${coords[0]},${coords[1]}`; -} - -/** - * Returns a length-2 array of the provided coordinates - * @param {string} key - 'x,y' - * @return {[number,number]} The coordinates of the piece, [x,y] - */ -function getCoordsFromKey(key) { - return key.split(',').map(Number); -} - -/** - * Returns true if the coordinates are equal - * @param {number[]} coord1 [x,y] - * @param {number[]} coord2 [x,y] - * @returns {boolean} Whether the coordinates are equal - */ -function areCoordsEqual(coord1, coord2) { - if (!coord1 || !coord2) return false; // One undefined, can't be equal - return coord1[0] === coord2[0] && coord1[1] === coord2[1]; -} - -function areCoordsEqual_noValidate(coord1, coord2) { - return coord1[0] === coord2[0] && coord1[1] === coord2[1]; -} - -/** - * Adds two coordinate pairs together component-wise. - * - * @param {number[]} coord1 - The first coordinate pair [x1, y1]. - * @param {number[]} coord2 - The second coordinate pair [x2, y2]. - * @returns {number[]} The resulting coordinate pair after addition [x1 + x2, y1 + y2]. - */ -function addCoordinates(coord1, coord2) { - return [coord1[0] + coord2[0], coord1[1] + coord2[1]]; -} - -/** - * Adds two coordinate pairs together component-wise. - * - * @param {number[]} minuendCoord - The first coordinate pair [x1, y1] to start with. - * @param {number[]} subtrahendCoord - The second coordinate pair [x2, y2] to subtract from the minuend. - * @returns {[number,number]} The resulting coordinate pair after subtracting [x1 - x2, y1 - y2]. - */ -function subtractCoordinates(minuendCoord, subtrahendCoord) { - return [minuendCoord[0] - subtrahendCoord[0], minuendCoord[1] - subtrahendCoord[1]]; -} - -/** - * Makes a deep copy of the provided coordinates - * @param {number[]} coords - [x,y] - * @returns Copied coords - */ -function copyCoords(coords) { - return [coords[0], coords[1]]; -} - - -export default { - areCoordsIntegers, - getKeyFromCoords, - getCoordsFromKey, - areCoordsEqual, - areCoordsEqual_noValidate, - addCoordinates, - subtractCoordinates, - copyCoords -}; \ No newline at end of file diff --git a/src/client/scripts/esm/chess/util/coordutil.ts b/src/client/scripts/esm/chess/util/coordutil.ts new file mode 100644 index 00000000..7e52f912 --- /dev/null +++ b/src/client/scripts/esm/chess/util/coordutil.ts @@ -0,0 +1,114 @@ + +/** + * This script contains utility methods for working with coordinates [x,y]. + * + * ZERO dependancies. + */ + + +// Type Definitions ------------------------------------------------------------ + + +/** A length-2 array of coordinates: `[x,y]` */ +type Coords = [number,number]; + +/** + * A pair of coordinates, represented in a string, separated by a `,`. + * + * This is often used as the key for a piece in piece lists. + * + * This is NOT compatible with "e" notation, so things will crash when + * coordinates go above Number.MAX_SAFE_INTEGER + */ +type CoordsKey = `${number},${number}`; + + +// Functions ------------------------------------------------------------------- + + +/** + * Checks if both the x-coordinate and the y-coordinate of a point are integers. + */ +function areCoordsIntegers(coords: Coords): boolean { + return Number.isInteger(coords[0]) && Number.isInteger(coords[1]); +} + +/** + * Returns the key string of the coordinates: [x,y] => 'x,y' + */ +function getKeyFromCoords(coords: Coords): CoordsKey { + return `${coords[0]},${coords[1]}`; +} + +/** + * Returns a length-2 array of the provided coordinates + * @param key - 'x,y' + * @returns The coordinates of the piece, [x,y] + */ +function getCoordsFromKey(key: CoordsKey): Coords { + return key.split(',').map(Number) as Coords; +} + +/** + * Returns true if the coordinates are equal. + * + * If one coordinate isn't provided, they are considered not equal. + */ +function areCoordsEqual(coord1?: Coords, coord2?: Coords): boolean { + if (!coord1 || !coord2) return false; // One undefined, can't be equal + return coord1[0] === coord2[0] && coord1[1] === coord2[1]; +} + +/** + * Returns true if the coordinates are equal + */ +function areCoordsEqual_noValidate(coord1: Coords, coord2: Coords): boolean { + return coord1[0] === coord2[0] && coord1[1] === coord2[1]; +} + +/** + * Adds two coordinate pairs together component-wise. + */ +function addCoordinates(coord1: Coords, coord2: Coords): Coords { + return [ + coord1[0] + coord2[0], + coord1[1] + coord2[1] + ]; +} + +/** + * Subtracts two coordinate pairs together component-wise. + * @param minuendCoord - The first coordinate pair [x1, y1] to start with. + * @param subtrahendCoord - The second coordinate pair [x2, y2] to subtract from the minuend. + * @returns The resulting coordinate pair after subtracting. + */ +function subtractCoordinates(minuendCoord: Coords, subtrahendCoord: Coords): Coords { + return [ + minuendCoord[0] - subtrahendCoord[0], + minuendCoord[1] - subtrahendCoord[1] + ]; +} + +/** + * Makes a deep copy of the provided coordinates + */ +function copyCoords(coords: Coords): Coords { + return [...coords] as Coords; +} + + + +export default { + areCoordsIntegers, + getKeyFromCoords, + getCoordsFromKey, + areCoordsEqual, + areCoordsEqual_noValidate, + addCoordinates, + subtractCoordinates, + copyCoords +}; + +export type { + Coords +} \ No newline at end of file From 46f5b875b0b292b014b6b5967caaf7c9e1a9557c Mon Sep 17 00:00:00 2001 From: Naviary Alt Date: Tue, 31 Dec 2024 17:48:22 -0700 Subject: [PATCH 2/4] Removed unneeded ts ignore lines when importing coordutil.ts --- .../scripts/esm/chess/variants/fivedimensionalgenerator.ts | 1 - .../esm/game/rendering/highlights/legalmovehighlights.ts | 3 +-- 2 files changed, 1 insertion(+), 3 deletions(-) diff --git a/src/client/scripts/esm/chess/variants/fivedimensionalgenerator.ts b/src/client/scripts/esm/chess/variants/fivedimensionalgenerator.ts index c21a41f7..a891d5fd 100644 --- a/src/client/scripts/esm/chess/variants/fivedimensionalgenerator.ts +++ b/src/client/scripts/esm/chess/variants/fivedimensionalgenerator.ts @@ -1,7 +1,6 @@ /* eslint-disable max-depth */ // @ts-ignore import formatconverter from "../logic/formatconverter.js"; -// @ts-ignore import coordutil from "../util/coordutil.js"; // Type definitions... diff --git a/src/client/scripts/esm/game/rendering/highlights/legalmovehighlights.ts b/src/client/scripts/esm/game/rendering/highlights/legalmovehighlights.ts index 7415a86c..ae60b18f 100644 --- a/src/client/scripts/esm/game/rendering/highlights/legalmovehighlights.ts +++ b/src/client/scripts/esm/game/rendering/highlights/legalmovehighlights.ts @@ -22,8 +22,6 @@ import board from '../board.js'; // @ts-ignore import math, { BoundingBox } from '../../../util/math.js'; // @ts-ignore -import coordutil from '../../../chess/util/coordutil.js'; -// @ts-ignore import frametracker from '../frametracker.js'; // @ts-ignore import preferences from '../../../components/header/preferences.js'; @@ -34,6 +32,7 @@ import legalmoveshapes from '../legalmoveshapes.js'; // @ts-ignore import shapes from '../shapes.js'; import { BufferModel, BufferModelInstanced, createModel, createModel_Instanced } from '../buffermodel.js'; +import coordutil from '../../../chess/util/coordutil.js'; // Type Definitions ----------------------------------------------------------------------------- From 2bb6c31efbf104d3aecd57b36ede6dfedfa29d6a Mon Sep 17 00:00:00 2001 From: Naviary Alt Date: Tue, 31 Dec 2024 17:55:44 -0700 Subject: [PATCH 3/4] Scripts now import the coordutil.ts Coords type instead of declaring their own --- src/client/scripts/esm/chess/logic/movesets.ts | 3 +-- .../scripts/esm/game/rendering/highlights/checkhighlight.ts | 4 +--- src/client/scripts/esm/game/rendering/legalmoveshapes.ts | 4 +--- 3 files changed, 3 insertions(+), 8 deletions(-) diff --git a/src/client/scripts/esm/chess/logic/movesets.ts b/src/client/scripts/esm/chess/logic/movesets.ts index 33c340e6..9cce72fc 100644 --- a/src/client/scripts/esm/chess/logic/movesets.ts +++ b/src/client/scripts/esm/chess/logic/movesets.ts @@ -16,9 +16,8 @@ import colorutil from '../util/colorutil.js'; import type { gamefile } from './gamefile.js'; // @ts-ignore import type { Piece } from './movepiece.js'; +import type { Coords } from '../util/coordutil.js'; -// TODO: move this to coordutil.js after that is converted to typescript. -type Coords = [number, number]; /** * A Movesets object containing the movesets for every piece type in a game diff --git a/src/client/scripts/esm/game/rendering/highlights/checkhighlight.ts b/src/client/scripts/esm/game/rendering/highlights/checkhighlight.ts index 5c705694..7c1f60a5 100644 --- a/src/client/scripts/esm/game/rendering/highlights/checkhighlight.ts +++ b/src/client/scripts/esm/game/rendering/highlights/checkhighlight.ts @@ -22,9 +22,7 @@ import { BufferModel, createModel } from '../buffermodel.js'; // @ts-ignore import type gamefile from '../../../chess/logic/gamefile.js'; - -// TO DO: MOVE TO coordutil.ts ONCE THAT'S CONVERTED TO TS -type Coords = [number,number]; +import type { Coords } from '../../../chess/util/coordutil.js'; // Functions ----------------------------------------------------------------------- diff --git a/src/client/scripts/esm/game/rendering/legalmoveshapes.ts b/src/client/scripts/esm/game/rendering/legalmoveshapes.ts index 23a3fe94..a099d591 100644 --- a/src/client/scripts/esm/game/rendering/legalmoveshapes.ts +++ b/src/client/scripts/esm/game/rendering/legalmoveshapes.ts @@ -5,9 +5,7 @@ import board from "./board.js"; import shapes from "./shapes.js"; - -type Coords = [number,number]; - +import type { Coords } from "../../chess/util/coordutil.js"; // Variables ------------------------------------------------------------------------------ From c9cd1cc9f80028cbb00b4a11cf8cda271d760290 Mon Sep 17 00:00:00 2001 From: Naviary2 Date: Tue, 31 Dec 2024 22:36:58 -0700 Subject: [PATCH 4/4] Fixed crashes --- src/client/scripts/esm/chess/util/coordutil.ts | 5 +++-- .../esm/chess/variants/fivedimensionalgenerator.ts | 12 ++++++------ .../game/rendering/highlights/legalmovehighlights.ts | 5 ++--- 3 files changed, 11 insertions(+), 11 deletions(-) diff --git a/src/client/scripts/esm/chess/util/coordutil.ts b/src/client/scripts/esm/chess/util/coordutil.ts index 7e52f912..ec0df907 100644 --- a/src/client/scripts/esm/chess/util/coordutil.ts +++ b/src/client/scripts/esm/chess/util/coordutil.ts @@ -110,5 +110,6 @@ export default { }; export type { - Coords -} \ No newline at end of file + Coords, + CoordsKey, +}; \ No newline at end of file diff --git a/src/client/scripts/esm/chess/variants/fivedimensionalgenerator.ts b/src/client/scripts/esm/chess/variants/fivedimensionalgenerator.ts index a891d5fd..57691e05 100644 --- a/src/client/scripts/esm/chess/variants/fivedimensionalgenerator.ts +++ b/src/client/scripts/esm/chess/variants/fivedimensionalgenerator.ts @@ -7,11 +7,10 @@ import coordutil from "../util/coordutil.js"; import type { Movesets } from "../logic/movesets.js"; import type { Position } from "./variant.js"; +import type { Coords, CoordsKey } from "../util/coordutil.js"; -'use strict'; - const BOARDS_X = 8; const BOARDS_Y = 8; @@ -30,8 +29,7 @@ function genPositionOfFiveDimensional() { const standardPosStr = 'P1,2+|P2,2+|P3,2+|P4,2+|P5,2+|P6,2+|P7,2+|P8,2+|p1,7+|p2,7+|p3,7+|p4,7+|p5,7+|p6,7+|p7,7+|p8,7+|R1,1+|R8,1+|r1,8+|r8,8+|N2,1|N7,1|n2,8|n7,8|B3,1|B6,1|b3,8|b6,8|Q4,1|q4,8|K5,1+|k5,8+'; // Store the standard position so we can reference it later - const standardPos = formatconverter.ShortToLong_Format(standardPosStr).startingPosition; - + const standardPos: { [coordsKey: CoordsKey]: string } = formatconverter.ShortToLong_Format(standardPosStr).startingPosition; const resultPos: Position = {}; // Loop through from the leftmost column that should be voids to the right most, and also vertically @@ -45,9 +43,11 @@ function genPositionOfFiveDimensional() { if ((i % BOARD_SPACING === 0) && (j % BOARD_SPACING) === 0 && i !== MAX_X && j !== MAX_Y) { for (const square in standardPos) { - resultPos[coordutil.getKeyFromCoords(coordutil.getCoordsFromKey(square).map((value: number, index: number) => { + const coords = coordutil.getCoordsFromKey(square as CoordsKey); + const key = coordutil.getKeyFromCoords(coords.map((value: number, index: number) => { return value + [i, j][index]!; - }))] = standardPos[square]; + }) as Coords); + resultPos[key] = standardPos[square as CoordsKey]!; } } } diff --git a/src/client/scripts/esm/game/rendering/highlights/legalmovehighlights.ts b/src/client/scripts/esm/game/rendering/highlights/legalmovehighlights.ts index ae60b18f..177017c8 100644 --- a/src/client/scripts/esm/game/rendering/highlights/legalmovehighlights.ts +++ b/src/client/scripts/esm/game/rendering/highlights/legalmovehighlights.ts @@ -46,9 +46,8 @@ import type { LegalMoves } from '../../chess/selection.js'; import type { Piece } from '../../../chess/logic/movepiece.js'; // @ts-ignore import game from '../../chess/game.js'; +import { Coords, CoordsKey } from '../../../chess/util/coordutil.js'; -// TO DO: MOVE TO coordutil.ts ONCE THAT'S CONVERTED TO TS -type Coords = [number,number]; // TO DO: MOVE TO colorutil.ts ONCE THAT'S CONVERTED TO TS type Color = [number,number,number,number]; @@ -429,7 +428,7 @@ function concatData_HighlightedMoves_Sliding(instanceData_NonCapture: number[], const slideLines = Object.keys(legalMoves.sliding); // ['1,0','1,1', ...] for (const lineKey of slideLines) { // '1,0' - const line: Coords = coordutil.getCoordsFromKey(lineKey); // [dx,dy] + const line: Coords = coordutil.getCoordsFromKey(lineKey as CoordsKey); // [dx,dy] const C = organizedlines.getCFromLine(line, coords); const corner1 = math.getAABBCornerOfLine(line, true); // "right"