play again

This commit is contained in:
Chris Wanstrath 2026-01-24 09:19:48 -08:00
parent 08f04e09d4
commit 6aeca0838d
3 changed files with 31 additions and 6 deletions

View File

@ -1,13 +1,21 @@
// DO NOT MODIFY!
// This file is generated by pyre.
// Generated: 2026-01-20 12:25:24.
// Generated: 2026-01-24 09:09:49.
import { send } from 'pyre/client'
export const playAgain = () => {
send({
type: 'action',
action: 'playAgain',
args: []
})
}
export const nameChange = (name: string) => {
send({
type: 'action',
action: 'name:change',
action: 'nameChange',
args: [name]
})
}

View File

@ -1,5 +1,5 @@
import { define, createThemes } from 'forge'
import { move, nameChange } from './actions'
import { move, nameChange, playAgain } from './actions'
import type { Game } from '../types'
import { sessionId } from 'pyre/client'
@ -271,6 +271,7 @@ const WinBanner = define('WinBanner', {
<Root>
<Title>{props.title}</Title>
<Button onClick={() => window.location.href = '/'}>New Game</Button>
<Button onClick={playAgain}>Play Again</Button>
</Root>
)
},

View File

@ -3,7 +3,7 @@ export { default } from 'pyre/server'
import type { Game, WinLine, xo } from './types'
const game: Game = {
const newGame: Game = {
phase: 'play',
turn: 'x',
players: {},
@ -15,7 +15,7 @@ const game: Game = {
]
}
const action = setup(game, {
const action = setup(newGame, {
onJoin(game, session) {
if (game.players[session]) return
@ -31,7 +31,20 @@ const action = setup(game, {
}
})
action('name:change', (ctx, name: string) => {
action('playAgain', (ctx) => {
const game = ctx.game as Game
game.phase = 'play'
game.turn = 'x'
delete game.winLine
game.board = [
['', '', ''],
['', '', ''],
['', '', ''],
]
return game
})
action('nameChange', (ctx, name: string) => {
const game = ctx.game as Game
const player = game.players[ctx.session]
@ -52,6 +65,9 @@ action('move', (ctx, x: number, y: number) => {
if (game.board[x] === undefined || game.board[x][y] === undefined)
throw new Error(`x,y not in board: ${x},${y}`)
if (game.board[x][y] !== '')
throw new Error(`someone already played at ${x},${y}`)
if (game.turn !== player)
throw new Error('not your turn!')