@dxos/plugin-chess 0.8.4-main.fd6878d → 0.8.4-main.fffef41
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-NNIOQIRP.mjs → ChessboardContainer-5YXEM4VR.mjs} +29 -23
- package/dist/lib/browser/ChessboardContainer-5YXEM4VR.mjs.map +7 -0
- package/dist/lib/browser/blueprint-definition-FD4TL5EE.mjs +11 -0
- package/dist/lib/browser/blueprint-definition-FD4TL5EE.mjs.map +7 -0
- package/dist/lib/browser/chunk-5DOZV3PW.mjs +294 -0
- package/dist/lib/browser/chunk-5DOZV3PW.mjs.map +7 -0
- package/dist/lib/browser/{chunk-PE3IWOFR.mjs → chunk-DUO4WTAM.mjs} +5 -5
- package/dist/lib/browser/chunk-DUO4WTAM.mjs.map +7 -0
- package/dist/lib/browser/{chunk-OBJAHY6H.mjs → chunk-FBLAVAMY.mjs} +17 -12
- package/dist/lib/browser/chunk-FBLAVAMY.mjs.map +7 -0
- package/dist/lib/browser/{chunk-PPBBLHAC.mjs → chunk-Q5TZCIH7.mjs} +2 -2
- package/dist/lib/browser/chunk-TIIGNOGK.mjs +169 -0
- package/dist/lib/browser/chunk-TIIGNOGK.mjs.map +7 -0
- package/dist/lib/browser/index.mjs +27 -19
- package/dist/lib/browser/index.mjs.map +3 -3
- package/dist/lib/browser/{intent-resolver-JWKKRNGG.mjs → intent-resolver-GXZN2EBT.mjs} +5 -5
- package/dist/lib/browser/{intent-resolver-JWKKRNGG.mjs.map → intent-resolver-GXZN2EBT.mjs.map} +1 -1
- package/dist/lib/browser/meta.json +1 -1
- package/dist/lib/browser/{react-surface-RIXD6RTS.mjs → react-surface-KTTAUHAA.mjs} +7 -7
- package/dist/lib/browser/{react-surface-RIXD6RTS.mjs.map → react-surface-KTTAUHAA.mjs.map} +1 -1
- package/dist/lib/browser/types/index.mjs +2 -2
- package/dist/lib/node-esm/{ChessboardContainer-FCPUV2T2.mjs → ChessboardContainer-7OQ6TOKB.mjs} +29 -23
- package/dist/lib/node-esm/ChessboardContainer-7OQ6TOKB.mjs.map +7 -0
- package/dist/lib/node-esm/blueprint-definition-O7ZM3HUF.mjs +12 -0
- package/dist/lib/node-esm/blueprint-definition-O7ZM3HUF.mjs.map +7 -0
- package/dist/lib/node-esm/chunk-F4C2HVZL.mjs +295 -0
- package/dist/lib/node-esm/chunk-F4C2HVZL.mjs.map +7 -0
- package/dist/lib/node-esm/{chunk-6OWCPDUA.mjs → chunk-KNOOJFIR.mjs} +5 -5
- package/dist/lib/node-esm/chunk-KNOOJFIR.mjs.map +7 -0
- package/dist/lib/node-esm/{chunk-LCCUF5X5.mjs → chunk-MP3A3AGA.mjs} +17 -12
- package/dist/lib/node-esm/chunk-MP3A3AGA.mjs.map +7 -0
- package/dist/lib/node-esm/chunk-RTCAAOLF.mjs +170 -0
- package/dist/lib/node-esm/chunk-RTCAAOLF.mjs.map +7 -0
- package/dist/lib/node-esm/{chunk-42EKKMOO.mjs → chunk-VVXTAVIS.mjs} +2 -2
- package/dist/lib/node-esm/index.mjs +27 -19
- package/dist/lib/node-esm/index.mjs.map +3 -3
- package/dist/lib/node-esm/{intent-resolver-G6XJAW6C.mjs → intent-resolver-HK5L6P6Z.mjs} +5 -5
- package/dist/lib/node-esm/{intent-resolver-G6XJAW6C.mjs.map → intent-resolver-HK5L6P6Z.mjs.map} +1 -1
- package/dist/lib/node-esm/meta.json +1 -1
- package/dist/lib/node-esm/{react-surface-GGRDWZJT.mjs → react-surface-VO4PSY6O.mjs} +7 -7
- package/dist/lib/node-esm/{react-surface-GGRDWZJT.mjs.map → react-surface-VO4PSY6O.mjs.map} +1 -1
- package/dist/lib/node-esm/types/index.mjs +2 -2
- package/dist/types/src/ChessPlugin.d.ts +1 -1
- package/dist/types/src/ChessPlugin.d.ts.map +1 -1
- package/dist/types/src/capabilities/artifact-definition.d.ts.map +1 -1
- package/dist/types/src/capabilities/blueprint-definition.d.ts +4 -1
- package/dist/types/src/capabilities/blueprint-definition.d.ts.map +1 -1
- package/dist/types/src/capabilities/index.d.ts +3 -2
- 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/react-surface.d.ts +1 -1
- package/dist/types/src/components/Card.stories.d.ts +14 -41
- package/dist/types/src/components/Card.stories.d.ts.map +1 -1
- package/dist/types/src/components/Chessboard.d.ts +19 -13
- package/dist/types/src/components/Chessboard.d.ts.map +1 -1
- package/dist/types/src/components/Chessboard.stories.d.ts +41 -39
- package/dist/types/src/components/Chessboard.stories.d.ts.map +1 -1
- package/dist/types/src/components/ChessboardArticle.d.ts +2 -1
- package/dist/types/src/components/ChessboardArticle.d.ts.map +1 -1
- package/dist/types/src/components/{ChessboardInfo.d.ts → Info.d.ts} +7 -6
- package/dist/types/src/components/Info.d.ts.map +1 -0
- 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 +1 -0
- package/dist/types/src/functions/index.d.ts.map +1 -1
- package/dist/types/src/functions/move.d.ts +3 -1
- package/dist/types/src/functions/move.d.ts.map +1 -1
- package/dist/types/src/functions/play.d.ts +2 -1
- package/dist/types/src/functions/play.d.ts.map +1 -1
- package/dist/types/src/meta.d.ts +0 -1
- package/dist/types/src/meta.d.ts.map +1 -1
- package/dist/types/src/translations.d.ts +8 -6
- package/dist/types/src/translations.d.ts.map +1 -1
- package/dist/types/src/types/Chess.d.ts +8 -5
- package/dist/types/src/types/Chess.d.ts.map +1 -1
- package/dist/types/src/types/ChessAction.d.ts +3 -2
- package/dist/types/src/types/ChessAction.d.ts.map +1 -1
- package/dist/types/tsconfig.tsbuildinfo +1 -1
- package/package.json +42 -35
- package/src/ChessPlugin.tsx +47 -46
- package/src/capabilities/artifact-definition.ts +3 -2
- package/src/capabilities/blueprint-definition.ts +9 -7
- package/src/capabilities/index.ts +2 -0
- package/src/components/Card.stories.tsx +13 -15
- package/src/components/Chessboard.stories.tsx +13 -14
- package/src/components/Chessboard.tsx +104 -28
- package/src/components/ChessboardArticle.tsx +32 -21
- package/src/components/ChessboardContainer.tsx +1 -1
- package/src/components/Info.tsx +236 -0
- package/src/functions/create.ts +32 -0
- package/src/functions/index.ts +1 -0
- package/src/functions/move.ts +16 -14
- package/src/functions/play.ts +25 -15
- package/src/meta.ts +4 -4
- package/src/translations.ts +8 -6
- package/src/types/Chess.ts +23 -19
- package/src/types/ChessAction.ts +1 -1
- package/dist/lib/browser/ChessboardContainer-NNIOQIRP.mjs.map +0 -7
- package/dist/lib/browser/blueprint-definition-OJQYAZGZ.mjs +0 -38
- package/dist/lib/browser/blueprint-definition-OJQYAZGZ.mjs.map +0 -7
- package/dist/lib/browser/chunk-2W3S5N6C.mjs +0 -257
- package/dist/lib/browser/chunk-2W3S5N6C.mjs.map +0 -7
- package/dist/lib/browser/chunk-EJMPHRSR.mjs +0 -94
- package/dist/lib/browser/chunk-EJMPHRSR.mjs.map +0 -7
- package/dist/lib/browser/chunk-OBJAHY6H.mjs.map +0 -7
- package/dist/lib/browser/chunk-PE3IWOFR.mjs.map +0 -7
- package/dist/lib/node-esm/ChessboardContainer-FCPUV2T2.mjs.map +0 -7
- package/dist/lib/node-esm/blueprint-definition-F5SGHCDA.mjs +0 -39
- package/dist/lib/node-esm/blueprint-definition-F5SGHCDA.mjs.map +0 -7
- package/dist/lib/node-esm/chunk-5APYXPBX.mjs +0 -95
- package/dist/lib/node-esm/chunk-5APYXPBX.mjs.map +0 -7
- package/dist/lib/node-esm/chunk-6OWCPDUA.mjs.map +0 -7
- package/dist/lib/node-esm/chunk-LCCUF5X5.mjs.map +0 -7
- package/dist/lib/node-esm/chunk-QAGOXJGH.mjs +0 -258
- package/dist/lib/node-esm/chunk-QAGOXJGH.mjs.map +0 -7
- package/dist/types/src/components/ChessboardInfo.d.ts.map +0 -1
- package/dist/types/src/components/ChessboardPlayers.d.ts +0 -8
- package/dist/types/src/components/ChessboardPlayers.d.ts.map +0 -1
- package/src/components/ChessboardInfo.tsx +0 -140
- package/src/components/ChessboardPlayers.tsx +0 -97
- /package/dist/lib/browser/{chunk-PPBBLHAC.mjs.map → chunk-Q5TZCIH7.mjs.map} +0 -0
- /package/dist/lib/node-esm/{chunk-42EKKMOO.mjs.map → chunk-VVXTAVIS.mjs.map} +0 -0
|
@@ -2,9 +2,9 @@
|
|
|
2
2
|
// Copyright 2024 DXOS.org
|
|
3
3
|
//
|
|
4
4
|
|
|
5
|
-
import React, { useState } from 'react';
|
|
5
|
+
import React, { useCallback, useRef, useState } from 'react';
|
|
6
6
|
|
|
7
|
-
import {
|
|
7
|
+
import { Toolbar, useTranslation } from '@dxos/react-ui';
|
|
8
8
|
import { type Player } from '@dxos/react-ui-gameboard';
|
|
9
9
|
import { StackItem } from '@dxos/react-ui-stack';
|
|
10
10
|
import { mx } from '@dxos/react-ui-theme';
|
|
@@ -12,19 +12,42 @@ import { mx } from '@dxos/react-ui-theme';
|
|
|
12
12
|
import { meta } from '../meta';
|
|
13
13
|
import { type Chess } from '../types';
|
|
14
14
|
|
|
15
|
-
import { Chessboard } from './Chessboard';
|
|
15
|
+
import { Chessboard, type ChessboardController, type ChessboardInfoProps } from './Chessboard';
|
|
16
16
|
|
|
17
|
-
export const ChessboardArticle = ({ game }: { game: Chess.Game }) => {
|
|
17
|
+
export const ChessboardArticle = ({ game, role }: { game: Chess.Game; role?: string }) => {
|
|
18
18
|
const { t } = useTranslation(meta.id);
|
|
19
19
|
const [orientation, setOrientation] = useState<Player>('white');
|
|
20
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
|
+
}, []);
|
|
21
27
|
|
|
22
28
|
return (
|
|
23
|
-
<StackItem.Content classNames='@container
|
|
24
|
-
<Chessboard.Root game={game}>
|
|
25
|
-
<
|
|
29
|
+
<StackItem.Content toolbar classNames='@container'>
|
|
30
|
+
<Chessboard.Root game={game} ref={controller}>
|
|
31
|
+
<Toolbar.Root>
|
|
32
|
+
<Toolbar.IconButton
|
|
33
|
+
icon='ph--info--regular'
|
|
34
|
+
iconOnly
|
|
35
|
+
label={t('toggle info button')}
|
|
36
|
+
disabled={open}
|
|
37
|
+
classNames={mx('invisible @3xl:visible')}
|
|
38
|
+
onClick={() => setOpen((open) => !open)}
|
|
39
|
+
/>
|
|
40
|
+
</Toolbar.Root>
|
|
41
|
+
<div
|
|
42
|
+
className={mx(
|
|
43
|
+
'grid bs-full is-full gap-2',
|
|
44
|
+
open && '@3xl:grid-cols-[1fr_320px]',
|
|
45
|
+
role === 'section' && 'aspect-square',
|
|
46
|
+
role === 'section' && open && '@3xl:aspect-auto',
|
|
47
|
+
)}
|
|
48
|
+
>
|
|
26
49
|
<Chessboard.Content>
|
|
27
|
-
<Chessboard.Board orientation={orientation} />
|
|
50
|
+
<Chessboard.Board classNames='m-4 rounded-sm overflow-hidden' orientation={orientation} />
|
|
28
51
|
</Chessboard.Content>
|
|
29
52
|
{open && (
|
|
30
53
|
<div className='hidden @3xl:flex flex-col p-8 justify-center items-center overflow-hidden'>
|
|
@@ -34,22 +57,10 @@ export const ChessboardArticle = ({ game }: { game: Chess.Game }) => {
|
|
|
34
57
|
max={8}
|
|
35
58
|
onOrientationChange={setOrientation}
|
|
36
59
|
onClose={() => setOpen(false)}
|
|
60
|
+
onSelect={handleSelect}
|
|
37
61
|
/>
|
|
38
62
|
</div>
|
|
39
63
|
)}
|
|
40
|
-
<div className='flex justify-center items-center'>
|
|
41
|
-
<Chessboard.Players>
|
|
42
|
-
<IconButton
|
|
43
|
-
variant='ghost'
|
|
44
|
-
icon='ph--info--regular'
|
|
45
|
-
iconOnly
|
|
46
|
-
label={t('button toggle info')}
|
|
47
|
-
size={6}
|
|
48
|
-
classNames={mx('invisible', !open && '@3xl:visible')}
|
|
49
|
-
onClick={() => setOpen((open) => !open)}
|
|
50
|
-
/>
|
|
51
|
-
</Chessboard.Players>
|
|
52
|
-
</div>
|
|
53
64
|
</div>
|
|
54
65
|
</Chessboard.Root>
|
|
55
66
|
</StackItem.Content>
|
|
@@ -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 label')
|
|
103
|
+
: model.game.isStalemate()
|
|
104
|
+
? t('game.stalemate label')
|
|
105
|
+
: t('game.draw label')
|
|
106
|
+
: model.game.isCheck()
|
|
107
|
+
? t('game.check label')
|
|
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-auto', 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] pli-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
|
+
};
|
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
//
|
|
2
|
+
// Copyright 2025 DXOS.org
|
|
3
|
+
//
|
|
4
|
+
|
|
5
|
+
import * as Effect from 'effect/Effect';
|
|
6
|
+
import * as Schema from 'effect/Schema';
|
|
7
|
+
|
|
8
|
+
import { DatabaseService, defineFunction } from '@dxos/functions';
|
|
9
|
+
|
|
10
|
+
import { Chess } from '../types';
|
|
11
|
+
|
|
12
|
+
export default defineFunction({
|
|
13
|
+
key: 'dxos.org/function/chess/create',
|
|
14
|
+
name: 'Create Chess',
|
|
15
|
+
description: 'Creates a new chess game.',
|
|
16
|
+
inputSchema: Schema.Struct({
|
|
17
|
+
pgn: Schema.optional(
|
|
18
|
+
Schema.String.annotations({
|
|
19
|
+
description: 'Portable Game Notation.',
|
|
20
|
+
}),
|
|
21
|
+
),
|
|
22
|
+
fen: Schema.optional(
|
|
23
|
+
Schema.String.annotations({
|
|
24
|
+
description: 'Forsyth-Edwards Notation.',
|
|
25
|
+
}),
|
|
26
|
+
),
|
|
27
|
+
}),
|
|
28
|
+
outputSchema: Chess.Game,
|
|
29
|
+
handler: Effect.fn(function* ({ data: { pgn, fen } }) {
|
|
30
|
+
return yield* DatabaseService.add(Chess.makeGame({ pgn, fen }));
|
|
31
|
+
}),
|
|
32
|
+
});
|
package/src/functions/index.ts
CHANGED
package/src/functions/move.ts
CHANGED
|
@@ -3,7 +3,8 @@
|
|
|
3
3
|
//
|
|
4
4
|
|
|
5
5
|
import { Chess as ChessJS } from 'chess.js';
|
|
6
|
-
import
|
|
6
|
+
import * as Effect from 'effect/Effect';
|
|
7
|
+
import * as Schema from 'effect/Schema';
|
|
7
8
|
|
|
8
9
|
import { ArtifactId } from '@dxos/assistant';
|
|
9
10
|
import { DatabaseService, defineFunction } from '@dxos/functions';
|
|
@@ -11,7 +12,8 @@ import { DatabaseService, defineFunction } from '@dxos/functions';
|
|
|
11
12
|
import { Chess } from '../types';
|
|
12
13
|
|
|
13
14
|
export default defineFunction({
|
|
14
|
-
|
|
15
|
+
key: 'dxos.org/function/chess/move',
|
|
16
|
+
name: 'Move',
|
|
15
17
|
description: 'Makes a move in the given chess game.',
|
|
16
18
|
inputSchema: Schema.Struct({
|
|
17
19
|
id: ArtifactId.annotations({
|
|
@@ -22,22 +24,22 @@ export default defineFunction({
|
|
|
22
24
|
examples: ['e4', 'Bf3'],
|
|
23
25
|
}),
|
|
24
26
|
}),
|
|
25
|
-
outputSchema: Schema.
|
|
26
|
-
|
|
27
|
+
outputSchema: Schema.Struct({
|
|
28
|
+
pgn: Schema.String.annotations({
|
|
29
|
+
description: 'The PGN of the game after the move was played.',
|
|
30
|
+
}),
|
|
27
31
|
}),
|
|
28
32
|
handler: Effect.fn(function* ({ data: { id, move } }) {
|
|
29
33
|
const object = yield* DatabaseService.resolve(ArtifactId.toDXN(id), Chess.Game);
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
// Use Effect's error handling mechanisms instead (e.g., Effect.try, Effect.tryPromise, Effect.catchAll, Effect.catchTag)
|
|
33
|
-
try {
|
|
34
|
-
const chess = new ChessJS();
|
|
34
|
+
const chess = new ChessJS();
|
|
35
|
+
if (object.pgn) {
|
|
35
36
|
chess.loadPgn(object.pgn);
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
return true;
|
|
39
|
-
} catch {
|
|
40
|
-
return false;
|
|
37
|
+
} else if (object.fen) {
|
|
38
|
+
chess.load(object.fen);
|
|
41
39
|
}
|
|
40
|
+
|
|
41
|
+
chess.move(move, { strict: false });
|
|
42
|
+
object.pgn = chess.pgn();
|
|
43
|
+
return { pgn: object.pgn };
|
|
42
44
|
}),
|
|
43
45
|
});
|
package/src/functions/play.ts
CHANGED
|
@@ -3,43 +3,53 @@
|
|
|
3
3
|
//
|
|
4
4
|
|
|
5
5
|
import { Chess as ChessJS } from 'chess.js';
|
|
6
|
-
import
|
|
6
|
+
import * as Effect from 'effect/Effect';
|
|
7
|
+
import * as Schema from 'effect/Schema';
|
|
7
8
|
|
|
8
9
|
import { ArtifactId } from '@dxos/assistant';
|
|
9
10
|
import { DatabaseService, defineFunction } from '@dxos/functions';
|
|
10
|
-
import { log } from '@dxos/log';
|
|
11
11
|
|
|
12
12
|
import { Chess } from '../types';
|
|
13
13
|
|
|
14
14
|
export default defineFunction({
|
|
15
|
-
|
|
16
|
-
|
|
15
|
+
key: 'dxos.org/function/chess/play',
|
|
16
|
+
name: 'Play',
|
|
17
|
+
description: 'Uses the chess engine to play the next move.',
|
|
17
18
|
inputSchema: Schema.Struct({
|
|
18
19
|
id: ArtifactId.annotations({
|
|
19
20
|
description: 'The ID of the chess object.',
|
|
20
21
|
}),
|
|
22
|
+
side: Schema.optional(Schema.Literal('white', 'black', 'any')).annotations({
|
|
23
|
+
description: 'The side to play.',
|
|
24
|
+
defaultValue: 'any',
|
|
25
|
+
}),
|
|
21
26
|
}),
|
|
22
27
|
outputSchema: Schema.Struct({
|
|
23
|
-
move: Schema.optional(Schema.String).annotations({
|
|
24
|
-
description: 'The move that was played.',
|
|
25
|
-
}),
|
|
26
28
|
pgn: Schema.String.annotations({
|
|
27
29
|
description: 'The PGN of the game after the move was played.',
|
|
28
30
|
}),
|
|
31
|
+
move: Schema.optional(Schema.String).annotations({
|
|
32
|
+
description: 'The move that was played.',
|
|
33
|
+
}),
|
|
29
34
|
}),
|
|
30
|
-
handler: Effect.fn(function* ({ data: { id } }) {
|
|
35
|
+
handler: Effect.fn(function* ({ data: { id, side = 'any' } }) {
|
|
31
36
|
const object = yield* DatabaseService.resolve(ArtifactId.toDXN(id), Chess.Game);
|
|
32
|
-
|
|
33
|
-
// Create game and make move.
|
|
34
37
|
const chess = new ChessJS();
|
|
35
|
-
|
|
38
|
+
if (object.pgn) {
|
|
39
|
+
chess.loadPgn(object.pgn);
|
|
40
|
+
} else if (object.fen) {
|
|
41
|
+
chess.load(object.fen);
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
if (!(side === 'any' || (chess.turn() === 'w' && side === 'white') || (chess.turn() === 'b' && side === 'black'))) {
|
|
45
|
+
return { move: undefined, pgn: object.pgn! };
|
|
46
|
+
}
|
|
47
|
+
|
|
36
48
|
const moves = chess.moves();
|
|
37
49
|
const move = moves[Math.floor(Math.random() * moves.length)];
|
|
38
|
-
log.info('move', { move });
|
|
39
|
-
chess.move(move);
|
|
40
50
|
|
|
41
|
-
|
|
51
|
+
chess.move(move, { strict: false });
|
|
42
52
|
object.pgn = chess.pgn();
|
|
43
|
-
return { move, pgn:
|
|
53
|
+
return { move, pgn: object.pgn };
|
|
44
54
|
}),
|
|
45
55
|
});
|
package/src/meta.ts
CHANGED
|
@@ -5,15 +5,15 @@
|
|
|
5
5
|
import { type PluginMeta } from '@dxos/app-framework';
|
|
6
6
|
import { trim } from '@dxos/util';
|
|
7
7
|
|
|
8
|
-
export const CHESS_PLUGIN = 'dxos.org/plugin/chess';
|
|
9
|
-
|
|
10
8
|
export const meta: PluginMeta = {
|
|
11
|
-
id:
|
|
9
|
+
id: 'dxos.org/plugin/chess',
|
|
12
10
|
name: 'Chess',
|
|
13
11
|
description: trim`
|
|
14
|
-
|
|
12
|
+
Full-featured chess game supporting multiplayer matches with friends or practice sessions against AI opponents.
|
|
13
|
+
Track game history and analyze moves in real-time.
|
|
15
14
|
`,
|
|
16
15
|
icon: 'ph--shield-chevron--regular',
|
|
16
|
+
iconHue: 'amber',
|
|
17
17
|
source: 'https://github.com/dxos/dxos/tree/main/packages/plugins/plugin-chess',
|
|
18
18
|
screenshots: ['https://dxos.network/plugin-details-chess-dark.png'],
|
|
19
19
|
};
|
package/src/translations.ts
CHANGED
|
@@ -18,16 +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
|
-
'button toggle info': 'Toggle info',
|
|
22
|
-
'button flip': 'Flip board',
|
|
23
21
|
},
|
|
24
22
|
[meta.id]: {
|
|
25
23
|
'plugin name': 'Chess',
|
|
26
24
|
'delete game label': 'Delete',
|
|
27
|
-
'game.checkmate': 'Checkmate',
|
|
28
|
-
'game.stalemate': 'Stalemate',
|
|
29
|
-
'game.draw': 'Draw',
|
|
30
|
-
'game.check': 'Check',
|
|
25
|
+
'game.checkmate label': 'Checkmate',
|
|
26
|
+
'game.stalemate label': 'Stalemate',
|
|
27
|
+
'game.draw label': 'Draw',
|
|
28
|
+
'game.check label': 'Check',
|
|
29
|
+
'select player button': 'Select player',
|
|
30
|
+
'toggle info button': 'Toggle info',
|
|
31
|
+
'close info button': 'Close info',
|
|
32
|
+
'flip board button': 'Flip board',
|
|
31
33
|
},
|
|
32
34
|
},
|
|
33
35
|
},
|
package/src/types/Chess.ts
CHANGED
|
@@ -3,42 +3,45 @@
|
|
|
3
3
|
//
|
|
4
4
|
|
|
5
5
|
import { Chess as ChessJS } from 'chess.js';
|
|
6
|
-
import
|
|
6
|
+
import * as Schema from 'effect/Schema';
|
|
7
7
|
|
|
8
8
|
import { Obj, Type } from '@dxos/echo';
|
|
9
|
-
import { LabelAnnotation } from '@dxos/echo
|
|
9
|
+
import { FormAnnotation, LabelAnnotation } from '@dxos/echo/internal';
|
|
10
10
|
import { log } from '@dxos/log';
|
|
11
|
+
import { ItemAnnotation } from '@dxos/schema';
|
|
11
12
|
|
|
12
13
|
export const Game = Schema.Struct({
|
|
13
14
|
name: Schema.optional(Schema.String),
|
|
14
|
-
players: Schema.
|
|
15
|
-
Schema.
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
}).pipe(Schema.mutable),
|
|
27
|
-
),
|
|
15
|
+
players: Schema.Struct({
|
|
16
|
+
white: Schema.optional(
|
|
17
|
+
Schema.String.annotations({
|
|
18
|
+
description: 'DID of white player',
|
|
19
|
+
}),
|
|
20
|
+
),
|
|
21
|
+
black: Schema.optional(
|
|
22
|
+
Schema.String.annotations({
|
|
23
|
+
description: 'DID of black player',
|
|
24
|
+
}),
|
|
25
|
+
),
|
|
26
|
+
}).pipe(Schema.mutable, FormAnnotation.set(false), Schema.optional),
|
|
28
27
|
pgn: Schema.String.annotations({
|
|
29
28
|
description: 'Portable Game Notation.',
|
|
30
|
-
}),
|
|
29
|
+
}).pipe(FormAnnotation.set(false), Schema.optional),
|
|
30
|
+
fen: Schema.String.annotations({
|
|
31
|
+
description: 'Forsyth-Edwards Notation.',
|
|
32
|
+
}).pipe(FormAnnotation.set(false), Schema.optional),
|
|
31
33
|
}).pipe(
|
|
32
34
|
Type.Obj({
|
|
33
35
|
typename: 'dxos.org/type/Chess',
|
|
34
36
|
version: '0.2.0',
|
|
35
37
|
}),
|
|
36
38
|
LabelAnnotation.set(['name']),
|
|
39
|
+
ItemAnnotation.set(true),
|
|
37
40
|
);
|
|
38
41
|
|
|
39
42
|
export interface Game extends Schema.Schema.Type<typeof Game> {}
|
|
40
43
|
|
|
41
|
-
export const makeGame = ({ name, pgn }: { name?: string; pgn?: string } = {}) => {
|
|
44
|
+
export const makeGame = ({ name, pgn, fen }: { name?: string; pgn?: string; fen?: string } = {}) => {
|
|
42
45
|
const chess = new ChessJS();
|
|
43
46
|
if (pgn) {
|
|
44
47
|
try {
|
|
@@ -51,6 +54,7 @@ export const makeGame = ({ name, pgn }: { name?: string; pgn?: string } = {}) =>
|
|
|
51
54
|
return Obj.make(Game, {
|
|
52
55
|
name,
|
|
53
56
|
players: {},
|
|
54
|
-
pgn
|
|
57
|
+
pgn,
|
|
58
|
+
fen,
|
|
55
59
|
});
|
|
56
60
|
};
|
package/src/types/ChessAction.ts
CHANGED
|
@@ -1,7 +0,0 @@
|
|
|
1
|
-
{
|
|
2
|
-
"version": 3,
|
|
3
|
-
"sources": ["../../../src/components/ChessboardContainer.tsx", "../../../src/components/ChessboardArticle.tsx"],
|
|
4
|
-
"sourcesContent": ["//\n// Copyright 2024 DXOS.org\n//\n\nimport React from 'react';\n\nimport { getSpace } from '@dxos/react-client/echo';\n\nimport { type Chess } from '../types';\n\nimport { Chessboard } from './Chessboard';\nimport { ChessboardArticle } from './ChessboardArticle';\n\nexport type ChessboardContainerProps = {\n game: Chess.Game;\n role?: string;\n};\n\nexport const ChessboardContainer = ({ game, role }: ChessboardContainerProps) => {\n const space = getSpace(game);\n if (!space) {\n return null;\n }\n\n switch (role) {\n case 'card--popover':\n case 'card--intrinsic':\n case 'card--extrinsic': {\n return (\n <Chessboard.Root game={game}>\n <Chessboard.Content role={role}>\n <Chessboard.Board />\n </Chessboard.Content>\n </Chessboard.Root>\n );\n }\n\n default: {\n return <ChessboardArticle game={game} />;\n }\n }\n};\n\nexport default ChessboardContainer;\n", "//\n// Copyright 2024 DXOS.org\n//\n\nimport React, { useState } from 'react';\n\nimport { IconButton, useTranslation } from '@dxos/react-ui';\nimport { type Player } from '@dxos/react-ui-gameboard';\nimport { StackItem } from '@dxos/react-ui-stack';\nimport { mx } from '@dxos/react-ui-theme';\n\nimport { meta } from '../meta';\nimport { type Chess } from '../types';\n\nimport { Chessboard } from './Chessboard';\n\nexport const ChessboardArticle = ({ game }: { game: Chess.Game }) => {\n const { t } = useTranslation(meta.id);\n const [orientation, setOrientation] = useState<Player>('white');\n const [open, setOpen] = useState(true);\n\n return (\n <StackItem.Content classNames='@container bs-full is-full overflow-hidden'>\n <Chessboard.Root game={game}>\n <div className={mx('grid grid-rows-[1fr_4rem] bs-full is-full gap-2', open && '@3xl:grid-cols-[1fr_320px]')}>\n <Chessboard.Content>\n <Chessboard.Board orientation={orientation} />\n </Chessboard.Content>\n {open && (\n <div className='hidden @3xl:flex flex-col p-8 justify-center items-center overflow-hidden'>\n <Chessboard.Info\n orientation={orientation}\n min={8}\n max={8}\n onOrientationChange={setOrientation}\n onClose={() => setOpen(false)}\n />\n </div>\n )}\n <div className='flex justify-center items-center'>\n <Chessboard.Players>\n <IconButton\n variant='ghost'\n icon='ph--info--regular'\n iconOnly\n label={t('button toggle info')}\n size={6}\n classNames={mx('invisible', !open && '@3xl:visible')}\n onClick={() => setOpen((open) => !open)}\n />\n </Chessboard.Players>\n </div>\n </div>\n </Chessboard.Root>\n </StackItem.Content>\n );\n};\n"],
|
|
5
|
-
"mappings": ";;;;;;;;;AAIA,OAAOA,YAAW;AAElB,SAASC,gBAAgB;;;;ACFzB,OAAOC,SAASC,gBAAgB;AAEhC,SAASC,YAAYC,sBAAsB;AAE3C,SAASC,iBAAiB;AAC1B,SAASC,UAAU;AAOZ,IAAMC,oBAAoB,CAAC,EAAEC,KAAI,MAAwB;;;AAC9D,UAAM,EAAEC,EAAC,IAAKC,eAAeC,KAAKC,EAAE;AACpC,UAAM,CAACC,aAAaC,cAAAA,IAAkBC,SAAiB,OAAA;AACvD,UAAM,CAACC,MAAMC,OAAAA,IAAWF,SAAS,IAAA;AAEjC,WACE,sBAAA,cAACG,UAAUC,SAAO;MAACC,YAAW;OAC5B,sBAAA,cAACC,WAAWC,MAAI;MAACd;OACf,sBAAA,cAACe,OAAAA;MAAIC,WAAWC,GAAG,mDAAmDT,QAAQ,4BAAA;OAC5E,sBAAA,cAACK,WAAWF,SAAO,MACjB,sBAAA,cAACE,WAAWK,OAAK;MAACb;SAEnBG,QACC,sBAAA,cAACO,OAAAA;MAAIC,WAAU;OACb,sBAAA,cAACH,WAAWM,MAAI;MACdd;MACAe,KAAK;MACLC,KAAK;MACLC,qBAAqBhB;MACrBiB,SAAS,MAAMd,QAAQ,KAAA;SAI7B,sBAAA,cAACM,OAAAA;MAAIC,WAAU;OACb,sBAAA,cAACH,WAAWW,SAAO,MACjB,sBAAA,cAACC,YAAAA;MACCC,SAAQ;MACRC,MAAK;MACLC,UAAAA;MACAC,OAAO5B,EAAE,oBAAA;MACT6B,MAAM;MACNlB,YAAYK,GAAG,aAAa,CAACT,QAAQ,cAAA;MACrCuB,SAAS,MAAMtB,QAAQ,CAACD,UAAS,CAACA,KAAAA;;;;;AAQlD;;;ADtCO,IAAMwB,sBAAsB,CAAC,EAAEC,MAAMC,KAAI,MAA4B;;;AAC1E,UAAMC,QAAQC,SAASH,IAAAA;AACvB,QAAI,CAACE,OAAO;AACV,aAAO;IACT;AAEA,YAAQD,MAAAA;MACN,KAAK;MACL,KAAK;MACL,KAAK,mBAAmB;AACtB,eACE,gBAAAG,OAAA,cAACC,WAAWC,MAAI;UAACN;WACf,gBAAAI,OAAA,cAACC,WAAWE,SAAO;UAACN;WAClB,gBAAAG,OAAA,cAACC,WAAWG,OAAK,IAAA,CAAA,CAAA;MAIzB;MAEA,SAAS;AACP,eAAO,gBAAAJ,OAAA,cAACK,mBAAAA;UAAkBT;;MAC5B;IACF;;;;AACF;AAEA,IAAA,8BAAeD;",
|
|
6
|
-
"names": ["React", "getSpace", "React", "useState", "IconButton", "useTranslation", "StackItem", "mx", "ChessboardArticle", "game", "t", "useTranslation", "meta", "id", "orientation", "setOrientation", "useState", "open", "setOpen", "StackItem", "Content", "classNames", "Chessboard", "Root", "div", "className", "mx", "Board", "Info", "min", "max", "onOrientationChange", "onClose", "Players", "IconButton", "variant", "icon", "iconOnly", "label", "size", "onClick", "ChessboardContainer", "game", "role", "space", "getSpace", "React", "Chessboard", "Root", "Content", "Board", "ChessboardArticle"]
|
|
7
|
-
}
|