diff --git a/src/clientGame.ts b/src/clientGame.ts index 62a3c30..db02d09 100644 --- a/src/clientGame.ts +++ b/src/clientGame.ts @@ -32,33 +32,40 @@ export const useGame = defineStore("game", () => { console.log(`⏩ ${action.type}`, action) }) + function join(code: string) { + return new Promise((resolve, reject) => { + trpcClient.join.subscribe({ lobbyCode: code }, { + onStarted: () => { + lobbyCode.value = code + resolve() + }, + onData: event => { + switch (event.type) { + case "action": + actionsBus.emit(event.action) + break + + case "new_round": + state.value = getUninitializedGameState() + actions.splice(0, actions.length) + join(code).catch(() => {}) + } + }, + onError: error => { + console.error("🔴", error) + reject(error) + } + }) + }) + } + return { lobbyCode: readonly(lobbyCode), isActive: computed(() => lobbyCode.value !== null), isOwnGame: computed(() => state.value.players.findIndex(p => p.id === (auth.authenticatedUser?.id ?? "")) === 0), state: readonly(state), actions: readonly(actions), - join(code: string) { - return new Promise((resolve, reject) => { - trpcClient.join.subscribe({ lobbyCode: code }, { - onStarted: () => { - lobbyCode.value = code - resolve() - }, - onData: event => { - switch (event.type) { - case "action": - actionsBus.emit(event.action) - break - } - }, - onError: error => { - console.error("🔴", error) - reject(error) - } - }) - }) - }, + join, async reset() { window.location.hash = "" lobbyCode.value = null @@ -68,6 +75,7 @@ export const useGame = defineStore("game", () => { start: () => trpcClient.game.start.mutate({ lobbyCode: lobbyCode.value! }), hit: () => trpcClient.game.hit.mutate({ lobbyCode: lobbyCode.value! }), stay: () => trpcClient.game.stay.mutate({ lobbyCode: lobbyCode.value! }), + newRound: () => trpcClient.game.newRound.mutate({ lobbyCode: lobbyCode.value! }), create: () => trpcClient.createGame.mutate() } }) \ No newline at end of file diff --git a/src/components/Game.vue b/src/components/Game.vue index 4ff6ee5..45005d8 100644 --- a/src/components/Game.vue +++ b/src/components/Game.vue @@ -4,10 +4,11 @@