Browse Source

* Changing username now changes it in game too

master
vvandenb 2 years ago
parent
commit
508829c3ee
  1. 11
      front/volume/src/App.svelte
  2. 12
      front/volume/src/components/Pong/Pong.svelte
  3. 22
      front/volume/src/components/Profile.svelte

11
front/volume/src/App.svelte

@ -89,12 +89,12 @@
setAppState(APPSTATE.PROFILE); setAppState(APPSTATE.PROFILE);
} }
let profileUsername: string = ""; let profileUsername: string = $store?.username ?? "";
async function openIdProfile(event: CustomEvent<string>) { async function openIdProfile(event: CustomEvent<string>) {
profileUsername = event.detail; profileUsername = event.detail;
setAppState(APPSTATE.PROFILE_ID); setAppState(APPSTATE.PROFILE_ID);
} }
$: console.log(profileUsername) $: console.log("New profileUsername:", profileUsername)
async function getDMs(username: string): Promise<Response | null> { async function getDMs(username: string): Promise<Response | null> {
const res = await fetch(API_URL + "/channels/dms/" + username, { const res = await fetch(API_URL + "/channels/dms/" + username, {
@ -177,6 +177,7 @@
// GAME // GAME
let pong: Pong; let pong: Pong;
let gamePlaying: boolean = false; let gamePlaying: boolean = false;
let resetGameConnection: () => void;
// FAKE LOGIN // FAKE LOGIN
let usernameFake = "test"; let usernameFake = "test";
@ -268,7 +269,7 @@
{/if} {/if}
{#if appState === APPSTATE.PROFILE} {#if appState === APPSTATE.PROFILE}
<div on:click={resetAppState} on:keydown={resetAppState}> <div on:click={resetAppState} on:keydown={resetAppState}>
<Profile {gamePlaying} on:view-history={() => setAppState(APPSTATE.HISTORY_ID)} /> <Profile {resetGameConnection} {gamePlaying} bind:username={profileUsername} on:view-history={() => setAppState(APPSTATE.HISTORY_ID)} />
</div> </div>
{/if} {/if}
{#if appState === APPSTATE.PROFILE_ID} {#if appState === APPSTATE.PROFILE_ID}
@ -280,7 +281,7 @@
> >
<Profile <Profile
{gamePlaying} {gamePlaying}
username={profileUsername} bind:username={profileUsername}
on:view-history={() => setAppState(APPSTATE.HISTORY_ID)} on:view-history={() => setAppState(APPSTATE.HISTORY_ID)}
/> />
</div> </div>
@ -291,7 +292,7 @@
<button on:click={impersonate}>Impersonate</button> <button on:click={impersonate}>Impersonate</button>
<button on:click={() => (fakemenu = false)}>No impersonate</button> <button on:click={() => (fakemenu = false)}>No impersonate</button>
{:else} {:else}
<Pong bind:gamePlaying={gamePlaying} bind:this={pong} {appState} {setAppState} {fakeUser} /> <Pong bind:gamePlaying={gamePlaying} bind:this={pong} {appState} {setAppState} {fakeUser} bind:resetGameConnection={resetGameConnection} />
{/if} {/if}
{/if} {/if}

12
front/volume/src/components/Pong/Pong.svelte

@ -18,6 +18,13 @@
invitedUsername = event.detail; invitedUsername = event.detail;
} }
export function resetGameConnection() {
connected = false;
loggedIn = false;
failedLogIn = false;
setupSocket(renderCanvas, canvas, context);
}
export let fakeUser: boolean; export let fakeUser: boolean;
export let appState: string; export let appState: string;
export let setAppState: (newState: APPSTATE | string) => void; export let setAppState: (newState: APPSTATE | string) => void;
@ -56,6 +63,8 @@
backgroundColor backgroundColor
); );
socket.on("connect", onSocketOpen);
socket.on("disconnect", onSocketClose);
socket.on(GAME_EVENTS.START_GAME, () => { socket.on(GAME_EVENTS.START_GAME, () => {
game.start(socket); game.start(socket);
}); });
@ -100,9 +109,6 @@
socket.on(GAME_EVENTS.READY, (succeeded: boolean) => { socket.on(GAME_EVENTS.READY, (succeeded: boolean) => {
game.youAreReady = succeeded; game.youAreReady = succeeded;
}); });
socket.on("connect", onSocketOpen);
socket.on("disconnect", onSocketClose);
} }
async function onSocketOpen() { async function onSocketOpen() {

22
front/volume/src/components/Profile.svelte

@ -20,9 +20,11 @@
export let username: string = $store.username; export let username: string = $store.username;
export let gamePlaying: boolean; export let gamePlaying: boolean;
export let resetGameConnection: () => void = () => {};
let edit: boolean = true; let edit: boolean = true;
let user: Player = $store; let user: Player = $store;
let newUsername: string = $store.username;
let avatarForm: HTMLFormElement; let avatarForm: HTMLFormElement;
@ -43,19 +45,25 @@
const dispatch = createEventDispatcher(); const dispatch = createEventDispatcher();
async function handleSubmit() { async function handleSubmit() {
if (gamePlaying) { 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; return;
} }
let response = await fetch(API_URL + "/users", { let response = await fetch(API_URL + "/users", {
headers: { "content-type": "application/json" }, headers: { "content-type": "application/json" },
method: "POST", method: "POST",
body: JSON.stringify({ username: user.username }), body: JSON.stringify({ username: newUsername }),
credentials: "include", credentials: "include",
}); });
if (response.ok) { if (response.ok) {
$store.username = user.username; $store.username = newUsername;
popup.set(bind(Alert, { message: "Succefully changed username.", form : false })) username = newUsername;
popup.set(bind(Alert, { message: "Succefully changed username.", form: false }))
resetGameConnection();
} }
} }
@ -79,7 +87,7 @@
<div class="overlay"> <div class="overlay">
<div class="profile" on:click|stopPropagation on:keydown|stopPropagation> <div class="profile" on:click|stopPropagation on:keydown|stopPropagation>
<h3>===| <mark>{user.username}'s Profile</mark> |===</h3> <h3>===| <mark>{username}'s Profile</mark> |===</h3>
<div class="profile-header"> <div class="profile-header">
{#if !edit} {#if !edit}
<img src={`${API_URL}/users/${user.ftId}/avatar`} alt="avatar" class="profile-img" /> <img src={`${API_URL}/users/${user.ftId}/avatar`} alt="avatar" class="profile-img" />
@ -109,7 +117,7 @@
</div> </div>
<div class="profile-body"> <div class="profile-body">
<p> <p>
<button on:click={() => dispatch("view-history", user.username)} <button on:click={() => dispatch("view-history")}
>View History</button >View History</button
> >
</p> </p>
@ -124,7 +132,7 @@
class="username" class="username"
on:submit|preventDefault={handleSubmit} on:submit|preventDefault={handleSubmit}
> >
<input type="text" id="username" bind:value={user.username} required/> <input type="text" id="username" bind:value={newUsername} required/>
<button type="submit" class="username" form="username-form" <button type="submit" class="username" form="username-form"
>Change</button >Change</button
> >

Loading…
Cancel
Save