diff --git a/front/volume/src/App.svelte b/front/volume/src/App.svelte index db7a283..f22f624 100644 --- a/front/volume/src/App.svelte +++ b/front/volume/src/App.svelte @@ -89,12 +89,12 @@ setAppState(APPSTATE.PROFILE); } - let profileUsername: string = ""; + let profileUsername: string = $store?.username ?? ""; async function openIdProfile(event: CustomEvent) { profileUsername = event.detail; setAppState(APPSTATE.PROFILE_ID); } - $: console.log(profileUsername) + $: console.log("New profileUsername:", profileUsername) async function getDMs(username: string): Promise { const res = await fetch(API_URL + "/channels/dms/" + username, { @@ -177,6 +177,7 @@ // GAME let pong: Pong; let gamePlaying: boolean = false; + let resetGameConnection: () => void; // FAKE LOGIN let usernameFake = "test"; @@ -268,7 +269,7 @@ {/if} {#if appState === APPSTATE.PROFILE}
- setAppState(APPSTATE.HISTORY_ID)} /> + setAppState(APPSTATE.HISTORY_ID)} />
{/if} {#if appState === APPSTATE.PROFILE_ID} @@ -280,7 +281,7 @@ > setAppState(APPSTATE.HISTORY_ID)} /> @@ -291,7 +292,7 @@ {:else} - + {/if} {/if} diff --git a/front/volume/src/components/Pong/Pong.svelte b/front/volume/src/components/Pong/Pong.svelte index b7abeef..33a5f81 100644 --- a/front/volume/src/components/Pong/Pong.svelte +++ b/front/volume/src/components/Pong/Pong.svelte @@ -18,6 +18,13 @@ invitedUsername = event.detail; } + export function resetGameConnection() { + connected = false; + loggedIn = false; + failedLogIn = false; + setupSocket(renderCanvas, canvas, context); + } + export let fakeUser: boolean; export let appState: string; export let setAppState: (newState: APPSTATE | string) => void; @@ -56,6 +63,8 @@ backgroundColor ); + socket.on("connect", onSocketOpen); + socket.on("disconnect", onSocketClose); socket.on(GAME_EVENTS.START_GAME, () => { game.start(socket); }); @@ -100,9 +109,6 @@ socket.on(GAME_EVENTS.READY, (succeeded: boolean) => { game.youAreReady = succeeded; }); - - socket.on("connect", onSocketOpen); - socket.on("disconnect", onSocketClose); } async function onSocketOpen() { diff --git a/front/volume/src/components/Profile.svelte b/front/volume/src/components/Profile.svelte index b41c458..bc7da4f 100644 --- a/front/volume/src/components/Profile.svelte +++ b/front/volume/src/components/Profile.svelte @@ -20,9 +20,11 @@ export let username: string = $store.username; export let gamePlaying: boolean; + export let resetGameConnection: () => void = () => {}; let edit: boolean = true; let user: Player = $store; + let newUsername: string = $store.username; let avatarForm: HTMLFormElement; @@ -43,19 +45,25 @@ const dispatch = createEventDispatcher(); async function handleSubmit() { if (gamePlaying) { - popup.set(bind(Alert, { message:"Cannot change username while playing.", form : false })) + popup.set(bind(Alert, { message: "Cannot change username while playing.", form: false })) + return; + } + if ($store.username === newUsername) { + popup.set(bind(Alert, { message: `Username is already set to ${newUsername}.`, form: false })) return; } let response = await fetch(API_URL + "/users", { headers: { "content-type": "application/json" }, method: "POST", - body: JSON.stringify({ username: user.username }), + body: JSON.stringify({ username: newUsername }), credentials: "include", }); if (response.ok) { - $store.username = user.username; - popup.set(bind(Alert, { message: "Succefully changed username.", form : false })) + $store.username = newUsername; + username = newUsername; + popup.set(bind(Alert, { message: "Succefully changed username.", form: false })) + resetGameConnection(); } } @@ -79,7 +87,7 @@
-

===| {user.username}'s Profile |===

+

===| {username}'s Profile |===

{#if !edit} avatar @@ -109,7 +117,7 @@

-

@@ -124,7 +132,7 @@ class="username" on:submit|preventDefault={handleSubmit} > - +