Skip to content

Commit

Permalink
support more than 2 players properly
Browse files Browse the repository at this point in the history
  • Loading branch information
troypoulter committed Apr 11, 2024
1 parent 959bca8 commit 577310b
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 18 deletions.
33 changes: 16 additions & 17 deletions party/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,23 +24,13 @@ export default class PigGameServer implements Party.Server {
currentScore: 0,
};

gameState.playerOrder.push(connection.id);

if (Object.keys(gameState.players).length === 1) {
gameState.currentPlayerId = connection.id;
console.log(`First player connected: ${connection.id}`);
}

// await this.room.storage.put("gameState", gameState); // Persist game state changes

// this.room.broadcast(
// JSON.stringify({ message: "Player joined the game", gameState })
// );

// if (Object.keys(gameState.players).length === gameState.maxPlayers) {
// console.log(`Second player connected: ${connection.id}`);
// console.log(`Game has started with ${gameState.maxPlayers} players.`);
// gameState.hasGameStarted = true;
// }

await this.room.storage.put("gameState", gameState); // Persist game state changes

this.room.broadcast(
Expand Down Expand Up @@ -110,7 +100,11 @@ export default class PigGameServer implements Party.Server {
targetAmount: newGame.data.targetScore,
maxPlayers: newGame.data.numberOfPlayers,
players: {},
playerOrder: [],
currentPlayerIndex: 0,
currentPlayerId: "",
lastRoll: undefined,
winnerId: undefined,
}; // Initialize the game state
console.log("New game room created");
await this.room.storage.put("gameState", gameState); // Persist the new game state
Expand Down Expand Up @@ -212,11 +206,16 @@ export default class PigGameServer implements Party.Server {
}

private async switchPlayer(gameState: GameState) {
const playerIds = Object.keys(gameState.players);
gameState.currentPlayerId = playerIds.find(
(id) => id !== gameState.currentPlayerId
)!;
await this.room.storage.put("gameState", gameState); // Ensure to persist any changes made during the switch
// Increment the current player index.
gameState.currentPlayerIndex =
(gameState.currentPlayerIndex + 1) % gameState.playerOrder.length;

// Update the current player ID using the playerOrder array.
gameState.currentPlayerId =
gameState.playerOrder[gameState.currentPlayerIndex];

// Persist the updated game state with the new current player.
await this.room.storage.put("gameState", gameState);
}
}

Expand Down
2 changes: 1 addition & 1 deletion src/app/_components/footer.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ export function Footer() {
<div className="container flex flex-col items-center justify-between gap-4 md:h-24 md:flex-row">
<div className="flex flex-col items-center gap-4 px-8 md:flex-row md:gap-2 md:px-0">
<p className="text-center text-sm leading-loose text-muted-foreground md:text-left">
© 2024-{new Date().getFullYear()} Troy Poulter. Built by{" "}
© {new Date().getFullYear()} Troy Poulter. Built by{" "}
<a
href="https://www.troypoulter.com/"
target="_blank"
Expand Down
2 changes: 2 additions & 0 deletions src/lib/types/GameState.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ export interface GameState {
hasGameStarted: boolean;
maxPlayers: number;
players: Record<PlayerId, PlayerState>;
playerOrder: PlayerId[];
currentPlayerIndex: number;
currentPlayerId: PlayerId;
lastRoll?: number;
winnerId?: PlayerId;
Expand Down

0 comments on commit 577310b

Please sign in to comment.