From 4a56f3ed627eddb8ff9ac9f6b159c321693d8b16 Mon Sep 17 00:00:00 2001 From: Legonzaur Date: Thu, 13 Jun 2024 10:00:29 -0400 Subject: [PATCH] Wip : Lobby --- app.vue | 3 +++ netcode/index.ts | 16 ++++++++++------ pages/game/[id].vue | 10 ++++++++-- stores/authStore.ts | 10 +++++++--- 4 files changed, 28 insertions(+), 11 deletions(-) diff --git a/app.vue b/app.vue index 835eee5..2157bea 100644 --- a/app.vue +++ b/app.vue @@ -3,9 +3,12 @@ const router = useRouter(); const authStore = useAuthStore() + await authStore.whoami() + + useHead ({ title diff --git a/netcode/index.ts b/netcode/index.ts index 7ca53a8..009e7d6 100644 --- a/netcode/index.ts +++ b/netcode/index.ts @@ -25,16 +25,19 @@ export async function getLobby() { export async function whoami() { const config = useRuntimeConfig(); - const req = useFetch(new URL("/whoami", config.public.endpoint).href, { - credentials: "include", - }); + const req = await useFetch( + new URL("/whoami", config.public.endpoint).href, + { + credentials: "include", + } + ); if ( req.error.value?.statusCode && [401].includes(req.error.value.statusCode) ) { - await navigateTo(config.public.discordLoginEndpoint, { - external: true, - }); + // await navigateTo(config.public.discordLoginEndpoint, { + // external: true, + // }); throw new Error("You are not logged in"); } if (req.error.value) { @@ -42,6 +45,7 @@ export async function whoami() { "Something went wrong while trying to get user information" ); } + return req.data; } diff --git a/pages/game/[id].vue b/pages/game/[id].vue index 4cbe6fb..61f544d 100644 --- a/pages/game/[id].vue +++ b/pages/game/[id].vue @@ -9,12 +9,17 @@ const config = useRuntimeConfig() const route = useRoute() const gameId = route.params.id -const { data: game, pending, error, refresh } = await useFetch( +const { data: game, pending, error, refresh } = (await useFetch( new URL(`/games/${gameId}`, config.public.endpoint).href, { credentials: "include", } -); +)) +// as { data: Ref, pending: Ref, error: Ref, refresh: (opts?: unknown | undefined) => Promise } + +if (game.value == null) { + throw new Error() +} const eventSource = subscribe(`/games/${gameId}/subscribe`) @@ -24,6 +29,7 @@ eventSource.addEventListener("message", (message) => { const authStore = useAuthStore() +const isSpectator = computed(() => authStore.logged && game.value.teams.every(t => t.players.every(p => p.user.userId !== authStore.selfUser.userId))) // const current_players = computed>(() => Array.from(goStore.current_game?.players ?? []).filter(p => p.team === goStore.current_game?.current_turn)) diff --git a/stores/authStore.ts b/stores/authStore.ts index 382d8ab..2ce24fb 100644 --- a/stores/authStore.ts +++ b/stores/authStore.ts @@ -8,9 +8,13 @@ export const useAuthStore = defineStore("auth", { }), actions: { async whoami() { - const data = await whoami(); - Object.assign(this.selfUser, data.value); - this.logged = true; + try { + const data = await whoami().then((data) => { + this.logged = true; + return data; + }); + Object.assign(this.selfUser, data.value); + } catch (e) {} }, oauth2_register_discord: () => { throw Error("not implemented");