@mirasen/chessboard 0.1.0
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/CHANGELOG.md +7 -0
- package/LICENSE +21 -0
- package/README.md +96 -0
- package/assets/pieces/chessnut/ATTRIBUTION.md +13 -0
- package/assets/pieces/chessnut/LICENSE.txt +202 -0
- package/assets/pieces/chessnut/bB.svg +58 -0
- package/assets/pieces/chessnut/bK.svg +51 -0
- package/assets/pieces/chessnut/bN.svg +19 -0
- package/assets/pieces/chessnut/bP.svg +17 -0
- package/assets/pieces/chessnut/bQ.svg +97 -0
- package/assets/pieces/chessnut/bR.svg +34 -0
- package/assets/pieces/chessnut/wB.svg +61 -0
- package/assets/pieces/chessnut/wK.svg +71 -0
- package/assets/pieces/chessnut/wN.svg +19 -0
- package/assets/pieces/chessnut/wP.svg +17 -0
- package/assets/pieces/chessnut/wQ.svg +40 -0
- package/assets/pieces/chessnut/wR.svg +38 -0
- package/dist/animation/planner.d.ts +6 -0
- package/dist/animation/planner.js +128 -0
- package/dist/animation/render/fade.d.ts +7 -0
- package/dist/animation/render/fade.js +24 -0
- package/dist/animation/render/move.d.ts +7 -0
- package/dist/animation/render/move.js +28 -0
- package/dist/animation/render/plan.d.ts +14 -0
- package/dist/animation/render/plan.js +69 -0
- package/dist/animation/render/static.d.ts +7 -0
- package/dist/animation/render/static.js +21 -0
- package/dist/animation/render/types.d.ts +21 -0
- package/dist/animation/render/types.js +1 -0
- package/dist/animation/types.d.ts +46 -0
- package/dist/animation/types.js +6 -0
- package/dist/extensions/animation/factory.d.ts +3 -0
- package/dist/extensions/animation/factory.js +88 -0
- package/dist/extensions/animation/types.d.ts +11 -0
- package/dist/extensions/animation/types.js +1 -0
- package/dist/extensions/factory/main.d.ts +2 -0
- package/dist/extensions/factory/main.js +101 -0
- package/dist/extensions/factory/runtime.d.ts +5 -0
- package/dist/extensions/factory/runtime.js +43 -0
- package/dist/extensions/first-party/active-target/factory.d.ts +2 -0
- package/dist/extensions/first-party/active-target/factory.js +109 -0
- package/dist/extensions/first-party/active-target/types.d.ts +25 -0
- package/dist/extensions/first-party/active-target/types.js +15 -0
- package/dist/extensions/first-party/board-events/factory.d.ts +2 -0
- package/dist/extensions/first-party/board-events/factory.js +65 -0
- package/dist/extensions/first-party/board-events/types.d.ts +27 -0
- package/dist/extensions/first-party/board-events/types.js +2 -0
- package/dist/extensions/first-party/common/helpers.d.ts +8 -0
- package/dist/extensions/first-party/common/helpers.js +33 -0
- package/dist/extensions/first-party/common/types.d.ts +10 -0
- package/dist/extensions/first-party/common/types.js +1 -0
- package/dist/extensions/first-party/last-move/factory.d.ts +2 -0
- package/dist/extensions/first-party/last-move/factory.js +106 -0
- package/dist/extensions/first-party/last-move/types.d.ts +19 -0
- package/dist/extensions/first-party/last-move/types.js +6 -0
- package/dist/extensions/first-party/legal-moves/factory.d.ts +2 -0
- package/dist/extensions/first-party/legal-moves/factory.js +101 -0
- package/dist/extensions/first-party/legal-moves/types.d.ts +29 -0
- package/dist/extensions/first-party/legal-moves/types.js +19 -0
- package/dist/extensions/first-party/main-renderer/animation/factory.d.ts +4 -0
- package/dist/extensions/first-party/main-renderer/animation/factory.js +35 -0
- package/dist/extensions/first-party/main-renderer/animation/render.d.ts +7 -0
- package/dist/extensions/first-party/main-renderer/animation/render.js +46 -0
- package/dist/extensions/first-party/main-renderer/animation/types.d.ts +24 -0
- package/dist/extensions/first-party/main-renderer/animation/types.js +1 -0
- package/dist/extensions/first-party/main-renderer/animation/update.d.ts +3 -0
- package/dist/extensions/first-party/main-renderer/animation/update.js +40 -0
- package/dist/extensions/first-party/main-renderer/board/factory.d.ts +3 -0
- package/dist/extensions/first-party/main-renderer/board/factory.js +16 -0
- package/dist/extensions/first-party/main-renderer/board/render.d.ts +3 -0
- package/dist/extensions/first-party/main-renderer/board/render.js +24 -0
- package/dist/extensions/first-party/main-renderer/board/types.d.ts +10 -0
- package/dist/extensions/first-party/main-renderer/board/types.js +1 -0
- package/dist/extensions/first-party/main-renderer/board/update.d.ts +3 -0
- package/dist/extensions/first-party/main-renderer/board/update.js +27 -0
- package/dist/extensions/first-party/main-renderer/coordinates/factory.d.ts +3 -0
- package/dist/extensions/first-party/main-renderer/coordinates/factory.js +9 -0
- package/dist/extensions/first-party/main-renderer/coordinates/render.d.ts +3 -0
- package/dist/extensions/first-party/main-renderer/coordinates/render.js +60 -0
- package/dist/extensions/first-party/main-renderer/coordinates/types.d.ts +8 -0
- package/dist/extensions/first-party/main-renderer/coordinates/types.js +1 -0
- package/dist/extensions/first-party/main-renderer/drag/factory.d.ts +4 -0
- package/dist/extensions/first-party/main-renderer/drag/factory.js +23 -0
- package/dist/extensions/first-party/main-renderer/drag/render.d.ts +3 -0
- package/dist/extensions/first-party/main-renderer/drag/render.js +27 -0
- package/dist/extensions/first-party/main-renderer/drag/types.d.ts +16 -0
- package/dist/extensions/first-party/main-renderer/drag/types.js +1 -0
- package/dist/extensions/first-party/main-renderer/drag/update.d.ts +3 -0
- package/dist/extensions/first-party/main-renderer/drag/update.js +23 -0
- package/dist/extensions/first-party/main-renderer/factory.d.ts +2 -0
- package/dist/extensions/first-party/main-renderer/factory.js +95 -0
- package/dist/extensions/first-party/main-renderer/normalize.d.ts +4 -0
- package/dist/extensions/first-party/main-renderer/normalize.js +27 -0
- package/dist/extensions/first-party/main-renderer/pieces/factory.d.ts +3 -0
- package/dist/extensions/first-party/main-renderer/pieces/factory.js +27 -0
- package/dist/extensions/first-party/main-renderer/pieces/render.d.ts +3 -0
- package/dist/extensions/first-party/main-renderer/pieces/render.js +45 -0
- package/dist/extensions/first-party/main-renderer/pieces/suppress.d.ts +5 -0
- package/dist/extensions/first-party/main-renderer/pieces/suppress.js +9 -0
- package/dist/extensions/first-party/main-renderer/pieces/types.d.ts +20 -0
- package/dist/extensions/first-party/main-renderer/pieces/types.js +1 -0
- package/dist/extensions/first-party/main-renderer/pieces/update.d.ts +6 -0
- package/dist/extensions/first-party/main-renderer/pieces/update.js +31 -0
- package/dist/extensions/first-party/main-renderer/types/extension.d.ts +15 -0
- package/dist/extensions/first-party/main-renderer/types/extension.js +10 -0
- package/dist/extensions/first-party/main-renderer/types/input.d.ts +4 -0
- package/dist/extensions/first-party/main-renderer/types/input.js +1 -0
- package/dist/extensions/first-party/main-renderer/types/instance.d.ts +16 -0
- package/dist/extensions/first-party/main-renderer/types/instance.js +1 -0
- package/dist/extensions/first-party/main-renderer/types/internal.d.ts +17 -0
- package/dist/extensions/first-party/main-renderer/types/internal.js +30 -0
- package/dist/extensions/first-party/main-renderer/types/template.d.ts +5 -0
- package/dist/extensions/first-party/main-renderer/types/template.js +1 -0
- package/dist/extensions/first-party/selected-square/factory.d.ts +2 -0
- package/dist/extensions/first-party/selected-square/factory.js +88 -0
- package/dist/extensions/first-party/selected-square/types.d.ts +18 -0
- package/dist/extensions/first-party/selected-square/types.js +6 -0
- package/dist/extensions/index.d.ts +8 -0
- package/dist/extensions/index.js +6 -0
- package/dist/extensions/invalidation/factory.d.ts +2 -0
- package/dist/extensions/invalidation/factory.js +22 -0
- package/dist/extensions/invalidation/types.d.ts +11 -0
- package/dist/extensions/invalidation/types.js +1 -0
- package/dist/extensions/types/basic/animation.d.ts +28 -0
- package/dist/extensions/types/basic/animation.js +6 -0
- package/dist/extensions/types/basic/events.d.ts +34 -0
- package/dist/extensions/types/basic/events.js +7 -0
- package/dist/extensions/types/basic/mount.d.ts +7 -0
- package/dist/extensions/types/basic/mount.js +14 -0
- package/dist/extensions/types/basic/render.d.ts +9 -0
- package/dist/extensions/types/basic/render.js +1 -0
- package/dist/extensions/types/basic/transient-visuals.d.ts +10 -0
- package/dist/extensions/types/basic/transient-visuals.js +1 -0
- package/dist/extensions/types/basic/update.d.ts +16 -0
- package/dist/extensions/types/basic/update.js +11 -0
- package/dist/extensions/types/context/animation.d.ts +31 -0
- package/dist/extensions/types/context/animation.js +9 -0
- package/dist/extensions/types/context/render.d.ts +6 -0
- package/dist/extensions/types/context/render.js +1 -0
- package/dist/extensions/types/context/transient-visuals.d.ts +7 -0
- package/dist/extensions/types/context/transient-visuals.js +1 -0
- package/dist/extensions/types/context/ui-move.d.ts +16 -0
- package/dist/extensions/types/context/ui-move.js +1 -0
- package/dist/extensions/types/context/update.d.ts +25 -0
- package/dist/extensions/types/context/update.js +10 -0
- package/dist/extensions/types/extension.d.ts +40 -0
- package/dist/extensions/types/extension.js +1 -0
- package/dist/extensions/types/main.d.ts +44 -0
- package/dist/extensions/types/main.js +1 -0
- package/dist/extensions/types/surface/commands.d.ts +26 -0
- package/dist/extensions/types/surface/commands.js +1 -0
- package/dist/extensions/types/surface/events.d.ts +5 -0
- package/dist/extensions/types/surface/events.js +1 -0
- package/dist/extensions/types/surface/main.d.ts +10 -0
- package/dist/extensions/types/surface/main.js +1 -0
- package/dist/extensions/types/surface/transient-visuals.d.ts +4 -0
- package/dist/extensions/types/surface/transient-visuals.js +1 -0
- package/dist/extensions/types/wrapper.d.ts +26 -0
- package/dist/extensions/types/wrapper.js +11 -0
- package/dist/extensions/update.d.ts +4 -0
- package/dist/extensions/update.js +29 -0
- package/dist/helpers/util.d.ts +1 -0
- package/dist/helpers/util.js +9 -0
- package/dist/index.d.ts +4 -0
- package/dist/index.js +1 -0
- package/dist/layout/factory.d.ts +2 -0
- package/dist/layout/factory.js +33 -0
- package/dist/layout/geometry/factory.d.ts +12 -0
- package/dist/layout/geometry/factory.js +33 -0
- package/dist/layout/geometry/types.d.ts +20 -0
- package/dist/layout/geometry/types.js +1 -0
- package/dist/layout/helpers.d.ts +4 -0
- package/dist/layout/helpers.js +6 -0
- package/dist/layout/mutation.d.ts +6 -0
- package/dist/layout/mutation.js +1 -0
- package/dist/layout/reducers.d.ts +2 -0
- package/dist/layout/reducers.js +30 -0
- package/dist/layout/types.d.ts +23 -0
- package/dist/layout/types.js +1 -0
- package/dist/mutation/pipeline.d.ts +2 -0
- package/dist/mutation/pipeline.js +36 -0
- package/dist/mutation/session.d.ts +3 -0
- package/dist/mutation/session.js +66 -0
- package/dist/mutation/types.d.ts +26 -0
- package/dist/mutation/types.js +1 -0
- package/dist/render/factory.d.ts +2 -0
- package/dist/render/factory.js +117 -0
- package/dist/render/mount.d.ts +3 -0
- package/dist/render/mount.js +28 -0
- package/dist/render/rendering/animation.d.ts +13 -0
- package/dist/render/rendering/animation.js +92 -0
- package/dist/render/rendering/helpers.d.ts +3 -0
- package/dist/render/rendering/helpers.js +10 -0
- package/dist/render/rendering/state.d.ts +4 -0
- package/dist/render/rendering/state.js +48 -0
- package/dist/render/rendering/visuals.d.ts +3 -0
- package/dist/render/rendering/visuals.js +21 -0
- package/dist/render/scheduler/scheduler.d.ts +2 -0
- package/dist/render/scheduler/scheduler.js +53 -0
- package/dist/render/scheduler/types.d.ts +12 -0
- package/dist/render/scheduler/types.js +1 -0
- package/dist/render/svg/factory.d.ts +4 -0
- package/dist/render/svg/factory.js +38 -0
- package/dist/render/svg/helpers.d.ts +18 -0
- package/dist/render/svg/helpers.js +34 -0
- package/dist/render/types.d.ts +42 -0
- package/dist/render/types.js +1 -0
- package/dist/runtime/factory/helpers.d.ts +6 -0
- package/dist/runtime/factory/helpers.js +42 -0
- package/dist/runtime/factory/input.d.ts +3 -0
- package/dist/runtime/factory/input.js +98 -0
- package/dist/runtime/factory/main.d.ts +2 -0
- package/dist/runtime/factory/main.js +182 -0
- package/dist/runtime/factory/ui-move.d.ts +4 -0
- package/dist/runtime/factory/ui-move.js +84 -0
- package/dist/runtime/input/adapter/factory.d.ts +2 -0
- package/dist/runtime/input/adapter/factory.js +55 -0
- package/dist/runtime/input/adapter/helpers.d.ts +5 -0
- package/dist/runtime/input/adapter/helpers.js +23 -0
- package/dist/runtime/input/adapter/pointer.d.ts +13 -0
- package/dist/runtime/input/adapter/pointer.js +97 -0
- package/dist/runtime/input/adapter/types.d.ts +16 -0
- package/dist/runtime/input/adapter/types.js +1 -0
- package/dist/runtime/input/controller/factory.d.ts +2 -0
- package/dist/runtime/input/controller/factory.js +32 -0
- package/dist/runtime/input/controller/helpers.d.ts +3 -0
- package/dist/runtime/input/controller/helpers.js +12 -0
- package/dist/runtime/input/controller/pointer.d.ts +7 -0
- package/dist/runtime/input/controller/pointer.js +104 -0
- package/dist/runtime/input/controller/transient-visuals.d.ts +3 -0
- package/dist/runtime/input/controller/transient-visuals.js +12 -0
- package/dist/runtime/input/controller/types.d.ts +25 -0
- package/dist/runtime/input/controller/types.js +1 -0
- package/dist/runtime/layout.d.ts +2 -0
- package/dist/runtime/layout.js +10 -0
- package/dist/runtime/lifecycle.d.ts +12 -0
- package/dist/runtime/lifecycle.js +49 -0
- package/dist/runtime/mutation/extension-update.d.ts +2 -0
- package/dist/runtime/mutation/extension-update.js +26 -0
- package/dist/runtime/mutation/factory.d.ts +2 -0
- package/dist/runtime/mutation/factory.js +47 -0
- package/dist/runtime/mutation/interaction.d.ts +2 -0
- package/dist/runtime/mutation/interaction.js +34 -0
- package/dist/runtime/mutation/invalidation.d.ts +0 -0
- package/dist/runtime/mutation/invalidation.js +2 -0
- package/dist/runtime/mutation/last-move.d.ts +3 -0
- package/dist/runtime/mutation/last-move.js +19 -0
- package/dist/runtime/mutation/layout.d.ts +2 -0
- package/dist/runtime/mutation/layout.js +8 -0
- package/dist/runtime/mutation/pipeline.d.ts +22 -0
- package/dist/runtime/mutation/pipeline.js +1 -0
- package/dist/runtime/mutation/request-render.d.ts +2 -0
- package/dist/runtime/mutation/request-render.js +29 -0
- package/dist/runtime/mutation/run.d.ts +2 -0
- package/dist/runtime/mutation/run.js +3 -0
- package/dist/runtime/mutation/types.d.ts +14 -0
- package/dist/runtime/mutation/types.js +1 -0
- package/dist/runtime/types/main.d.ts +36 -0
- package/dist/runtime/types/main.js +1 -0
- package/dist/runtime/types/ui-move.d.ts +9 -0
- package/dist/runtime/types/ui-move.js +1 -0
- package/dist/state/board/check.d.ts +27 -0
- package/dist/state/board/check.js +148 -0
- package/dist/state/board/coords.d.ts +4 -0
- package/dist/state/board/coords.js +9 -0
- package/dist/state/board/denormalize.d.ts +11 -0
- package/dist/state/board/denormalize.js +77 -0
- package/dist/state/board/factory.d.ts +3 -0
- package/dist/state/board/factory.js +46 -0
- package/dist/state/board/mutation.d.ts +9 -0
- package/dist/state/board/mutation.js +1 -0
- package/dist/state/board/normalize.d.ts +8 -0
- package/dist/state/board/normalize.js +76 -0
- package/dist/state/board/piece.d.ts +3 -0
- package/dist/state/board/piece.js +11 -0
- package/dist/state/board/position.d.ts +4 -0
- package/dist/state/board/position.js +94 -0
- package/dist/state/board/reducers.d.ts +8 -0
- package/dist/state/board/reducers.js +80 -0
- package/dist/state/board/types/input.d.ts +35 -0
- package/dist/state/board/types/input.js +1 -0
- package/dist/state/board/types/internal.d.ts +54 -0
- package/dist/state/board/types/internal.js +17 -0
- package/dist/state/board/types/main.d.ts +18 -0
- package/dist/state/board/types/main.js +1 -0
- package/dist/state/board/types/output.d.ts +5 -0
- package/dist/state/board/types/output.js +1 -0
- package/dist/state/board/types/template.d.ts +23 -0
- package/dist/state/board/types/template.js +1 -0
- package/dist/state/change/factory.d.ts +2 -0
- package/dist/state/change/factory.js +28 -0
- package/dist/state/change/helpers.d.ts +8 -0
- package/dist/state/change/helpers.js +86 -0
- package/dist/state/change/mutation.d.ts +6 -0
- package/dist/state/change/mutation.js +1 -0
- package/dist/state/change/reducers.d.ts +5 -0
- package/dist/state/change/reducers.js +16 -0
- package/dist/state/change/types.d.ts +17 -0
- package/dist/state/change/types.js +1 -0
- package/dist/state/factory.d.ts +2 -0
- package/dist/state/factory.js +24 -0
- package/dist/state/interaction/factory.d.ts +2 -0
- package/dist/state/interaction/factory.js +83 -0
- package/dist/state/interaction/helpers.d.ts +4 -0
- package/dist/state/interaction/helpers.js +16 -0
- package/dist/state/interaction/movability.d.ts +5 -0
- package/dist/state/interaction/movability.js +87 -0
- package/dist/state/interaction/mutation.d.ts +11 -0
- package/dist/state/interaction/mutation.js +1 -0
- package/dist/state/interaction/normalize.d.ts +5 -0
- package/dist/state/interaction/normalize.js +50 -0
- package/dist/state/interaction/reducers.d.ts +11 -0
- package/dist/state/interaction/reducers.js +81 -0
- package/dist/state/interaction/types/input.d.ts +18 -0
- package/dist/state/interaction/types/input.js +1 -0
- package/dist/state/interaction/types/internal.d.ts +35 -0
- package/dist/state/interaction/types/internal.js +1 -0
- package/dist/state/interaction/types/main.d.ts +33 -0
- package/dist/state/interaction/types/main.js +1 -0
- package/dist/state/types.d.ts +30 -0
- package/dist/state/types.js +1 -0
- package/dist/state/view/factory.d.ts +2 -0
- package/dist/state/view/factory.js +24 -0
- package/dist/state/view/mutation.d.ts +5 -0
- package/dist/state/view/mutation.js +1 -0
- package/dist/state/view/reducers.d.ts +3 -0
- package/dist/state/view/reducers.js +6 -0
- package/dist/state/view/types/internal.d.ts +6 -0
- package/dist/state/view/types/internal.js +1 -0
- package/dist/state/view/types/main.d.ts +12 -0
- package/dist/state/view/types/main.js +1 -0
- package/dist/wrapper/factory.d.ts +3 -0
- package/dist/wrapper/factory.js +61 -0
- package/dist/wrapper/types.d.ts +56 -0
- package/dist/wrapper/types.js +1 -0
- package/package.json +98 -0
|
@@ -0,0 +1,47 @@
|
|
|
1
|
+
import { createMutationPipeline } from '../../mutation/pipeline';
|
|
2
|
+
import { extensionSystemUpdatePipe } from './extension-update';
|
|
3
|
+
import { reconcileInteractionSelectionAfterBoardStateChange } from './interaction';
|
|
4
|
+
import { clearLastMoveOnBoardSetPosition, updateLastMoveOnBoardMove } from './last-move';
|
|
5
|
+
import { layoutRefreshGeometryPipe } from './layout';
|
|
6
|
+
import { requestRenderPipe } from './request-render';
|
|
7
|
+
function buildPreviousContext(current) {
|
|
8
|
+
return {
|
|
9
|
+
state: current.state.getSnapshot(),
|
|
10
|
+
layout: current.layout.getSnapshot()
|
|
11
|
+
};
|
|
12
|
+
}
|
|
13
|
+
export function createRuntimeMutationPipeline() {
|
|
14
|
+
let previousContext = null;
|
|
15
|
+
// Construct the pipes
|
|
16
|
+
const pipes = [
|
|
17
|
+
updateLastMoveOnBoardMove,
|
|
18
|
+
clearLastMoveOnBoardSetPosition,
|
|
19
|
+
reconcileInteractionSelectionAfterBoardStateChange,
|
|
20
|
+
layoutRefreshGeometryPipe,
|
|
21
|
+
extensionSystemUpdatePipe,
|
|
22
|
+
requestRenderPipe
|
|
23
|
+
];
|
|
24
|
+
const basicPipeline = createMutationPipeline(pipes);
|
|
25
|
+
return {
|
|
26
|
+
getSession() {
|
|
27
|
+
return basicPipeline.getSession();
|
|
28
|
+
},
|
|
29
|
+
addMutation: basicPipeline.addMutation,
|
|
30
|
+
run(currentContext) {
|
|
31
|
+
const pipelineContext = {
|
|
32
|
+
previous: previousContext,
|
|
33
|
+
current: currentContext
|
|
34
|
+
};
|
|
35
|
+
let didRun = false;
|
|
36
|
+
try {
|
|
37
|
+
didRun = basicPipeline.run(pipelineContext);
|
|
38
|
+
}
|
|
39
|
+
finally {
|
|
40
|
+
if (didRun) {
|
|
41
|
+
previousContext = buildPreviousContext(currentContext);
|
|
42
|
+
}
|
|
43
|
+
}
|
|
44
|
+
return didRun;
|
|
45
|
+
}
|
|
46
|
+
};
|
|
47
|
+
}
|
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
import { isEmptyPieceCode } from '../../state/board/check';
|
|
2
|
+
import { selectedEqual } from '../../state/interaction/helpers';
|
|
3
|
+
export const reconcileInteractionSelectionAfterBoardStateChange = (context, mutationSession) => {
|
|
4
|
+
const { current } = context;
|
|
5
|
+
if (!mutationSession.hasMutation({ prefixes: ['state.board.'] }) ||
|
|
6
|
+
mutationSession.hasMutation({
|
|
7
|
+
causes: [
|
|
8
|
+
'state.interaction.clear',
|
|
9
|
+
'runtime.interaction.dropTo',
|
|
10
|
+
'runtime.interaction.releaseTo'
|
|
11
|
+
]
|
|
12
|
+
})) {
|
|
13
|
+
return;
|
|
14
|
+
}
|
|
15
|
+
const currentSelected = current.state.interaction.selected ?? null;
|
|
16
|
+
const currentSelectedSquare = currentSelected?.square ?? null;
|
|
17
|
+
const wouldBeCurrentSelectedPieceCode = currentSelectedSquare !== null
|
|
18
|
+
? current.state.board.getPieceCodeAt(currentSelectedSquare)
|
|
19
|
+
: null;
|
|
20
|
+
const wouldBeCurrentSelected = currentSelectedSquare !== null &&
|
|
21
|
+
wouldBeCurrentSelectedPieceCode !== null &&
|
|
22
|
+
!isEmptyPieceCode(wouldBeCurrentSelectedPieceCode)
|
|
23
|
+
? {
|
|
24
|
+
square: currentSelectedSquare,
|
|
25
|
+
pieceCode: wouldBeCurrentSelectedPieceCode
|
|
26
|
+
}
|
|
27
|
+
: null;
|
|
28
|
+
const notChanged = selectedEqual(currentSelected, wouldBeCurrentSelected);
|
|
29
|
+
if (notChanged) {
|
|
30
|
+
current.state.interaction.updateActiveDestinations(mutationSession);
|
|
31
|
+
return;
|
|
32
|
+
}
|
|
33
|
+
current.state.interaction.clear(mutationSession);
|
|
34
|
+
};
|
|
File without changes
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
import assert from '@ktarmyshov/assert';
|
|
2
|
+
export const updateLastMoveOnBoardMove = (context, mutationSession) => {
|
|
3
|
+
const { current } = context;
|
|
4
|
+
if (!mutationSession.hasMutation({ causes: ['state.board.move'] })) {
|
|
5
|
+
return;
|
|
6
|
+
}
|
|
7
|
+
const lastMove = mutationSession.getPayloads('state.board.move')?.[0];
|
|
8
|
+
assert(lastMove, 'Expected last move payload to be present after state.board.move mutation');
|
|
9
|
+
current.state.change.setLastMove(lastMove, mutationSession);
|
|
10
|
+
};
|
|
11
|
+
export const clearLastMoveOnBoardSetPosition = (context, mutationSession) => {
|
|
12
|
+
const { current } = context;
|
|
13
|
+
if (!mutationSession.hasMutation({
|
|
14
|
+
causes: ['state.board.setPosition', 'state.board.setPiecePosition']
|
|
15
|
+
})) {
|
|
16
|
+
return;
|
|
17
|
+
}
|
|
18
|
+
current.state.change.setLastMove(null, mutationSession);
|
|
19
|
+
};
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
export const layoutRefreshGeometryPipe = (context, mutationSession) => {
|
|
2
|
+
const { current } = context;
|
|
3
|
+
if (mutationSession.hasMutation({ causes: ['state.view.setOrientation'] })) {
|
|
4
|
+
current.layout.refreshGeometry({
|
|
5
|
+
orientation: current.state.view.orientation
|
|
6
|
+
}, mutationSession);
|
|
7
|
+
}
|
|
8
|
+
};
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
import { ExtensionSystem } from '../../extensions/types/main';
|
|
2
|
+
import { Layout, LayoutSnapshot } from '../../layout/types';
|
|
3
|
+
import { MutationPipe, MutationPipeline } from '../../mutation/types';
|
|
4
|
+
import { RenderSystem } from '../../render/types';
|
|
5
|
+
import { RuntimeState, RuntimeStateSnapshot } from '../../state/types';
|
|
6
|
+
import { RuntimeMutationPayloadByCause } from './types';
|
|
7
|
+
export interface RuntimeMutationPipelineContext {
|
|
8
|
+
readonly state: RuntimeState;
|
|
9
|
+
readonly layout: Layout;
|
|
10
|
+
readonly renderSystem: RenderSystem;
|
|
11
|
+
readonly extensionSystem: ExtensionSystem;
|
|
12
|
+
}
|
|
13
|
+
export type RuntimeMutationPipeline = MutationPipeline<RuntimeMutationPayloadByCause, RuntimeMutationPipelineContext>;
|
|
14
|
+
export interface RuntimeMutationPipeContextPrevious {
|
|
15
|
+
readonly state: RuntimeStateSnapshot;
|
|
16
|
+
readonly layout: LayoutSnapshot;
|
|
17
|
+
}
|
|
18
|
+
export interface RuntimeMutationPipeContext {
|
|
19
|
+
previous: RuntimeMutationPipeContextPrevious | null;
|
|
20
|
+
current: RuntimeMutationPipelineContext;
|
|
21
|
+
}
|
|
22
|
+
export type RuntimeMutationPipe = MutationPipe<RuntimeMutationPayloadByCause, RuntimeMutationPipeContext>;
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
import { isFrameRenderable } from '../../extensions/types/basic/update';
|
|
2
|
+
export const requestRenderPipe = (context) => {
|
|
3
|
+
const { current } = context;
|
|
4
|
+
const currentFrame = current.extensionSystem.currentFrame;
|
|
5
|
+
if (currentFrame === null || !isFrameRenderable(currentFrame))
|
|
6
|
+
return; // no-op if we can't render
|
|
7
|
+
if (needsRender(context)) {
|
|
8
|
+
current.renderSystem.requestRender(currentFrame);
|
|
9
|
+
}
|
|
10
|
+
if (needsRenderAnimation(context)) {
|
|
11
|
+
current.renderSystem.requestRenderAnimation();
|
|
12
|
+
}
|
|
13
|
+
};
|
|
14
|
+
function needsRender(context) {
|
|
15
|
+
for (const extensionRec of context.current.renderSystem.extensions.values()) {
|
|
16
|
+
if (extensionRec.extension.invalidation.dirtyLayers !== 0) {
|
|
17
|
+
return true;
|
|
18
|
+
}
|
|
19
|
+
}
|
|
20
|
+
return false;
|
|
21
|
+
}
|
|
22
|
+
function needsRenderAnimation(context) {
|
|
23
|
+
for (const extensionRec of context.current.renderSystem.extensions.values()) {
|
|
24
|
+
if (extensionRec.extension.animation.getAll(['submitted', 'active']).length > 0) {
|
|
25
|
+
return true;
|
|
26
|
+
}
|
|
27
|
+
}
|
|
28
|
+
return false;
|
|
29
|
+
}
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
import { LayoutMutationPayloadByCause } from '../../layout/mutation';
|
|
2
|
+
import { MutationSession, ReadonlyMutationSession } from '../../mutation/types';
|
|
3
|
+
import { BoardStateMutationPayloadByCause } from '../../state/board/mutation';
|
|
4
|
+
import { ChangeStateMutationPayloadByCause } from '../../state/change/mutation';
|
|
5
|
+
import { InteractionStateMutationPayloadByCause } from '../../state/interaction/mutation';
|
|
6
|
+
import { ViewStateMutationPayloadByCause } from '../../state/view/mutation';
|
|
7
|
+
type RuntimeOwnMutationPayloadByCause = {
|
|
8
|
+
'runtime.interaction.dropTo': undefined;
|
|
9
|
+
'runtime.interaction.releaseTo': undefined;
|
|
10
|
+
};
|
|
11
|
+
export type RuntimeMutationPayloadByCause = BoardStateMutationPayloadByCause & ChangeStateMutationPayloadByCause & InteractionStateMutationPayloadByCause & ViewStateMutationPayloadByCause & LayoutMutationPayloadByCause & RuntimeOwnMutationPayloadByCause;
|
|
12
|
+
export type RuntimeMutationSession = MutationSession<RuntimeMutationPayloadByCause>;
|
|
13
|
+
export type RuntimeReadonlyMutationSession = ReadonlyMutationSession<RuntimeMutationPayloadByCause>;
|
|
14
|
+
export {};
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
import { AnyExtensionDefinition } from '../../extensions/types/extension';
|
|
2
|
+
import { ExtensionRuntimeSurfaceCommands } from '../../extensions/types/surface/commands';
|
|
3
|
+
import { RuntimeStateInitOptions } from '../../state/types';
|
|
4
|
+
import { InputAdapter } from '../input/adapter/types';
|
|
5
|
+
import { InteractionController } from '../input/controller/types';
|
|
6
|
+
import { RuntimeMutationPipeline, RuntimeMutationPipelineContext } from '../mutation/pipeline';
|
|
7
|
+
export type RuntimeStatus = 'constructing' | 'unmounted' | 'mounted' | 'destroyed';
|
|
8
|
+
export interface RuntimeInternal extends RuntimeMutationPipelineContext {
|
|
9
|
+
readonly mutation: RuntimeMutationPipeline;
|
|
10
|
+
readonly interactionController: InteractionController;
|
|
11
|
+
inputAdapter: InputAdapter | null;
|
|
12
|
+
resizeObserver: ResizeObserver | null;
|
|
13
|
+
}
|
|
14
|
+
export type GetInternalState = () => RuntimeInternal;
|
|
15
|
+
export interface RuntimeInitOptions {
|
|
16
|
+
doc: Document;
|
|
17
|
+
state?: RuntimeStateInitOptions;
|
|
18
|
+
extensions?: readonly AnyExtensionDefinition[];
|
|
19
|
+
}
|
|
20
|
+
export interface RuntimeInitOptionsInternal extends RuntimeInitOptions {
|
|
21
|
+
extensionRuntimeSurfaceCommands: ExtensionRuntimeSurfaceCommands;
|
|
22
|
+
getInternalState: GetInternalState;
|
|
23
|
+
}
|
|
24
|
+
/**
|
|
25
|
+
* Public interface for the internal runtime.
|
|
26
|
+
* Orchestrates board state, view state, interaction state, and invalidation.
|
|
27
|
+
* Board/view/interaction reducers own mutation logic; the runtime coordinates scheduling
|
|
28
|
+
* and geometry updates in response to state changes.
|
|
29
|
+
*/
|
|
30
|
+
export interface Runtime extends ExtensionRuntimeSurfaceCommands {
|
|
31
|
+
readonly status: RuntimeStatus;
|
|
32
|
+
mount(container: HTMLElement): void;
|
|
33
|
+
unmount(): void;
|
|
34
|
+
destroy(): void;
|
|
35
|
+
getExtensionsPublicRecord(): Readonly<Record<string, unknown>>;
|
|
36
|
+
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
import { ExtensionUIMoveRequestStatus } from '../../extensions/types/context/ui-move';
|
|
2
|
+
import { MoveRequest, Square } from '../../state/board/types/internal';
|
|
3
|
+
import { MoveDestinationSnapshot } from '../../state/interaction/types/internal';
|
|
4
|
+
export interface UIMoveRequestContextInternal {
|
|
5
|
+
readonly sourceSquare: Square;
|
|
6
|
+
readonly destination: MoveDestinationSnapshot;
|
|
7
|
+
status: ExtensionUIMoveRequestStatus;
|
|
8
|
+
resolvedMoveRequest: MoveRequest | null;
|
|
9
|
+
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
import { Color, ColorShort, MoveRequestInput, Piece, PiecePositionInput, PiecePositionRecord, PiecePositionRecordShort, PiecePositionRecordString, PieceShort, PieceString, Role, RoleShort, SquareString } from './types/input';
|
|
2
|
+
import { EmptyPieceCode, MoveRequest, NonEmptyPieceCode, PieceCode, RolePromotionCode, Square, SquareFile, SquareRank } from './types/internal';
|
|
3
|
+
import { BoardStateSnapshot } from './types/main';
|
|
4
|
+
export declare function isWhitePieceCode(code: NonEmptyPieceCode): boolean;
|
|
5
|
+
export declare function isBlackPieceCode(code: NonEmptyPieceCode): boolean;
|
|
6
|
+
export declare function isOccupied(board: BoardStateSnapshot, sq: Square): boolean;
|
|
7
|
+
export declare function isEmptyPieceCode(code: PieceCode): code is EmptyPieceCode;
|
|
8
|
+
export declare function isNonEmptyPieceCode(code: PieceCode): code is NonEmptyPieceCode;
|
|
9
|
+
export declare function piecePositionsEqual(pos1: BoardStateSnapshot, pos2: BoardStateSnapshot): boolean;
|
|
10
|
+
export declare function isRank(n: number): n is SquareRank;
|
|
11
|
+
export declare function isFile(n: number): n is SquareFile;
|
|
12
|
+
export declare function isValidSquare(n: number): n is Square;
|
|
13
|
+
export declare function assertValidSquare(n: number): asserts n is Square;
|
|
14
|
+
export declare function isRolePromotionCode(role: number): role is RolePromotionCode;
|
|
15
|
+
export declare function isPieceString(value: unknown): value is PieceString;
|
|
16
|
+
export declare function isSquareString(value: unknown): value is SquareString;
|
|
17
|
+
export declare function isPositionMapString(pos: PiecePositionInput): pos is PiecePositionRecordString;
|
|
18
|
+
export declare function isColorShort(value: unknown): value is ColorShort;
|
|
19
|
+
export declare function isRoleShort(value: unknown): value is RoleShort;
|
|
20
|
+
export declare function isPieceShort(value: unknown): value is PieceShort;
|
|
21
|
+
export declare function isPositionMapShort(pos: PiecePositionInput): pos is PiecePositionRecordShort;
|
|
22
|
+
export declare function isColor(value: unknown): value is Color;
|
|
23
|
+
export declare function isRole(value: unknown): value is Role;
|
|
24
|
+
export declare function isPiece(value: unknown): value is Piece;
|
|
25
|
+
export declare function isPositionMap(pos: PiecePositionInput): pos is PiecePositionRecord;
|
|
26
|
+
export declare function assertValidRoleCode(n: number): asserts n is RolePromotionCode;
|
|
27
|
+
export declare function isNormalizedMoveRequest(input: MoveRequest | MoveRequestInput): input is MoveRequest;
|
|
@@ -0,0 +1,148 @@
|
|
|
1
|
+
import { SQUARE_COUNT } from './types/internal';
|
|
2
|
+
export function isWhitePieceCode(code) {
|
|
3
|
+
return code > 0 /* PieceCode.Empty */ && code < 8 /* ColorCode.Black */;
|
|
4
|
+
}
|
|
5
|
+
export function isBlackPieceCode(code) {
|
|
6
|
+
return code >= 8 /* ColorCode.Black */;
|
|
7
|
+
}
|
|
8
|
+
export function isOccupied(board, sq) {
|
|
9
|
+
return board.pieces[sq] > 0 /* PieceCode.Empty */;
|
|
10
|
+
}
|
|
11
|
+
export function isEmptyPieceCode(code) {
|
|
12
|
+
return code === 0 /* PieceCode.Empty */;
|
|
13
|
+
}
|
|
14
|
+
export function isNonEmptyPieceCode(code) {
|
|
15
|
+
return code !== 0 /* PieceCode.Empty */;
|
|
16
|
+
}
|
|
17
|
+
export function piecePositionsEqual(pos1, pos2) {
|
|
18
|
+
if (pos1.positionEpoch !== pos2.positionEpoch)
|
|
19
|
+
return false;
|
|
20
|
+
for (let i = 0; i < pos1.pieces.length; i++) {
|
|
21
|
+
if (pos1.pieces[i] !== pos2.pieces[i])
|
|
22
|
+
return false;
|
|
23
|
+
}
|
|
24
|
+
return true;
|
|
25
|
+
}
|
|
26
|
+
export function isRank(n) {
|
|
27
|
+
return Number.isInteger(n) && n >= 0 && n < 8;
|
|
28
|
+
}
|
|
29
|
+
export function isFile(n) {
|
|
30
|
+
return Number.isInteger(n) && n >= 0 && n < 8;
|
|
31
|
+
}
|
|
32
|
+
export function isValidSquare(n) {
|
|
33
|
+
return Number.isInteger(n) && n >= 0 && n < SQUARE_COUNT;
|
|
34
|
+
}
|
|
35
|
+
export function assertValidSquare(n) {
|
|
36
|
+
if (!isValidSquare(n)) {
|
|
37
|
+
throw new RangeError(`Invalid square index: ${n}`);
|
|
38
|
+
}
|
|
39
|
+
}
|
|
40
|
+
export function isRolePromotionCode(role) {
|
|
41
|
+
return (role === 2 /* RoleCode.Knight */ ||
|
|
42
|
+
role === 3 /* RoleCode.Bishop */ ||
|
|
43
|
+
role === 4 /* RoleCode.Rook */ ||
|
|
44
|
+
role === 5 /* RoleCode.Queen */);
|
|
45
|
+
}
|
|
46
|
+
const PIECE_STRING_REGEX = /^[bw][KQRBNP]$/;
|
|
47
|
+
export function isPieceString(value) {
|
|
48
|
+
return typeof value === 'string' && PIECE_STRING_REGEX.test(value);
|
|
49
|
+
}
|
|
50
|
+
export function isSquareString(value) {
|
|
51
|
+
return (typeof value === 'string' &&
|
|
52
|
+
value.length === 2 &&
|
|
53
|
+
value[0] >= 'a' &&
|
|
54
|
+
value[0] <= 'h' &&
|
|
55
|
+
value[1] >= '1' &&
|
|
56
|
+
value[1] <= '8');
|
|
57
|
+
}
|
|
58
|
+
export function isPositionMapString(pos) {
|
|
59
|
+
for (const [sq, piece] of Object.entries(pos)) {
|
|
60
|
+
if (!isSquareString(sq)) {
|
|
61
|
+
return false;
|
|
62
|
+
}
|
|
63
|
+
if (!isPieceString(piece)) {
|
|
64
|
+
return false;
|
|
65
|
+
}
|
|
66
|
+
}
|
|
67
|
+
return true;
|
|
68
|
+
}
|
|
69
|
+
export function isColorShort(value) {
|
|
70
|
+
return value === 'w' || value === 'b';
|
|
71
|
+
}
|
|
72
|
+
export function isRoleShort(value) {
|
|
73
|
+
return (value === 'K' ||
|
|
74
|
+
value === 'Q' ||
|
|
75
|
+
value === 'R' ||
|
|
76
|
+
value === 'B' ||
|
|
77
|
+
value === 'N' ||
|
|
78
|
+
value === 'P');
|
|
79
|
+
}
|
|
80
|
+
export function isPieceShort(value) {
|
|
81
|
+
return (typeof value === 'object' &&
|
|
82
|
+
value !== null &&
|
|
83
|
+
'color' in value &&
|
|
84
|
+
'role' in value &&
|
|
85
|
+
isColorShort(value.color) &&
|
|
86
|
+
isRoleShort(value.role));
|
|
87
|
+
}
|
|
88
|
+
export function isPositionMapShort(pos) {
|
|
89
|
+
for (const [sq, piece] of Object.entries(pos)) {
|
|
90
|
+
if (!isSquareString(sq)) {
|
|
91
|
+
return false;
|
|
92
|
+
}
|
|
93
|
+
if (!isPieceShort(piece)) {
|
|
94
|
+
return false;
|
|
95
|
+
}
|
|
96
|
+
}
|
|
97
|
+
return true;
|
|
98
|
+
}
|
|
99
|
+
export function isColor(value) {
|
|
100
|
+
return value === 'white' || value === 'black';
|
|
101
|
+
}
|
|
102
|
+
export function isRole(value) {
|
|
103
|
+
return (value === 'king' ||
|
|
104
|
+
value === 'queen' ||
|
|
105
|
+
value === 'rook' ||
|
|
106
|
+
value === 'bishop' ||
|
|
107
|
+
value === 'knight' ||
|
|
108
|
+
value === 'pawn');
|
|
109
|
+
}
|
|
110
|
+
export function isPiece(value) {
|
|
111
|
+
return (typeof value === 'object' &&
|
|
112
|
+
value !== null &&
|
|
113
|
+
'color' in value &&
|
|
114
|
+
'role' in value &&
|
|
115
|
+
isColor(value.color) &&
|
|
116
|
+
isRole(value.role));
|
|
117
|
+
}
|
|
118
|
+
export function isPositionMap(pos) {
|
|
119
|
+
for (const [sq, piece] of Object.entries(pos)) {
|
|
120
|
+
if (!isSquareString(sq)) {
|
|
121
|
+
return false;
|
|
122
|
+
}
|
|
123
|
+
if (!isPiece(piece)) {
|
|
124
|
+
return false;
|
|
125
|
+
}
|
|
126
|
+
}
|
|
127
|
+
return true;
|
|
128
|
+
}
|
|
129
|
+
export function assertValidRoleCode(n) {
|
|
130
|
+
if (!isRolePromotionCode(n)) {
|
|
131
|
+
throw new RangeError(`Invalid role promotion code: ${n}`);
|
|
132
|
+
}
|
|
133
|
+
}
|
|
134
|
+
export function isNormalizedMoveRequest(input) {
|
|
135
|
+
return (typeof input === 'object' &&
|
|
136
|
+
input !== null &&
|
|
137
|
+
'from' in input &&
|
|
138
|
+
'to' in input &&
|
|
139
|
+
typeof input.from === 'number' &&
|
|
140
|
+
isValidSquare(input.from) &&
|
|
141
|
+
typeof input.to === 'number' &&
|
|
142
|
+
isValidSquare(input.to) &&
|
|
143
|
+
(input.capturedSquare === undefined ||
|
|
144
|
+
(typeof input.capturedSquare === 'number' && isValidSquare(input.capturedSquare))) &&
|
|
145
|
+
(input.promotedTo === undefined ||
|
|
146
|
+
(typeof input.promotedTo === 'number' && isRolePromotionCode(input.promotedTo))) &&
|
|
147
|
+
(input.secondary === undefined || isNormalizedMoveRequest(input.secondary)));
|
|
148
|
+
}
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
import { ColorShort, PieceString, RolePromotionShort, RoleShort, SquareString } from './types/input';
|
|
2
|
+
import { ColorCode, Move, MoveBase, MoveCaptured, NonEmptyPieceCode, RoleCode, RolePromotionCode, Square } from './types/internal';
|
|
3
|
+
import { MoveBaseOutput, MoveCapturedOutput, MoveOutput } from './types/output';
|
|
4
|
+
export declare function denormalizeSquare(sq: Square): SquareString;
|
|
5
|
+
export declare function denormalizeColorShort(color: ColorCode): ColorShort;
|
|
6
|
+
export declare function denormalizeRoleShort(role: RoleCode): RoleShort;
|
|
7
|
+
export declare function denormalizeRolePromotionShort(role: RolePromotionCode): RolePromotionShort;
|
|
8
|
+
export declare function denormalizePieceString(code: NonEmptyPieceCode): PieceString;
|
|
9
|
+
export declare function denormalizeMoveBase(move: MoveBase): MoveBaseOutput;
|
|
10
|
+
export declare function denormalizeMoveCaptured(captured: MoveCaptured): MoveCapturedOutput;
|
|
11
|
+
export declare function denormalizeMove(move: Move): MoveOutput;
|
|
@@ -0,0 +1,77 @@
|
|
|
1
|
+
import { isValidSquare } from './check';
|
|
2
|
+
import { fileOf, rankOf } from './coords';
|
|
3
|
+
import { fromPieceCode } from './piece';
|
|
4
|
+
import { FILE_START, RANK_START } from './types/internal';
|
|
5
|
+
export function denormalizeSquare(sq) {
|
|
6
|
+
if (!isValidSquare(sq)) {
|
|
7
|
+
throw new RangeError(`Invalid square index: ${sq}`);
|
|
8
|
+
}
|
|
9
|
+
const file = fileOf(sq);
|
|
10
|
+
const rank = rankOf(sq);
|
|
11
|
+
const f = String.fromCharCode(FILE_START + file);
|
|
12
|
+
const r = String.fromCharCode(RANK_START + rank);
|
|
13
|
+
return `${f}${r}`;
|
|
14
|
+
}
|
|
15
|
+
export function denormalizeColorShort(color) {
|
|
16
|
+
switch (color) {
|
|
17
|
+
case 0 /* ColorCode.White */:
|
|
18
|
+
return 'w';
|
|
19
|
+
case 8 /* ColorCode.Black */:
|
|
20
|
+
return 'b';
|
|
21
|
+
default:
|
|
22
|
+
throw new RangeError(`Invalid color code: ${color}`);
|
|
23
|
+
}
|
|
24
|
+
}
|
|
25
|
+
export function denormalizeRoleShort(role) {
|
|
26
|
+
switch (role) {
|
|
27
|
+
case 1 /* RoleCode.Pawn */:
|
|
28
|
+
return 'P';
|
|
29
|
+
case 6 /* RoleCode.King */:
|
|
30
|
+
return 'K';
|
|
31
|
+
case 2 /* RoleCode.Knight */:
|
|
32
|
+
return 'N';
|
|
33
|
+
case 3 /* RoleCode.Bishop */:
|
|
34
|
+
return 'B';
|
|
35
|
+
case 4 /* RoleCode.Rook */:
|
|
36
|
+
return 'R';
|
|
37
|
+
case 5 /* RoleCode.Queen */:
|
|
38
|
+
return 'Q';
|
|
39
|
+
default:
|
|
40
|
+
throw new RangeError(`Invalid role code: ${role}`);
|
|
41
|
+
}
|
|
42
|
+
}
|
|
43
|
+
export function denormalizeRolePromotionShort(role) {
|
|
44
|
+
const roleShort = denormalizeRoleShort(role);
|
|
45
|
+
if (roleShort === 'K' || roleShort === 'P') {
|
|
46
|
+
throw new RangeError(`Invalid role for promotion: ${role}`);
|
|
47
|
+
}
|
|
48
|
+
return roleShort;
|
|
49
|
+
}
|
|
50
|
+
export function denormalizePieceString(code) {
|
|
51
|
+
const piece = fromPieceCode(code);
|
|
52
|
+
const colorPrefix = denormalizeColorShort(piece.color);
|
|
53
|
+
const roleStr = denormalizeRoleShort(piece.role);
|
|
54
|
+
return `${colorPrefix}${roleStr}`;
|
|
55
|
+
}
|
|
56
|
+
export function denormalizeMoveBase(move) {
|
|
57
|
+
return {
|
|
58
|
+
from: denormalizeSquare(move.from),
|
|
59
|
+
to: denormalizeSquare(move.to),
|
|
60
|
+
piece: denormalizePieceString(move.piece)
|
|
61
|
+
};
|
|
62
|
+
}
|
|
63
|
+
export function denormalizeMoveCaptured(captured) {
|
|
64
|
+
return {
|
|
65
|
+
square: denormalizeSquare(captured.square),
|
|
66
|
+
piece: denormalizePieceString(captured.piece)
|
|
67
|
+
};
|
|
68
|
+
}
|
|
69
|
+
export function denormalizeMove(move) {
|
|
70
|
+
const baseMove = denormalizeMoveBase(move);
|
|
71
|
+
return {
|
|
72
|
+
...baseMove,
|
|
73
|
+
...(move.promotedTo ? { promotedTo: denormalizeRolePromotionShort(move.promotedTo) } : {}),
|
|
74
|
+
...(move.captured ? { captured: denormalizeMoveCaptured(move.captured) } : {}),
|
|
75
|
+
...(move.secondary ? { secondary: denormalizeMoveBase(move.secondary) } : {})
|
|
76
|
+
};
|
|
77
|
+
}
|
|
@@ -0,0 +1,46 @@
|
|
|
1
|
+
import { cloneDeep } from 'es-toolkit/object';
|
|
2
|
+
import { isNormalizedMoveRequest } from './check';
|
|
3
|
+
import { normalizeColor, normalizeMoveRequest } from './normalize';
|
|
4
|
+
import { boardParsePiecePositionInput, boardParsePosition } from './position';
|
|
5
|
+
import { boardMove, boardSetPosition, boardSetTurn } from './reducers';
|
|
6
|
+
function createBoardStateInternal(position) {
|
|
7
|
+
const parsedPosition = boardParsePosition(position);
|
|
8
|
+
return {
|
|
9
|
+
pieces: parsedPosition.pieces,
|
|
10
|
+
turn: parsedPosition.turn,
|
|
11
|
+
positionEpoch: 0
|
|
12
|
+
};
|
|
13
|
+
}
|
|
14
|
+
export function createBoardState(position) {
|
|
15
|
+
const internalState = createBoardStateInternal(position ?? 'start');
|
|
16
|
+
return {
|
|
17
|
+
setPosition(input, mutationSession) {
|
|
18
|
+
const position = boardParsePosition(input);
|
|
19
|
+
let changed = boardSetPosition(internalState, position.pieces);
|
|
20
|
+
changed = boardSetTurn(internalState, position.turn) || changed;
|
|
21
|
+
return mutationSession.addMutation('state.board.setPosition', changed);
|
|
22
|
+
},
|
|
23
|
+
setPiecePosition(input, mutationSession) {
|
|
24
|
+
const pieces = boardParsePiecePositionInput(input);
|
|
25
|
+
return mutationSession.addMutation('state.board.setPiecePosition', boardSetPosition(internalState, pieces));
|
|
26
|
+
},
|
|
27
|
+
setTurn(turn, mutationSession) {
|
|
28
|
+
const normalizedTurn = normalizeColor(turn);
|
|
29
|
+
return mutationSession.addMutation('state.board.setTurn', boardSetTurn(internalState, normalizedTurn));
|
|
30
|
+
},
|
|
31
|
+
move(request, mutationSession) {
|
|
32
|
+
const moveRequest = isNormalizedMoveRequest(request)
|
|
33
|
+
? request
|
|
34
|
+
: normalizeMoveRequest(request);
|
|
35
|
+
const result = boardMove(internalState, moveRequest);
|
|
36
|
+
mutationSession.addMutation('state.board.move', true, result);
|
|
37
|
+
return result;
|
|
38
|
+
},
|
|
39
|
+
getPieceCodeAt(square) {
|
|
40
|
+
return internalState.pieces[square];
|
|
41
|
+
},
|
|
42
|
+
getSnapshot() {
|
|
43
|
+
return cloneDeep(internalState);
|
|
44
|
+
}
|
|
45
|
+
};
|
|
46
|
+
}
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
import type { MutationSession } from '../../mutation/types';
|
|
2
|
+
import type { MoveSnapshot } from './types/internal';
|
|
3
|
+
export type BoardStateMutationPayloadByCause = {
|
|
4
|
+
'state.board.setPosition': undefined;
|
|
5
|
+
'state.board.setPiecePosition': undefined;
|
|
6
|
+
'state.board.setTurn': undefined;
|
|
7
|
+
'state.board.move': MoveSnapshot;
|
|
8
|
+
};
|
|
9
|
+
export type BoardStateMutationSession = MutationSession<BoardStateMutationPayloadByCause>;
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
import { ColorInput, MoveRequestInput, PieceInput, RoleInput, RolePromotionInput, SquareString } from './types/input';
|
|
2
|
+
import { ColorCode, MoveRequest, PieceCode, RoleCode, RolePromotionCode, Square } from './types/internal';
|
|
3
|
+
export declare function normalizeColor(input: ColorInput): ColorCode;
|
|
4
|
+
export declare function normalizeRole(input: RoleInput): RoleCode;
|
|
5
|
+
export declare function normalizePiece(input: PieceInput): PieceCode;
|
|
6
|
+
export declare function normalizeRolePromotion(input: RolePromotionInput): RolePromotionCode;
|
|
7
|
+
export declare function normalizeSquare(s: SquareString): Square;
|
|
8
|
+
export declare function normalizeMoveRequest(move: MoveRequestInput): MoveRequest;
|