Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 9 additions & 1 deletion mise.toml
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,17 @@ bun = "1.2.18"
node = "20.5.1"
pnpm = "10.12.1"

[tasks.dev]
description = "Run dev"
run = "pnpm run --quiet dev"

[tasks.check]
description = "Run linting and type checking"
run = "pnpm run --quiet check"

[tasks."test"]
description = "Run the tests"
run = "pnpm run --silent test"
run = "pnpm run --quiet test"

[tasks."uci"]
description = "Run the UCI interface"
Expand Down
5 changes: 4 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
"@mantine/hooks": "^6.0.21",
"@p5-wrapper/next": "^0.2.0",
"@p5-wrapper/react": "^4.2.0",
"@tabler/icons-react": "^3.34.1",
"@vercel/analytics": "^1.2.2",
"class-transformer": "^0.5.1",
"howler": "^2.2.3",
Expand All @@ -38,7 +39,9 @@
"react-dom": "18.3.0",
"react-no-ssr": "^1.1.0",
"reflect-metadata": "^0.1.14",
"ts-pattern": "^5.0.6"
"ts-pattern": "^5.0.6",
"y-webrtc": "^10.3.0",
"yjs": "^13.6.27"
},
"devDependencies": {
"@testing-library/jest-dom": "^6.1.3",
Expand Down
8 changes: 8 additions & 0 deletions pages/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -289,6 +289,14 @@ export default function Home() {
<Button onClick={leaderboard.open} leftIcon="🏆" color="yellow">
Leaderboard
</Button>
<Button
onClick={async () => await router.push('/multiplayer')}
leftIcon="👥"
color="violet"
variant="light"
>
Multiplayer
</Button>
</Group>

<div style={{ paddingBottom: '1rem' }}>
Expand Down
98 changes: 98 additions & 0 deletions pages/multiplayer.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
import React, { useState, useEffect } from 'react'
import Head from 'next/head'
import { useRouter } from 'next/router'
import { useElementSize } from '@mantine/hooks'
import { Stack } from '@mantine/core'

import { GameProps } from '@/game/types'
import { useMultiplayerConnection } from '@/multiplayer/hooks'
import { ConnectionManager, MultiplayerGameBoard } from '@/multiplayer/components'
import { getShareableLink } from '@/multiplayer/utils'
import styles from '../styles/index.module.scss'

/**
* Main multiplayer chess page component.
*/
export default function Multiplayer() {
const router = useRouter()
const { ref: containerRef, width, height } = useElementSize()

const {
// State
multiplayerGame,
connectionStatus,
gameId,
joinGameId,
error,
isHost,
// Actions
setJoinGameId,
createGame,
joinGame,
disconnect,
} = useMultiplayerConnection()

// Game state for the board
const [bestMove, setBestMove] = useState<Parameters<GameProps['onBestMoveChange']>[0]>(null)
const [history, setHistory] = useState<Parameters<GameProps['onHistoryChange']>[0]>([])
const [output, setOutput] = useState<Parameters<GameProps['onOutputChange']>[0]>('')
const [gameStatus, setGameStatus] =
useState<Parameters<GameProps['onStatusChange']>[0]>('playing')

// Handle join link from URL
useEffect(() => {
const { join } = router.query
if (join && typeof join === 'string') {
setJoinGameId(join)
}
}, [router.query, setJoinGameId])

const handleGetShareableLink = () => getShareableLink(gameId)

const showGameBoard = connectionStatus === 'connected'

return (
<>
<Head>
<title>Multiplayer Chess - Ches</title>
<meta name="viewport" content="width=device-width, initial-scale=1" />
</Head>

<main className={styles.main}>
<div ref={containerRef}>
<Stack mb="md" spacing="md">
<ConnectionManager
connectionStatus={connectionStatus}
gameId={gameId}
joinGameId={joinGameId}
error={error}
isHost={isHost}
onCreateGame={createGame}
onJoinGame={joinGame}
onDisconnect={disconnect}
onJoinGameIdChange={setJoinGameId}
getShareableLink={handleGetShareableLink}
/>
</Stack>

{/* Game Board - Only show when both players are connected */}
{showGameBoard && (
<MultiplayerGameBoard
multiplayerGame={multiplayerGame}
width={width}
height={height}
history={history}
bestMove={bestMove}
gameStatus={gameStatus}
output={output}
onBestMoveChange={setBestMove}
onStatusChange={setGameStatus}
onOutputChange={setOutput}
onHistoryChange={setHistory}
/>
)}
</div>
</main>
</>
)
}
Loading
Loading