How to make moves on behalf of a non-current player?
nsaritzky opened this issue · 1 comments
nsaritzky commented
I'm trying to set up simultaneous moves. My code looks like this:
Game.ts
...
export const playCard: Move<GameState> = (
G,
ctx,
cardIndex: number,
playerID: PlayerID
) => {
const curr = G.players[playerID]
curr.playedCard = curr.hand.splice(cardIndex, 1)[0]
if (Object.values(G.players).every((p) => p.playedCard)) {
determineResolutionOrder(G)
}
}
const determineResolutionOrder = (G: GameState) => {
Object.values(G.players)
.sort((playerA, playerB) => playerA.playedCard!.val - playerB.playedCard!.val)
.forEach((player, index) => (player.resolveOrder = index))
export const Game: Game<GameState> = {
setup,
moves: {
draw: drawCards,
},
phases: {
play: {
start: true,
turn: {
activePlayers: { all: "playStage", minMoves: 1, maxMoves: 1 },
stages: {
playStage: {
moves: { playCard },
},
},
},
moves: { playCard },
next: "resolve",
},
resolve: {
turn: {
order: TurnOrder.CUSTOM_FROM("resolveOrder"),
},
},
moves: { resolveCardMove },
},
},
}App.ts
...
const App = () => (
<div>
<h1>Player 0</h1>
<GameClient playerID="0" />
<h1>Player 1</h1>
<GameClient playerID="1" />
</div>
)Board.ts
...
const GameBoard = ({ G, ctx, playerID, moves }: GameProps) => (
<h1>
{G.players[playerID!].hand.map(({ val }, i) => (
<input
type="button"
value={val}
onClick={() => moves.playCard(i, playerID)}
key={i}
/>
))}
</h1>
)The goal is to allow for the players to choose their cards simultaneously. In the first turn, the players all properly go into the playStage. Either can make a move. But if both try to, then I get a "playOrder is undefined" error. If I reference ctx.currentPlayer instead of passing playerID to playCard, then only the first player is referenced regardless of which client I send it through. I've looked at #774 and the code from the example in the "Clients" page in the docs but remain stumped.
nsaritzky commented
Of course, this wasn't an issue with stages or clients, but with my incorrect use of TurnOrder.CUSTOM_FROM.