@vnejs/plugins.controllers.screens.checkers 0.0.0-stage → 0.2.12
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/index.d.ts +4 -0
- package/dist/index.js +28 -0
- package/dist/modules/board.d.ts +10 -0
- package/dist/modules/board.js +32 -0
- package/dist/modules/bridge.d.ts +8 -0
- package/dist/modules/bridge.js +10 -0
- package/dist/modules/click.d.ts +9 -0
- package/dist/modules/click.js +14 -0
- package/dist/modules/controller.accept.d.ts +12 -0
- package/dist/modules/controller.accept.js +18 -0
- package/dist/modules/controller.d.ts +14 -0
- package/dist/modules/controller.grid.d.ts +20 -0
- package/dist/modules/controller.grid.js +25 -0
- package/dist/modules/controller.js +12 -0
- package/dist/modules/player.interact.d.ts +9 -0
- package/dist/modules/player.interact.js +33 -0
- package/dist/modules/player.select.d.ts +9 -0
- package/dist/modules/player.select.js +29 -0
- package/dist/modules/status.d.ts +18 -0
- package/dist/modules/status.js +24 -0
- package/dist/modules/view.d.ts +10 -0
- package/dist/modules/view.js +9 -0
- package/dist/types.d.ts +13 -0
- package/dist/types.js +1 -0
- package/dist/utils/checkers.d.ts +49 -0
- package/dist/utils/checkers.js +47 -0
- package/dist/view/index.d.ts +3 -0
- package/dist/view/index.js +187 -0
- package/package.json +67 -4
- package/src/index.ts +32 -0
- package/src/modules/board.ts +49 -0
- package/src/modules/bridge.ts +21 -0
- package/src/modules/click.ts +31 -0
- package/src/modules/controller.accept.ts +35 -0
- package/src/modules/controller.grid.ts +40 -0
- package/src/modules/controller.ts +25 -0
- package/src/modules/player.interact.ts +50 -0
- package/src/modules/player.select.ts +44 -0
- package/src/modules/status.ts +46 -0
- package/src/modules/view.ts +25 -0
- package/src/tests/player.interact.test.ts +59 -0
- package/src/tests/setup.ts +49 -0
- package/src/types.ts +41 -0
- package/src/utils/checkers.ts +110 -0
- package/src/view/index.tsx +287 -0
- package/tsconfig.json +10 -0
- package/README.md +0 -3
|
@@ -0,0 +1,59 @@
|
|
|
1
|
+
import { afterEach, beforeEach, describe, expect, it, vi } from "vitest";
|
|
2
|
+
|
|
3
|
+
import { stubEvent } from "@vnejs/test-utils";
|
|
4
|
+
|
|
5
|
+
import { SUBSCRIBE_EVENTS as CHECKERS_VIEW_EVENTS } from "@vnejs/contracts.controllers.screens.checkers";
|
|
6
|
+
import { SUBSCRIBE_EVENTS as CHECKERS_EVENTS } from "@vnejs/contracts.game.checkers";
|
|
7
|
+
|
|
8
|
+
import { CheckersViewPlayerInteract } from "../modules/player.interact.js";
|
|
9
|
+
|
|
10
|
+
import { BOARD_SIZE, type CheckersBoard, type CheckersPiece } from "../utils/checkers.js";
|
|
11
|
+
|
|
12
|
+
import { createCheckersViewTestModule, registerCheckersViewPluginVne } from "./setup.js";
|
|
13
|
+
|
|
14
|
+
const emptyBoard = (): CheckersBoard => Array.from({ length: BOARD_SIZE }, () => Array.from({ length: BOARD_SIZE }, () => null));
|
|
15
|
+
|
|
16
|
+
const piece = (id: string, color: CheckersPiece["color"]): CheckersPiece => ({ id, color, king: false });
|
|
17
|
+
|
|
18
|
+
/** White at (5,2): (4,1) hangs to black at (3,0); (4,3) is safe. */
|
|
19
|
+
const hangingScenarioBoard = (): CheckersBoard => {
|
|
20
|
+
const board = emptyBoard();
|
|
21
|
+
board[5]![2] = piece("w1", "white");
|
|
22
|
+
board[3]![0] = piece("b1", "black");
|
|
23
|
+
return board;
|
|
24
|
+
};
|
|
25
|
+
|
|
26
|
+
describe("checkers_view player.interact", () => {
|
|
27
|
+
beforeEach(() => {
|
|
28
|
+
registerCheckersViewPluginVne();
|
|
29
|
+
});
|
|
30
|
+
|
|
31
|
+
afterEach(() => {
|
|
32
|
+
vi.restoreAllMocks();
|
|
33
|
+
});
|
|
34
|
+
|
|
35
|
+
it("PLAYER_INTERACT selects piece and emits PLAYER_SELECT", async () => {
|
|
36
|
+
const board = hangingScenarioBoard();
|
|
37
|
+
const { module, observer } = createCheckersViewTestModule(CheckersViewPlayerInteract, { subscribe: false });
|
|
38
|
+
const interact = module as CheckersViewPlayerInteract;
|
|
39
|
+
interact.updateState({ board, turn: "white", selected: null, legalMoves: [] });
|
|
40
|
+
|
|
41
|
+
const playerSelect = vi.fn(() => null);
|
|
42
|
+
stubEvent(observer, CHECKERS_VIEW_EVENTS.PLAYER_SELECT, playerSelect);
|
|
43
|
+
stubEvent(observer, CHECKERS_EVENTS.MOVES_GET, () => ({
|
|
44
|
+
moves: [
|
|
45
|
+
{ from: { row: 5, column: 2 }, to: { row: 4, column: 1 }, captures: [] },
|
|
46
|
+
{ from: { row: 5, column: 2 }, to: { row: 4, column: 3 }, captures: [] },
|
|
47
|
+
],
|
|
48
|
+
hanging: [],
|
|
49
|
+
safe: [],
|
|
50
|
+
}));
|
|
51
|
+
|
|
52
|
+
await interact.onPlayerInteract({ row: 5, column: 2 });
|
|
53
|
+
|
|
54
|
+
expect(playerSelect).toHaveBeenCalledOnce();
|
|
55
|
+
const args = playerSelect.mock.calls[0]?.[0] as { selected: { row: number; column: number }; legalMoves: unknown[] };
|
|
56
|
+
expect(args.selected).toEqual({ row: 5, column: 2 });
|
|
57
|
+
expect(args.legalMoves).toHaveLength(2);
|
|
58
|
+
});
|
|
59
|
+
});
|
|
@@ -0,0 +1,49 @@
|
|
|
1
|
+
import { SourcesSet } from "@vnejs/sources-set";
|
|
2
|
+
import { createTestModule as baseCreateTestModule, registerCoreVne, registerVnePlugin, stubSilentEvent } from "@vnejs/test-utils";
|
|
3
|
+
|
|
4
|
+
import { CONSTANTS as AI_CONST, PARAMS as AI_PARAMS, PLUGIN_NAME as AI, SUBSCRIBE_EVENTS as AI_EVENTS } from "@vnejs/contracts.ai";
|
|
5
|
+
import { PLUGIN_NAME as GRID, SUBSCRIBE_EVENTS as GRID_EVENTS } from "@vnejs/contracts.common.grid";
|
|
6
|
+
import { PARAMS, PLUGIN_NAME, SUBSCRIBE_EVENTS } from "@vnejs/contracts.controllers.screens.checkers";
|
|
7
|
+
import { CONSTANTS as CONTROLS_CONST, PLUGIN_NAME as CONTROLS, SUBSCRIBE_EVENTS as CONTROLS_EVENTS } from "@vnejs/contracts.controls";
|
|
8
|
+
import {
|
|
9
|
+
CONSTANTS as CHECKERS_CONST,
|
|
10
|
+
PARAMS as CHECKERS_PARAMS,
|
|
11
|
+
PLUGIN_NAME as CHECKERS,
|
|
12
|
+
SUBSCRIBE_EVENTS as CHECKERS_EVENTS,
|
|
13
|
+
} from "@vnejs/contracts.game.checkers";
|
|
14
|
+
|
|
15
|
+
export const registerCheckersViewPluginVne = () => {
|
|
16
|
+
registerCoreVne();
|
|
17
|
+
registerVnePlugin(CONTROLS, { constants: CONTROLS_CONST, events: CONTROLS_EVENTS });
|
|
18
|
+
registerVnePlugin(AI, { constants: AI_CONST, events: AI_EVENTS, params: AI_PARAMS });
|
|
19
|
+
registerVnePlugin(CHECKERS, { constants: CHECKERS_CONST, events: CHECKERS_EVENTS, params: CHECKERS_PARAMS });
|
|
20
|
+
registerVnePlugin(GRID, { events: GRID_EVENTS });
|
|
21
|
+
registerVnePlugin(PLUGIN_NAME, { events: SUBSCRIBE_EVENTS, params: PARAMS });
|
|
22
|
+
};
|
|
23
|
+
|
|
24
|
+
export const createCheckersViewTestModule = <T extends Parameters<typeof baseCreateTestModule>[0]>(
|
|
25
|
+
ModuleClass: T,
|
|
26
|
+
options: Parameters<typeof baseCreateTestModule>[1] = {},
|
|
27
|
+
) => {
|
|
28
|
+
const result = baseCreateTestModule(ModuleClass, {
|
|
29
|
+
...options,
|
|
30
|
+
shared: {
|
|
31
|
+
checkersInteractDisableSources: new SourcesSet(),
|
|
32
|
+
...(options.shared ?? {}),
|
|
33
|
+
},
|
|
34
|
+
});
|
|
35
|
+
|
|
36
|
+
stubSilentEvent(result.observer, SUBSCRIBE_EVENTS.SHOW);
|
|
37
|
+
stubSilentEvent(result.observer, SUBSCRIBE_EVENTS.HIDE);
|
|
38
|
+
stubSilentEvent(result.observer, SUBSCRIBE_EVENTS.UPDATE);
|
|
39
|
+
stubSilentEvent(result.observer, SUBSCRIBE_EVENTS.UPDATE_FAST);
|
|
40
|
+
stubSilentEvent(result.observer, SUBSCRIBE_EVENTS.UPDATE_STATE);
|
|
41
|
+
stubSilentEvent(result.observer, SUBSCRIBE_EVENTS.GRID_UPDATE_ITEMS);
|
|
42
|
+
stubSilentEvent(result.observer, GRID_EVENTS.SET);
|
|
43
|
+
stubSilentEvent(result.observer, GRID_EVENTS.CHANGED);
|
|
44
|
+
stubSilentEvent(result.observer, CHECKERS_EVENTS.FINISH);
|
|
45
|
+
|
|
46
|
+
return result;
|
|
47
|
+
};
|
|
48
|
+
|
|
49
|
+
export { SUBSCRIBE_EVENTS, PARAMS, PLUGIN_NAME, CONTROLS_EVENTS, CHECKERS_EVENTS, GRID_EVENTS };
|
package/src/types.ts
ADDED
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
import type { ModuleComponentsConstants, ModuleComponentsEvents, ModuleComponentsParams, ModuleComponentsSettings } from "@vnejs/module.components";
|
|
2
|
+
|
|
3
|
+
import type { Constants as AiConstants, Params as AiParams, PluginName as AiPluginName, SubscribeEvents as AiSubscribeEvents } from "@vnejs/contracts.ai";
|
|
4
|
+
import type { PluginName as GridPluginName, SubscribeEvents as GridSubscribeEvents } from "@vnejs/contracts.common.grid";
|
|
5
|
+
import type { PluginName as TurnsPluginName, SubscribeEvents as TurnsSubscribeEvents } from "@vnejs/contracts.common.turn";
|
|
6
|
+
import type { Params, PluginName, SubscribeEvents } from "@vnejs/contracts.controllers.screens.checkers";
|
|
7
|
+
import type { Constants as ControlsConstants, PluginName as ControlsPluginName } from "@vnejs/contracts.controls";
|
|
8
|
+
import type { PluginName as LogsPluginName, SubscribeEvents as LogsSubscribeEvents } from "@vnejs/contracts.core.logs";
|
|
9
|
+
import type {
|
|
10
|
+
Constants as ScenarioConstants,
|
|
11
|
+
PluginName as ScenarioPluginName,
|
|
12
|
+
SubscribeEvents as ScenarioSubscribeEvents,
|
|
13
|
+
} from "@vnejs/contracts.core.scenario";
|
|
14
|
+
import type {
|
|
15
|
+
Constants as CheckersConstants,
|
|
16
|
+
Params as CheckersParams,
|
|
17
|
+
PluginName as CheckersPluginName,
|
|
18
|
+
SubscribeEvents as CheckersSubscribeEvents,
|
|
19
|
+
} from "@vnejs/contracts.game.checkers";
|
|
20
|
+
|
|
21
|
+
export type CheckersViewPluginEvents = ModuleComponentsEvents &
|
|
22
|
+
Record<PluginName, SubscribeEvents> &
|
|
23
|
+
Record<CheckersPluginName, CheckersSubscribeEvents> &
|
|
24
|
+
Record<GridPluginName, GridSubscribeEvents> &
|
|
25
|
+
Record<TurnsPluginName, TurnsSubscribeEvents> &
|
|
26
|
+
Record<AiPluginName, AiSubscribeEvents> &
|
|
27
|
+
Record<ScenarioPluginName, ScenarioSubscribeEvents> &
|
|
28
|
+
Record<LogsPluginName, LogsSubscribeEvents>;
|
|
29
|
+
|
|
30
|
+
export type CheckersViewPluginConstants = ModuleComponentsConstants &
|
|
31
|
+
Record<CheckersPluginName, CheckersConstants> &
|
|
32
|
+
Record<AiPluginName, AiConstants> &
|
|
33
|
+
Record<ControlsPluginName, ControlsConstants> &
|
|
34
|
+
Record<ScenarioPluginName, ScenarioConstants>;
|
|
35
|
+
|
|
36
|
+
export type CheckersViewPluginSettings = ModuleComponentsSettings;
|
|
37
|
+
|
|
38
|
+
export type CheckersViewPluginParams = ModuleComponentsParams &
|
|
39
|
+
Record<PluginName, Params> &
|
|
40
|
+
Record<CheckersPluginName, CheckersParams> &
|
|
41
|
+
Record<AiPluginName, AiParams>;
|
|
@@ -0,0 +1,110 @@
|
|
|
1
|
+
import type { GridChangedPayload } from "@vnejs/contracts.common.grid";
|
|
2
|
+
import type { CheckersMovePayload, CheckersPosPayload } from "@vnejs/contracts.game.checkers";
|
|
3
|
+
|
|
4
|
+
export const BOARD_SIZE = 8;
|
|
5
|
+
|
|
6
|
+
export type CheckersColor = "white" | "black";
|
|
7
|
+
|
|
8
|
+
export type CheckersPiece = {
|
|
9
|
+
id: string;
|
|
10
|
+
color: CheckersColor;
|
|
11
|
+
king: boolean;
|
|
12
|
+
};
|
|
13
|
+
|
|
14
|
+
export type CheckersPos = CheckersPosPayload;
|
|
15
|
+
|
|
16
|
+
export type CheckersMove = CheckersMovePayload;
|
|
17
|
+
|
|
18
|
+
export type CheckersBoard = (CheckersPiece | null)[][];
|
|
19
|
+
|
|
20
|
+
export type CheckersGridItem = {
|
|
21
|
+
row: number;
|
|
22
|
+
column: number;
|
|
23
|
+
isDark: boolean;
|
|
24
|
+
piece: CheckersPiece | null;
|
|
25
|
+
isSelected?: boolean;
|
|
26
|
+
isLegalTarget?: boolean;
|
|
27
|
+
isFocus?: boolean;
|
|
28
|
+
};
|
|
29
|
+
|
|
30
|
+
export type CheckersViewState = {
|
|
31
|
+
isShow: boolean;
|
|
32
|
+
isForce: boolean;
|
|
33
|
+
board?: CheckersBoard;
|
|
34
|
+
turn?: CheckersColor;
|
|
35
|
+
selected?: CheckersPos | null;
|
|
36
|
+
legalMoves?: CheckersMove[];
|
|
37
|
+
winner?: CheckersColor | null;
|
|
38
|
+
statusText?: string;
|
|
39
|
+
gridRow?: number | null;
|
|
40
|
+
gridColumn?: number | null;
|
|
41
|
+
gridItems?: CheckersGridItem[];
|
|
42
|
+
gridIndex?: number;
|
|
43
|
+
gridsCount?: number;
|
|
44
|
+
};
|
|
45
|
+
|
|
46
|
+
export const isDarkSquare = (row: number, column: number) => (row + column) % 2 === 1;
|
|
47
|
+
|
|
48
|
+
export const samePos = (a: CheckersPos, b: CheckersPos) => a.row === b.row && a.column === b.column;
|
|
49
|
+
|
|
50
|
+
export const buildGridItems = ({
|
|
51
|
+
board,
|
|
52
|
+
selected = null,
|
|
53
|
+
legalMoves = [],
|
|
54
|
+
gridRow = null,
|
|
55
|
+
gridColumn = null,
|
|
56
|
+
}: {
|
|
57
|
+
board: CheckersBoard;
|
|
58
|
+
selected?: CheckersPos | null;
|
|
59
|
+
legalMoves?: CheckersMove[];
|
|
60
|
+
gridRow?: number | null;
|
|
61
|
+
gridColumn?: number | null;
|
|
62
|
+
}): CheckersGridItem[] => {
|
|
63
|
+
const legalKeys = new Set(legalMoves.map((move) => `${move.to.row}:${move.to.column}`));
|
|
64
|
+
const items: CheckersGridItem[] = [];
|
|
65
|
+
|
|
66
|
+
for (let row = 0; row < BOARD_SIZE; row++) {
|
|
67
|
+
for (let column = 0; column < BOARD_SIZE; column++) {
|
|
68
|
+
items.push({
|
|
69
|
+
row,
|
|
70
|
+
column,
|
|
71
|
+
isDark: isDarkSquare(row, column),
|
|
72
|
+
piece: board[row]?.[column] ?? null,
|
|
73
|
+
isSelected: selected?.row === row && selected?.column === column,
|
|
74
|
+
isLegalTarget: legalKeys.has(`${row}:${column}`),
|
|
75
|
+
isFocus: gridRow === row && gridColumn === column,
|
|
76
|
+
});
|
|
77
|
+
}
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
return items;
|
|
81
|
+
};
|
|
82
|
+
|
|
83
|
+
export const listBoardPieces = (board: CheckersBoard) => {
|
|
84
|
+
const pieces: Array<CheckersPiece & CheckersPos> = [];
|
|
85
|
+
|
|
86
|
+
for (let row = 0; row < BOARD_SIZE; row++) {
|
|
87
|
+
for (let column = 0; column < BOARD_SIZE; column++) {
|
|
88
|
+
const piece = board[row]?.[column];
|
|
89
|
+
if (!piece) continue;
|
|
90
|
+
pieces.push({ ...piece, row, column });
|
|
91
|
+
}
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
return pieces;
|
|
95
|
+
};
|
|
96
|
+
|
|
97
|
+
export const emptyBoard = (): CheckersBoard => Array.from({ length: BOARD_SIZE }, () => Array.from({ length: BOARD_SIZE }, () => null));
|
|
98
|
+
|
|
99
|
+
export const statusForTurn = (turn?: CheckersColor, winner: CheckersColor | null = null) => {
|
|
100
|
+
if (winner) return winner === "white" ? "White wins" : "Black wins";
|
|
101
|
+
if (!turn) return "";
|
|
102
|
+
return turn === "white" ? "White to move" : "Black to move";
|
|
103
|
+
};
|
|
104
|
+
|
|
105
|
+
export const fromGridChanged = (payload: GridChangedPayload = {}): Partial<CheckersViewState> => ({
|
|
106
|
+
board: payload.cells as CheckersBoard | undefined,
|
|
107
|
+
// selection is view-only — reset on every game model change
|
|
108
|
+
selected: null,
|
|
109
|
+
legalMoves: [],
|
|
110
|
+
});
|
|
@@ -0,0 +1,287 @@
|
|
|
1
|
+
import type { CSSProperties } from "react";
|
|
2
|
+
|
|
3
|
+
import type { ViewRenderFunc } from "@vnejs/module.components";
|
|
4
|
+
|
|
5
|
+
import { Flex, PositionBox, Screen, Text, createRenderFunc, useEffect, useMemo, useState, useStoreState } from "@vnejs/uis.react";
|
|
6
|
+
import { getVneLength } from "@vnejs/uis.utils";
|
|
7
|
+
|
|
8
|
+
import type { ReactComponentProps } from "@vnejs/uis.react";
|
|
9
|
+
|
|
10
|
+
import type { CheckersColor, CheckersGridItem, CheckersPiece, CheckersViewState } from "../utils/checkers.js";
|
|
11
|
+
import { listBoardPieces } from "../utils/checkers.js";
|
|
12
|
+
|
|
13
|
+
import type { CheckersViewPluginConstants, CheckersViewPluginEvents, CheckersViewPluginParams, CheckersViewPluginSettings } from "../types.js";
|
|
14
|
+
|
|
15
|
+
type CheckersComponentProps = ReactComponentProps<
|
|
16
|
+
CheckersViewPluginEvents,
|
|
17
|
+
CheckersViewPluginConstants,
|
|
18
|
+
CheckersViewPluginSettings,
|
|
19
|
+
CheckersViewPluginParams,
|
|
20
|
+
CheckersViewState
|
|
21
|
+
>;
|
|
22
|
+
|
|
23
|
+
type VisualPiece = {
|
|
24
|
+
id: string;
|
|
25
|
+
color: CheckersColor;
|
|
26
|
+
king: boolean;
|
|
27
|
+
row: number;
|
|
28
|
+
column: number;
|
|
29
|
+
opacity: number;
|
|
30
|
+
zone: "board" | "graveyard";
|
|
31
|
+
graveyardIndex: number;
|
|
32
|
+
};
|
|
33
|
+
|
|
34
|
+
const SELECTED_OPACITY = 0.7;
|
|
35
|
+
const GRAVEYARD_STACK = 0.55;
|
|
36
|
+
|
|
37
|
+
const cellBackground = (item: CheckersGridItem & { isFocus?: boolean }) => {
|
|
38
|
+
if (item.isFocus) return "rgba(255, 214, 64, 0.85)";
|
|
39
|
+
if (item.isLegalTarget) return "rgba(80, 200, 120, 0.75)";
|
|
40
|
+
if (item.isSelected) return "rgba(90, 160, 255, 0.8)";
|
|
41
|
+
return item.isDark ? "rgba(60, 40, 20, 0.95)" : "rgba(220, 200, 160, 0.95)";
|
|
42
|
+
};
|
|
43
|
+
|
|
44
|
+
const pieceLook = (piece: Pick<CheckersPiece, "color" | "king">): CSSProperties => {
|
|
45
|
+
const isWhite = piece.color === "white";
|
|
46
|
+
return {
|
|
47
|
+
width: "62%",
|
|
48
|
+
height: "62%",
|
|
49
|
+
borderRadius: "50%",
|
|
50
|
+
background: isWhite ? "#f4f0e6" : "#1a1a1a",
|
|
51
|
+
border: `${getVneLength(6)} solid ${piece.king ? "#d4af37" : isWhite ? "#bbb" : "#444"}`,
|
|
52
|
+
boxSizing: "border-box",
|
|
53
|
+
display: "flex",
|
|
54
|
+
alignItems: "center",
|
|
55
|
+
justifyContent: "center",
|
|
56
|
+
};
|
|
57
|
+
};
|
|
58
|
+
|
|
59
|
+
const kingMarkLook = (): CSSProperties => ({
|
|
60
|
+
width: "50%",
|
|
61
|
+
height: "50%",
|
|
62
|
+
borderRadius: "50%",
|
|
63
|
+
boxSizing: "border-box",
|
|
64
|
+
border: `${getVneLength(6)} solid #d4af37`,
|
|
65
|
+
background: "transparent",
|
|
66
|
+
});
|
|
67
|
+
|
|
68
|
+
const CheckersScreen = ({ store, onMount, PARAMS, emit, EVENTS }: CheckersComponentProps) => {
|
|
69
|
+
const {
|
|
70
|
+
isShow = false,
|
|
71
|
+
isForce = false,
|
|
72
|
+
board,
|
|
73
|
+
gridItems = [],
|
|
74
|
+
gridRow = null,
|
|
75
|
+
gridColumn = null,
|
|
76
|
+
selected = null,
|
|
77
|
+
statusText = "",
|
|
78
|
+
} = useStoreState<CheckersViewState>(store, onMount);
|
|
79
|
+
|
|
80
|
+
const transition = PARAMS.CHECKERS_VIEW.TRANSITION;
|
|
81
|
+
const propsByView = PARAMS.CHECKERS_VIEW.VIEW_PROPS;
|
|
82
|
+
const rows = PARAMS.CHECKERS_VIEW.ROWS;
|
|
83
|
+
const columns = PARAMS.CHECKERS_VIEW.COLUMNS;
|
|
84
|
+
const cellSize = PARAMS.CHECKERS_VIEW.CELL_SIZE;
|
|
85
|
+
const cellGap = PARAMS.CHECKERS_VIEW.CELL_GAP;
|
|
86
|
+
const graveyardGap = PARAMS.CHECKERS_VIEW.GRAVEYARD_GAP;
|
|
87
|
+
const boardSize = columns * cellSize + (columns - 1) * cellGap;
|
|
88
|
+
const playfieldWidth = cellSize + graveyardGap + boardSize + graveyardGap + cellSize;
|
|
89
|
+
|
|
90
|
+
const boardPieces = useMemo(() => (board ? listBoardPieces(board) : []), [board]);
|
|
91
|
+
const selectedId = !selected || !board ? null : (board[selected.row]?.[selected.column]?.id ?? null);
|
|
92
|
+
|
|
93
|
+
const [visualPieces, setVisualPieces] = useState<VisualPiece[]>([]);
|
|
94
|
+
|
|
95
|
+
useEffect(() => {
|
|
96
|
+
if (!isShow) {
|
|
97
|
+
setVisualPieces([]);
|
|
98
|
+
return;
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
setVisualPieces((prev) => {
|
|
102
|
+
const nextById = new Map(boardPieces.map((piece) => [piece.id, piece]));
|
|
103
|
+
const next: VisualPiece[] = [];
|
|
104
|
+
let whiteGraveyardCount = 0;
|
|
105
|
+
let blackGraveyardCount = 0;
|
|
106
|
+
|
|
107
|
+
for (const piece of prev) {
|
|
108
|
+
if (piece.zone === "graveyard") {
|
|
109
|
+
if (piece.color === "white") whiteGraveyardCount = Math.max(whiteGraveyardCount, piece.graveyardIndex + 1);
|
|
110
|
+
else blackGraveyardCount = Math.max(blackGraveyardCount, piece.graveyardIndex + 1);
|
|
111
|
+
}
|
|
112
|
+
}
|
|
113
|
+
|
|
114
|
+
for (const piece of prev) {
|
|
115
|
+
const live = nextById.get(piece.id);
|
|
116
|
+
if (live) {
|
|
117
|
+
next.push({
|
|
118
|
+
id: live.id,
|
|
119
|
+
color: live.color,
|
|
120
|
+
king: live.king,
|
|
121
|
+
row: live.row,
|
|
122
|
+
column: live.column,
|
|
123
|
+
opacity: selectedId === live.id ? SELECTED_OPACITY : 1,
|
|
124
|
+
zone: "board",
|
|
125
|
+
graveyardIndex: 0,
|
|
126
|
+
});
|
|
127
|
+
nextById.delete(piece.id);
|
|
128
|
+
continue;
|
|
129
|
+
}
|
|
130
|
+
|
|
131
|
+
if (piece.zone === "graveyard") {
|
|
132
|
+
next.push(piece);
|
|
133
|
+
continue;
|
|
134
|
+
}
|
|
135
|
+
|
|
136
|
+
const graveyardIndex = piece.color === "white" ? whiteGraveyardCount++ : blackGraveyardCount++;
|
|
137
|
+
next.push({
|
|
138
|
+
...piece,
|
|
139
|
+
opacity: 1,
|
|
140
|
+
zone: "graveyard",
|
|
141
|
+
graveyardIndex,
|
|
142
|
+
});
|
|
143
|
+
}
|
|
144
|
+
|
|
145
|
+
for (const piece of nextById.values()) {
|
|
146
|
+
next.push({
|
|
147
|
+
id: piece.id,
|
|
148
|
+
color: piece.color,
|
|
149
|
+
king: piece.king,
|
|
150
|
+
row: piece.row,
|
|
151
|
+
column: piece.column,
|
|
152
|
+
opacity: selectedId === piece.id ? SELECTED_OPACITY : 1,
|
|
153
|
+
zone: "board",
|
|
154
|
+
graveyardIndex: 0,
|
|
155
|
+
});
|
|
156
|
+
}
|
|
157
|
+
|
|
158
|
+
return next;
|
|
159
|
+
});
|
|
160
|
+
}, [boardPieces, isShow, selectedId]);
|
|
161
|
+
|
|
162
|
+
const cellLen = getVneLength(cellSize);
|
|
163
|
+
const gapLen = getVneLength(cellGap);
|
|
164
|
+
const graveyardGapLen = getVneLength(graveyardGap);
|
|
165
|
+
const stepLen = cellGap ? `calc(${cellLen} + ${gapLen})` : cellLen;
|
|
166
|
+
const boardOffsetX = `calc(${cellLen} + ${graveyardGapLen})`;
|
|
167
|
+
const blackGraveyardX = `calc(${cellLen} + ${graveyardGapLen} + ${getVneLength(boardSize)} + ${graveyardGapLen})`;
|
|
168
|
+
|
|
169
|
+
const playfieldStyle: CSSProperties = {
|
|
170
|
+
position: "relative",
|
|
171
|
+
width: getVneLength(playfieldWidth),
|
|
172
|
+
height: getVneLength(boardSize),
|
|
173
|
+
boxSizing: "border-box",
|
|
174
|
+
};
|
|
175
|
+
|
|
176
|
+
const boardStyle: CSSProperties = {
|
|
177
|
+
position: "absolute",
|
|
178
|
+
top: 0,
|
|
179
|
+
left: boardOffsetX,
|
|
180
|
+
display: "grid",
|
|
181
|
+
gridTemplateColumns: `repeat(${columns}, ${cellLen})`,
|
|
182
|
+
gridTemplateRows: `repeat(${rows}, ${cellLen})`,
|
|
183
|
+
gap: gapLen,
|
|
184
|
+
width: getVneLength(boardSize),
|
|
185
|
+
height: getVneLength(boardSize),
|
|
186
|
+
boxSizing: "border-box",
|
|
187
|
+
};
|
|
188
|
+
|
|
189
|
+
const graveyardStyle = (side: "left" | "right"): CSSProperties => ({
|
|
190
|
+
position: "absolute",
|
|
191
|
+
top: 0,
|
|
192
|
+
left: side === "left" ? 0 : blackGraveyardX,
|
|
193
|
+
width: cellLen,
|
|
194
|
+
height: getVneLength(boardSize),
|
|
195
|
+
boxSizing: "border-box",
|
|
196
|
+
borderRadius: getVneLength(12),
|
|
197
|
+
background: "rgba(255, 255, 255, 0.06)",
|
|
198
|
+
});
|
|
199
|
+
|
|
200
|
+
const cellBaseStyle: CSSProperties = {
|
|
201
|
+
width: cellLen,
|
|
202
|
+
height: cellLen,
|
|
203
|
+
boxSizing: "border-box",
|
|
204
|
+
transition: `background-color ${transition}ms`,
|
|
205
|
+
};
|
|
206
|
+
|
|
207
|
+
const piecesLayerStyle: CSSProperties = {
|
|
208
|
+
position: "absolute",
|
|
209
|
+
inset: 0,
|
|
210
|
+
pointerEvents: "none",
|
|
211
|
+
};
|
|
212
|
+
|
|
213
|
+
const pieceTransform = (piece: VisualPiece) => {
|
|
214
|
+
if (piece.zone === "graveyard") {
|
|
215
|
+
const x = piece.color === "white" ? "0px" : blackGraveyardX;
|
|
216
|
+
const y = `calc(${piece.graveyardIndex} * ${cellLen} * ${GRAVEYARD_STACK})`;
|
|
217
|
+
return `translate(${x}, ${y})`;
|
|
218
|
+
}
|
|
219
|
+
|
|
220
|
+
return `translate(calc(${boardOffsetX} + ${piece.column} * ${stepLen}), calc(${piece.row} * ${stepLen}))`;
|
|
221
|
+
};
|
|
222
|
+
|
|
223
|
+
return (
|
|
224
|
+
<Screen
|
|
225
|
+
{...propsByView.screen}
|
|
226
|
+
isShow={isShow}
|
|
227
|
+
isForce={isForce}
|
|
228
|
+
>
|
|
229
|
+
<PositionBox {...propsByView.position}>
|
|
230
|
+
<Flex {...propsByView.flex}>
|
|
231
|
+
<Text
|
|
232
|
+
{...propsByView.status}
|
|
233
|
+
text={statusText}
|
|
234
|
+
width={playfieldWidth}
|
|
235
|
+
/>
|
|
236
|
+
<div style={playfieldStyle}>
|
|
237
|
+
<div style={graveyardStyle("left")} />
|
|
238
|
+
<div style={graveyardStyle("right")} />
|
|
239
|
+
<div style={boardStyle}>
|
|
240
|
+
{gridItems.map((item) => {
|
|
241
|
+
const isFocus = gridRow === item.row && gridColumn === item.column;
|
|
242
|
+
|
|
243
|
+
return (
|
|
244
|
+
<div
|
|
245
|
+
key={`${item.row}-${item.column}`}
|
|
246
|
+
onClick={() => void emit(EVENTS.CHECKERS_VIEW.CLICK, { row: item.row, column: item.column })}
|
|
247
|
+
vne-cursor-type="pointer"
|
|
248
|
+
vne-cursor-target-possible=""
|
|
249
|
+
style={{
|
|
250
|
+
...cellBaseStyle,
|
|
251
|
+
background: cellBackground({ ...item, isFocus }),
|
|
252
|
+
}}
|
|
253
|
+
/>
|
|
254
|
+
);
|
|
255
|
+
})}
|
|
256
|
+
</div>
|
|
257
|
+
<div style={piecesLayerStyle}>
|
|
258
|
+
{visualPieces.map((piece) => (
|
|
259
|
+
<div
|
|
260
|
+
key={piece.id}
|
|
261
|
+
style={{
|
|
262
|
+
position: "absolute",
|
|
263
|
+
top: 0,
|
|
264
|
+
left: 0,
|
|
265
|
+
width: cellLen,
|
|
266
|
+
height: cellLen,
|
|
267
|
+
display: "flex",
|
|
268
|
+
alignItems: "center",
|
|
269
|
+
justifyContent: "center",
|
|
270
|
+
transform: pieceTransform(piece),
|
|
271
|
+
opacity: piece.opacity,
|
|
272
|
+
transition: `transform ${transition}ms, opacity ${transition}ms`,
|
|
273
|
+
willChange: "transform, opacity",
|
|
274
|
+
}}
|
|
275
|
+
>
|
|
276
|
+
<div style={pieceLook(piece)}>{piece.king ? <div style={kingMarkLook()} /> : null}</div>
|
|
277
|
+
</div>
|
|
278
|
+
))}
|
|
279
|
+
</div>
|
|
280
|
+
</div>
|
|
281
|
+
</Flex>
|
|
282
|
+
</PositionBox>
|
|
283
|
+
</Screen>
|
|
284
|
+
);
|
|
285
|
+
};
|
|
286
|
+
|
|
287
|
+
export const render: ViewRenderFunc<CheckersViewState> = createRenderFunc(CheckersScreen);
|
package/tsconfig.json
ADDED
package/README.md
DELETED