@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.
Files changed (47) hide show
  1. package/dist/index.d.ts +4 -0
  2. package/dist/index.js +28 -0
  3. package/dist/modules/board.d.ts +10 -0
  4. package/dist/modules/board.js +32 -0
  5. package/dist/modules/bridge.d.ts +8 -0
  6. package/dist/modules/bridge.js +10 -0
  7. package/dist/modules/click.d.ts +9 -0
  8. package/dist/modules/click.js +14 -0
  9. package/dist/modules/controller.accept.d.ts +12 -0
  10. package/dist/modules/controller.accept.js +18 -0
  11. package/dist/modules/controller.d.ts +14 -0
  12. package/dist/modules/controller.grid.d.ts +20 -0
  13. package/dist/modules/controller.grid.js +25 -0
  14. package/dist/modules/controller.js +12 -0
  15. package/dist/modules/player.interact.d.ts +9 -0
  16. package/dist/modules/player.interact.js +33 -0
  17. package/dist/modules/player.select.d.ts +9 -0
  18. package/dist/modules/player.select.js +29 -0
  19. package/dist/modules/status.d.ts +18 -0
  20. package/dist/modules/status.js +24 -0
  21. package/dist/modules/view.d.ts +10 -0
  22. package/dist/modules/view.js +9 -0
  23. package/dist/types.d.ts +13 -0
  24. package/dist/types.js +1 -0
  25. package/dist/utils/checkers.d.ts +49 -0
  26. package/dist/utils/checkers.js +47 -0
  27. package/dist/view/index.d.ts +3 -0
  28. package/dist/view/index.js +187 -0
  29. package/package.json +67 -4
  30. package/src/index.ts +32 -0
  31. package/src/modules/board.ts +49 -0
  32. package/src/modules/bridge.ts +21 -0
  33. package/src/modules/click.ts +31 -0
  34. package/src/modules/controller.accept.ts +35 -0
  35. package/src/modules/controller.grid.ts +40 -0
  36. package/src/modules/controller.ts +25 -0
  37. package/src/modules/player.interact.ts +50 -0
  38. package/src/modules/player.select.ts +44 -0
  39. package/src/modules/status.ts +46 -0
  40. package/src/modules/view.ts +25 -0
  41. package/src/tests/player.interact.test.ts +59 -0
  42. package/src/tests/setup.ts +49 -0
  43. package/src/types.ts +41 -0
  44. package/src/utils/checkers.ts +110 -0
  45. package/src/view/index.tsx +287 -0
  46. package/tsconfig.json +10 -0
  47. package/README.md +0 -3
