@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,4 @@
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";
package/dist/index.js ADDED
@@ -0,0 +1,28 @@
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
+ import { regPlugin } from "@vnejs/registry";
6
+ import { PARAMS, PLUGIN_NAME, SUBSCRIBE_EVENTS } from "@vnejs/contracts.controllers.screens.checkers";
7
+ import { CheckersViewBoard } from "./modules/board.js";
8
+ import { CheckersViewBridge } from "./modules/bridge.js";
9
+ import { CheckersViewClick } from "./modules/click.js";
10
+ import { CheckersViewControllerAccept } from "./modules/controller.accept.js";
11
+ import { CheckersViewControllerGrid } from "./modules/controller.grid.js";
12
+ import { CheckersViewController } from "./modules/controller.js";
13
+ import { CheckersViewPlayerInteract } from "./modules/player.interact.js";
14
+ import { CheckersViewPlayerSelect } from "./modules/player.select.js";
15
+ import { CheckersViewStatus } from "./modules/status.js";
16
+ import { CheckersViewView } from "./modules/view.js";
17
+ regPlugin(PLUGIN_NAME, { events: SUBSCRIBE_EVENTS, params: PARAMS }, [
18
+ CheckersViewBridge,
19
+ CheckersViewController,
20
+ CheckersViewControllerGrid,
21
+ CheckersViewControllerAccept,
22
+ CheckersViewPlayerInteract,
23
+ CheckersViewPlayerSelect,
24
+ CheckersViewClick,
25
+ CheckersViewBoard,
26
+ CheckersViewStatus,
27
+ CheckersViewView,
28
+ ]);
@@ -0,0 +1,10 @@
1
+ import { ModuleCore } from "@vnejs/module.core";
2
+ import type { GridChangedPayload, GridUnsetPayload } from "@vnejs/contracts.common.grid";
3
+ import { type CheckersViewState } from "../utils/checkers.js";
4
+ import type { CheckersViewPluginConstants, CheckersViewPluginEvents, CheckersViewPluginParams, CheckersViewPluginSettings } from "../types.js";
5
+ export declare class CheckersViewBoard extends ModuleCore<CheckersViewPluginEvents, CheckersViewPluginConstants, CheckersViewPluginSettings, CheckersViewPluginParams, CheckersViewState> {
6
+ name: string;
7
+ subscribe: () => void;
8
+ onGridChanged: (payload?: GridChangedPayload) => Promise<void>;
9
+ onGridUnset: ({ id }?: GridUnsetPayload) => Promise<void>;
10
+ }
@@ -0,0 +1,32 @@
1
+ import { ModuleCore } from "@vnejs/module.core";
2
+ import { buildGridItems, emptyBoard, fromGridChanged } from "../utils/checkers.js";
3
+ export class CheckersViewBoard extends ModuleCore {
4
+ name = "checkers_view.board";
5
+ subscribe = () => {
6
+ this.on(this.EVENTS.GRID.CHANGED, this.onGridChanged);
7
+ this.on(this.EVENTS.GRID.UNSET, this.onGridUnset);
8
+ this.on(this.EVENTS.CHECKERS_VIEW.UPDATE_STATE, this.updateState);
9
+ };
10
+ onGridChanged = async (payload = {}) => {
11
+ if (payload.id !== this.CONST.CHECKERS.GRID_ID)
12
+ return;
13
+ const mirror = fromGridChanged(payload);
14
+ const board = mirror.board ?? this.state.board ?? emptyBoard();
15
+ const gridItems = buildGridItems({ board, gridRow: this.state.gridRow ?? null, gridColumn: this.state.gridColumn ?? null });
16
+ await this.emit(this.EVENTS.CHECKERS_VIEW.UPDATE_STATE, { ...mirror, board, gridItems });
17
+ await this.emit(this.EVENTS.CHECKERS_VIEW.UPDATE_FAST);
18
+ };
19
+ onGridUnset = async ({ id } = {}) => {
20
+ if (id !== this.CONST.CHECKERS.GRID_ID)
21
+ return;
22
+ const board = emptyBoard();
23
+ const gridItems = buildGridItems({ board, gridRow: this.state.gridRow ?? null, gridColumn: this.state.gridColumn ?? null });
24
+ await this.emit(this.EVENTS.CHECKERS_VIEW.UPDATE_STATE, {
25
+ board,
26
+ selected: null,
27
+ legalMoves: [],
28
+ gridItems,
29
+ });
30
+ await this.emit(this.EVENTS.CHECKERS_VIEW.UPDATE_FAST);
31
+ };
32
+ }
@@ -0,0 +1,8 @@
1
+ import { ModuleCore } from "@vnejs/module.core";
2
+ import type { CheckersViewPluginConstants, CheckersViewPluginEvents, CheckersViewPluginParams, CheckersViewPluginSettings } from "../types.js";
3
+ export declare class CheckersViewBridge extends ModuleCore<CheckersViewPluginEvents, CheckersViewPluginConstants, CheckersViewPluginSettings, CheckersViewPluginParams> {
4
+ name: string;
5
+ subscribe: () => void;
6
+ onCheckersShow: () => Promise<import("@vnejs/registry").EmitResults<"vne:checkers_view:show">> | undefined;
7
+ onCheckersHide: () => Promise<import("@vnejs/registry").EmitResults<"vne:checkers_view:hide">> | undefined;
8
+ }
@@ -0,0 +1,10 @@
1
+ import { ModuleCore } from "@vnejs/module.core";
2
+ export class CheckersViewBridge extends ModuleCore {
3
+ name = "checkers_view.bridge";
4
+ subscribe = () => {
5
+ this.on(this.EVENTS.CHECKERS.SHOW, this.onCheckersShow);
6
+ this.on(this.EVENTS.CHECKERS.HIDE, this.onCheckersHide);
7
+ };
8
+ onCheckersShow = () => this.emit(this.EVENTS.CHECKERS_VIEW.SHOW);
9
+ onCheckersHide = () => this.emit(this.EVENTS.CHECKERS_VIEW.HIDE);
10
+ }
@@ -0,0 +1,9 @@
1
+ import { ModuleCore } from "@vnejs/module.core";
2
+ import type { CheckersClickPayload } from "@vnejs/contracts.controllers.screens.checkers";
3
+ import type { CheckersViewState } from "../utils/checkers.js";
4
+ import type { CheckersViewPluginConstants, CheckersViewPluginEvents, CheckersViewPluginParams, CheckersViewPluginSettings } from "../types.js";
5
+ export declare class CheckersViewClick extends ModuleCore<CheckersViewPluginEvents, CheckersViewPluginConstants, CheckersViewPluginSettings, CheckersViewPluginParams, CheckersViewState> {
6
+ name: string;
7
+ subscribe: () => void;
8
+ onClick: ({ row, column }?: CheckersClickPayload) => Promise<void>;
9
+ }
@@ -0,0 +1,14 @@
1
+ import { ModuleCore } from "@vnejs/module.core";
2
+ export class CheckersViewClick extends ModuleCore {
3
+ name = "checkers_view.click";
4
+ subscribe = () => {
5
+ this.on(this.EVENTS.CHECKERS_VIEW.CLICK, this.onClick);
6
+ this.on(this.EVENTS.CHECKERS_VIEW.UPDATE_STATE, this.updateState);
7
+ };
8
+ onClick = async ({ row, column } = {}) => {
9
+ if (this.shared.checkersInteractDisableSources.isNotEmpty() || row == null || column == null)
10
+ return;
11
+ await this.emit(this.EVENTS.CHECKERS_VIEW.GRID_CLEAR);
12
+ this.emit(this.EVENTS.CHECKERS_VIEW.PLAYER_INTERACT, { row, column });
13
+ };
14
+ }
@@ -0,0 +1,12 @@
1
+ import { ModuleControllerAccept } from "@vnejs/module.controller.accept";
2
+ import type { CheckersViewState } from "../utils/checkers.js";
3
+ import type { CheckersViewPluginConstants, CheckersViewPluginEvents, CheckersViewPluginParams, CheckersViewPluginSettings } from "../types.js";
4
+ export declare class CheckersViewControllerAccept extends ModuleControllerAccept<CheckersViewPluginEvents, CheckersViewPluginConstants, CheckersViewPluginSettings, CheckersViewPluginParams, CheckersViewState> {
5
+ name: string;
6
+ EVENT_SHOW: "vne:checkers_view:show";
7
+ EVENT_HIDE: "vne:checkers_view:hide";
8
+ EVENT_ACCEPT: "vne:checkers_view:accept";
9
+ EVENT_UPDATE_STATE: "vne:checkers_view:update_state";
10
+ CONTROLS_ZINDEX: number;
11
+ onAccept: () => Promise<void>;
12
+ }
@@ -0,0 +1,18 @@
1
+ import { ModuleControllerAccept } from "@vnejs/module.controller.accept";
2
+ export class CheckersViewControllerAccept extends ModuleControllerAccept {
3
+ name = "checkers_view.controller.accept";
4
+ EVENT_SHOW = this.EVENTS.CHECKERS_VIEW.SHOW;
5
+ EVENT_HIDE = this.EVENTS.CHECKERS_VIEW.HIDE;
6
+ EVENT_ACCEPT = this.EVENTS.CHECKERS_VIEW.ACCEPT;
7
+ EVENT_UPDATE_STATE = this.EVENTS.CHECKERS_VIEW.UPDATE_STATE;
8
+ CONTROLS_ZINDEX = this.PARAMS.CHECKERS_VIEW.ZINDEX;
9
+ onAccept = async () => {
10
+ if (this.shared.checkersInteractDisableSources.isNotEmpty())
11
+ return;
12
+ const { gridRow, gridColumn } = this.state;
13
+ if (gridRow == null || gridColumn == null)
14
+ return;
15
+ await this.emit(this.EVENTS.CHECKERS_VIEW.GRID_CLEAR);
16
+ this.emit(this.EVENTS.CHECKERS_VIEW.PLAYER_INTERACT, { row: gridRow, column: gridColumn });
17
+ };
18
+ }
@@ -0,0 +1,14 @@
1
+ import { ModuleController } from "@vnejs/module.components";
2
+ import type { CheckersViewState } from "../utils/checkers.js";
3
+ import type { CheckersViewPluginConstants, CheckersViewPluginEvents, CheckersViewPluginParams, CheckersViewPluginSettings } from "../types.js";
4
+ export declare class CheckersViewController extends ModuleController<CheckersViewPluginEvents, CheckersViewPluginConstants, CheckersViewPluginSettings, CheckersViewPluginParams, CheckersViewState> {
5
+ name: string;
6
+ EVENT_UPDATE_VIEW: "vne:checkers_view:update";
7
+ EVENT_UPDATE_STATE: "vne:checkers_view:update_state";
8
+ EVENT_UPDATE_FAST: "vne:checkers_view:update_fast";
9
+ EVENT_UPDATE_FORCE: "vne:checkers_view:update_force";
10
+ EVENT_SHOW: "vne:checkers_view:show";
11
+ EVENT_HIDE: "vne:checkers_view:hide";
12
+ CONTROLS: {};
13
+ CONTROLS_INDEX: number;
14
+ }
@@ -0,0 +1,20 @@
1
+ import { ModuleControllerArrowsGrid } from "@vnejs/module.controller.arrows.grid";
2
+ import { type CheckersViewState } from "../utils/checkers.js";
3
+ import type { CheckersViewPluginConstants, CheckersViewPluginEvents, CheckersViewPluginParams, CheckersViewPluginSettings } from "../types.js";
4
+ export declare class CheckersViewControllerGrid extends ModuleControllerArrowsGrid<CheckersViewPluginEvents, CheckersViewPluginConstants, CheckersViewPluginSettings, CheckersViewPluginParams, CheckersViewState> {
5
+ name: string;
6
+ EVENT_SHOW: "vne:checkers_view:show";
7
+ EVENT_HIDE: "vne:checkers_view:hide";
8
+ EVENT_UPDATE_STATE: "vne:checkers_view:update_state";
9
+ EVENT_UPDATE_FAST: "vne:checkers_view:update_fast";
10
+ EVENT_GRID_NEXT: "vne:checkers_view:grid_next";
11
+ EVENT_GRID_PREV: "vne:checkers_view:grid_prev";
12
+ EVENT_GRID_CLEAR: "vne:checkers_view:grid_clear";
13
+ EVENT_GRID_UPDATE_ITEMS: "vne:checkers_view:grid_update_items";
14
+ CONTROLS_ZINDEX: number;
15
+ isDisableGridChangeHorizontal: boolean;
16
+ isDisableGridChangeVertical: boolean;
17
+ getMaxRows: () => number;
18
+ getMaxColumns: () => number;
19
+ getGridItems: () => Promise<import("../utils/checkers.js").CheckersGridItem[]>;
20
+ }
@@ -0,0 +1,25 @@
1
+ import { ModuleControllerArrowsGrid } from "@vnejs/module.controller.arrows.grid";
2
+ import { buildGridItems, emptyBoard } from "../utils/checkers.js";
3
+ export class CheckersViewControllerGrid extends ModuleControllerArrowsGrid {
4
+ name = "checkers_view.controller.grid";
5
+ EVENT_SHOW = this.EVENTS.CHECKERS_VIEW.SHOW;
6
+ EVENT_HIDE = this.EVENTS.CHECKERS_VIEW.HIDE;
7
+ EVENT_UPDATE_STATE = this.EVENTS.CHECKERS_VIEW.UPDATE_STATE;
8
+ EVENT_UPDATE_FAST = this.EVENTS.CHECKERS_VIEW.UPDATE_FAST;
9
+ EVENT_GRID_NEXT = this.EVENTS.CHECKERS_VIEW.GRID_NEXT;
10
+ EVENT_GRID_PREV = this.EVENTS.CHECKERS_VIEW.GRID_PREV;
11
+ EVENT_GRID_CLEAR = this.EVENTS.CHECKERS_VIEW.GRID_CLEAR;
12
+ EVENT_GRID_UPDATE_ITEMS = this.EVENTS.CHECKERS_VIEW.GRID_UPDATE_ITEMS;
13
+ CONTROLS_ZINDEX = this.PARAMS.CHECKERS_VIEW.ZINDEX;
14
+ isDisableGridChangeHorizontal = true;
15
+ isDisableGridChangeVertical = true;
16
+ getMaxRows = () => this.PARAMS.CHECKERS_VIEW.ROWS;
17
+ getMaxColumns = () => this.PARAMS.CHECKERS_VIEW.COLUMNS;
18
+ getGridItems = async () => buildGridItems({
19
+ board: this.state.board ?? emptyBoard(),
20
+ selected: this.state.selected ?? null,
21
+ legalMoves: this.state.legalMoves ?? [],
22
+ gridRow: this.state.gridRow ?? null,
23
+ gridColumn: this.state.gridColumn ?? null,
24
+ });
25
+ }
@@ -0,0 +1,12 @@
1
+ import { ModuleController } from "@vnejs/module.components";
2
+ export class CheckersViewController extends ModuleController {
3
+ name = "checkers_view.controller";
4
+ EVENT_UPDATE_VIEW = this.EVENTS.CHECKERS_VIEW.UPDATE;
5
+ EVENT_UPDATE_STATE = this.EVENTS.CHECKERS_VIEW.UPDATE_STATE;
6
+ EVENT_UPDATE_FAST = this.EVENTS.CHECKERS_VIEW.UPDATE_FAST;
7
+ EVENT_UPDATE_FORCE = this.EVENTS.CHECKERS_VIEW.UPDATE_FORCE;
8
+ EVENT_SHOW = this.EVENTS.CHECKERS_VIEW.SHOW;
9
+ EVENT_HIDE = this.EVENTS.CHECKERS_VIEW.HIDE;
10
+ CONTROLS = {};
11
+ CONTROLS_INDEX = this.PARAMS.CHECKERS_VIEW.ZINDEX;
12
+ }
@@ -0,0 +1,9 @@
1
+ import { ModuleCore } from "@vnejs/module.core";
2
+ import type { CheckersPlayerInteractPayload } from "@vnejs/contracts.controllers.screens.checkers";
3
+ import { type CheckersViewState } from "../utils/checkers.js";
4
+ import type { CheckersViewPluginConstants, CheckersViewPluginEvents, CheckersViewPluginParams, CheckersViewPluginSettings } from "../types.js";
5
+ export declare class CheckersViewPlayerInteract extends ModuleCore<CheckersViewPluginEvents, CheckersViewPluginConstants, CheckersViewPluginSettings, CheckersViewPluginParams, CheckersViewState> {
6
+ name: string;
7
+ subscribe: () => void;
8
+ onPlayerInteract: ({ row, column }?: CheckersPlayerInteractPayload) => Promise<undefined>;
9
+ }
@@ -0,0 +1,33 @@
1
+ import { ModuleCore } from "@vnejs/module.core";
2
+ import { samePos } from "../utils/checkers.js";
3
+ export class CheckersViewPlayerInteract extends ModuleCore {
4
+ name = "checkers_view.player.interact";
5
+ subscribe = () => {
6
+ this.on(this.EVENTS.CHECKERS_VIEW.PLAYER_INTERACT, this.onPlayerInteract);
7
+ this.on(this.EVENTS.CHECKERS_VIEW.UPDATE_STATE, this.updateState);
8
+ };
9
+ onPlayerInteract = async ({ row, column } = {}) => {
10
+ const { board, turn, selected } = this.state;
11
+ if (row == null || column == null || !board || !turn)
12
+ return;
13
+ const focus = { row, column };
14
+ const piece = board[focus.row]?.[focus.column] ?? null;
15
+ const legalFromState = this.state.legalMoves ?? [];
16
+ const move = selected && legalFromState.find((m) => samePos(m.from, selected) && samePos(m.to, focus));
17
+ if (move)
18
+ return void this.emitOne(this.EVENTS.CHECKERS.MOVE_PLAY, { move, controllerType: this.CONST.AI.PLAYER_TYPE });
19
+ if (piece && piece.color === turn) {
20
+ const result = (await this.emitOne(this.EVENTS.CHECKERS.MOVES_GET, { board, turn }));
21
+ const legalMoves = (result?.moves ?? []).filter((m) => samePos(m.from, focus));
22
+ if (legalMoves.length === 0)
23
+ return;
24
+ const nextSelected = selected && samePos(selected, focus) ? null : focus;
25
+ return void this.emit(this.EVENTS.CHECKERS_VIEW.PLAYER_SELECT, {
26
+ selected: nextSelected,
27
+ legalMoves: nextSelected ? legalMoves : [],
28
+ });
29
+ }
30
+ if (selected)
31
+ return void this.emit(this.EVENTS.CHECKERS_VIEW.PLAYER_SELECT, { selected: null, legalMoves: [] });
32
+ };
33
+ }
@@ -0,0 +1,9 @@
1
+ import { ModuleCore } from "@vnejs/module.core";
2
+ import type { CheckersPlayerSelectPayload } from "@vnejs/contracts.controllers.screens.checkers";
3
+ import { type CheckersViewState } from "../utils/checkers.js";
4
+ import type { CheckersViewPluginConstants, CheckersViewPluginEvents, CheckersViewPluginParams, CheckersViewPluginSettings } from "../types.js";
5
+ export declare class CheckersViewPlayerSelect extends ModuleCore<CheckersViewPluginEvents, CheckersViewPluginConstants, CheckersViewPluginSettings, CheckersViewPluginParams, CheckersViewState> {
6
+ name: string;
7
+ subscribe: () => void;
8
+ onPlayerSelect: ({ selected, legalMoves }?: CheckersPlayerSelectPayload) => Promise<void>;
9
+ }
@@ -0,0 +1,29 @@
1
+ import { ModuleCore } from "@vnejs/module.core";
2
+ import { buildGridItems } from "../utils/checkers.js";
3
+ export class CheckersViewPlayerSelect extends ModuleCore {
4
+ name = "checkers_view.player.select";
5
+ subscribe = () => {
6
+ this.on(this.EVENTS.CHECKERS_VIEW.PLAYER_SELECT, this.onPlayerSelect);
7
+ this.on(this.EVENTS.CHECKERS_VIEW.UPDATE_STATE, this.updateState);
8
+ };
9
+ onPlayerSelect = async ({ selected = null, legalMoves = [] } = {}) => {
10
+ const board = this.state.board;
11
+ if (!board)
12
+ return;
13
+ const nextSelected = selected;
14
+ const nextLegalMoves = legalMoves;
15
+ const gridItems = buildGridItems({
16
+ board,
17
+ selected: nextSelected,
18
+ legalMoves: nextLegalMoves,
19
+ gridRow: this.state.gridRow ?? null,
20
+ gridColumn: this.state.gridColumn ?? null,
21
+ });
22
+ await this.emit(this.EVENTS.CHECKERS_VIEW.UPDATE_STATE, {
23
+ selected: nextSelected,
24
+ legalMoves: nextLegalMoves,
25
+ gridItems,
26
+ });
27
+ await this.emit(this.EVENTS.CHECKERS_VIEW.UPDATE_FAST);
28
+ };
29
+ }
@@ -0,0 +1,18 @@
1
+ import { ModuleCore } from "@vnejs/module.core";
2
+ import type { TurnsChangedPayload, TurnsUnsetPayload } from "@vnejs/contracts.common.turn";
3
+ import type { CheckersFinishPayload } from "@vnejs/contracts.game.checkers";
4
+ import { type CheckersColor, type CheckersViewState } from "../utils/checkers.js";
5
+ import type { CheckersViewPluginConstants, CheckersViewPluginEvents, CheckersViewPluginParams, CheckersViewPluginSettings } from "../types.js";
6
+ export declare class CheckersViewStatus extends ModuleCore<CheckersViewPluginEvents, CheckersViewPluginConstants, CheckersViewPluginSettings, CheckersViewPluginParams, CheckersViewState> {
7
+ name: string;
8
+ subscribe: () => void;
9
+ onTurnsChanged: ({ id, owner }?: TurnsChangedPayload) => false | Promise<void>;
10
+ onTurnsUnset: ({ id }?: TurnsUnsetPayload) => false | Promise<void>;
11
+ onFinish: ({ winner }?: CheckersFinishPayload) => Promise<void>;
12
+ onShow: () => Promise<void>;
13
+ applyStatus: ({ turn, winner, clearSelection }: {
14
+ turn?: CheckersColor;
15
+ winner: CheckersColor | null;
16
+ clearSelection?: boolean;
17
+ }) => Promise<void>;
18
+ }
@@ -0,0 +1,24 @@
1
+ import { ModuleCore } from "@vnejs/module.core";
2
+ import { statusForTurn } from "../utils/checkers.js";
3
+ export class CheckersViewStatus extends ModuleCore {
4
+ name = "checkers_view.status";
5
+ subscribe = () => {
6
+ this.on(this.EVENTS.TURNS.CHANGED, this.onTurnsChanged);
7
+ this.on(this.EVENTS.TURNS.UNSET, this.onTurnsUnset);
8
+ this.on(this.EVENTS.CHECKERS.FINISH, this.onFinish);
9
+ this.on(this.EVENTS.CHECKERS.SHOW, this.onShow);
10
+ this.on(this.EVENTS.CHECKERS_VIEW.UPDATE_STATE, this.updateState);
11
+ };
12
+ onTurnsChanged = ({ id, owner } = {}) => id === this.CONST.CHECKERS.TURN_ID &&
13
+ this.applyStatus({ turn: owner, winner: this.state.winner ?? null, clearSelection: true });
14
+ onTurnsUnset = ({ id } = {}) => id === this.CONST.CHECKERS.TURN_ID && this.applyStatus({ turn: undefined, winner: this.state.winner ?? null, clearSelection: true });
15
+ onFinish = ({ winner } = {}) => this.applyStatus({ turn: this.state.turn, winner: (winner ?? null) });
16
+ onShow = () => this.applyStatus({ turn: this.state.turn, winner: null });
17
+ applyStatus = async ({ turn, winner, clearSelection = false }) => {
18
+ const next = { turn, winner, statusText: statusForTurn(turn, winner) };
19
+ if (clearSelection)
20
+ Object.assign(next, { selected: null, legalMoves: [] });
21
+ await this.emit(this.EVENTS.CHECKERS_VIEW.UPDATE_STATE, next);
22
+ await this.emit(this.EVENTS.CHECKERS_VIEW.UPDATE_FAST);
23
+ };
24
+ }
@@ -0,0 +1,10 @@
1
+ import { ModuleView } from "@vnejs/module.components";
2
+ import type { CheckersViewState } from "../utils/checkers.js";
3
+ import type { CheckersViewPluginConstants, CheckersViewPluginEvents, CheckersViewPluginParams, CheckersViewPluginSettings } from "../types.js";
4
+ export declare class CheckersViewView extends ModuleView<CheckersViewPluginEvents, CheckersViewPluginConstants, CheckersViewPluginSettings, CheckersViewPluginParams, CheckersViewState> {
5
+ name: string;
6
+ EVENT_UPDATE_VIEW: "vne:checkers_view:update";
7
+ LOC_LABEL: null;
8
+ TRANSITION: number;
9
+ renderFunc: import("@vnejs/module.components").ViewRenderFunc<CheckersViewState>;
10
+ }
@@ -0,0 +1,9 @@
1
+ import { ModuleView } from "@vnejs/module.components";
2
+ import { render } from "../view/index.js";
3
+ export class CheckersViewView extends ModuleView {
4
+ name = "checkers_view.view";
5
+ EVENT_UPDATE_VIEW = this.EVENTS.CHECKERS_VIEW.UPDATE;
6
+ LOC_LABEL = null;
7
+ TRANSITION = this.PARAMS.CHECKERS_VIEW.TRANSITION;
8
+ renderFunc = render;
9
+ }
@@ -0,0 +1,13 @@
1
+ import type { ModuleComponentsConstants, ModuleComponentsEvents, ModuleComponentsParams, ModuleComponentsSettings } from "@vnejs/module.components";
2
+ import type { Constants as AiConstants, Params as AiParams, PluginName as AiPluginName, SubscribeEvents as AiSubscribeEvents } from "@vnejs/contracts.ai";
3
+ import type { PluginName as GridPluginName, SubscribeEvents as GridSubscribeEvents } from "@vnejs/contracts.common.grid";
4
+ import type { PluginName as TurnsPluginName, SubscribeEvents as TurnsSubscribeEvents } from "@vnejs/contracts.common.turn";
5
+ import type { Params, PluginName, SubscribeEvents } from "@vnejs/contracts.controllers.screens.checkers";
6
+ import type { Constants as ControlsConstants, PluginName as ControlsPluginName } from "@vnejs/contracts.controls";
7
+ import type { PluginName as LogsPluginName, SubscribeEvents as LogsSubscribeEvents } from "@vnejs/contracts.core.logs";
8
+ import type { Constants as ScenarioConstants, PluginName as ScenarioPluginName, SubscribeEvents as ScenarioSubscribeEvents } from "@vnejs/contracts.core.scenario";
9
+ import type { Constants as CheckersConstants, Params as CheckersParams, PluginName as CheckersPluginName, SubscribeEvents as CheckersSubscribeEvents } from "@vnejs/contracts.game.checkers";
10
+ export type CheckersViewPluginEvents = ModuleComponentsEvents & Record<PluginName, SubscribeEvents> & Record<CheckersPluginName, CheckersSubscribeEvents> & Record<GridPluginName, GridSubscribeEvents> & Record<TurnsPluginName, TurnsSubscribeEvents> & Record<AiPluginName, AiSubscribeEvents> & Record<ScenarioPluginName, ScenarioSubscribeEvents> & Record<LogsPluginName, LogsSubscribeEvents>;
11
+ export type CheckersViewPluginConstants = ModuleComponentsConstants & Record<CheckersPluginName, CheckersConstants> & Record<AiPluginName, AiConstants> & Record<ControlsPluginName, ControlsConstants> & Record<ScenarioPluginName, ScenarioConstants>;
12
+ export type CheckersViewPluginSettings = ModuleComponentsSettings;
13
+ export type CheckersViewPluginParams = ModuleComponentsParams & Record<PluginName, Params> & Record<CheckersPluginName, CheckersParams> & Record<AiPluginName, AiParams>;
package/dist/types.js ADDED
@@ -0,0 +1 @@
1
+ export {};
@@ -0,0 +1,49 @@
1
+ import type { GridChangedPayload } from "@vnejs/contracts.common.grid";
2
+ import type { CheckersMovePayload, CheckersPosPayload } from "@vnejs/contracts.game.checkers";
3
+ export declare const BOARD_SIZE = 8;
4
+ export type CheckersColor = "white" | "black";
5
+ export type CheckersPiece = {
6
+ id: string;
7
+ color: CheckersColor;
8
+ king: boolean;
9
+ };
10
+ export type CheckersPos = CheckersPosPayload;
11
+ export type CheckersMove = CheckersMovePayload;
12
+ export type CheckersBoard = (CheckersPiece | null)[][];
13
+ export type CheckersGridItem = {
14
+ row: number;
15
+ column: number;
16
+ isDark: boolean;
17
+ piece: CheckersPiece | null;
18
+ isSelected?: boolean;
19
+ isLegalTarget?: boolean;
20
+ isFocus?: boolean;
21
+ };
22
+ export type CheckersViewState = {
23
+ isShow: boolean;
24
+ isForce: boolean;
25
+ board?: CheckersBoard;
26
+ turn?: CheckersColor;
27
+ selected?: CheckersPos | null;
28
+ legalMoves?: CheckersMove[];
29
+ winner?: CheckersColor | null;
30
+ statusText?: string;
31
+ gridRow?: number | null;
32
+ gridColumn?: number | null;
33
+ gridItems?: CheckersGridItem[];
34
+ gridIndex?: number;
35
+ gridsCount?: number;
36
+ };
37
+ export declare const isDarkSquare: (row: number, column: number) => boolean;
38
+ export declare const samePos: (a: CheckersPos, b: CheckersPos) => boolean;
39
+ export declare const buildGridItems: ({ board, selected, legalMoves, gridRow, gridColumn, }: {
40
+ board: CheckersBoard;
41
+ selected?: CheckersPos | null;
42
+ legalMoves?: CheckersMove[];
43
+ gridRow?: number | null;
44
+ gridColumn?: number | null;
45
+ }) => CheckersGridItem[];
46
+ export declare const listBoardPieces: (board: CheckersBoard) => (CheckersPiece & CheckersPosPayload)[];
47
+ export declare const emptyBoard: () => CheckersBoard;
48
+ export declare const statusForTurn: (turn?: CheckersColor, winner?: CheckersColor | null) => "" | "Black to move" | "Black wins" | "White to move" | "White wins";
49
+ export declare const fromGridChanged: (payload?: GridChangedPayload) => Partial<CheckersViewState>;
@@ -0,0 +1,47 @@
1
+ export const BOARD_SIZE = 8;
2
+ export const isDarkSquare = (row, column) => (row + column) % 2 === 1;
3
+ export const samePos = (a, b) => a.row === b.row && a.column === b.column;
4
+ export const buildGridItems = ({ board, selected = null, legalMoves = [], gridRow = null, gridColumn = null, }) => {
5
+ const legalKeys = new Set(legalMoves.map((move) => `${move.to.row}:${move.to.column}`));
6
+ const items = [];
7
+ for (let row = 0; row < BOARD_SIZE; row++) {
8
+ for (let column = 0; column < BOARD_SIZE; column++) {
9
+ items.push({
10
+ row,
11
+ column,
12
+ isDark: isDarkSquare(row, column),
13
+ piece: board[row]?.[column] ?? null,
14
+ isSelected: selected?.row === row && selected?.column === column,
15
+ isLegalTarget: legalKeys.has(`${row}:${column}`),
16
+ isFocus: gridRow === row && gridColumn === column,
17
+ });
18
+ }
19
+ }
20
+ return items;
21
+ };
22
+ export const listBoardPieces = (board) => {
23
+ const pieces = [];
24
+ for (let row = 0; row < BOARD_SIZE; row++) {
25
+ for (let column = 0; column < BOARD_SIZE; column++) {
26
+ const piece = board[row]?.[column];
27
+ if (!piece)
28
+ continue;
29
+ pieces.push({ ...piece, row, column });
30
+ }
31
+ }
32
+ return pieces;
33
+ };
34
+ export const emptyBoard = () => Array.from({ length: BOARD_SIZE }, () => Array.from({ length: BOARD_SIZE }, () => null));
35
+ export const statusForTurn = (turn, winner = null) => {
36
+ if (winner)
37
+ return winner === "white" ? "White wins" : "Black wins";
38
+ if (!turn)
39
+ return "";
40
+ return turn === "white" ? "White to move" : "Black to move";
41
+ };
42
+ export const fromGridChanged = (payload = {}) => ({
43
+ board: payload.cells,
44
+ // selection is view-only — reset on every game model change
45
+ selected: null,
46
+ legalMoves: [],
47
+ });
@@ -0,0 +1,3 @@
1
+ import type { ViewRenderFunc } from "@vnejs/module.components";
2
+ import type { CheckersViewState } from "../utils/checkers.js";
3
+ export declare const render: ViewRenderFunc<CheckersViewState>;