Browse Source

* Pong: leaver is now loser

* Updated overlay CSS class to zindex 50: lower than alert popups
master
vvandenb 2 years ago
parent
commit
c54d1fc4ff
  1. 17
      back/volume/src/pong/game/Game.ts
  2. 6
      back/volume/src/pong/pong.gateway.ts
  3. 16
      back/volume/src/pong/pong.service.ts
  4. 6
      front/volume/src/App.svelte
  5. 51
      front/volume/src/components/Alert/Alert.svelte
  6. 2
      front/volume/src/components/Friends.svelte
  7. 2
      front/volume/src/components/Leaderboard.svelte
  8. 2
      front/volume/src/components/MatchHistory.svelte
  9. 2
      front/volume/src/components/Pong/GameCreation.svelte
  10. 2
      front/volume/src/components/Pong/Matchmaking.svelte
  11. 2
      front/volume/src/components/Pong/Pong.svelte
  12. 19
      front/volume/src/components/Profile.svelte

17
back/volume/src/pong/game/Game.ts

@ -117,13 +117,26 @@ export class Game {
} }
} }
stop (): void { stop (nameWhoLeft?: string): void {
if (this.timer !== null) { if (this.timer !== null) {
clearInterval(this.timer) clearInterval(this.timer)
} }
let nameWhoWon: string
if (this.players[0].score > this.players[1].score) {
nameWhoWon = this.players[0].name
} else {
nameWhoWon = this.players[1].name
}
if (nameWhoLeft !== undefined) {
this.players.forEach((p) => {
if (p.name !== nameWhoLeft) {
nameWhoWon = p.name
}
})
}
this.timer = null this.timer = null
this.pongService this.pongService
.saveResult(this.players, this.ranked, DEFAULT_WIN_SCORE) .saveResult(this.players, this.ranked, nameWhoWon)
.then(() => { .then(() => {
this.gameStoppedCallback(this.players[0].name) this.gameStoppedCallback(this.players[0].name)
this.players = [] this.players = []

6
back/volume/src/pong/pong.gateway.ts

@ -46,10 +46,10 @@ export class PongGateway implements OnGatewayConnection, OnGatewayDisconnect {
): void { ): void {
const name: string | undefined = this.socketToPlayerName.get(client) const name: string | undefined = this.socketToPlayerName.get(client)
const game: Game | undefined = this.games.playerGame(name) const game: Game | undefined = this.games.playerGame(name)
if (name !== undefined) {
if (game !== undefined) { if (game !== undefined) {
game.stop() game.stop(name)
} }
if (name !== undefined) {
console.log('Disconnected ', this.socketToPlayerName.get(client)) console.log('Disconnected ', this.socketToPlayerName.get(client))
this.matchmakingQueue.removePlayer(name) this.matchmakingQueue.removePlayer(name)
this.socketToPlayerName.delete(client) this.socketToPlayerName.delete(client)
@ -66,7 +66,7 @@ export class PongGateway implements OnGatewayConnection, OnGatewayDisconnect {
): Promise<{ event: string, data: boolean }> { ): Promise<{ event: string, data: boolean }> {
let succeeded: boolean = false let succeeded: boolean = false
const user = await this.usersService.findUserByName(playerName.value) const user = await this.usersService.findUserByName(playerName.value)
console.log(socketKey.value, user?.socketKey) // console.log(socketKey.value, user?.socketKey)
if ( if (
user !== null && user !== null &&
user.socketKey === socketKey.value && user.socketKey === socketKey.value &&

16
back/volume/src/pong/pong.service.ts

@ -17,12 +17,10 @@ export class PongService {
async updateStats ( async updateStats (
player: User, player: User,
i: number, nameWhoWon: string
result: Result,
maxScore: number
): Promise<void> { ): Promise<void> {
player.matchs++ player.matchs++
if (result.score[i] === maxScore) player.wins++ if (player.username === nameWhoWon) player.wins++
else player.looses++ else player.looses++
player.winrate = (100 * player.wins) / player.matchs player.winrate = (100 * player.wins) / player.matchs
} }
@ -30,11 +28,11 @@ export class PongService {
async updatePlayer ( async updatePlayer (
i: number, i: number,
result: Result, result: Result,
maxScore: number nameWhoWon: string
): Promise<void> { ): Promise<void> {
const player: User | null = result.players[i] const player: User | null = result.players[i]
if (player == null) return if (player == null) return
if (result.ranked) await this.updateStats(player, i, result, maxScore) if (result.ranked) await this.updateStats(player, nameWhoWon)
player.results.push(result) player.results.push(result)
player.status = 'online' player.status = 'online'
await this.usersService.save(player) await this.usersService.save(player)
@ -49,7 +47,7 @@ export class PongService {
async saveResult ( async saveResult (
players: Player[], players: Player[],
ranked: boolean, ranked: boolean,
maxScore: number nameWhoWon: string
): Promise<void> { ): Promise<void> {
const result = new Result() const result = new Result()
const ply = new Array<User | null>() const ply = new Array<User | null>()
@ -59,8 +57,8 @@ export class PongService {
result.players = ply result.players = ply
result.score = [players[0].score, players[1].score] result.score = [players[0].score, players[1].score]
await this.resultsRepository.save(result) await this.resultsRepository.save(result)
await this.updatePlayer(0, result, maxScore) await this.updatePlayer(0, result, nameWhoWon)
await this.updatePlayer(1, result, maxScore) await this.updatePlayer(1, result, nameWhoWon)
} }
async getHistory ( async getHistory (

6
front/volume/src/App.svelte

@ -172,6 +172,7 @@
// GAME // GAME
let pong: Pong; let pong: Pong;
let gamePlaying: boolean = false;
// FAKE LOGIN // FAKE LOGIN
let usernameFake = "test"; let usernameFake = "test";
@ -260,7 +261,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 on:view-history={() => setAppState(APPSTATE.HISTORY_ID)} /> <Profile {gamePlaying} on:view-history={() => setAppState(APPSTATE.HISTORY_ID)} />
</div> </div>
{/if} {/if}
{#if appState === APPSTATE.PROFILE_ID} {#if appState === APPSTATE.PROFILE_ID}
@ -271,6 +272,7 @@
setAppState(APPSTATE.CHANNELS + "#" + selectedChannel.name)} setAppState(APPSTATE.CHANNELS + "#" + selectedChannel.name)}
> >
<Profile <Profile
{gamePlaying}
username={profileUsername} username={profileUsername}
on:view-history={() => setAppState(APPSTATE.HISTORY_ID)} on:view-history={() => setAppState(APPSTATE.HISTORY_ID)}
/> />
@ -282,7 +284,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:this={pong} {appState} {setAppState} {fakeUser} /> <Pong bind:gamePlaying={gamePlaying} bind:this={pong} {appState} {setAppState} {fakeUser} />
{/if} {/if}
{/if} {/if}

51
front/volume/src/components/Alert/Alert.svelte

@ -1,32 +1,46 @@
<script> <script>
import { getContext } from 'svelte';
export let message; export let message;
export let form = true; export let form = true;
export let onCancel = () => {}; export let onCancel = () => {};
export let onOkay = () => {}; export let onOkay = () => {};
import { content, popup } from './content' import { content, popup } from "./content";
let value; let value;
let onChange = () => { let onChange = () => {
$content='' $content = "";
}; };
function _onCancel() { function _onCancel() {
onCancel(); onCancel();
$content='' $content = "";
popup.set(null); popup.set(null);
} }
function _onOkay() { function _onOkay() {
onOkay(); onOkay();
$content=value $content = value;
popup.set(null); popup.set(null);
} }
$: onChange() $: onChange();
</script> </script>
<div class="overlay">
<h2>{message}</h2>
{#if form === true}
<input
type="text"
bind:value
on:keydown={(e) => e.which === 13 && _onOkay()}
/>
{/if}
<div class="buttons">
<button on:click={_onCancel}> Cancel </button>
<button on:click={_onOkay}> Okay </button>
</div>
</div>
<style> <style>
h2 { h2 {
font-size: 2rem; font-size: 2rem;
@ -41,25 +55,4 @@
display: flex; display: flex;
justify-content: space-between; justify-content: space-between;
} }
.hidden {
display: none;
}
</style> </style>
<h2>{message}</h2>
{#if form === true}
<input
type="text"
bind:value
on:keydown={e => e.which === 13 && _onOkay()} />
{/if}
<div class="buttons">
<button on:click={_onCancel}>
Cancel
</button>
<button on:click={_onOkay}>
Okay
</button>
</div>

2
front/volume/src/components/Friends.svelte

@ -108,7 +108,7 @@
width: 100%; width: 100%;
height: 100%; height: 100%;
background-color: rgba(0, 0, 0, 0.5); background-color: rgba(0, 0, 0, 0.5);
z-index: 9998; z-index: 50;
display: flex; display: flex;
justify-content: center; justify-content: center;
align-items: center; align-items: center;

2
front/volume/src/components/Leaderboard.svelte

@ -60,7 +60,7 @@
width: 100%; width: 100%;
height: 100%; height: 100%;
background-color: rgba(0, 0, 0, 0.5); background-color: rgba(0, 0, 0, 0.5);
z-index: 9998; z-index: 50;
display: flex; display: flex;
justify-content: center; justify-content: center;
align-items: center; align-items: center;

2
front/volume/src/components/MatchHistory.svelte

@ -110,7 +110,7 @@
width: 100%; width: 100%;
height: 100%; height: 100%;
background-color: rgba(0, 0, 0, 0.5); background-color: rgba(0, 0, 0, 0.5);
z-index: 9998; z-index: 50;
display: flex; display: flex;
justify-content: center; justify-content: center;
align-items: center; align-items: center;

2
front/volume/src/components/Pong/GameCreation.svelte

@ -67,7 +67,7 @@
width: 100%; width: 100%;
height: 100%; height: 100%;
background-color: rgba(0, 0, 0, 0.5); background-color: rgba(0, 0, 0, 0.5);
z-index: 9998; z-index: 50;
display: flex; display: flex;
justify-content: center; justify-content: center;
align-items: center; align-items: center;

2
front/volume/src/components/Pong/Matchmaking.svelte

@ -17,7 +17,7 @@
width: 100%; width: 100%;
height: 100%; height: 100%;
background-color: rgba(0, 0, 0, 0.5); background-color: rgba(0, 0, 0, 0.5);
z-index: 9998; z-index: 50;
display: flex; display: flex;
justify-content: center; justify-content: center;
align-items: center; align-items: center;

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

@ -26,7 +26,7 @@
import.meta.env.VITE_BACK_PORT import.meta.env.VITE_BACK_PORT
}`; }`;
let gamePlaying: boolean = false; export let gamePlaying: boolean;
let connected: boolean = false; let connected: boolean = false;
let loggedIn: boolean = false; let loggedIn: boolean = false;
let failedLogIn: boolean = false; let failedLogIn: boolean = false;

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

@ -7,14 +7,19 @@
winrate: number; winrate: number;
rank: number; rank: number;
twoFA: boolean; twoFA: boolean;
ftId: number;
} }
</script> </script>
<script lang="ts"> <script lang="ts">
import { createEventDispatcher, onMount } from "svelte"; import { createEventDispatcher, onMount } from "svelte";
import { bind } from 'svelte-simple-modal';
import { API_URL, store, logout } from "../Auth"; import { API_URL, store, logout } from "../Auth";
import Alert from "./Alert/Alert.svelte";
import { popup } from "./Alert/content";
export let username: string = $store.username; export let username: string = $store.username;
export let gamePlaying: boolean;
let edit: boolean = true; let edit: boolean = true;
let user: Player = $store; let user: Player = $store;
@ -37,6 +42,11 @@
const dispatch = createEventDispatcher(); const dispatch = createEventDispatcher();
async function handleSubmit() { async function handleSubmit() {
if (gamePlaying) {
popup.set(bind(Alert, { message:"Cannot change username while playing.", form : false }))
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",
@ -44,8 +54,8 @@
credentials: "include", credentials: "include",
}); });
if (response.ok) { if (response.ok) {
alert("Succefully changed username.");
$store.username = user.username; $store.username = user.username;
popup.set(bind(Alert, { message: "Succefully changed username.", form : false }))
} }
} }
@ -59,7 +69,10 @@
credentials: "include", credentials: "include",
}); });
if (response.ok) { if (response.ok) {
alert("Succefully " + (user.twoFA ? "enabled" : "disabled") + " 2FA"); popup.set(bind(Alert, {
message: "Succefully " + (user.twoFA ? "enabled" : "disabled") + " 2FA",
form : false
}))
} }
} }
</script> </script>
@ -136,7 +149,7 @@
width: 100%; width: 100%;
height: 100%; height: 100%;
background-color: rgba(0, 0, 0, 0.5); background-color: rgba(0, 0, 0, 0.5);
z-index: 9998; z-index: 50;
display: flex; display: flex;
justify-content: center; justify-content: center;
align-items: center; align-items: center;

Loading…
Cancel
Save