From 582922a21a1aaa7b606453199a4c6a6e10bcf08f Mon Sep 17 00:00:00 2001 From: Settel Date: Wed, 5 Oct 2022 08:44:18 +0200 Subject: [PATCH] bugfix: reset stores when user logs out bugfix: ignore pending request that do not elong to current game --- client/src/composables/engine/fetchUpdate.ts | 16 +++++++++++----- client/src/composables/useAuth.ts | 13 +++++++++++-- client/src/stores/EngineStore.ts | 3 +++ client/src/stores/GameinfoStore.ts | 8 ++++++++ client/src/stores/PlayersStore.ts | 3 +++ client/src/stores/RoundStore.ts | 11 +++++++++++ client/src/stores/UserinfoStore.ts | 10 ++++++++++ 7 files changed, 57 insertions(+), 7 deletions(-) diff --git a/client/src/composables/engine/fetchUpdate.ts b/client/src/composables/engine/fetchUpdate.ts index 35977cb..f72817b 100644 --- a/client/src/composables/engine/fetchUpdate.ts +++ b/client/src/composables/engine/fetchUpdate.ts @@ -21,9 +21,9 @@ type EngineResponse = { const { $t } = useI18n({ '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) { if (this.shouldStop || !this.isActive) { useAlert().clearAlert() @@ -44,10 +44,16 @@ export async function fetchUpdate(this: EngineContext) { 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.isConnected.value = true this.retry.value = 0 - + useAlert().clearAlert() useEngineStore().setJson(response) useGameinfoStore().setGameinfo(response.game) @@ -66,7 +72,7 @@ export async function fetchUpdate(this: EngineContext) { this.isConnected.value = false this.retry.value++ useAlert().setAlert([ - $t('connection-broken'), + $t('connection-broken'), this.retry.value > 0 ? $t('retrying') + '...'.slice(0, this.retry.value % 4) : '' ]) diff --git a/client/src/composables/useAuth.ts b/client/src/composables/useAuth.ts index 2b8be60..43df1a8 100644 --- a/client/src/composables/useAuth.ts +++ b/client/src/composables/useAuth.ts @@ -1,5 +1,9 @@ -import { useUserinfoStore, Userinfo } from "@/stores/UserinfoStore" -import useI18n from "./useI18n" +import { useUserinfoStore, Userinfo } from '@/stores/UserinfoStore' +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' export type AllowRole = '' | 'player' | 'gamemaster' | 'admin' @@ -49,6 +53,11 @@ export default (): useAuth => { logout: async (): Promise => { await $fetch('/api/logout') + useEngineStore().reset() + useGameinfoStore().reset() + usePlayersStore().reset() + useUserinfoStore().reset() + useRoundStore().reset() }, } } diff --git a/client/src/stores/EngineStore.ts b/client/src/stores/EngineStore.ts index 21915f8..0cfe09c 100644 --- a/client/src/stores/EngineStore.ts +++ b/client/src/stores/EngineStore.ts @@ -10,5 +10,8 @@ export const useEngineStore = defineStore('EngineStore', { setJson(json: any): void { this.json = json }, + reset(): void { + this.json = {} + }, }, }) diff --git a/client/src/stores/GameinfoStore.ts b/client/src/stores/GameinfoStore.ts index 2d1b324..ff105a0 100644 --- a/client/src/stores/GameinfoStore.ts +++ b/client/src/stores/GameinfoStore.ts @@ -33,5 +33,13 @@ export const useGameinfoStore = defineStore('GameinfoStore', { setGameinfo(gameInfo: Gameinfo): void { this.gameInfo = gameInfo }, + reset(): void { + this.gameInfo = { + id: '', + name: '', + state: '', + phase: '', + } + }, }, }) diff --git a/client/src/stores/PlayersStore.ts b/client/src/stores/PlayersStore.ts index c931d34..e25768f 100644 --- a/client/src/stores/PlayersStore.ts +++ b/client/src/stores/PlayersStore.ts @@ -12,5 +12,8 @@ export const usePlayersStore = defineStore('PlayersStore', { players = players || [] this.players.splice(0, this.players.length, ...players) }, + reset(): void { + this.players.splice(0, 0) + }, }, }) diff --git a/client/src/stores/RoundStore.ts b/client/src/stores/RoundStore.ts index 7052588..6cc9742 100644 --- a/client/src/stores/RoundStore.ts +++ b/client/src/stores/RoundStore.ts @@ -24,5 +24,16 @@ export const useRoundStore = defineStore('RoundStore', { this.round.revelation.votes = round.revelation?.votes || {} as RevelationVotes this.round.revelation.sources = round.revelation?.sources || {} as RevelationSources }, + reset(): void { + this.round = { + quote: '', + sources: [], + selections: {}, + revelation: { + votes: {}, + sources: {}, + }, + } + }, }, }) diff --git a/client/src/stores/UserinfoStore.ts b/client/src/stores/UserinfoStore.ts index 07ed6df..35e4430 100644 --- a/client/src/stores/UserinfoStore.ts +++ b/client/src/stores/UserinfoStore.ts @@ -34,5 +34,15 @@ export const useUserinfoStore = defineStore('UserinfoStore', { setUserInfo(userInfo: Userinfo): void { this.userInfo = userInfo }, + reset(): void { + this.userInfo = { + id: '', + name: '', + role: '', + game: '', + lang: 'en', + isCameo: '', + } + }, }, })