bugfix: reset stores when user logs out

bugfix: ignore pending request that do not elong to current game
This commit is contained in:
Settel 2022-10-05 08:44:18 +02:00
parent 258067cf84
commit 582922a21a
7 changed files with 57 additions and 7 deletions

View File

@ -21,9 +21,9 @@ type EngineResponse = {
const { $t } = useI18n({ const { $t } = useI18n({
'connection-broken': { en: 'connection to server broken.', de: 'Verbindung zum Server ist unterbrochen.' }, 'connection-broken': { en: 'connection to server broken.', de: 'Verbindung zum Server ist unterbrochen.' },
'retrying': { en: 'retrying', de: 'verbinde erneut'}, 'retrying': { en: 'retrying', de: 'verbinde erneut' },
}) })
export async function fetchUpdate(this: EngineContext) { export async function fetchUpdate(this: EngineContext) {
if (this.shouldStop || !this.isActive) { if (this.shouldStop || !this.isActive) {
useAlert().clearAlert() useAlert().clearAlert()
@ -44,10 +44,16 @@ export async function fetchUpdate(this: EngineContext) {
throw Error('unexpected response from /api/sync') throw Error('unexpected response from /api/sync')
} }
if (response.game.id != userInfoStore.gameId) {
// happens when user changes game and an old request is still pending
console.warn('response gameId does not match current gameId')
return
}
this.version = parseInt(response.version) this.version = parseInt(response.version)
this.isConnected.value = true this.isConnected.value = true
this.retry.value = 0 this.retry.value = 0
useAlert().clearAlert() useAlert().clearAlert()
useEngineStore().setJson(response) useEngineStore().setJson(response)
useGameinfoStore().setGameinfo(response.game) useGameinfoStore().setGameinfo(response.game)
@ -66,7 +72,7 @@ export async function fetchUpdate(this: EngineContext) {
this.isConnected.value = false this.isConnected.value = false
this.retry.value++ this.retry.value++
useAlert().setAlert([ useAlert().setAlert([
$t('connection-broken'), $t('connection-broken'),
this.retry.value > 0 ? $t('retrying') + '...'.slice(0, this.retry.value % 4) : '' this.retry.value > 0 ? $t('retrying') + '...'.slice(0, this.retry.value % 4) : ''
]) ])

View File

@ -1,5 +1,9 @@
import { useUserinfoStore, Userinfo } from "@/stores/UserinfoStore" import { useUserinfoStore, Userinfo } from '@/stores/UserinfoStore'
import useI18n from "./useI18n" import { useEngineStore } from '@/stores/EngineStore'
import { useGameinfoStore } from '@/stores/GameinfoStore'
import { usePlayersStore } from '@/stores/PlayersStore'
import { useRoundStore } from '@/stores/RoundStore'
import useI18n from './useI18n'
import { $fetch } from 'ohmyfetch' import { $fetch } from 'ohmyfetch'
export type AllowRole = '' | 'player' | 'gamemaster' | 'admin' export type AllowRole = '' | 'player' | 'gamemaster' | 'admin'
@ -49,6 +53,11 @@ export default (): useAuth => {
logout: async (): Promise<void> => { logout: async (): Promise<void> => {
await $fetch('/api/logout') await $fetch('/api/logout')
useEngineStore().reset()
useGameinfoStore().reset()
usePlayersStore().reset()
useUserinfoStore().reset()
useRoundStore().reset()
}, },
} }
} }

View File

@ -10,5 +10,8 @@ export const useEngineStore = defineStore('EngineStore', {
setJson(json: any): void { setJson(json: any): void {
this.json = json this.json = json
}, },
reset(): void {
this.json = {}
},
}, },
}) })

View File

@ -33,5 +33,13 @@ export const useGameinfoStore = defineStore('GameinfoStore', {
setGameinfo(gameInfo: Gameinfo): void { setGameinfo(gameInfo: Gameinfo): void {
this.gameInfo = gameInfo this.gameInfo = gameInfo
}, },
reset(): void {
this.gameInfo = {
id: '',
name: '',
state: '',
phase: '',
}
},
}, },
}) })

View File

@ -12,5 +12,8 @@ export const usePlayersStore = defineStore('PlayersStore', {
players = players || [] players = players || []
this.players.splice(0, this.players.length, ...players) this.players.splice(0, this.players.length, ...players)
}, },
reset(): void {
this.players.splice(0, 0)
},
}, },
}) })

View File

@ -24,5 +24,16 @@ export const useRoundStore = defineStore('RoundStore', {
this.round.revelation.votes = round.revelation?.votes || {} as RevelationVotes this.round.revelation.votes = round.revelation?.votes || {} as RevelationVotes
this.round.revelation.sources = round.revelation?.sources || {} as RevelationSources this.round.revelation.sources = round.revelation?.sources || {} as RevelationSources
}, },
reset(): void {
this.round = {
quote: '',
sources: [],
selections: {},
revelation: {
votes: {},
sources: {},
},
}
},
}, },
}) })

View File

@ -34,5 +34,15 @@ export const useUserinfoStore = defineStore('UserinfoStore', {
setUserInfo(userInfo: Userinfo): void { setUserInfo(userInfo: Userinfo): void {
this.userInfo = userInfo this.userInfo = userInfo
}, },
reset(): void {
this.userInfo = {
id: '',
name: '',
role: '',
game: '',
lang: 'en',
isCameo: '',
}
},
}, },
}) })