Skip to content

Commit

Permalink
chore: pnpm format
Browse files Browse the repository at this point in the history
  • Loading branch information
vincentfrochot committed Nov 13, 2024
1 parent c2d4634 commit 3e65410
Show file tree
Hide file tree
Showing 13 changed files with 472 additions and 503 deletions.
9 changes: 3 additions & 6 deletions src/draw.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,7 @@ import { unselect, cancelMove, getKeyAtDomPos, getSnappedKeyAtDomPos } from './b
import { eventPosition, isRightButton } from './util';
import * as cg from './types';

import {
start as abaloneStart,
processDraw as abaloneProcessDraw
} from './variants/abalone/draw';
import { start as abaloneStart, processDraw as abaloneProcessDraw } from './variants/abalone/draw';

export interface DrawShape {
orig: cg.Key;
Expand Down Expand Up @@ -67,7 +64,7 @@ export interface DrawCurrent {
const brushes = ['green', 'red', 'blue', 'yellow'];

export function start(state: State, e: cg.MouchEvent): void {
if(state.variant === "abalone") {
if (state.variant === 'abalone') {
return abaloneStart(state, e);
}
// support one finger touch only
Expand All @@ -89,7 +86,7 @@ export function start(state: State, e: cg.MouchEvent): void {
}

export function processDraw(state: State): void {
if(state.variant === "abalone") {
if (state.variant === 'abalone') {
return abaloneProcessDraw(state);
}
requestAnimationFrame(() => {
Expand Down
2 changes: 1 addition & 1 deletion src/fen.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ function letters(role: cg.Role) {
}

export function read(fen: cg.FEN, dimensions: cg.BoardDimensions, variant: cg.Variant): cg.Pieces {
if(variant === "abalone") return abaloneRead(fen, dimensions);
if (variant === 'abalone') return abaloneRead(fen, dimensions);
if (fen === 'start') fen = initial;
if (fen.indexOf('[') !== -1) fen = fen.slice(0, fen.indexOf('['));
const pieces: cg.Pieces = new Map();
Expand Down
5 changes: 1 addition & 4 deletions src/premove.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1150,10 +1150,7 @@ export function premove(
break;

case 'abalone':
mobility = abaloneMarble(
pieces,
playerIndex
);
mobility = abaloneMarble(pieces, playerIndex);
break;

// Variants using standard pieces and additional fairy pieces like S-chess, Capablanca, etc.
Expand Down
16 changes: 4 additions & 12 deletions src/svg.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,7 @@ import { Drawable, DrawShape, DrawShapePiece, DrawBrush, DrawBrushes, DrawModifi
import * as cg from './types';
import * as T from './transformations';

import {
renderPiece as abaloneRenderPiece,
renderShape as abaloneRenderShape
} from './variants/abalone/svg';
import { renderPiece as abaloneRenderPiece, renderShape as abaloneRenderShape } from './variants/abalone/svg';

export function createElement(tagName: string): SVGElement {
return document.createElementNS('http://www.w3.org/2000/svg', tagName);
Expand Down Expand Up @@ -190,7 +187,7 @@ function renderShape(
arrowDests: ArrowDests,
bounds: ClientRect,
): SVGElement {
if (state.variant === "abalone") {
if (state.variant === 'abalone') {
return abaloneRenderShape(state, { shape, current, hash }, brushes, arrowDests, bounds);
}
let el: SVGElement;
Expand Down Expand Up @@ -305,13 +302,8 @@ function renderPiece(
myPlayerIndex: cg.PlayerIndex,
variant: cg.Variant,
): SVGElement {
if (variant === "abalone") {
return abaloneRenderPiece(baseUrl,
pos,
piece,
bounds,
bd,
myPlayerIndex);
if (variant === 'abalone') {
return abaloneRenderPiece(baseUrl, pos, piece, bounds, bd, myPlayerIndex);
}
const o = pos2px(pos, bounds, bd),
width = (bounds.width / bd.width) * (piece.scale || 1),
Expand Down
2 changes: 1 addition & 1 deletion src/variants/abalone/README.md
Original file line number Diff line number Diff line change
@@ -1 +1 @@
Ideas from mid-investigation : We need to define our own key2pos and pos2key functions as the grid is an hexagon ?
Ideas from mid-investigation : We need to define our own key2pos and pos2key functions as the grid is an hexagon ?
14 changes: 7 additions & 7 deletions src/variants/abalone/board.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,19 +6,19 @@ import { distanceSq } from '../../util';
import { allPos, computeSquareCenter, key2pos, pos2key } from './util';

export const getKeyAtDomPos = (
pos: cg.NumberPair,
orientation: cg.Orientation,
bounds: ClientRect,
bd: cg.BoardDimensions
): cg.Key | undefined => {
pos: cg.NumberPair,
orientation: cg.Orientation,
bounds: ClientRect,
bd: cg.BoardDimensions,
): cg.Key | undefined => {
let file = Math.ceil(bd.width * ((pos[0] - bounds.left) / bounds.width));
const rank = Math.ceil(bd.height - bd.height * ((pos[1] - bounds.top) / bounds.height));

if (rank === undefined || file === undefined) return undefined;
pos = [file, rank];
pos = T.mapToP1[orientation](pos, bd);
return pos[0] > 0 && pos[0] < bd.width + 1 && pos[1] > 0 && pos[1] < bd.height + 1 ? pos2key(pos) : undefined;
}
};

export const getSnappedKeyAtDomPos = (
orig: cg.Key,
Expand All @@ -38,4 +38,4 @@ export const getSnappedKeyAtDomPos = (
[validSnapDistances[0], 0],
);
return pos2key(validSnapPos[closestSnapIndex]);
}
};
89 changes: 42 additions & 47 deletions src/variants/abalone/draw.ts
Original file line number Diff line number Diff line change
@@ -1,51 +1,46 @@
import { cancelMove, unselect } from "../../board";
import { eventBrush } from "../../draw";
import { State } from "../../state";
import { eventPosition } from "../../util";
import { getKeyAtDomPos, getSnappedKeyAtDomPos } from "./board";
import { cancelMove, unselect } from '../../board';
import { eventBrush } from '../../draw';
import { State } from '../../state';
import { eventPosition } from '../../util';
import { getKeyAtDomPos, getSnappedKeyAtDomPos } from './board';
import * as cg from '../../types';

export const start = (state: State, e: cg.MouchEvent): void => {
// support one finger touch only
if (e.touches && e.touches.length > 1) return;
e.stopPropagation();
e.preventDefault();
e.ctrlKey ? unselect(state) : cancelMove(state);
const pos = eventPosition(e)!,
orig = getKeyAtDomPos(pos, state.orientation, state.dom.bounds(), state.dimensions);
if (!orig) return;
state.drawable.current = {
orig,
pos,
brush: eventBrush(e),
snapToValidMove: state.drawable.defaultSnapToValidMove,
};

processDraw(state);
}

export const processDraw = (state: State): void => {
requestAnimationFrame(() => {
const cur = state.drawable.current;
if (cur) {
const keyAtDomPos = getKeyAtDomPos(
cur.pos,
state.orientation,
state.dom.bounds(),
state.dimensions
);
if (!keyAtDomPos) {
cur.snapToValidMove = false;
}
const mouseSq = cur.snapToValidMove
? getSnappedKeyAtDomPos(cur.orig, cur.pos, state.orientation, state.dom.bounds(), state.dimensions)
: keyAtDomPos;
if (mouseSq !== cur.mouseSq) {
cur.mouseSq = mouseSq;
cur.dest = mouseSq !== cur.orig ? mouseSq : undefined;
state.dom.redrawNow();
}
processDraw(state);
// support one finger touch only
if (e.touches && e.touches.length > 1) return;
e.stopPropagation();
e.preventDefault();
e.ctrlKey ? unselect(state) : cancelMove(state);
const pos = eventPosition(e)!,
orig = getKeyAtDomPos(pos, state.orientation, state.dom.bounds(), state.dimensions);
if (!orig) return;
state.drawable.current = {
orig,
pos,
brush: eventBrush(e),
snapToValidMove: state.drawable.defaultSnapToValidMove,
};

processDraw(state);
};

export const processDraw = (state: State): void => {
requestAnimationFrame(() => {
const cur = state.drawable.current;
if (cur) {
const keyAtDomPos = getKeyAtDomPos(cur.pos, state.orientation, state.dom.bounds(), state.dimensions);
if (!keyAtDomPos) {
cur.snapToValidMove = false;
}
const mouseSq = cur.snapToValidMove
? getSnappedKeyAtDomPos(cur.orig, cur.pos, state.orientation, state.dom.bounds(), state.dimensions)
: keyAtDomPos;
if (mouseSq !== cur.mouseSq) {
cur.mouseSq = mouseSq;
cur.dest = mouseSq !== cur.orig ? mouseSq : undefined;
state.dom.redrawNow();
}
});
}
processDraw(state);
}
});
};
55 changes: 27 additions & 28 deletions src/variants/abalone/fen.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,35 +3,34 @@ import * as cg from '../../types';
import { pos2key } from '../../util';

export const read = (fen: cg.FEN, dimensions: cg.BoardDimensions): cg.Pieces => {
const pieces: cg.Pieces = new Map();

const pieces: cg.Pieces = new Map();
let row: number = dimensions.height;
const padding = [0, 0, 0, 0, 0, 0, 1, 2, 3, 4];
let file = padding[row];

let row: number = dimensions.height;
const padding = [0,0,0,0,0,0,1,2,3,4];
let file = padding[row];

for (let i = 0; i < fen.length; i++) {
const c = fen[i];
if (c === ' ') break
else if (c == '/') {
file = padding[row - 1];
--row;
} else {
const step = parseInt(c, 10);
if (step > 0) {
file += step;
} else {
file++;
const letter = c.toLowerCase();
const playerIndex = (c === letter ? 'p2' : 'p1') as cg.PlayerIndex;
const piece = {
role: roles(letter),
playerIndex: playerIndex,
} as cg.Piece;
pieces.set(pos2key([file, row]), piece); // @TODO VFR: check all cases are correctly handled to prevent a js error in console
}
}
for (let i = 0; i < fen.length; i++) {
const c = fen[i];
if (c === ' ') break;
else if (c == '/') {
file = padding[row - 1];
--row;
} else {
const step = parseInt(c, 10);
if (step > 0) {
file += step;
} else {
file++;
const letter = c.toLowerCase();
const playerIndex = (c === letter ? 'p2' : 'p1') as cg.PlayerIndex;
const piece = {
role: roles(letter),
playerIndex: playerIndex,
} as cg.Piece;
pieces.set(pos2key([file, row]), piece); // @TODO VFR: check all cases are correctly handled to prevent a js error in console
}
}
}

return pieces;
}
return pieces;
};
12 changes: 6 additions & 6 deletions src/variants/abalone/premove.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@ import { pos2key } from './util';

// @VFR: TODO: update this, the code is just a wrong copy paste
export const marble = (pieces: cg.Pieces, playerIndex: cg.PlayerIndex): Mobility => {
return (x1, y1 /*, x2, y2*/) => {
const pos = pos2key([x1, y1 + (playerIndex === 'p1' ? 1 : -1)]) as cg.Key;
if (pieces.has(pos) && pieces.get(pos)?.playerIndex === playerIndex) return false;
return false;
}
}
return (x1, y1 /*, x2, y2*/) => {
const pos = pos2key([x1, y1 + (playerIndex === 'p1' ? 1 : -1)]) as cg.Key;
if (pieces.has(pos) && pieces.get(pos)?.playerIndex === playerIndex) return false;
return false;
};
};
Loading

0 comments on commit 3e65410

Please sign in to comment.