@@ -0,0 +1,187 @@
1
+ import { jsx as _jsx, jsxs as _jsxs } from "react/jsx-runtime";
2
+ import { Flex, PositionBox, Screen, Text, createRenderFunc, useEffect, useMemo, useState, useStoreState } from "@vnejs/uis.react";
3
+ import { getVneLength } from "@vnejs/uis.utils";
4
+ import { listBoardPieces } from "../utils/checkers.js";
5
+ const SELECTED_OPACITY = 0.7;
6
+ const GRAVEYARD_STACK = 0.55;
7
+ const cellBackground = (item) => {
8
+ if (item.isFocus)
9
+ return "rgba(255, 214, 64, 0.85)";
10
+ if (item.isLegalTarget)
11
+ return "rgba(80, 200, 120, 0.75)";
12
+ if (item.isSelected)
13
+ return "rgba(90, 160, 255, 0.8)";
14
+ return item.isDark ? "rgba(60, 40, 20, 0.95)" : "rgba(220, 200, 160, 0.95)";
15
+ };
16
+ const pieceLook = (piece) => {
17
+ const isWhite = piece.color === "white";
18
+ return {
19
+ width: "62%",
20
+ height: "62%",
21
+ borderRadius: "50%",
22
+ background: isWhite ? "#f4f0e6" : "#1a1a1a",
23
+ border: `${getVneLength(6)} solid ${piece.king ? "#d4af37" : isWhite ? "#bbb" : "#444"}`,
24
+ boxSizing: "border-box",
25
+ display: "flex",
26
+ alignItems: "center",
27
+ justifyContent: "center",
28
+ };
29
+ };
30
+ const kingMarkLook = () => ({
31
+ width: "50%",
32
+ height: "50%",
33
+ borderRadius: "50%",
34
+ boxSizing: "border-box",
35
+ border: `${getVneLength(6)} solid #d4af37`,
36
+ background: "transparent",
37
+ });
38
+ const CheckersScreen = ({ store, onMount, PARAMS, emit, EVENTS }) => {
39
+ const { isShow = false, isForce = false, board, gridItems = [], gridRow = null, gridColumn = null, selected = null, statusText = "", } = useStoreState(store, onMount);
40
+ const transition = PARAMS.CHECKERS_VIEW.TRANSITION;
41
+ const propsByView = PARAMS.CHECKERS_VIEW.VIEW_PROPS;
42
+ const rows = PARAMS.CHECKERS_VIEW.ROWS;
43
+ const columns = PARAMS.CHECKERS_VIEW.COLUMNS;
44
+ const cellSize = PARAMS.CHECKERS_VIEW.CELL_SIZE;
45
+ const cellGap = PARAMS.CHECKERS_VIEW.CELL_GAP;
46
+ const graveyardGap = PARAMS.CHECKERS_VIEW.GRAVEYARD_GAP;
47
+ const boardSize = columns * cellSize + (columns - 1) * cellGap;
48
+ const playfieldWidth = cellSize + graveyardGap + boardSize + graveyardGap + cellSize;
49
+ const boardPieces = useMemo(() => (board ? listBoardPieces(board) : []), [board]);
50
+ const selectedId = !selected || !board ? null : (board[selected.row]?.[selected.column]?.id ?? null);
51
+ const [visualPieces, setVisualPieces] = useState([]);
52
+ useEffect(() => {
53
+ if (!isShow) {
54
+ setVisualPieces([]);
55
+ return;
56
+ }
57
+ setVisualPieces((prev) => {
58
+ const nextById = new Map(boardPieces.map((piece) => [piece.id, piece]));
59
+ const next = [];
60
+ let whiteGraveyardCount = 0;
61
+ let blackGraveyardCount = 0;
62
+ for (const piece of prev) {
63
+ if (piece.zone === "graveyard") {
64
+ if (piece.color === "white")
65
+ whiteGraveyardCount = Math.max(whiteGraveyardCount, piece.graveyardIndex + 1);
66
+ else
67
+ blackGraveyardCount = Math.max(blackGraveyardCount, piece.graveyardIndex + 1);
68
+ }
69
+ }
70
+ for (const piece of prev) {
71
+ const live = nextById.get(piece.id);
72
+ if (live) {
73
+ next.push({
74
+ id: live.id,
75
+ color: live.color,
76
+ king: live.king,
77
+ row: live.row,
78
+ column: live.column,
79
+ opacity: selectedId === live.id ? SELECTED_OPACITY : 1,
80
+ zone: "board",
81
+ graveyardIndex: 0,
82
+ });
83
+ nextById.delete(piece.id);
84
+ continue;
85
+ }
86
+ if (piece.zone === "graveyard") {
87
+ next.push(piece);
88
+ continue;
89
+ }
90
+ const graveyardIndex = piece.color === "white" ? whiteGraveyardCount++ : blackGraveyardCount++;
91
+ next.push({
92
+ ...piece,
93
+ opacity: 1,
94
+ zone: "graveyard",
95
+ graveyardIndex,
96
+ });
97
+ }
98
+ for (const piece of nextById.values()) {
99
+ next.push({
100
+ id: piece.id,
101
+ color: piece.color,
102
+ king: piece.king,
103
+ row: piece.row,
104
+ column: piece.column,
105
+ opacity: selectedId === piece.id ? SELECTED_OPACITY : 1,
106
+ zone: "board",
107
+ graveyardIndex: 0,
108
+ });
109
+ }
110
+ return next;
111
+ });
112
+ }, [boardPieces, isShow, selectedId]);
113
+ const cellLen = getVneLength(cellSize);
114
+ const gapLen = getVneLength(cellGap);
115
+ const graveyardGapLen = getVneLength(graveyardGap);
116
+ const stepLen = cellGap ? `calc(${cellLen} + ${gapLen})` : cellLen;
117
+ const boardOffsetX = `calc(${cellLen} + ${graveyardGapLen})`;
118
+ const blackGraveyardX = `calc(${cellLen} + ${graveyardGapLen} + ${getVneLength(boardSize)} + ${graveyardGapLen})`;
119
+ const playfieldStyle = {
120
+ position: "relative",
121
+ width: getVneLength(playfieldWidth),
122
+ height: getVneLength(boardSize),
123
+ boxSizing: "border-box",
124
+ };
125
+ const boardStyle = {
126
+ position: "absolute",
127
+ top: 0,
128
+ left: boardOffsetX,
129
+ display: "grid",
130
+ gridTemplateColumns: `repeat(${columns}, ${cellLen})`,
131
+ gridTemplateRows: `repeat(${rows}, ${cellLen})`,
132
+ gap: gapLen,
133
+ width: getVneLength(boardSize),
134
+ height: getVneLength(boardSize),
135
+ boxSizing: "border-box",
136
+ };
137
+ const graveyardStyle = (side) => ({
138
+ position: "absolute",
139
+ top: 0,
140
+ left: side === "left" ? 0 : blackGraveyardX,
141
+ width: cellLen,
142
+ height: getVneLength(boardSize),
143
+ boxSizing: "border-box",
144
+ borderRadius: getVneLength(12),
145
+ background: "rgba(255, 255, 255, 0.06)",
146
+ });
147
+ const cellBaseStyle = {
148
+ width: cellLen,
149
+ height: cellLen,
150
+ boxSizing: "border-box",
151
+ transition: `background-color ${transition}ms`,
152
+ };
153
+ const piecesLayerStyle = {
154
+ position: "absolute",
155
+ inset: 0,
156
+ pointerEvents: "none",
157
+ };
158
+ const pieceTransform = (piece) => {
159
+ if (piece.zone === "graveyard") {
160
+ const x = piece.color === "white" ? "0px" : blackGraveyardX;
161
+ const y = `calc(${piece.graveyardIndex} * ${cellLen} * ${GRAVEYARD_STACK})`;
162
+ return `translate(${x}, ${y})`;
163
+ }
164
+ return `translate(calc(${boardOffsetX} + ${piece.column} * ${stepLen}), calc(${piece.row} * ${stepLen}))`;
165
+ };
166
+ return (_jsx(Screen, { ...propsByView.screen, isShow: isShow, isForce: isForce, children: _jsx(PositionBox, { ...propsByView.position, children: _jsxs(Flex, { ...propsByView.flex, children: [_jsx(Text, { ...propsByView.status, text: statusText, width: playfieldWidth }), _jsxs("div", { style: playfieldStyle, children: [_jsx("div", { style: graveyardStyle("left") }), _jsx("div", { style: graveyardStyle("right") }), _jsx("div", { style: boardStyle, children: gridItems.map((item) => {
167
+ const isFocus = gridRow === item.row && gridColumn === item.column;
168
+ return (_jsx("div", { onClick: () => void emit(EVENTS.CHECKERS_VIEW.CLICK, { row: item.row, column: item.column }), "vne-cursor-type": "pointer", "vne-cursor-target-possible": "", style: {
169
+ ...cellBaseStyle,
170
+ background: cellBackground({ ...item, isFocus }),
171
+ } }, `${item.row}-${item.column}`));
172
+ }) }), _jsx("div", { style: piecesLayerStyle, children: visualPieces.map((piece) => (_jsx("div", { style: {
173
+ position: "absolute",
174
+ top: 0,
175
+ left: 0,
176
+ width: cellLen,
177
+ height: cellLen,
178
+ display: "flex",
179
+ alignItems: "center",
180
+ justifyContent: "center",
181
+ transform: pieceTransform(piece),
182
+ opacity: piece.opacity,
183
+ transition: `transform ${transition}ms, opacity ${transition}ms`,
184
+ willChange: "transform, opacity",
185
+ }, children: _jsx("div", { style: pieceLook(piece), children: piece.king ? _jsx("div", { style: kingMarkLook() }) : null }) }, piece.id))) })] })] }) }) }));
186
+ };
187
+ export const render = createRenderFunc(CheckersScreen);
package/package.json CHANGED
@@ -1,6 +1,69 @@
1
1
  {
2
2
  "name": "@vnejs/plugins.controllers.screens.checkers",
3
- "version": "0.0.0-stage",
4
- "stub": true,
5
- "description": "Temporary package placeholder for staged publishing"
6
- }
3
+ "version": "0.2.12",
4
+ "description": "Checkers screen view (CHECKERS_VIEW)",
5
+ "main": "dist/index.js",
6
+ "types": "dist/index.d.ts",
7
+ "exports": {
8
+ ".": {
9
+ "types": "./dist/index.d.ts",
10
+ "import": "./dist/index.js",
11
+ "require": "./dist/index.js",
12
+ "default": "./dist/index.js"
13
+ }
14
+ },
15
+ "files": [
16
+ "dist",
17
+ "src",
18
+ "tsconfig.json"
19
+ ],
20
+ "scripts": {
21
+ "test": "npx @vnejs/monorepo test",
22
+ "build": "npx @vnejs/monorepo package",
23
+ "publish:major:plugin": "npm run publish:major",
24
+ "publish:minor:plugin": "npm run publish:minor",
25
+ "publish:patch:plugin": "npm run publish:patch",
26
+ "publish:major": "npx @vnejs/monorepo publish major --access public",
27
+ "publish:minor": "npx @vnejs/monorepo publish minor --access public",
28
+ "publish:patch": "npx @vnejs/monorepo publish patch --access public"
29
+ },
30
+ "author": "",
31
+ "license": "ISC",
32
+ "dependencies": {
33
+ "@vnejs/contracts.ai": "~0.2.0",
34
+ "@vnejs/contracts.common.grid": "~0.2.0",
35
+ "@vnejs/contracts.common.turn": "~0.2.0",
36
+ "@vnejs/contracts.game.checkers": "~0.2.0",
37
+ "@vnejs/contracts.controllers.screens.checkers": "~0.2.0"
38
+ },
39
+ "peerDependencies": {
40
+ "@vnejs/helpers": "~0.2.0",
41
+ "@vnejs/module.core": "~0.2.0",
42
+ "@vnejs/module.components": "~0.2.0",
43
+ "@vnejs/module.controller.accept": "~0.2.0",
44
+ "@vnejs/module.controller.arrows.grid": "~0.2.0",
45
+ "@vnejs/contracts.controls": "~0.2.0",
46
+ "@vnejs/registry": "~0.2.0",
47
+ "@vnejs/sources-set": "~0.2.0",
48
+ "@vnejs/uis.react": "~0.2.0"
49
+ },
50
+ "devDependencies": {
51
+ "@vnejs/configs.ts-common": "~0.2.0",
52
+ "@vnejs/configs.vitest": "~0.2.0",
53
+ "@vnejs/test-utils": "~0.2.0",
54
+ "@vnejs/contracts.ai": "~0.2.0",
55
+ "@vnejs/contracts.common.grid": "~0.2.0",
56
+ "@vnejs/contracts.common.turn": "~0.2.0",
57
+ "@vnejs/contracts.controls": "~0.2.0",
58
+ "@vnejs/contracts.game.checkers": "~0.2.0",
59
+ "@vnejs/contracts.controllers.screens.checkers": "~0.2.0",
60
+ "@vnejs/helpers": "~0.2.0",
61
+ "@vnejs/module.core": "~0.2.0",
62
+ "@vnejs/module.components": "~0.2.0",
63
+ "@vnejs/module.controller.accept": "~0.2.0",
64
+ "@vnejs/module.controller.arrows.grid": "~0.2.0",
65
+ "@vnejs/registry": "~0.2.0",
66
+ "@vnejs/sources-set": "~0.2.0",
67
+ "@vnejs/uis.react": "~0.2.0"
68
+ }
69
+ }
package/src/index.ts ADDED
@@ -0,0 +1,32 @@
1
+ import "@vnejs/contracts.common.grid";
2
+ import "@vnejs/contracts.common.turn";
3
+ import "@vnejs/contracts.game.checkers";
4
+ import "@vnejs/contracts.controllers.screens.checkers";
5
+
6
+ import { regPlugin } from "@vnejs/registry";
7
+
8
+ import { PARAMS, PLUGIN_NAME, SUBSCRIBE_EVENTS } from "@vnejs/contracts.controllers.screens.checkers";
9
+
10
+ import { CheckersViewBoard } from "./modules/board.js";
11
+ import { CheckersViewBridge } from "./modules/bridge.js";
12
+ import { CheckersViewClick } from "./modules/click.js";
13
+ import { CheckersViewControllerAccept } from "./modules/controller.accept.js";
14
+ import { CheckersViewControllerGrid } from "./modules/controller.grid.js";
15
+ import { CheckersViewController } from "./modules/controller.js";
16
+ import { CheckersViewPlayerInteract } from "./modules/player.interact.js";
17
+ import { CheckersViewPlayerSelect } from "./modules/player.select.js";
18
+ import { CheckersViewStatus } from "./modules/status.js";
19
+ import { CheckersViewView } from "./modules/view.js";
20
+
21
+ regPlugin(PLUGIN_NAME, { events: SUBSCRIBE_EVENTS, params: PARAMS }, [
22
+ CheckersViewBridge,
23
+ CheckersViewController,
24
+ CheckersViewControllerGrid,
25
+ CheckersViewControllerAccept,
26
+ CheckersViewPlayerInteract,
27
+ CheckersViewPlayerSelect,
28
+ CheckersViewClick,
29
+ CheckersViewBoard,
30
+ CheckersViewStatus,
31
+ CheckersViewView,
32
+ ]);
@@ -0,0 +1,49 @@
1
+ import { ModuleCore } from "@vnejs/module.core";
2
+
3
+ import type { GridChangedPayload, GridUnsetPayload } from "@vnejs/contracts.common.grid";
4
+
5
+ import { buildGridItems, emptyBoard, fromGridChanged, type CheckersViewState } from "../utils/checkers.js";
6
+
7
+ import type { CheckersViewPluginConstants, CheckersViewPluginEvents, CheckersViewPluginParams, CheckersViewPluginSettings } from "../types.js";
8
+
9
+ export class CheckersViewBoard extends ModuleCore<
10
+ CheckersViewPluginEvents,
11
+ CheckersViewPluginConstants,
12
+ CheckersViewPluginSettings,
13
+ CheckersViewPluginParams,
14
+ CheckersViewState
15
+ > {
16
+ name = "checkers_view.board";
17
+
18
+ subscribe = () => {
19
+ this.on(this.EVENTS.GRID.CHANGED, this.onGridChanged);
20
+ this.on(this.EVENTS.GRID.UNSET, this.onGridUnset);
21
+ this.on(this.EVENTS.CHECKERS_VIEW.UPDATE_STATE, this.updateState);
22
+ };
23
+
24
+ onGridChanged = async (payload: GridChangedPayload = {}) => {
25
+ if (payload.id !== this.CONST.CHECKERS.GRID_ID) return;
26
+
27
+ const mirror = fromGridChanged(payload);
28
+ const board = mirror.board ?? this.state.board ?? emptyBoard();
29
+ const gridItems = buildGridItems({ board, gridRow: this.state.gridRow ?? null, gridColumn: this.state.gridColumn ?? null });
30
+
31
+ await this.emit(this.EVENTS.CHECKERS_VIEW.UPDATE_STATE, { ...mirror, board, gridItems } satisfies Partial<CheckersViewState>);
32
+ await this.emit(this.EVENTS.CHECKERS_VIEW.UPDATE_FAST);
33
+ };
34
+
35
+ onGridUnset = async ({ id }: GridUnsetPayload = {}) => {
36
+ if (id !== this.CONST.CHECKERS.GRID_ID) return;
37
+
38
+ const board = emptyBoard();
39
+ const gridItems = buildGridItems({ board, gridRow: this.state.gridRow ?? null, gridColumn: this.state.gridColumn ?? null });
40
+
41
+ await this.emit(this.EVENTS.CHECKERS_VIEW.UPDATE_STATE, {
42
+ board,
43
+ selected: null,
44
+ legalMoves: [],
45
+ gridItems,
46
+ } satisfies Partial<CheckersViewState>);
47
+ await this.emit(this.EVENTS.CHECKERS_VIEW.UPDATE_FAST);
48
+ };
49
+ }
@@ -0,0 +1,21 @@
1
+ import { ModuleCore } from "@vnejs/module.core";
2
+
3
+ import type { CheckersViewPluginConstants, CheckersViewPluginEvents, CheckersViewPluginParams, CheckersViewPluginSettings } from "../types.js";
4
+
5
+ export class CheckersViewBridge extends ModuleCore<
6
+ CheckersViewPluginEvents,
7
+ CheckersViewPluginConstants,
8
+ CheckersViewPluginSettings,
9
+ CheckersViewPluginParams
10
+ > {
11
+ name = "checkers_view.bridge";
12
+
13
+ subscribe = () => {
14
+ this.on(this.EVENTS.CHECKERS.SHOW, this.onCheckersShow);
15
+ this.on(this.EVENTS.CHECKERS.HIDE, this.onCheckersHide);
16
+ };
17
+
18
+ onCheckersShow = () => this.emit(this.EVENTS.CHECKERS_VIEW.SHOW);
19
+
20
+ onCheckersHide = () => this.emit(this.EVENTS.CHECKERS_VIEW.HIDE);
21
+ }
@@ -0,0 +1,31 @@
1
+ import { ModuleCore } from "@vnejs/module.core";
2
+
3
+ import type { CheckersPlayerInteractPayload } from "@vnejs/contracts.controllers.screens.checkers";
4
+ import type { CheckersClickPayload } from "@vnejs/contracts.controllers.screens.checkers";
5
+
6
+ import type { CheckersViewState } from "../utils/checkers.js";
7
+
8
+ import type { CheckersViewPluginConstants, CheckersViewPluginEvents, CheckersViewPluginParams, CheckersViewPluginSettings } from "../types.js";
9
+
10
+ export class CheckersViewClick extends ModuleCore<
11
+ CheckersViewPluginEvents,
12
+ CheckersViewPluginConstants,
13
+ CheckersViewPluginSettings,
14
+ CheckersViewPluginParams,
15
+ CheckersViewState
16
+ > {
17
+ name = "checkers_view.click";
18
+
19
+ subscribe = () => {
20
+ this.on(this.EVENTS.CHECKERS_VIEW.CLICK, this.onClick);
21
+ this.on(this.EVENTS.CHECKERS_VIEW.UPDATE_STATE, this.updateState);
22
+ };
23
+
24
+ onClick = async ({ row, column }: CheckersClickPayload = {}) => {
25
+ if (this.shared.checkersInteractDisableSources.isNotEmpty() || row == null || column == null) return;
26
+
27
+ await this.emit(this.EVENTS.CHECKERS_VIEW.GRID_CLEAR);
28
+
29
+ this.emit(this.EVENTS.CHECKERS_VIEW.PLAYER_INTERACT, { row, column } satisfies CheckersPlayerInteractPayload);
30
+ };
31
+ }
@@ -0,0 +1,35 @@
1
+ import { ModuleControllerAccept } from "@vnejs/module.controller.accept";
2
+
3
+ import type { CheckersPlayerInteractPayload } from "@vnejs/contracts.controllers.screens.checkers";
4
+
5
+ import type { CheckersViewState } from "../utils/checkers.js";
6
+
7
+ import type { CheckersViewPluginConstants, CheckersViewPluginEvents, CheckersViewPluginParams, CheckersViewPluginSettings } from "../types.js";
8
+
9
+ export class CheckersViewControllerAccept extends ModuleControllerAccept<
10
+ CheckersViewPluginEvents,
11
+ CheckersViewPluginConstants,
12
+ CheckersViewPluginSettings,
13
+ CheckersViewPluginParams,
14
+ CheckersViewState
15
+ > {
16
+ name = "checkers_view.controller.accept";
17
+
18
+ EVENT_SHOW = this.EVENTS.CHECKERS_VIEW.SHOW;
19
+ EVENT_HIDE = this.EVENTS.CHECKERS_VIEW.HIDE;
20
+ EVENT_ACCEPT = this.EVENTS.CHECKERS_VIEW.ACCEPT;
21
+ EVENT_UPDATE_STATE = this.EVENTS.CHECKERS_VIEW.UPDATE_STATE;
22
+
23
+ CONTROLS_ZINDEX = this.PARAMS.CHECKERS_VIEW.ZINDEX;
24
+
25
+ onAccept = async () => {
26
+ if (this.shared.checkersInteractDisableSources.isNotEmpty()) return;
27
+
28
+ const { gridRow, gridColumn } = this.state;
29
+ if (gridRow == null || gridColumn == null) return;
30
+
31
+ await this.emit(this.EVENTS.CHECKERS_VIEW.GRID_CLEAR);
32
+
33
+ this.emit(this.EVENTS.CHECKERS_VIEW.PLAYER_INTERACT, { row: gridRow, column: gridColumn } satisfies CheckersPlayerInteractPayload);
34
+ };
35
+ }
@@ -0,0 +1,40 @@
1
+ import { ModuleControllerArrowsGrid } from "@vnejs/module.controller.arrows.grid";
2
+
3
+ import { buildGridItems, emptyBoard, type CheckersViewState } from "../utils/checkers.js";
4
+
5
+ import type { CheckersViewPluginConstants, CheckersViewPluginEvents, CheckersViewPluginParams, CheckersViewPluginSettings } from "../types.js";
6
+
7
+ export class CheckersViewControllerGrid extends ModuleControllerArrowsGrid<
8
+ CheckersViewPluginEvents,
9
+ CheckersViewPluginConstants,
10
+ CheckersViewPluginSettings,
11
+ CheckersViewPluginParams,
12
+ CheckersViewState
13
+ > {
14
+ name = "checkers_view.controller.grid";
15
+
16
+ EVENT_SHOW = this.EVENTS.CHECKERS_VIEW.SHOW;
17
+ EVENT_HIDE = this.EVENTS.CHECKERS_VIEW.HIDE;
18
+ EVENT_UPDATE_STATE = this.EVENTS.CHECKERS_VIEW.UPDATE_STATE;
19
+ EVENT_UPDATE_FAST = this.EVENTS.CHECKERS_VIEW.UPDATE_FAST;
20
+ EVENT_GRID_NEXT = this.EVENTS.CHECKERS_VIEW.GRID_NEXT;
21
+ EVENT_GRID_PREV = this.EVENTS.CHECKERS_VIEW.GRID_PREV;
22
+ EVENT_GRID_CLEAR = this.EVENTS.CHECKERS_VIEW.GRID_CLEAR;
23
+ EVENT_GRID_UPDATE_ITEMS = this.EVENTS.CHECKERS_VIEW.GRID_UPDATE_ITEMS;
24
+
25
+ CONTROLS_ZINDEX = this.PARAMS.CHECKERS_VIEW.ZINDEX;
26
+
27
+ isDisableGridChangeHorizontal = true;
28
+ isDisableGridChangeVertical = true;
29
+
30
+ getMaxRows = () => this.PARAMS.CHECKERS_VIEW.ROWS;
31
+ getMaxColumns = () => this.PARAMS.CHECKERS_VIEW.COLUMNS;
32
+ getGridItems = async () =>
33
+ buildGridItems({
34
+ board: this.state.board ?? emptyBoard(),
35
+ selected: this.state.selected ?? null,
36
+ legalMoves: this.state.legalMoves ?? [],
37
+ gridRow: this.state.gridRow ?? null,
38
+ gridColumn: this.state.gridColumn ?? null,
39
+ });
40
+ }
@@ -0,0 +1,25 @@
1
+ import { ModuleController } from "@vnejs/module.components";
2
+
3
+ import type { CheckersViewState } from "../utils/checkers.js";
4
+
5
+ import type { CheckersViewPluginConstants, CheckersViewPluginEvents, CheckersViewPluginParams, CheckersViewPluginSettings } from "../types.js";
6
+
7
+ export class CheckersViewController extends ModuleController<
8
+ CheckersViewPluginEvents,
9
+ CheckersViewPluginConstants,
10
+ CheckersViewPluginSettings,
11
+ CheckersViewPluginParams,
12
+ CheckersViewState
13
+ > {
14
+ name = "checkers_view.controller";
15
+
16
+ EVENT_UPDATE_VIEW = this.EVENTS.CHECKERS_VIEW.UPDATE;
17
+ EVENT_UPDATE_STATE = this.EVENTS.CHECKERS_VIEW.UPDATE_STATE;
18
+ EVENT_UPDATE_FAST = this.EVENTS.CHECKERS_VIEW.UPDATE_FAST;
19
+ EVENT_UPDATE_FORCE = this.EVENTS.CHECKERS_VIEW.UPDATE_FORCE;
20
+ EVENT_SHOW = this.EVENTS.CHECKERS_VIEW.SHOW;
21
+ EVENT_HIDE = this.EVENTS.CHECKERS_VIEW.HIDE;
22
+
23
+ CONTROLS = {};
24
+ CONTROLS_INDEX = this.PARAMS.CHECKERS_VIEW.ZINDEX;
25
+ }
@@ -0,0 +1,50 @@
1
+ import { ModuleCore } from "@vnejs/module.core";
2
+
3
+ import type { CheckersPlayerInteractPayload, CheckersPlayerSelectPayload } from "@vnejs/contracts.controllers.screens.checkers";
4
+ import type { CheckersMovePayload, CheckersMovePlayPayload, CheckersMovesGetResult } from "@vnejs/contracts.game.checkers";
5
+
6
+ import { samePos, type CheckersPos, type CheckersViewState } from "../utils/checkers.js";
7
+
8
+ import type { CheckersViewPluginConstants, CheckersViewPluginEvents, CheckersViewPluginParams, CheckersViewPluginSettings } from "../types.js";
9
+
10
+ export class CheckersViewPlayerInteract extends ModuleCore<
11
+ CheckersViewPluginEvents,
12
+ CheckersViewPluginConstants,
13
+ CheckersViewPluginSettings,
14
+ CheckersViewPluginParams,
15
+ CheckersViewState
16
+ > {
17
+ name = "checkers_view.player.interact";
18
+
19
+ subscribe = () => {
20
+ this.on(this.EVENTS.CHECKERS_VIEW.PLAYER_INTERACT, this.onPlayerInteract);
21
+ this.on(this.EVENTS.CHECKERS_VIEW.UPDATE_STATE, this.updateState);
22
+ };
23
+
24
+ onPlayerInteract = async ({ row, column }: CheckersPlayerInteractPayload = {}) => {
25
+ const { board, turn, selected } = this.state;
26
+ if (row == null || column == null || !board || !turn) return;
27
+
28
+ const focus: CheckersPos = { row, column };
29
+ const piece = board[focus.row]?.[focus.column] ?? null;
30
+ const legalFromState = this.state.legalMoves ?? [];
31
+ const move = selected && legalFromState.find((m) => samePos(m.from, selected) && samePos(m.to, focus));
32
+
33
+ if (move) return void this.emitOne(this.EVENTS.CHECKERS.MOVE_PLAY, { move, controllerType: this.CONST.AI.PLAYER_TYPE } satisfies CheckersMovePlayPayload);
34
+
35
+ if (piece && piece.color === turn) {
36
+ const result = (await this.emitOne(this.EVENTS.CHECKERS.MOVES_GET, { board, turn })) as CheckersMovesGetResult<CheckersMovePayload> | null;
37
+ const legalMoves = (result?.moves ?? []).filter((m) => samePos(m.from, focus));
38
+ if (legalMoves.length === 0) return;
39
+
40
+ const nextSelected = selected && samePos(selected, focus) ? null : focus;
41
+
42
+ return void this.emit(this.EVENTS.CHECKERS_VIEW.PLAYER_SELECT, {
43
+ selected: nextSelected,
44
+ legalMoves: nextSelected ? legalMoves : [],
45
+ } satisfies CheckersPlayerSelectPayload);
46
+ }
47
+
48
+ if (selected) return void this.emit(this.EVENTS.CHECKERS_VIEW.PLAYER_SELECT, { selected: null, legalMoves: [] } satisfies CheckersPlayerSelectPayload);
49
+ };
50
+ }
@@ -0,0 +1,44 @@
1
+ import { ModuleCore } from "@vnejs/module.core";
2
+
3
+ import type { CheckersPlayerSelectPayload } from "@vnejs/contracts.controllers.screens.checkers";
4
+
5
+ import { buildGridItems, type CheckersMove, type CheckersPos, type CheckersViewState } from "../utils/checkers.js";
6
+
7
+ import type { CheckersViewPluginConstants, CheckersViewPluginEvents, CheckersViewPluginParams, CheckersViewPluginSettings } from "../types.js";
8
+
9
+ export class CheckersViewPlayerSelect extends ModuleCore<
10
+ CheckersViewPluginEvents,
11
+ CheckersViewPluginConstants,
12
+ CheckersViewPluginSettings,
13
+ CheckersViewPluginParams,
14
+ CheckersViewState
15
+ > {
16
+ name = "checkers_view.player.select";
17
+
18
+ subscribe = () => {
19
+ this.on(this.EVENTS.CHECKERS_VIEW.PLAYER_SELECT, this.onPlayerSelect);
20
+ this.on(this.EVENTS.CHECKERS_VIEW.UPDATE_STATE, this.updateState);
21
+ };
22
+
23
+ onPlayerSelect = async ({ selected = null, legalMoves = [] }: CheckersPlayerSelectPayload = {}) => {
24
+ const board = this.state.board;
25
+ if (!board) return;
26
+
27
+ const nextSelected = selected as CheckersPos | null;
28
+ const nextLegalMoves = legalMoves as CheckersMove[];
29
+ const gridItems = buildGridItems({
30
+ board,
31
+ selected: nextSelected,
32
+ legalMoves: nextLegalMoves,
33
+ gridRow: this.state.gridRow ?? null,
34
+ gridColumn: this.state.gridColumn ?? null,
35
+ });
36
+
37
+ await this.emit(this.EVENTS.CHECKERS_VIEW.UPDATE_STATE, {
38
+ selected: nextSelected,
39
+ legalMoves: nextLegalMoves,
40
+ gridItems,
41
+ } satisfies Partial<CheckersViewState>);
42
+ await this.emit(this.EVENTS.CHECKERS_VIEW.UPDATE_FAST);
43
+ };
44
+ }
@@ -0,0 +1,46 @@
1
+ import { ModuleCore } from "@vnejs/module.core";
2
+
3
+ import type { TurnsChangedPayload, TurnsUnsetPayload } from "@vnejs/contracts.common.turn";
4
+ import type { CheckersFinishPayload } from "@vnejs/contracts.game.checkers";
5
+
6
+ import { statusForTurn, type CheckersColor, type CheckersViewState } from "../utils/checkers.js";
7
+
8
+ import type { CheckersViewPluginConstants, CheckersViewPluginEvents, CheckersViewPluginParams, CheckersViewPluginSettings } from "../types.js";
9
+
10
+ export class CheckersViewStatus extends ModuleCore<
11
+ CheckersViewPluginEvents,
12
+ CheckersViewPluginConstants,
13
+ CheckersViewPluginSettings,
14
+ CheckersViewPluginParams,
15
+ CheckersViewState
16
+ > {
17
+ name = "checkers_view.status";
18
+
19
+ subscribe = () => {
20
+ this.on(this.EVENTS.TURNS.CHANGED, this.onTurnsChanged);
21
+ this.on(this.EVENTS.TURNS.UNSET, this.onTurnsUnset);
22
+ this.on(this.EVENTS.CHECKERS.FINISH, this.onFinish);
23
+ this.on(this.EVENTS.CHECKERS.SHOW, this.onShow);
24
+ this.on(this.EVENTS.CHECKERS_VIEW.UPDATE_STATE, this.updateState);
25
+ };
26
+
27
+ onTurnsChanged = ({ id, owner }: TurnsChangedPayload = {}) =>
28
+ id === this.CONST.CHECKERS.TURN_ID &&
29
+ this.applyStatus({ turn: owner as CheckersColor | undefined, winner: this.state.winner ?? null, clearSelection: true });
30
+
31
+ onTurnsUnset = ({ id }: TurnsUnsetPayload = {}) =>
32
+ id === this.CONST.CHECKERS.TURN_ID && this.applyStatus({ turn: undefined, winner: this.state.winner ?? null, clearSelection: true });
33
+
34
+ onFinish = ({ winner }: CheckersFinishPayload = {}) => this.applyStatus({ turn: this.state.turn, winner: (winner ?? null) as CheckersColor | null });
35
+
36
+ onShow = () => this.applyStatus({ turn: this.state.turn, winner: null });
37
+
38
+ applyStatus = async ({ turn, winner, clearSelection = false }: { turn?: CheckersColor; winner: CheckersColor | null; clearSelection?: boolean }) => {
39
+ const next: Partial<CheckersViewState> = { turn, winner, statusText: statusForTurn(turn, winner) };
40
+
41
+ if (clearSelection) Object.assign(next, { selected: null, legalMoves: [] });
42
+
43
+ await this.emit(this.EVENTS.CHECKERS_VIEW.UPDATE_STATE, next);
44
+ await this.emit(this.EVENTS.CHECKERS_VIEW.UPDATE_FAST);
45
+ };
46
+ }
@@ -0,0 +1,25 @@
1
+ import { ModuleView } from "@vnejs/module.components";
2
+
3
+ import type { CheckersViewState } from "../utils/checkers.js";
4
+
5
+ import type { CheckersViewPluginConstants, CheckersViewPluginEvents, CheckersViewPluginParams, CheckersViewPluginSettings } from "../types.js";
6
+
7
+ import { render } from "../view/index.js";
8
+
9
+ export class CheckersViewView extends ModuleView<
10
+ CheckersViewPluginEvents,
11
+ CheckersViewPluginConstants,
12
+ CheckersViewPluginSettings,
13
+ CheckersViewPluginParams,
14
+ CheckersViewState
15
+ > {
16
+ name = "checkers_view.view";
17
+
18
+ EVENT_UPDATE_VIEW = this.EVENTS.CHECKERS_VIEW.UPDATE;
19
+
20
+ LOC_LABEL = null;
21
+
22
+ TRANSITION = this.PARAMS.CHECKERS_VIEW.TRANSITION;
23
+
24
+ renderFunc = render;
25
+ }