|
@ -8,8 +8,8 @@ |
|
|
import SpectateFriend from "./SpectateFriend.svelte"; |
|
|
import SpectateFriend from "./SpectateFriend.svelte"; |
|
|
import Matchmaking from "./Matchmaking.svelte"; |
|
|
import Matchmaking from "./Matchmaking.svelte"; |
|
|
import type { MatchmakingDto } from "./dtos/MatchmakingDto"; |
|
|
import type { MatchmakingDto } from "./dtos/MatchmakingDto"; |
|
|
import { store } from "../../Auth"; |
|
|
import { getUser, store } from "../../Auth"; |
|
|
import ColorPicker from "./ColorPicker.svelte"; |
|
|
import ColorPicker from "./ColorPicker.svelte"; |
|
|
|
|
|
|
|
|
const SERVER_URL = `ws://${import.meta.env.VITE_HOST}:${ |
|
|
const SERVER_URL = `ws://${import.meta.env.VITE_HOST}:${ |
|
|
import.meta.env.VITE_BACK_PORT |
|
|
import.meta.env.VITE_BACK_PORT |
|
@ -22,6 +22,7 @@ |
|
|
let gameCanvas: HTMLCanvasElement; |
|
|
let gameCanvas: HTMLCanvasElement; |
|
|
let connected: boolean = false; |
|
|
let connected: boolean = false; |
|
|
let loggedIn: boolean = false; |
|
|
let loggedIn: boolean = false; |
|
|
|
|
|
let failedLogIn: boolean = false; |
|
|
let socket: WebSocket; |
|
|
let socket: WebSocket; |
|
|
let username: string = $store.username; |
|
|
let username: string = $store.username; |
|
|
let elementsColor: string = "#FFFFFF"; |
|
|
let elementsColor: string = "#FFFFFF"; |
|
@ -52,11 +53,13 @@ |
|
|
} else gamePlaying = false; |
|
|
} else gamePlaying = false; |
|
|
} |
|
|
} |
|
|
} else if (event == GAME_EVENTS.REGISTER_PLAYER) { |
|
|
} else if (event == GAME_EVENTS.REGISTER_PLAYER) { |
|
|
if (data.value == username) { |
|
|
if (data) { |
|
|
loggedIn = true; |
|
|
loggedIn = true; |
|
|
setInterval(() => { |
|
|
setInterval(() => { |
|
|
updateGameInfo(); |
|
|
updateGameInfo(); |
|
|
}, 1000); |
|
|
}, 1000); |
|
|
|
|
|
} else { |
|
|
|
|
|
failedLogIn = true; |
|
|
} |
|
|
} |
|
|
} else if (event == GAME_EVENTS.CREATE_GAME) { |
|
|
} else if (event == GAME_EVENTS.CREATE_GAME) { |
|
|
if (data) gamePlaying = true; |
|
|
if (data) gamePlaying = true; |
|
@ -76,6 +79,7 @@ |
|
|
} |
|
|
} |
|
|
}; |
|
|
}; |
|
|
socket.onopen = () => { |
|
|
socket.onopen = () => { |
|
|
|
|
|
void logIn(); |
|
|
connected = true; |
|
|
connected = true; |
|
|
}; |
|
|
}; |
|
|
socket.onclose = () => { |
|
|
socket.onclose = () => { |
|
@ -88,8 +92,13 @@ |
|
|
socket.send(formatWebsocketData(GAME_EVENTS.GET_GAME_INFO)); |
|
|
socket.send(formatWebsocketData(GAME_EVENTS.GET_GAME_INFO)); |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
function logIn() { |
|
|
async function logIn() { |
|
|
const data: StringDto = { value: username }; |
|
|
await getUser(); |
|
|
|
|
|
const socketKey = $store.socketKey; |
|
|
|
|
|
const data: { playerName: StringDto; socketKey: StringDto } = { |
|
|
|
|
|
playerName: { value: username }, |
|
|
|
|
|
socketKey: { value: socketKey }, |
|
|
|
|
|
}; |
|
|
socket.send(formatWebsocketData(GAME_EVENTS.REGISTER_PLAYER, data)); |
|
|
socket.send(formatWebsocketData(GAME_EVENTS.REGISTER_PLAYER, data)); |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
@ -105,63 +114,56 @@ |
|
|
|
|
|
|
|
|
$: { |
|
|
$: { |
|
|
if (game !== undefined) { |
|
|
if (game !== undefined) { |
|
|
game.updateColors(elementsColor, backgroundColor) |
|
|
game.updateColors(elementsColor, backgroundColor); |
|
|
} |
|
|
} |
|
|
} |
|
|
} |
|
|
</script> |
|
|
</script> |
|
|
|
|
|
|
|
|
<div> |
|
|
<main> |
|
|
{#if !loggedIn} |
|
|
<GameComponent {gameCanvas} {gamePlaying} {setupSocket} {socket} /> |
|
|
Log in: |
|
|
{#if gamePlaying} |
|
|
<input bind:value={username} /> |
|
|
<div /> |
|
|
<button on:click={logIn} disabled={!connected}> Log in </button> |
|
|
{:else if loggedIn} |
|
|
<br /> |
|
|
<h1>Choose a gamemode</h1> |
|
|
{/if} |
|
|
<button on:click={startMatchmaking}>Matchmaking</button> |
|
|
<div hidden={!loggedIn}> |
|
|
<button on:click={() => (createMatchWindow = true)} |
|
|
<main> |
|
|
>Play with a friend</button |
|
|
<GameComponent {gameCanvas} {gamePlaying} {setupSocket} {socket} /> |
|
|
> |
|
|
{#if gamePlaying} |
|
|
<button on:click={() => (spectateWindow = true)}>Spectate a friend</button> |
|
|
<div /> |
|
|
<label for="colorPicker">Elements color:</label> |
|
|
{:else if connected} |
|
|
<ColorPicker bind:color={elementsColor} /> |
|
|
<h1>Choose a gamemode</h1> |
|
|
<label for="colorPicker">Background color:</label> |
|
|
<button on:click={startMatchmaking}>Matchmaking</button> |
|
|
<ColorPicker bind:color={backgroundColor} /> |
|
|
<button on:click={() => (createMatchWindow = true)} |
|
|
|
|
|
>Play with a friend</button |
|
|
|
|
|
> |
|
|
|
|
|
<button on:click={() => (spectateWindow = true)} |
|
|
|
|
|
>Spectate a friend</button |
|
|
|
|
|
> |
|
|
|
|
|
<label for="colorPicker">Elements color:</label> |
|
|
|
|
|
<ColorPicker bind:color={elementsColor} /> |
|
|
|
|
|
<label for="colorPicker">Background color:</label> |
|
|
|
|
|
<ColorPicker bind:color={backgroundColor} /> |
|
|
|
|
|
|
|
|
|
|
|
{#if matchmaking} |
|
|
{#if matchmaking} |
|
|
<div on:click={stopMatchmaking} on:keydown={stopMatchmaking}> |
|
|
<div on:click={stopMatchmaking} on:keydown={stopMatchmaking}> |
|
|
<Matchmaking {stopMatchmaking} /> |
|
|
<Matchmaking {stopMatchmaking} /> |
|
|
</div> |
|
|
</div> |
|
|
<button on:click={() => console.log(elementsColor)}>tet</button> |
|
|
<button on:click={() => console.log(elementsColor)}>tet</button> |
|
|
{:else if createMatchWindow} |
|
|
{:else if createMatchWindow} |
|
|
<div |
|
|
<div |
|
|
on:click={() => (createMatchWindow = false)} |
|
|
on:click={() => (createMatchWindow = false)} |
|
|
on:keydown={() => (createMatchWindow = false)} |
|
|
on:keydown={() => (createMatchWindow = false)} |
|
|
> |
|
|
> |
|
|
<GameCreation {socket} {username} /> |
|
|
<GameCreation {socket} {username} /> |
|
|
</div> |
|
|
</div> |
|
|
{:else if spectateWindow} |
|
|
{:else if spectateWindow} |
|
|
<div |
|
|
<div |
|
|
on:click={() => (spectateWindow = false)} |
|
|
on:click={() => (spectateWindow = false)} |
|
|
on:keydown={() => (spectateWindow = false)} |
|
|
on:keydown={() => (spectateWindow = false)} |
|
|
> |
|
|
> |
|
|
<SpectateFriend {socket} /> |
|
|
<SpectateFriend {socket} /> |
|
|
</div> |
|
|
</div> |
|
|
{/if} |
|
|
{/if} |
|
|
{:else} |
|
|
{:else if !connected} |
|
|
Connecting to game server... |
|
|
Connecting to game server... |
|
|
{/if} |
|
|
{:else if failedLogIn} |
|
|
</main> |
|
|
Failed to log in to game server. Do you have multiple pages open at the same |
|
|
</div> |
|
|
time? If yes, please close them and try again. |
|
|
</div> |
|
|
{:else if !loggedIn} |
|
|
|
|
|
Logging in to game server... |
|
|
|
|
|
{/if} |
|
|
|
|
|
</main> |
|
|
|
|
|
|
|
|
<style> |
|
|
<style> |
|
|
main { |
|
|
main { |
|
|