Skip to content

Commit

Permalink
Add input from spectator
Browse files Browse the repository at this point in the history
  • Loading branch information
magnetenstad committed Jan 30, 2024
1 parent 8f907f1 commit 66a5402
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 7 deletions.
43 changes: 38 additions & 5 deletions src/stores/online.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
import { supabase } from '@/supabase'
import { Game } from '@/types/game'
import { Game, Segment } from '@/types/game'
import { RealtimeChannel } from '@supabase/supabase-js'
import { acceptHMRUpdate, defineStore } from 'pinia'
import { useAuthStore } from './auth'
import { useGameStore } from './game'

type OnlinePresence = {
userId: string
Expand Down Expand Up @@ -41,7 +42,27 @@ export const useOnlineStore = defineStore('online', {
this.sendUpdate({ userId })
})

this.outChannel = supabase.channel(`game-${userId}`).subscribe()
this.outChannel = supabase
.channel(`game-${userId}`)
.on('broadcast', { event: 'input' }, (args) => {
const gameStore = useGameStore()
const gameController = gameStore.getController()
const player = gameStore.gameState?.player
if (args.payload.userId == player) {
switch (args.payload.type) {
case 'hit':
gameController.recordHit(args.payload.segment)
break
case 'miss':
gameController.recordMiss()
break
case 'undo':
gameStore.undoScore()
break
}
}
})
.subscribe()
},

async sendUpdate(presence: Partial<OnlinePresence>) {
Expand All @@ -60,10 +81,10 @@ export const useOnlineStore = defineStore('online', {
await this.inChannel?.unsubscribe()
this.inChannel = supabase
.channel(`game-${this.presence.spectating}`)
.on('broadcast', { event: 'game' }, (game) => {
// @ts-ignore
this.spectatingGame = game.payload as Game
.on('broadcast', { event: 'game' }, (args) => {
this.spectatingGame = args.payload as Game
})

.subscribe()
},

Expand All @@ -81,6 +102,18 @@ export const useOnlineStore = defineStore('online', {
payload: game,
})
},

sendInput(type: 'hit' | 'miss' | 'undo', segment?: Segment) {
this.inChannel?.send({
type: 'broadcast',
event: 'input',
payload: {
userId: useAuthStore().auth?.id,
type,
segment,
},
})
},
},

getters: {
Expand Down
4 changes: 3 additions & 1 deletion src/views/GameView.vue
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,9 @@

<MainGame
v-if="gameStore.game && gameStore.gameState"
:show-input="true"
:show-input="
!spectators.find((p) => p.userId == gameStore.gameState?.player)
"
:show-save="true"
:game="gameStore.game"
:game-state="gameStore.gameState"
Expand Down
8 changes: 7 additions & 1 deletion src/views/SpectateView.vue
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,16 @@

<template v-if="onlineStore.spectatingGame && gameController && gameState">
<MainGame
:show-input="false"
:show-input="
!!gameState.player && gameState.player == useAuthStore().auth?.id
"
:show-save="false"
:game="onlineStore.spectatingGame"
:game-state="gameState"
:game-controller="gameController"
@hit="onlineStore.sendInput('hit', $event)"
@miss="onlineStore.sendInput('miss')"
@undo="onlineStore.sendInput('undo')"
></MainGame>
</template>
<p v-else-if="onlineStore.getSpectating?.inGame">
Expand All @@ -24,6 +29,7 @@
import MainGame from '@/components/MainGame.vue'
import { getGameController } from '@/games/games'
import { router } from '@/router'
import { useAuthStore } from '@/stores/auth'
import { useOnlineStore } from '@/stores/online'
import { useUsersStore } from '@/stores/users'
import { computed, onUnmounted, watch } from 'vue'
Expand Down

0 comments on commit 66a5402

Please sign in to comment.