Skip to content

Commit

Permalink
feat: round-robin, multiple rounds
Browse files Browse the repository at this point in the history
  • Loading branch information
plushdohn committed Dec 20, 2023
1 parent 3e15153 commit c379d64
Show file tree
Hide file tree
Showing 9 changed files with 37 additions and 15 deletions.
1 change: 1 addition & 0 deletions components/Game.vue
Original file line number Diff line number Diff line change
Expand Up @@ -35,4 +35,5 @@
:hasGuesserWon="game.hasGuesserWon"
:guesserId="game.guesserId"
/>
<GameOver v-else-if="game.phase === 'GAME_OVER'" :players="game.players" />
</template>
2 changes: 1 addition & 1 deletion components/game/GameOver.vue
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
</script>

<template>
<div class="flex flex-col gap-6">
<div class="flex flex-col gap-6 w-full">
<DescribedHeader :title="$t('game.gameOverScreen.header.title')">
{{
$t("game.gameOverScreen.header.description", {
Expand Down
4 changes: 2 additions & 2 deletions i18n.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ export default defineI18nConfig(() => ({
header: {
title: "Room Lobby",
description:
"Have your friends join the room by entering the code {roomCode}.",
"Have your friends join the room by entering the code {roomCode} or send them the link to this page.",
},
statusText: {
waitingForMorePlayers: "Waiting for more players...",
Expand Down Expand Up @@ -176,7 +176,7 @@ export default defineI18nConfig(() => ({
header: {
title: "Lobby",
description:
"Fai entrare i tuoi amici alla partita inserendo il codice {roomCode}.",
"Fai entrare i tuoi amici alla partita inserendo il codice {roomCode} o inviando loro il link a questa pagina.",
},
statusText: {
waitingForMorePlayers: "In attesa di altri giocatori...",
Expand Down
2 changes: 1 addition & 1 deletion pages/game/[id].vue
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
});
const { locale, t } = useI18n({
useScope: "local",
useScope: "global",
});
const route = useRoute();
Expand Down
14 changes: 8 additions & 6 deletions pages/index.vue
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,6 @@
const localePath = useLocalePath();
const STATIC_ROOM_CODE = useState(() => getRandomString(5));
const { siteName } = useAppConfig();
const state = ref({
Expand All @@ -22,12 +20,16 @@
</p>
</header>
<div class="flex flex-col gap-4 w-full">
<UButton :to="localePath(`/game/${STATIC_ROOM_CODE}`)" block size="lg">
{{ $t("home.createRoomLink") }}
</UButton>
<ClientOnly>
<UButton :to="localePath(`/game/${getRandomString(5)}`)" block size="lg">
{{ $t("home.createRoomLink") }}
</UButton>
</ClientOnly>
<UDivider />
<form
:action="state.code ? localePath(`/game/${state.code}`) : undefined"
:action="
state.code ? localePath(`/game/${state.code.toUpperCase()}`) : undefined
"
class="flex flex-col gap-2"
>
<UInput
Expand Down
21 changes: 17 additions & 4 deletions party/game.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,13 +31,15 @@ type GameState = {
}
>;
currentPhaseEndsAt?: number;
maxRounds: number;
currentRound: number;
roomLocale: string;
};

type Notifier = (playerId: string, event: GameEvent) => void;

export class Game {
static TIME_TO_CHOOSE_TOPIC = 30000;
static TIME_TO_CHOOSE_TOPIC = 60000;
static TIME_TO_SHOW_SCOREBOARD = 10000;

state: GameState = {
Expand All @@ -51,6 +53,8 @@ export class Game {
},
topics: {},
roomLocale: "en",
maxRounds: 2,
currentRound: 0,
};

notifier?: Notifier;
Expand Down Expand Up @@ -162,7 +166,9 @@ export class Game {
}
}

async startGame() {
async startGame(options: { maxRounds: number }) {
this.state.maxRounds = options.maxRounds;

await this.startNewRound();
}

Expand All @@ -173,6 +179,10 @@ export class Game {
this.state.truthtellerId = this.getNextTruthTellerId();
this.state.currentPhaseEndsAt = Date.now() + Game.TIME_TO_CHOOSE_TOPIC;

if (this.state.guesserId === this.state.players.allIds[0]) {
this.state.currentRound += 1;
}

for (const playerId of this.state.players.allIds) {
this.notifier?.(playerId, {
type: "STATE_UPDATE",
Expand Down Expand Up @@ -244,7 +254,8 @@ export class Game {
setTimeout(() => {
if (
this.state.players.allIds.indexOf(this.state.guesserId!) ===
this.state.players.allIds.length - 1
this.state.players.allIds.length - 1 &&
this.state.currentRound === this.state.maxRounds
) {
this.endGame();
return;
Expand Down Expand Up @@ -353,7 +364,9 @@ export class Game {
this.state.guesserId
);

return this.state.players.allIds[guesserIndex + 1];
return this.state.players.allIds[
(guesserIndex + 1) % this.state.players.allIds.length
];
}

private getNextTruthTellerId() {
Expand Down
2 changes: 1 addition & 1 deletion party/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ export default class Server implements Party.Server {
return Server.createResponse({ error: "Unauthorized" }, 401);
}

await this.game.startGame();
await this.game.startGame(body.options);

return Server.createResponse({ success: true });
}
Expand Down
3 changes: 3 additions & 0 deletions party/types.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,9 @@ export type GameEvent =
export type PartyClientRequest =
| {
type: "START_GAME";
options: {
maxRounds: number;
};
}
| {
type: "CHOOSE_TRUTHTELLER";
Expand Down
3 changes: 3 additions & 0 deletions stores/game.ts
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,9 @@ export const useGameStore = defineStore("game", {
async startGame() {
await this.socketFetch({
type: "START_GAME",
options: {
maxRounds: 2,
},
});
},
async chooseTruthteller(playerId: string) {
Expand Down

0 comments on commit c379d64

Please sign in to comment.