From 2975b2820b420c458fd87bd91289dae4fab61c2b Mon Sep 17 00:00:00 2001 From: Moritz Ruth Date: Sun, 23 Apr 2023 15:14:44 +0200 Subject: [PATCH] commit #6 --- src/clientGame.ts | 50 ++++++++++++++++++++++---------------- src/components/Game.vue | 7 +++--- src/server/game.ts | 17 ++++++++----- src/server/trpc/game.ts | 12 +++++++-- src/server/trpc/index.ts | 3 +++ src/shared/game/actions.ts | 6 ++--- src/shared/game/cards.ts | 37 +++++++++++++++++++--------- src/shared/game/events.ts | 2 ++ src/shared/game/state.ts | 37 ++++++---------------------- src/specialCards.ts | 7 ++++++ 10 files changed, 102 insertions(+), 76 deletions(-) create mode 100644 src/specialCards.ts 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 @@