@dxos/plugin-chess 0.8.4-main.b97322e → 0.8.4-main.dedc0f3
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-LIZDOGHI.mjs +97 -0
- package/dist/lib/browser/ChessboardContainer-LIZDOGHI.mjs.map +7 -0
- package/dist/lib/browser/blueprint-definition-RVS32T67.mjs +11 -0
- package/dist/lib/browser/blueprint-definition-RVS32T67.mjs.map +7 -0
- package/dist/lib/browser/chunk-EKNL3H3U.mjs +306 -0
- package/dist/lib/browser/chunk-EKNL3H3U.mjs.map +7 -0
- package/dist/lib/browser/chunk-KD4I5VTZ.mjs +8 -0
- package/dist/lib/browser/chunk-KD4I5VTZ.mjs.map +7 -0
- package/dist/lib/browser/chunk-KYO4ODJJ.mjs +163 -0
- package/dist/lib/browser/chunk-KYO4ODJJ.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/index.mjs +52 -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-MIMI6LPO.mjs} +11 -9
- package/dist/lib/browser/react-surface-MIMI6LPO.mjs.map +7 -0
- package/dist/lib/browser/types/index.mjs +6 -7
- package/dist/lib/node-esm/ChessboardContainer-XWVQ5K7N.mjs +98 -0
- package/dist/lib/node-esm/ChessboardContainer-XWVQ5K7N.mjs.map +7 -0
- package/dist/lib/node-esm/blueprint-definition-F57ZSRAG.mjs +12 -0
- package/dist/lib/node-esm/blueprint-definition-F57ZSRAG.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-A34JOK3K.mjs +164 -0
- package/dist/lib/node-esm/chunk-A34JOK3K.mjs.map +7 -0
- package/dist/lib/node-esm/chunk-EGBWRJWH.mjs +307 -0
- package/dist/lib/node-esm/chunk-EGBWRJWH.mjs.map +7 -0
- package/dist/lib/node-esm/{chunk-QFINI7CC.mjs → chunk-Q5LJYIMO.mjs} +3 -3
- package/dist/lib/node-esm/chunk-Q5LJYIMO.mjs.map +7 -0
- package/dist/lib/node-esm/index.mjs +52 -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-XMKFR5BC.mjs} +11 -9
- package/dist/lib/node-esm/react-surface-XMKFR5BC.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 +38 -0
- package/dist/types/src/components/Chessboard.d.ts.map +1 -0
- package/dist/types/src/components/Chessboard.stories.d.ts +40 -4
- package/dist/types/src/components/Chessboard.stories.d.ts.map +1 -1
- package/dist/types/src/components/ChessboardArticle.d.ts +7 -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/Info.d.ts +21 -0
- package/dist/types/src/components/Info.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 +9 -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 +10 -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 +38 -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 +177 -0
- package/src/components/ChessboardArticle.tsx +69 -0
- package/src/components/ChessboardContainer.tsx +44 -0
- package/src/components/Info.tsx +236 -0
- 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 +53 -0
- package/src/index.ts +2 -0
- package/src/meta.ts +4 -2
- package/src/translations.ts +10 -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/components/PlayerSelector.tsx +0 -82
- package/src/types/schema.ts +0 -34
- package/src/types/types.ts +0 -20
|
@@ -0,0 +1,69 @@
|
|
|
1
|
+
//
|
|
2
|
+
// Copyright 2024 DXOS.org
|
|
3
|
+
//
|
|
4
|
+
|
|
5
|
+
import React, { useCallback, useRef, useState } from 'react';
|
|
6
|
+
|
|
7
|
+
import { IconButton, Toolbar, 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, role }: { game: Chess.Game; role?: string }) => {
|
|
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 toolbar classNames='@container bs-full is-full overflow-hidden'>
|
|
30
|
+
<Chessboard.Root game={game} ref={controller}>
|
|
31
|
+
<Toolbar.Root>
|
|
32
|
+
<IconButton
|
|
33
|
+
variant='ghost'
|
|
34
|
+
icon='ph--info--regular'
|
|
35
|
+
iconOnly
|
|
36
|
+
label={t('toggle info button')}
|
|
37
|
+
size={6}
|
|
38
|
+
classNames={mx('invisible', !open && '@3xl:visible')}
|
|
39
|
+
onClick={() => setOpen((open) => !open)}
|
|
40
|
+
/>
|
|
41
|
+
</Toolbar.Root>
|
|
42
|
+
<div
|
|
43
|
+
className={mx(
|
|
44
|
+
'grid _grid-rows-[1fr_4rem] bs-full is-full gap-2',
|
|
45
|
+
open && '@3xl:grid-cols-[1fr_320px]',
|
|
46
|
+
role === 'section' && 'aspect-square',
|
|
47
|
+
role === 'section' && open && '@3xl:aspect-auto',
|
|
48
|
+
)}
|
|
49
|
+
>
|
|
50
|
+
<Chessboard.Content>
|
|
51
|
+
<Chessboard.Board classNames='m-4 rounded-sm overflow-hidden' orientation={orientation} />
|
|
52
|
+
</Chessboard.Content>
|
|
53
|
+
{open && (
|
|
54
|
+
<div className='hidden @3xl:flex flex-col p-8 justify-center items-center overflow-hidden'>
|
|
55
|
+
<Chessboard.Info
|
|
56
|
+
orientation={orientation}
|
|
57
|
+
min={8}
|
|
58
|
+
max={8}
|
|
59
|
+
onOrientationChange={setOrientation}
|
|
60
|
+
onClose={() => setOpen(false)}
|
|
61
|
+
onSelect={handleSelect}
|
|
62
|
+
/>
|
|
63
|
+
</div>
|
|
64
|
+
)}
|
|
65
|
+
</div>
|
|
66
|
+
</Chessboard.Root>
|
|
67
|
+
</StackItem.Content>
|
|
68
|
+
);
|
|
69
|
+
};
|
|
@@ -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} role={role} />;
|
|
40
|
+
}
|
|
41
|
+
}
|
|
42
|
+
};
|
|
43
|
+
|
|
44
|
+
export default ChessboardContainer;
|
|
@@ -0,0 +1,236 @@
|
|
|
1
|
+
//
|
|
2
|
+
// Copyright 2023 DXOS.org
|
|
3
|
+
//
|
|
4
|
+
|
|
5
|
+
import React, { type JSX, type PropsWithChildren, useEffect, useMemo, useRef } from 'react';
|
|
6
|
+
|
|
7
|
+
import { generateName } from '@dxos/display-name';
|
|
8
|
+
import { type SpaceMember, getSpace, useMembers } from '@dxos/react-client/echo';
|
|
9
|
+
import { Icon, IconButton, Select, type ThemedClassName, useTranslation } from '@dxos/react-ui';
|
|
10
|
+
import { type Player, useGameboardContext } from '@dxos/react-ui-gameboard';
|
|
11
|
+
import { mx } from '@dxos/react-ui-theme';
|
|
12
|
+
|
|
13
|
+
import { meta } from '../meta';
|
|
14
|
+
|
|
15
|
+
import { type ExtendedChessModel } from './Chessboard';
|
|
16
|
+
|
|
17
|
+
export type InfoProps = ThemedClassName<
|
|
18
|
+
{
|
|
19
|
+
orientation?: Player;
|
|
20
|
+
onOrientationChange?: (orientation: Player) => void;
|
|
21
|
+
onClose?: () => void;
|
|
22
|
+
} & Pick<HistoryProps, 'min' | 'max' | 'onSelect'>
|
|
23
|
+
>;
|
|
24
|
+
|
|
25
|
+
export const Info = ({ classNames, orientation = 'white', onOrientationChange, onClose, ...props }: InfoProps) => {
|
|
26
|
+
const { t } = useTranslation(meta.id);
|
|
27
|
+
const { model } = useGameboardContext<ExtendedChessModel>(Info.displayName);
|
|
28
|
+
const members = useMembers(getSpace(model.object)?.key);
|
|
29
|
+
|
|
30
|
+
return (
|
|
31
|
+
<div
|
|
32
|
+
className={mx(
|
|
33
|
+
'grid grid-rows-[min-content_1fr_min-content] is-full min-is-[18rem] p-2 overflow-hidden bg-groupSurface rounded-sm',
|
|
34
|
+
classNames,
|
|
35
|
+
)}
|
|
36
|
+
>
|
|
37
|
+
<PlayerIndicator
|
|
38
|
+
model={model}
|
|
39
|
+
player={orientation === 'white' ? 'black' : 'white'}
|
|
40
|
+
icon={
|
|
41
|
+
onClose && (
|
|
42
|
+
<IconButton icon='ph--x--regular' iconOnly label={t('close info button')} size={4} onClick={onClose} />
|
|
43
|
+
)
|
|
44
|
+
}
|
|
45
|
+
>
|
|
46
|
+
<PlayerSelector
|
|
47
|
+
value={model.object.players?.[orientation === 'white' ? 'black' : 'white']}
|
|
48
|
+
onValueChange={(value) => {
|
|
49
|
+
model.object.players![orientation === 'white' ? 'black' : 'white'] = value;
|
|
50
|
+
}}
|
|
51
|
+
members={members}
|
|
52
|
+
/>
|
|
53
|
+
</PlayerIndicator>
|
|
54
|
+
|
|
55
|
+
<History model={model} {...props} />
|
|
56
|
+
|
|
57
|
+
<PlayerIndicator
|
|
58
|
+
model={model}
|
|
59
|
+
player={orientation}
|
|
60
|
+
icon={
|
|
61
|
+
onOrientationChange && (
|
|
62
|
+
<IconButton
|
|
63
|
+
classNames={mx('transition duration-200 ease-linear', orientation === 'white' && 'rotate-180')}
|
|
64
|
+
icon='ph--arrows-clockwise--regular'
|
|
65
|
+
iconOnly
|
|
66
|
+
label={t('flip board button')}
|
|
67
|
+
size={4}
|
|
68
|
+
onClick={() => onOrientationChange(orientation === 'white' ? 'black' : 'white')}
|
|
69
|
+
/>
|
|
70
|
+
)
|
|
71
|
+
}
|
|
72
|
+
>
|
|
73
|
+
<PlayerSelector
|
|
74
|
+
value={model.object.players?.[orientation]}
|
|
75
|
+
onValueChange={(value) => {
|
|
76
|
+
model.object.players![orientation] = value;
|
|
77
|
+
}}
|
|
78
|
+
members={members}
|
|
79
|
+
/>
|
|
80
|
+
</PlayerIndicator>
|
|
81
|
+
</div>
|
|
82
|
+
);
|
|
83
|
+
};
|
|
84
|
+
|
|
85
|
+
Info.displayName = 'Chessboard.Info';
|
|
86
|
+
|
|
87
|
+
//
|
|
88
|
+
// History
|
|
89
|
+
//
|
|
90
|
+
|
|
91
|
+
type HistoryProps = ThemedClassName<{
|
|
92
|
+
model: ExtendedChessModel;
|
|
93
|
+
min?: number;
|
|
94
|
+
max?: number;
|
|
95
|
+
onSelect?: (index: number) => void;
|
|
96
|
+
}>;
|
|
97
|
+
|
|
98
|
+
const History = ({ classNames, model, min, max, onSelect }: HistoryProps) => {
|
|
99
|
+
const { t } = useTranslation(meta.id);
|
|
100
|
+
const label = model.game.isGameOver()
|
|
101
|
+
? model.game.isCheckmate()
|
|
102
|
+
? t('game.checkmate')
|
|
103
|
+
: model.game.isStalemate()
|
|
104
|
+
? t('game.stalemate')
|
|
105
|
+
: t('game.draw')
|
|
106
|
+
: model.game.isCheck()
|
|
107
|
+
? t('game.check')
|
|
108
|
+
: undefined;
|
|
109
|
+
|
|
110
|
+
const history = model.game.history();
|
|
111
|
+
const moves = useMemo(
|
|
112
|
+
() =>
|
|
113
|
+
history.reduce(
|
|
114
|
+
(acc, move, index) => {
|
|
115
|
+
if (index % 2 === 0) {
|
|
116
|
+
acc.push([{ index, move }]);
|
|
117
|
+
} else {
|
|
118
|
+
acc[acc.length - 1].push({ index, move });
|
|
119
|
+
}
|
|
120
|
+
return acc;
|
|
121
|
+
},
|
|
122
|
+
[] as { index: number; move: string }[][],
|
|
123
|
+
),
|
|
124
|
+
[history.length],
|
|
125
|
+
);
|
|
126
|
+
|
|
127
|
+
const scrollerRef = useRef<HTMLDivElement>(null);
|
|
128
|
+
useEffect(() => {
|
|
129
|
+
scrollerRef.current?.scrollTo({ top: scrollerRef.current.scrollHeight });
|
|
130
|
+
}, [history.length]);
|
|
131
|
+
|
|
132
|
+
useEffect(() => {
|
|
133
|
+
const div = scrollerRef.current?.querySelector(`[data-index="${model.moveIndex.value - 1}"]`);
|
|
134
|
+
scrollerRef.current?.classList.add('scrollbar-none');
|
|
135
|
+
div?.scrollIntoView({ behavior: 'smooth' });
|
|
136
|
+
scrollerRef.current?.classList.remove('scrollbar-none');
|
|
137
|
+
}, [model.moveIndex.value]);
|
|
138
|
+
|
|
139
|
+
return (
|
|
140
|
+
<div
|
|
141
|
+
ref={scrollerRef}
|
|
142
|
+
className={mx('overflow-y-scroll', classNames)}
|
|
143
|
+
style={{
|
|
144
|
+
minHeight: min === undefined ? 'auto' : `${min * 24}px`,
|
|
145
|
+
maxHeight: max === undefined ? 'auto' : `${max * 24}px`,
|
|
146
|
+
}}
|
|
147
|
+
>
|
|
148
|
+
{moves.map(([a, b], index) => (
|
|
149
|
+
<div key={index} className='grid grid-cols-[3rem_1fr_1fr_1rem] gap-2 pis-4 leading-1'>
|
|
150
|
+
<div className='content-center text-xs text-subdued'>{index + 1}</div>
|
|
151
|
+
{a && (
|
|
152
|
+
<div
|
|
153
|
+
data-index={a.index}
|
|
154
|
+
className={mx('pis-2 cursor-pointer', a.index === model.moveIndex.value - 1 && 'bg-primary-500')}
|
|
155
|
+
onClick={() => onSelect?.(a.index + 1)}
|
|
156
|
+
>
|
|
157
|
+
{a.move}
|
|
158
|
+
</div>
|
|
159
|
+
)}
|
|
160
|
+
{b && (
|
|
161
|
+
<div
|
|
162
|
+
data-index={b.index}
|
|
163
|
+
className={mx('pis-2 cursor-pointer', b.index === model.moveIndex.value - 1 && 'bg-primary-500')}
|
|
164
|
+
onClick={() => onSelect?.(b.index + 1)}
|
|
165
|
+
>
|
|
166
|
+
{b.move}
|
|
167
|
+
</div>
|
|
168
|
+
)}
|
|
169
|
+
</div>
|
|
170
|
+
))}
|
|
171
|
+
{label && <div className='text-center'>{label}</div>}
|
|
172
|
+
</div>
|
|
173
|
+
);
|
|
174
|
+
};
|
|
175
|
+
|
|
176
|
+
//
|
|
177
|
+
// PlayerIndicator
|
|
178
|
+
//
|
|
179
|
+
|
|
180
|
+
type PlayerIndicatorProps = PropsWithChildren<{
|
|
181
|
+
model: ExtendedChessModel;
|
|
182
|
+
player: Player;
|
|
183
|
+
icon?: JSX.Element;
|
|
184
|
+
}>;
|
|
185
|
+
|
|
186
|
+
const PlayerIndicator = ({ children, model, player, icon }: PlayerIndicatorProps) => {
|
|
187
|
+
const turn = player === (model.game.turn() === 'w' ? 'white' : 'black');
|
|
188
|
+
return (
|
|
189
|
+
<div className='grid grid-cols-[2rem_1fr_2rem] gap-2 bs-[--rail-size] pis-1 pie-1 flex items-center overflow-hidden'>
|
|
190
|
+
<div className='place-items-center'>
|
|
191
|
+
<Icon
|
|
192
|
+
icon={turn ? 'ph--circle--fill' : 'ph--circle--thin'}
|
|
193
|
+
size={6}
|
|
194
|
+
classNames={mx(turn && (model.game.isCheckmate() ? 'text-red-500' : 'text-green-500'))}
|
|
195
|
+
/>
|
|
196
|
+
</div>
|
|
197
|
+
<div className='truncate overflow-hidden items-center'>{children}</div>
|
|
198
|
+
{icon}
|
|
199
|
+
</div>
|
|
200
|
+
);
|
|
201
|
+
};
|
|
202
|
+
|
|
203
|
+
//
|
|
204
|
+
// PlayerSelector
|
|
205
|
+
//
|
|
206
|
+
|
|
207
|
+
type PlayerSelectorProps = {
|
|
208
|
+
value?: string;
|
|
209
|
+
onValueChange: (player: string) => void;
|
|
210
|
+
members: SpaceMember[];
|
|
211
|
+
};
|
|
212
|
+
|
|
213
|
+
const PlayerSelector = ({ value, onValueChange, members }: PlayerSelectorProps) => {
|
|
214
|
+
const { t } = useTranslation(meta.id);
|
|
215
|
+
return (
|
|
216
|
+
<Select.Root value={value} onValueChange={onValueChange}>
|
|
217
|
+
<Select.TriggerButton placeholder={t('select player button')} />
|
|
218
|
+
<Select.Portal>
|
|
219
|
+
<Select.Content>
|
|
220
|
+
<Select.Viewport>
|
|
221
|
+
{members.map((member) => {
|
|
222
|
+
const memberKey = member.identity.identityKey.toHex();
|
|
223
|
+
const displayName = member.identity?.profile?.displayName || generateName(memberKey);
|
|
224
|
+
return (
|
|
225
|
+
<Select.Option key={memberKey} value={memberKey}>
|
|
226
|
+
{displayName}
|
|
227
|
+
</Select.Option>
|
|
228
|
+
);
|
|
229
|
+
})}
|
|
230
|
+
</Select.Viewport>
|
|
231
|
+
<Select.Arrow />
|
|
232
|
+
</Select.Content>
|
|
233
|
+
</Select.Portal>
|
|
234
|
+
</Select.Root>
|
|
235
|
+
);
|
|
236
|
+
};
|
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,53 @@
|
|
|
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
|
+
side: Schema.optional(Schema.Literal('white', 'black', 'any')).annotations({
|
|
21
|
+
description: 'The side to play.',
|
|
22
|
+
defaultValue: 'any',
|
|
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
|
+
move: Schema.optional(Schema.String).annotations({
|
|
30
|
+
description: 'The move that was played.',
|
|
31
|
+
}),
|
|
32
|
+
}),
|
|
33
|
+
handler: Effect.fn(function* ({ data: { id, side = 'any' } }) {
|
|
34
|
+
const object = yield* DatabaseService.resolve(ArtifactId.toDXN(id), Chess.Game);
|
|
35
|
+
const chess = new ChessJS();
|
|
36
|
+
if (object.pgn) {
|
|
37
|
+
chess.loadPgn(object.pgn);
|
|
38
|
+
} else if (object.fen) {
|
|
39
|
+
chess.load(object.fen);
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
if (!(side === 'any' || (chess.turn() === 'w' && side === 'white') || (chess.turn() === 'b' && side === 'black'))) {
|
|
43
|
+
return { move: undefined, pgn: object.pgn! };
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
const moves = chess.moves();
|
|
47
|
+
const move = moves[Math.floor(Math.random() * moves.length)];
|
|
48
|
+
|
|
49
|
+
chess.move(move, { strict: false });
|
|
50
|
+
object.pgn = chess.pgn();
|
|
51
|
+
return { move, pgn: object.pgn };
|
|
52
|
+
}),
|
|
53
|
+
});
|
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,18 @@ 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
|
+
'toggle info button': 'Toggle info',
|
|
22
|
+
'close info button': 'Close info',
|
|
23
|
+
'flip board button': 'Flip board',
|
|
24
|
+
'select player button': 'Select player',
|
|
21
25
|
},
|
|
22
26
|
[meta.id]: {
|
|
23
27
|
'plugin name': 'Chess',
|
|
24
28
|
'delete game label': 'Delete',
|
|
29
|
+
'game.checkmate': 'Checkmate',
|
|
30
|
+
'game.stalemate': 'Stalemate',
|
|
31
|
+
'game.draw': 'Draw',
|
|
32
|
+
'game.check': 'Check',
|
|
25
33
|
},
|
|
26
34
|
},
|
|
27
35
|
},
|
|
@@ -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