@dxos/plugin-chess 0.8.4-main.3f58842 → 0.8.4-main.5ea62a8
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/lib/browser/ChessboardContainer-HBHUXBG3.mjs +96 -0
- package/dist/lib/browser/ChessboardContainer-HBHUXBG3.mjs.map +7 -0
- package/dist/lib/browser/blueprint-definition-W5PLLXPP.mjs +11 -0
- package/dist/lib/browser/blueprint-definition-W5PLLXPP.mjs.map +7 -0
- package/dist/lib/browser/chunk-H65TQTPW.mjs +8 -0
- package/dist/lib/browser/chunk-H65TQTPW.mjs.map +7 -0
- package/dist/lib/browser/chunk-PE3IWOFR.mjs +28 -0
- package/dist/lib/browser/chunk-PE3IWOFR.mjs.map +7 -0
- package/dist/lib/browser/chunk-T275KG4U.mjs +84 -0
- package/dist/lib/browser/chunk-T275KG4U.mjs.map +7 -0
- package/dist/lib/browser/chunk-UDWRTBWA.mjs +324 -0
- package/dist/lib/browser/chunk-UDWRTBWA.mjs.map +7 -0
- package/dist/lib/browser/chunk-VTIITHYS.mjs +153 -0
- package/dist/lib/browser/chunk-VTIITHYS.mjs.map +7 -0
- package/dist/lib/browser/index.mjs +50 -25
- package/dist/lib/browser/index.mjs.map +3 -3
- package/dist/lib/browser/{intent-resolver-KQYYX44R.mjs → intent-resolver-DA5H5633.mjs} +8 -11
- package/dist/lib/browser/intent-resolver-DA5H5633.mjs.map +7 -0
- package/dist/lib/browser/meta.json +1 -1
- package/dist/lib/browser/{react-surface-PLUCWIYP.mjs → react-surface-FSDNVKZN.mjs} +11 -9
- package/dist/lib/browser/react-surface-FSDNVKZN.mjs.map +7 -0
- package/dist/lib/browser/types/index.mjs +6 -7
- package/dist/lib/node-esm/ChessboardContainer-PF75AEMW.mjs +97 -0
- package/dist/lib/node-esm/ChessboardContainer-PF75AEMW.mjs.map +7 -0
- package/dist/lib/node-esm/blueprint-definition-PR5YZC6M.mjs +12 -0
- package/dist/lib/node-esm/blueprint-definition-PR5YZC6M.mjs.map +7 -0
- package/dist/lib/node-esm/chunk-3NZRGOSV.mjs +154 -0
- package/dist/lib/node-esm/chunk-3NZRGOSV.mjs.map +7 -0
- package/dist/lib/node-esm/chunk-5DYLCQBR.mjs +85 -0
- package/dist/lib/node-esm/chunk-5DYLCQBR.mjs.map +7 -0
- package/dist/lib/node-esm/chunk-6OWCPDUA.mjs +29 -0
- package/dist/lib/node-esm/chunk-6OWCPDUA.mjs.map +7 -0
- package/dist/lib/node-esm/{chunk-QFINI7CC.mjs → chunk-IFDV434X.mjs} +3 -3
- package/dist/lib/node-esm/chunk-IFDV434X.mjs.map +7 -0
- package/dist/lib/node-esm/chunk-PIRITYWV.mjs +325 -0
- package/dist/lib/node-esm/chunk-PIRITYWV.mjs.map +7 -0
- package/dist/lib/node-esm/index.mjs +50 -25
- package/dist/lib/node-esm/index.mjs.map +3 -3
- package/dist/lib/node-esm/{intent-resolver-GXK2JROQ.mjs → intent-resolver-PODONUD3.mjs} +8 -11
- package/dist/lib/node-esm/intent-resolver-PODONUD3.mjs.map +7 -0
- package/dist/lib/node-esm/meta.json +1 -1
- package/dist/lib/node-esm/{react-surface-NGLNIWN3.mjs → react-surface-2TW3PIGQ.mjs} +11 -9
- package/dist/lib/node-esm/react-surface-2TW3PIGQ.mjs.map +7 -0
- package/dist/lib/node-esm/types/index.mjs +6 -7
- package/dist/types/src/ChessPlugin.d.ts.map +1 -1
- package/dist/types/src/capabilities/artifact-definition.d.ts +1 -1
- package/dist/types/src/capabilities/artifact-definition.d.ts.map +1 -1
- package/dist/types/src/capabilities/blueprint-definition.d.ts +6 -0
- package/dist/types/src/capabilities/blueprint-definition.d.ts.map +1 -0
- package/dist/types/src/capabilities/index.d.ts +4 -3
- package/dist/types/src/capabilities/index.d.ts.map +1 -1
- package/dist/types/src/capabilities/intent-resolver.d.ts +1 -1
- package/dist/types/src/capabilities/intent-resolver.d.ts.map +1 -1
- package/dist/types/src/capabilities/react-surface.d.ts +1 -1
- package/dist/types/src/components/Card.stories.d.ts +14 -0
- package/dist/types/src/components/Card.stories.d.ts.map +1 -0
- package/dist/types/src/components/Chessboard.d.ts +39 -0
- package/dist/types/src/components/Chessboard.d.ts.map +1 -0
- package/dist/types/src/components/Chessboard.stories.d.ts +37 -4
- package/dist/types/src/components/Chessboard.stories.d.ts.map +1 -1
- package/dist/types/src/components/ChessboardArticle.d.ts +6 -0
- package/dist/types/src/components/ChessboardArticle.d.ts.map +1 -0
- package/dist/types/src/components/ChessboardContainer.d.ts +9 -0
- package/dist/types/src/components/ChessboardContainer.d.ts.map +1 -0
- package/dist/types/src/components/ChessboardInfo.d.ts +21 -0
- package/dist/types/src/components/ChessboardInfo.d.ts.map +1 -0
- package/dist/types/src/components/ChessboardPlayers.d.ts +8 -0
- package/dist/types/src/components/ChessboardPlayers.d.ts.map +1 -0
- package/dist/types/src/components/index.d.ts +2 -5
- package/dist/types/src/components/index.d.ts.map +1 -1
- package/dist/types/src/functions/create.d.ts +14 -0
- package/dist/types/src/functions/create.d.ts.map +1 -0
- package/dist/types/src/functions/index.d.ts +4 -0
- package/dist/types/src/functions/index.d.ts.map +1 -0
- package/dist/types/src/functions/move.d.ts +8 -0
- package/dist/types/src/functions/move.d.ts.map +1 -0
- package/dist/types/src/functions/play.d.ts +8 -0
- package/dist/types/src/functions/play.d.ts.map +1 -0
- package/dist/types/src/index.d.ts +2 -0
- package/dist/types/src/index.d.ts.map +1 -1
- package/dist/types/src/meta.d.ts.map +1 -1
- package/dist/types/src/translations.d.ts +8 -2
- package/dist/types/src/translations.d.ts.map +1 -1
- package/dist/types/src/types/Chess.d.ts +27 -0
- package/dist/types/src/types/Chess.d.ts.map +1 -0
- package/dist/types/src/types/ChessAction.d.ts +24 -0
- package/dist/types/src/types/ChessAction.d.ts.map +1 -0
- package/dist/types/src/types/index.d.ts +2 -2
- package/dist/types/src/types/index.d.ts.map +1 -1
- package/dist/types/tsconfig.tsbuildinfo +1 -1
- package/package.json +31 -26
- package/src/ChessPlugin.tsx +8 -7
- package/src/capabilities/artifact-definition.ts +24 -20
- package/src/capabilities/blueprint-definition.ts +36 -0
- package/src/capabilities/index.ts +3 -1
- package/src/capabilities/intent-resolver.ts +3 -7
- package/src/capabilities/react-surface.tsx +5 -5
- package/src/components/Card.stories.tsx +68 -0
- package/src/components/Chessboard.stories.tsx +35 -26
- package/src/components/Chessboard.tsx +153 -0
- package/src/components/ChessboardArticle.tsx +64 -0
- package/src/components/ChessboardContainer.tsx +44 -0
- package/src/components/ChessboardInfo.tsx +173 -0
- package/src/components/{PlayerSelector.tsx → ChessboardPlayers.tsx} +26 -10
- package/src/components/index.ts +2 -2
- package/src/functions/create.ts +30 -0
- package/src/functions/index.ts +7 -0
- package/src/functions/move.ts +43 -0
- package/src/functions/play.ts +45 -0
- package/src/index.ts +2 -0
- package/src/meta.ts +4 -2
- package/src/translations.ts +8 -2
- package/src/types/Chess.ts +66 -0
- package/src/types/ChessAction.ts +19 -0
- package/src/types/index.ts +2 -2
- package/dist/lib/browser/ChessContainer-SC4ZV6XZ.mjs +0 -129
- package/dist/lib/browser/ChessContainer-SC4ZV6XZ.mjs.map +0 -7
- package/dist/lib/browser/artifact-definition-A2FSM255.mjs +0 -182
- package/dist/lib/browser/artifact-definition-A2FSM255.mjs.map +0 -7
- package/dist/lib/browser/chunk-A3WEUKPK.mjs +0 -61
- package/dist/lib/browser/chunk-A3WEUKPK.mjs.map +0 -7
- package/dist/lib/browser/chunk-KDADGLPC.mjs +0 -8
- package/dist/lib/browser/chunk-KDADGLPC.mjs.map +0 -7
- package/dist/lib/browser/chunk-ZXH3N4NA.mjs +0 -66
- package/dist/lib/browser/chunk-ZXH3N4NA.mjs.map +0 -7
- package/dist/lib/browser/intent-resolver-KQYYX44R.mjs.map +0 -7
- package/dist/lib/browser/react-surface-PLUCWIYP.mjs.map +0 -7
- package/dist/lib/node-esm/ChessContainer-7GLY5KLY.mjs +0 -130
- package/dist/lib/node-esm/ChessContainer-7GLY5KLY.mjs.map +0 -7
- package/dist/lib/node-esm/artifact-definition-2H7OUBZO.mjs +0 -183
- package/dist/lib/node-esm/artifact-definition-2H7OUBZO.mjs.map +0 -7
- package/dist/lib/node-esm/chunk-27D3JA3K.mjs +0 -68
- package/dist/lib/node-esm/chunk-27D3JA3K.mjs.map +0 -7
- package/dist/lib/node-esm/chunk-IS3Y25PL.mjs +0 -63
- package/dist/lib/node-esm/chunk-IS3Y25PL.mjs.map +0 -7
- package/dist/lib/node-esm/chunk-QFINI7CC.mjs.map +0 -7
- package/dist/lib/node-esm/intent-resolver-GXK2JROQ.mjs.map +0 -7
- package/dist/lib/node-esm/react-surface-NGLNIWN3.mjs.map +0 -7
- package/dist/types/src/components/Chess.d.ts +0 -14
- package/dist/types/src/components/Chess.d.ts.map +0 -1
- package/dist/types/src/components/ChessContainer.d.ts +0 -8
- package/dist/types/src/components/ChessContainer.d.ts.map +0 -1
- package/dist/types/src/components/ChessContainer.stories.d.ts +0 -23
- package/dist/types/src/components/ChessContainer.stories.d.ts.map +0 -1
- package/dist/types/src/components/ChessPanel.d.ts +0 -11
- package/dist/types/src/components/ChessPanel.d.ts.map +0 -1
- package/dist/types/src/components/PlayerSelector.d.ts +0 -8
- package/dist/types/src/components/PlayerSelector.d.ts.map +0 -1
- package/dist/types/src/types/schema.d.ts +0 -29
- package/dist/types/src/types/schema.d.ts.map +0 -1
- package/dist/types/src/types/types.d.ts +0 -32
- package/dist/types/src/types/types.d.ts.map +0 -1
- package/src/components/Chess.tsx +0 -60
- package/src/components/ChessContainer.stories.tsx +0 -110
- package/src/components/ChessContainer.tsx +0 -71
- package/src/components/ChessPanel.tsx +0 -67
- package/src/types/schema.ts +0 -34
- package/src/types/types.ts +0 -20
|
@@ -0,0 +1,64 @@
|
|
|
1
|
+
//
|
|
2
|
+
// Copyright 2024 DXOS.org
|
|
3
|
+
//
|
|
4
|
+
|
|
5
|
+
import React, { useCallback, useRef, useState } from 'react';
|
|
6
|
+
|
|
7
|
+
import { IconButton, useTranslation } from '@dxos/react-ui';
|
|
8
|
+
import { type Player } from '@dxos/react-ui-gameboard';
|
|
9
|
+
import { StackItem } from '@dxos/react-ui-stack';
|
|
10
|
+
import { mx } from '@dxos/react-ui-theme';
|
|
11
|
+
|
|
12
|
+
import { meta } from '../meta';
|
|
13
|
+
import { type Chess } from '../types';
|
|
14
|
+
|
|
15
|
+
import { Chessboard, type ChessboardController, type ChessboardInfoProps } from './Chessboard';
|
|
16
|
+
|
|
17
|
+
export const ChessboardArticle = ({ game }: { game: Chess.Game }) => {
|
|
18
|
+
const { t } = useTranslation(meta.id);
|
|
19
|
+
const [orientation, setOrientation] = useState<Player>('white');
|
|
20
|
+
const [open, setOpen] = useState(true);
|
|
21
|
+
const controller = useRef<ChessboardController>(null);
|
|
22
|
+
|
|
23
|
+
// TODO(burdon): Keyboard handler.
|
|
24
|
+
const handleSelect = useCallback<NonNullable<ChessboardInfoProps['onSelect']>>((index) => {
|
|
25
|
+
controller.current?.setMoveNumber(index);
|
|
26
|
+
}, []);
|
|
27
|
+
|
|
28
|
+
return (
|
|
29
|
+
<StackItem.Content classNames='@container bs-full is-full overflow-hidden'>
|
|
30
|
+
<Chessboard.Root game={game} ref={controller}>
|
|
31
|
+
<div className={mx('grid grid-rows-[1fr_4rem] bs-full is-full gap-2', open && '@3xl:grid-cols-[1fr_320px]')}>
|
|
32
|
+
<Chessboard.Content>
|
|
33
|
+
<Chessboard.Board orientation={orientation} />
|
|
34
|
+
</Chessboard.Content>
|
|
35
|
+
{open && (
|
|
36
|
+
<div className='hidden @3xl:flex flex-col p-8 justify-center items-center overflow-hidden'>
|
|
37
|
+
<Chessboard.Info
|
|
38
|
+
orientation={orientation}
|
|
39
|
+
min={8}
|
|
40
|
+
max={8}
|
|
41
|
+
onOrientationChange={setOrientation}
|
|
42
|
+
onClose={() => setOpen(false)}
|
|
43
|
+
onSelect={handleSelect}
|
|
44
|
+
/>
|
|
45
|
+
</div>
|
|
46
|
+
)}
|
|
47
|
+
<div className='flex justify-center items-center'>
|
|
48
|
+
<Chessboard.Players>
|
|
49
|
+
<IconButton
|
|
50
|
+
variant='ghost'
|
|
51
|
+
icon='ph--info--regular'
|
|
52
|
+
iconOnly
|
|
53
|
+
label={t('button toggle info')}
|
|
54
|
+
size={6}
|
|
55
|
+
classNames={mx('invisible', !open && '@3xl:visible')}
|
|
56
|
+
onClick={() => setOpen((open) => !open)}
|
|
57
|
+
/>
|
|
58
|
+
</Chessboard.Players>
|
|
59
|
+
</div>
|
|
60
|
+
</div>
|
|
61
|
+
</Chessboard.Root>
|
|
62
|
+
</StackItem.Content>
|
|
63
|
+
);
|
|
64
|
+
};
|
|
@@ -0,0 +1,44 @@
|
|
|
1
|
+
//
|
|
2
|
+
// Copyright 2024 DXOS.org
|
|
3
|
+
//
|
|
4
|
+
|
|
5
|
+
import React from 'react';
|
|
6
|
+
|
|
7
|
+
import { getSpace } from '@dxos/react-client/echo';
|
|
8
|
+
|
|
9
|
+
import { type Chess } from '../types';
|
|
10
|
+
|
|
11
|
+
import { Chessboard } from './Chessboard';
|
|
12
|
+
import { ChessboardArticle } from './ChessboardArticle';
|
|
13
|
+
|
|
14
|
+
export type ChessboardContainerProps = {
|
|
15
|
+
game: Chess.Game;
|
|
16
|
+
role?: string;
|
|
17
|
+
};
|
|
18
|
+
|
|
19
|
+
export const ChessboardContainer = ({ game, role }: ChessboardContainerProps) => {
|
|
20
|
+
const space = getSpace(game);
|
|
21
|
+
if (!space) {
|
|
22
|
+
return null;
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
switch (role) {
|
|
26
|
+
case 'card--popover':
|
|
27
|
+
case 'card--intrinsic':
|
|
28
|
+
case 'card--extrinsic': {
|
|
29
|
+
return (
|
|
30
|
+
<Chessboard.Root game={game}>
|
|
31
|
+
<Chessboard.Content role={role}>
|
|
32
|
+
<Chessboard.Board />
|
|
33
|
+
</Chessboard.Content>
|
|
34
|
+
</Chessboard.Root>
|
|
35
|
+
);
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
default: {
|
|
39
|
+
return <ChessboardArticle game={game} />;
|
|
40
|
+
}
|
|
41
|
+
}
|
|
42
|
+
};
|
|
43
|
+
|
|
44
|
+
export default ChessboardContainer;
|
|
@@ -0,0 +1,173 @@
|
|
|
1
|
+
//
|
|
2
|
+
// Copyright 2023 DXOS.org
|
|
3
|
+
//
|
|
4
|
+
|
|
5
|
+
import React, { type PropsWithChildren, useEffect, useMemo, useRef } from 'react';
|
|
6
|
+
|
|
7
|
+
import { Icon, IconButton, type ThemedClassName, useTranslation } from '@dxos/react-ui';
|
|
8
|
+
import { type Player, useGameboardContext } from '@dxos/react-ui-gameboard';
|
|
9
|
+
import { mx } from '@dxos/react-ui-theme';
|
|
10
|
+
|
|
11
|
+
import { meta } from '../meta';
|
|
12
|
+
|
|
13
|
+
import { type ExtendedChessModel } from './Chessboard';
|
|
14
|
+
|
|
15
|
+
export type ChessboardInfoProps = ThemedClassName<
|
|
16
|
+
{
|
|
17
|
+
orientation?: Player;
|
|
18
|
+
onOrientationChange?: (orientation: Player) => void;
|
|
19
|
+
onClose?: () => void;
|
|
20
|
+
} & Pick<HistoryProps, 'min' | 'max' | 'onSelect'>
|
|
21
|
+
>;
|
|
22
|
+
|
|
23
|
+
export const ChessboardInfo = ({
|
|
24
|
+
classNames,
|
|
25
|
+
orientation = 'white',
|
|
26
|
+
onOrientationChange,
|
|
27
|
+
onClose,
|
|
28
|
+
...props
|
|
29
|
+
}: ChessboardInfoProps) => {
|
|
30
|
+
const { t } = useTranslation(meta.id);
|
|
31
|
+
const { model } = useGameboardContext<ExtendedChessModel>(ChessboardInfo.displayName);
|
|
32
|
+
|
|
33
|
+
return (
|
|
34
|
+
<div
|
|
35
|
+
className={mx(
|
|
36
|
+
'grid grid-rows-[min-content_1fr_min-content] is-full min-is-[16rem] p-2 overflow-hidden bg-inputSurface rounded',
|
|
37
|
+
classNames,
|
|
38
|
+
)}
|
|
39
|
+
>
|
|
40
|
+
<PlayerIndicator
|
|
41
|
+
model={model}
|
|
42
|
+
player={orientation === 'white' ? 'black' : 'white'}
|
|
43
|
+
title={model.object.players?.[orientation === 'white' ? 'black' : 'white']}
|
|
44
|
+
>
|
|
45
|
+
{onClose && <IconButton icon='ph--x--regular' iconOnly label={t('button flip')} size={4} onClick={onClose} />}
|
|
46
|
+
</PlayerIndicator>
|
|
47
|
+
|
|
48
|
+
<History model={model} {...props} />
|
|
49
|
+
|
|
50
|
+
<PlayerIndicator model={model} player={orientation} title={model.object.players?.[orientation]}>
|
|
51
|
+
{onOrientationChange && (
|
|
52
|
+
<IconButton
|
|
53
|
+
icon='ph--arrows-down-up--regular'
|
|
54
|
+
iconOnly
|
|
55
|
+
label={t('button flip')}
|
|
56
|
+
size={6}
|
|
57
|
+
classNames={mx('transition duration-200 ease-linear', orientation === 'white' && 'rotate-180')}
|
|
58
|
+
onClick={() => onOrientationChange(orientation === 'white' ? 'black' : 'white')}
|
|
59
|
+
/>
|
|
60
|
+
)}
|
|
61
|
+
</PlayerIndicator>
|
|
62
|
+
</div>
|
|
63
|
+
);
|
|
64
|
+
};
|
|
65
|
+
|
|
66
|
+
ChessboardInfo.displayName = 'Chessboard.Info';
|
|
67
|
+
|
|
68
|
+
type HistoryProps = ThemedClassName<{
|
|
69
|
+
model: ExtendedChessModel;
|
|
70
|
+
min?: number;
|
|
71
|
+
max?: number;
|
|
72
|
+
onSelect?: (index: number) => void;
|
|
73
|
+
}>;
|
|
74
|
+
|
|
75
|
+
const History = ({ classNames, model, min, max, onSelect }: HistoryProps) => {
|
|
76
|
+
const { t } = useTranslation(meta.id);
|
|
77
|
+
const label = model.game.isGameOver()
|
|
78
|
+
? model.game.isCheckmate()
|
|
79
|
+
? t('game.checkmate')
|
|
80
|
+
: model.game.isStalemate()
|
|
81
|
+
? t('game.stalemate')
|
|
82
|
+
: t('game.draw')
|
|
83
|
+
: model.game.isCheck()
|
|
84
|
+
? t('game.check')
|
|
85
|
+
: undefined;
|
|
86
|
+
|
|
87
|
+
const history = model.game.history();
|
|
88
|
+
const moves = useMemo(
|
|
89
|
+
() =>
|
|
90
|
+
history.reduce(
|
|
91
|
+
(acc, move, index) => {
|
|
92
|
+
if (index % 2 === 0) {
|
|
93
|
+
acc.push([{ index, move }]);
|
|
94
|
+
} else {
|
|
95
|
+
acc[acc.length - 1].push({ index, move });
|
|
96
|
+
}
|
|
97
|
+
return acc;
|
|
98
|
+
},
|
|
99
|
+
[] as { index: number; move: string }[][],
|
|
100
|
+
),
|
|
101
|
+
[history.length],
|
|
102
|
+
);
|
|
103
|
+
|
|
104
|
+
const scrollerRef = useRef<HTMLDivElement>(null);
|
|
105
|
+
useEffect(() => {
|
|
106
|
+
scrollerRef.current?.scrollTo({ top: scrollerRef.current.scrollHeight });
|
|
107
|
+
}, [history.length]);
|
|
108
|
+
|
|
109
|
+
useEffect(() => {
|
|
110
|
+
const div = scrollerRef.current?.querySelector(`[data-index="${model.moveIndex.value - 1}"]`);
|
|
111
|
+
scrollerRef.current?.classList.add('scrollbar-none');
|
|
112
|
+
div?.scrollIntoView({ behavior: 'smooth' });
|
|
113
|
+
scrollerRef.current?.classList.remove('scrollbar-none');
|
|
114
|
+
}, [model.moveIndex.value]);
|
|
115
|
+
|
|
116
|
+
return (
|
|
117
|
+
<div
|
|
118
|
+
ref={scrollerRef}
|
|
119
|
+
className={mx('overflow-y-scroll', classNames)}
|
|
120
|
+
style={{
|
|
121
|
+
minHeight: min === undefined ? 'auto' : `${min * 24}px`,
|
|
122
|
+
maxHeight: max === undefined ? 'auto' : `${max * 24}px`,
|
|
123
|
+
}}
|
|
124
|
+
>
|
|
125
|
+
{moves.map(([a, b], index) => (
|
|
126
|
+
<div key={index} className='grid grid-cols-[3rem_1fr_1fr_1rem] gap-2 pis-4 leading-1'>
|
|
127
|
+
<div className='content-center text-xs text-subdued'>{index + 1}</div>
|
|
128
|
+
{a && (
|
|
129
|
+
<div
|
|
130
|
+
data-index={a.index}
|
|
131
|
+
className={mx('pis-2 cursor-pointer', a.index === model.moveIndex.value - 1 && 'bg-primary-500')}
|
|
132
|
+
onClick={() => onSelect?.(a.index + 1)}
|
|
133
|
+
>
|
|
134
|
+
{a.move}
|
|
135
|
+
</div>
|
|
136
|
+
)}
|
|
137
|
+
{b && (
|
|
138
|
+
<div
|
|
139
|
+
data-index={b.index}
|
|
140
|
+
className={mx('pis-2 cursor-pointer', b.index === model.moveIndex.value - 1 && 'bg-primary-500')}
|
|
141
|
+
onClick={() => onSelect?.(b.index + 1)}
|
|
142
|
+
>
|
|
143
|
+
{b.move}
|
|
144
|
+
</div>
|
|
145
|
+
)}
|
|
146
|
+
</div>
|
|
147
|
+
))}
|
|
148
|
+
{label && <div className='text-center'>{label}</div>}
|
|
149
|
+
</div>
|
|
150
|
+
);
|
|
151
|
+
};
|
|
152
|
+
|
|
153
|
+
const PlayerIndicator = ({
|
|
154
|
+
children,
|
|
155
|
+
model,
|
|
156
|
+
player,
|
|
157
|
+
title,
|
|
158
|
+
}: PropsWithChildren<{ model: ExtendedChessModel; player: Player; title?: string }>) => {
|
|
159
|
+
const turn = player === (model.game.turn() === 'w' ? 'white' : 'black');
|
|
160
|
+
return (
|
|
161
|
+
<div className='grid grid-cols-[2rem_1fr_2rem] gap-1 bs-[--rail-size] pis-1 pie-1 flex items-center overflow-hidden'>
|
|
162
|
+
<div className='place-items-center'>
|
|
163
|
+
<Icon
|
|
164
|
+
icon={turn ? 'ph--circle--fill' : 'ph--circle--thin'}
|
|
165
|
+
size={6}
|
|
166
|
+
classNames={mx(turn && (model.game.isCheckmate() ? 'text-red-500' : 'text-green-500'))}
|
|
167
|
+
/>
|
|
168
|
+
</div>
|
|
169
|
+
<div className='truncate'>{title}</div>
|
|
170
|
+
{children}
|
|
171
|
+
</div>
|
|
172
|
+
);
|
|
173
|
+
};
|
|
@@ -2,32 +2,45 @@
|
|
|
2
2
|
// Copyright 2024 DXOS.org
|
|
3
3
|
//
|
|
4
4
|
|
|
5
|
-
import React from 'react';
|
|
5
|
+
import React, { type PropsWithChildren } from 'react';
|
|
6
6
|
|
|
7
7
|
import { generateName } from '@dxos/display-name';
|
|
8
|
-
import {
|
|
8
|
+
import { type SpaceMember, getSpace, useMembers } from '@dxos/react-client/echo';
|
|
9
9
|
import { Icon, Input, Select, useThemeContext } from '@dxos/react-ui';
|
|
10
|
+
import { type ThemedClassName } from '@dxos/react-ui';
|
|
11
|
+
import { useGameboardContext } from '@dxos/react-ui-gameboard';
|
|
12
|
+
import { mx } from '@dxos/react-ui-theme';
|
|
10
13
|
|
|
11
|
-
import { type
|
|
14
|
+
import { type Chess } from '../types';
|
|
12
15
|
|
|
13
|
-
|
|
14
|
-
|
|
16
|
+
import { type ExtendedChessModel } from './Chessboard';
|
|
17
|
+
|
|
18
|
+
export type ChessboardPlayersProps = ThemedClassName<PropsWithChildren>;
|
|
19
|
+
|
|
20
|
+
export const ChessboardPlayers = ({ classNames, children }: ChessboardPlayersProps) => {
|
|
21
|
+
const { model } = useGameboardContext<ExtendedChessModel>(ChessboardPlayers.displayName);
|
|
22
|
+
const members = useMembers(getSpace(model.object)?.key);
|
|
23
|
+
const players: Chess.Game['players'] = model.object.players;
|
|
24
|
+
if (!players) {
|
|
25
|
+
return null;
|
|
26
|
+
}
|
|
15
27
|
|
|
16
28
|
return (
|
|
17
|
-
<div role='none' className='grid grid-cols-2
|
|
29
|
+
<div role='none' className={mx('grid grid-cols-[1fr_min-content_1fr] pis-2 pie-2 overflow-hidden', classNames)}>
|
|
18
30
|
<div role='none' className='flex flex-row-reverse items-center gap-2'>
|
|
19
31
|
<PlayerSelect
|
|
20
32
|
side='white'
|
|
21
|
-
value={
|
|
22
|
-
onValueChange={(player) => (
|
|
33
|
+
value={players.white}
|
|
34
|
+
onValueChange={(player) => (players.white = player)}
|
|
23
35
|
members={members}
|
|
24
36
|
/>
|
|
25
37
|
</div>
|
|
38
|
+
<div className='flex flex-row justify-center items-center'>{children}</div>
|
|
26
39
|
<div role='none' className='flex flex-row items-center gap-2'>
|
|
27
40
|
<PlayerSelect
|
|
28
41
|
side='black'
|
|
29
|
-
value={
|
|
30
|
-
onValueChange={(player) => (
|
|
42
|
+
value={players.black}
|
|
43
|
+
onValueChange={(player) => (players.black = player)}
|
|
31
44
|
members={members}
|
|
32
45
|
/>
|
|
33
46
|
</div>
|
|
@@ -35,6 +48,8 @@ export const PlayerSelector = ({ space, game }: { space: Space; game: ChessType
|
|
|
35
48
|
);
|
|
36
49
|
};
|
|
37
50
|
|
|
51
|
+
ChessboardPlayers.displayName = 'Chessboard.Players';
|
|
52
|
+
|
|
38
53
|
const PlayerSelect = ({
|
|
39
54
|
side,
|
|
40
55
|
value,
|
|
@@ -74,6 +89,7 @@ const PlayerSelect = ({
|
|
|
74
89
|
);
|
|
75
90
|
})}
|
|
76
91
|
</Select.Viewport>
|
|
92
|
+
<Select.Arrow />
|
|
77
93
|
</Select.Content>
|
|
78
94
|
</Select.Portal>
|
|
79
95
|
</Select.Root>
|
package/src/components/index.ts
CHANGED
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
//
|
|
2
|
+
// Copyright 2025 DXOS.org
|
|
3
|
+
//
|
|
4
|
+
|
|
5
|
+
import { Effect, Schema } from 'effect';
|
|
6
|
+
|
|
7
|
+
import { DatabaseService, defineFunction } from '@dxos/functions';
|
|
8
|
+
|
|
9
|
+
import { Chess } from '../types';
|
|
10
|
+
|
|
11
|
+
export default defineFunction({
|
|
12
|
+
name: 'dxos.org/function/chess/create',
|
|
13
|
+
description: 'Creates a new chess game.',
|
|
14
|
+
inputSchema: Schema.Struct({
|
|
15
|
+
pgn: Schema.optional(
|
|
16
|
+
Schema.String.annotations({
|
|
17
|
+
description: 'Portable Game Notation.',
|
|
18
|
+
}),
|
|
19
|
+
),
|
|
20
|
+
fen: Schema.optional(
|
|
21
|
+
Schema.String.annotations({
|
|
22
|
+
description: 'Forsyth-Edwards Notation.',
|
|
23
|
+
}),
|
|
24
|
+
),
|
|
25
|
+
}),
|
|
26
|
+
outputSchema: Chess.Game,
|
|
27
|
+
handler: Effect.fn(function* ({ data: { pgn, fen } }) {
|
|
28
|
+
return yield* DatabaseService.add(Chess.makeGame({ pgn, fen }));
|
|
29
|
+
}),
|
|
30
|
+
});
|
|
@@ -0,0 +1,43 @@
|
|
|
1
|
+
//
|
|
2
|
+
// Copyright 2025 DXOS.org
|
|
3
|
+
//
|
|
4
|
+
|
|
5
|
+
import { Chess as ChessJS } from 'chess.js';
|
|
6
|
+
import { Effect, Schema } from 'effect';
|
|
7
|
+
|
|
8
|
+
import { ArtifactId } from '@dxos/assistant';
|
|
9
|
+
import { DatabaseService, defineFunction } from '@dxos/functions';
|
|
10
|
+
|
|
11
|
+
import { Chess } from '../types';
|
|
12
|
+
|
|
13
|
+
export default defineFunction({
|
|
14
|
+
name: 'dxos.org/function/chess/move',
|
|
15
|
+
description: 'Makes a move in the given chess game.',
|
|
16
|
+
inputSchema: Schema.Struct({
|
|
17
|
+
id: ArtifactId.annotations({
|
|
18
|
+
description: 'The ID of the chess object.',
|
|
19
|
+
}),
|
|
20
|
+
move: Schema.String.annotations({
|
|
21
|
+
description: 'The move to make in the chess game.',
|
|
22
|
+
examples: ['e4', 'Bf3'],
|
|
23
|
+
}),
|
|
24
|
+
}),
|
|
25
|
+
outputSchema: Schema.Struct({
|
|
26
|
+
pgn: Schema.String.annotations({
|
|
27
|
+
description: 'The PGN of the game after the move was played.',
|
|
28
|
+
}),
|
|
29
|
+
}),
|
|
30
|
+
handler: Effect.fn(function* ({ data: { id, move } }) {
|
|
31
|
+
const object = yield* DatabaseService.resolve(ArtifactId.toDXN(id), Chess.Game);
|
|
32
|
+
const chess = new ChessJS();
|
|
33
|
+
if (object.pgn) {
|
|
34
|
+
chess.loadPgn(object.pgn);
|
|
35
|
+
} else if (object.fen) {
|
|
36
|
+
chess.load(object.fen);
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
chess.move(move, { strict: false });
|
|
40
|
+
object.pgn = chess.pgn();
|
|
41
|
+
return { pgn: object.pgn };
|
|
42
|
+
}),
|
|
43
|
+
});
|
|
@@ -0,0 +1,45 @@
|
|
|
1
|
+
//
|
|
2
|
+
// Copyright 2025 DXOS.org
|
|
3
|
+
//
|
|
4
|
+
|
|
5
|
+
import { Chess as ChessJS } from 'chess.js';
|
|
6
|
+
import { Effect, Schema } from 'effect';
|
|
7
|
+
|
|
8
|
+
import { ArtifactId } from '@dxos/assistant';
|
|
9
|
+
import { DatabaseService, defineFunction } from '@dxos/functions';
|
|
10
|
+
|
|
11
|
+
import { Chess } from '../types';
|
|
12
|
+
|
|
13
|
+
export default defineFunction({
|
|
14
|
+
name: 'dxos.org/function/chess/play',
|
|
15
|
+
description: 'Uses the chess engine to play the next move.',
|
|
16
|
+
inputSchema: Schema.Struct({
|
|
17
|
+
id: ArtifactId.annotations({
|
|
18
|
+
description: 'The ID of the chess object.',
|
|
19
|
+
}),
|
|
20
|
+
}),
|
|
21
|
+
outputSchema: Schema.Struct({
|
|
22
|
+
pgn: Schema.String.annotations({
|
|
23
|
+
description: 'The PGN of the game after the move was played.',
|
|
24
|
+
}),
|
|
25
|
+
move: Schema.optional(Schema.String).annotations({
|
|
26
|
+
description: 'The move that was played.',
|
|
27
|
+
}),
|
|
28
|
+
}),
|
|
29
|
+
handler: Effect.fn(function* ({ data: { id } }) {
|
|
30
|
+
const object = yield* DatabaseService.resolve(ArtifactId.toDXN(id), Chess.Game);
|
|
31
|
+
const chess = new ChessJS();
|
|
32
|
+
if (object.pgn) {
|
|
33
|
+
chess.loadPgn(object.pgn);
|
|
34
|
+
} else if (object.fen) {
|
|
35
|
+
chess.load(object.fen);
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
const moves = chess.moves();
|
|
39
|
+
const move = moves[Math.floor(Math.random() * moves.length)];
|
|
40
|
+
|
|
41
|
+
chess.move(move, { strict: false });
|
|
42
|
+
object.pgn = chess.pgn();
|
|
43
|
+
return { move, pgn: object.pgn };
|
|
44
|
+
}),
|
|
45
|
+
});
|
package/src/index.ts
CHANGED
package/src/meta.ts
CHANGED
|
@@ -3,14 +3,16 @@
|
|
|
3
3
|
//
|
|
4
4
|
|
|
5
5
|
import { type PluginMeta } from '@dxos/app-framework';
|
|
6
|
+
import { trim } from '@dxos/util';
|
|
6
7
|
|
|
7
8
|
export const CHESS_PLUGIN = 'dxos.org/plugin/chess';
|
|
8
9
|
|
|
9
10
|
export const meta: PluginMeta = {
|
|
10
11
|
id: CHESS_PLUGIN,
|
|
11
12
|
name: 'Chess',
|
|
12
|
-
description:
|
|
13
|
-
|
|
13
|
+
description: trim`
|
|
14
|
+
Play chess with friends or practice with the AI.
|
|
15
|
+
`,
|
|
14
16
|
icon: 'ph--shield-chevron--regular',
|
|
15
17
|
source: 'https://github.com/dxos/dxos/tree/main/packages/plugins/plugin-chess',
|
|
16
18
|
screenshots: ['https://dxos.network/plugin-details-chess-dark.png'],
|
package/src/translations.ts
CHANGED
|
@@ -5,12 +5,12 @@
|
|
|
5
5
|
import { type Resource } from '@dxos/react-ui';
|
|
6
6
|
|
|
7
7
|
import { meta } from './meta';
|
|
8
|
-
import {
|
|
8
|
+
import { Chess } from './types';
|
|
9
9
|
|
|
10
10
|
export const translations = [
|
|
11
11
|
{
|
|
12
12
|
'en-US': {
|
|
13
|
-
[
|
|
13
|
+
[Chess.Game.typename]: {
|
|
14
14
|
'typename label': 'Game',
|
|
15
15
|
'typename label_zero': 'Games',
|
|
16
16
|
'typename label_one': 'Game',
|
|
@@ -18,10 +18,16 @@ export const translations = [
|
|
|
18
18
|
'object name placeholder': 'New game',
|
|
19
19
|
'rename object label': 'Rename game',
|
|
20
20
|
'delete object label': 'Delete game',
|
|
21
|
+
'button toggle info': 'Toggle info',
|
|
22
|
+
'button flip': 'Flip board',
|
|
21
23
|
},
|
|
22
24
|
[meta.id]: {
|
|
23
25
|
'plugin name': 'Chess',
|
|
24
26
|
'delete game label': 'Delete',
|
|
27
|
+
'game.checkmate': 'Checkmate',
|
|
28
|
+
'game.stalemate': 'Stalemate',
|
|
29
|
+
'game.draw': 'Draw',
|
|
30
|
+
'game.check': 'Check',
|
|
25
31
|
},
|
|
26
32
|
},
|
|
27
33
|
},
|
|
@@ -0,0 +1,66 @@
|
|
|
1
|
+
//
|
|
2
|
+
// Copyright 2024 DXOS.org
|
|
3
|
+
//
|
|
4
|
+
|
|
5
|
+
import { Chess as ChessJS } from 'chess.js';
|
|
6
|
+
import { Schema } from 'effect';
|
|
7
|
+
|
|
8
|
+
import { Obj, Type } from '@dxos/echo';
|
|
9
|
+
import { LabelAnnotation } from '@dxos/echo-schema';
|
|
10
|
+
import { log } from '@dxos/log';
|
|
11
|
+
import { ItemAnnotation } from '@dxos/schema';
|
|
12
|
+
|
|
13
|
+
export const Game = Schema.Struct({
|
|
14
|
+
name: Schema.optional(Schema.String),
|
|
15
|
+
players: Schema.optional(
|
|
16
|
+
Schema.Struct({
|
|
17
|
+
white: Schema.optional(
|
|
18
|
+
Schema.String.annotations({
|
|
19
|
+
description: 'DID of white player',
|
|
20
|
+
}),
|
|
21
|
+
),
|
|
22
|
+
black: Schema.optional(
|
|
23
|
+
Schema.String.annotations({
|
|
24
|
+
description: 'DID of black player',
|
|
25
|
+
}),
|
|
26
|
+
),
|
|
27
|
+
}).pipe(Schema.mutable),
|
|
28
|
+
),
|
|
29
|
+
pgn: Schema.optional(
|
|
30
|
+
Schema.String.annotations({
|
|
31
|
+
description: 'Portable Game Notation.',
|
|
32
|
+
}),
|
|
33
|
+
),
|
|
34
|
+
fen: Schema.optional(
|
|
35
|
+
Schema.String.annotations({
|
|
36
|
+
description: 'Forsyth-Edwards Notation.',
|
|
37
|
+
}),
|
|
38
|
+
),
|
|
39
|
+
}).pipe(
|
|
40
|
+
Type.Obj({
|
|
41
|
+
typename: 'dxos.org/type/Chess',
|
|
42
|
+
version: '0.2.0',
|
|
43
|
+
}),
|
|
44
|
+
LabelAnnotation.set(['name']),
|
|
45
|
+
ItemAnnotation.set(true),
|
|
46
|
+
);
|
|
47
|
+
|
|
48
|
+
export interface Game extends Schema.Schema.Type<typeof Game> {}
|
|
49
|
+
|
|
50
|
+
export const makeGame = ({ name, pgn, fen }: { name?: string; pgn?: string; fen?: string } = {}) => {
|
|
51
|
+
const chess = new ChessJS();
|
|
52
|
+
if (pgn) {
|
|
53
|
+
try {
|
|
54
|
+
chess.loadPgn(pgn);
|
|
55
|
+
} catch {
|
|
56
|
+
log.warn(pgn);
|
|
57
|
+
}
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
return Obj.make(Game, {
|
|
61
|
+
name,
|
|
62
|
+
players: {},
|
|
63
|
+
pgn,
|
|
64
|
+
fen,
|
|
65
|
+
});
|
|
66
|
+
};
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
//
|
|
2
|
+
// Copyright 2023 DXOS.org
|
|
3
|
+
//
|
|
4
|
+
|
|
5
|
+
import { Schema } from 'effect';
|
|
6
|
+
|
|
7
|
+
import { meta } from '../meta';
|
|
8
|
+
|
|
9
|
+
import { Game } from './Chess';
|
|
10
|
+
|
|
11
|
+
export class Create extends Schema.TaggedClass<Create>()(`${meta.id}/action/create`, {
|
|
12
|
+
input: Schema.Struct({
|
|
13
|
+
name: Schema.optional(Schema.String),
|
|
14
|
+
pgn: Schema.optional(Schema.String),
|
|
15
|
+
}),
|
|
16
|
+
output: Schema.Struct({
|
|
17
|
+
object: Game,
|
|
18
|
+
}),
|
|
19
|
+
}) {}
|
package/src/types/index.ts
CHANGED