Lobby Start Mechanik und abfangen aller Möglichen Fehler
This commit is contained in:
@@ -32,7 +32,8 @@ class ExpressManager{
|
|||||||
// Routen wo man für Angemeldet sein muss
|
// Routen wo man für Angemeldet sein muss
|
||||||
this.authRoutes = [
|
this.authRoutes = [
|
||||||
"/api/dashboard",
|
"/api/dashboard",
|
||||||
"/dashboard"
|
"/dashboard",
|
||||||
|
"/game"
|
||||||
];
|
];
|
||||||
|
|
||||||
// Routen Einbinden
|
// Routen Einbinden
|
||||||
|
|||||||
@@ -1,10 +1,13 @@
|
|||||||
const socketIO = require("socket.io");
|
const socketIO = require("socket.io");
|
||||||
const SocketUser = require("./SocketUser");
|
const SocketUser = require("./SocketUser");
|
||||||
|
const LobbyManager = require("../LobbyManager");
|
||||||
|
const TemporaryLobby = require("./TemporaryLobby");
|
||||||
|
|
||||||
class Lobby {
|
class Lobby {
|
||||||
/** @param {socketIO.Server} io @param {Map<number, SocketUser>} users*/
|
/** @param {socketIO.Server} io @param {LobbyManager} lobbyManager @param {number} code */
|
||||||
constructor(io, code) {
|
constructor(io, lobbyManager, code) {
|
||||||
this.io = io;
|
this.io = io;
|
||||||
|
this.lobbyManager = lobbyManager;
|
||||||
this.code = code;
|
this.code = code;
|
||||||
/** @type {Array<SocketUser>} */
|
/** @type {Array<SocketUser>} */
|
||||||
this.users = [];
|
this.users = [];
|
||||||
@@ -21,7 +24,7 @@ class Lobby {
|
|||||||
|
|
||||||
if(this.users.length === 2) {
|
if(this.users.length === 2) {
|
||||||
console.log("Starte Spiel");
|
console.log("Starte Spiel");
|
||||||
setTimeout(this.startGame.bind(this), 2000);
|
this.startGame();
|
||||||
}
|
}
|
||||||
|
|
||||||
return 0;
|
return 0;
|
||||||
@@ -75,6 +78,23 @@ class Lobby {
|
|||||||
this.io.to(`lobby-${this.code}`).emit("startGame", data);
|
this.io.to(`lobby-${this.code}`).emit("startGame", data);
|
||||||
this.currentSecond--;
|
this.currentSecond--;
|
||||||
|
|
||||||
|
if(this.currentSecond === 0){
|
||||||
|
const newUserList = [];
|
||||||
|
|
||||||
|
this.users.forEach(x => {
|
||||||
|
const user = new SocketUser(x.id, x.username);
|
||||||
|
newUserList.push(user);
|
||||||
|
})
|
||||||
|
|
||||||
|
const tempLobby = new TemporaryLobby(
|
||||||
|
this.code,
|
||||||
|
newUserList,
|
||||||
|
this.lobbyManager
|
||||||
|
);
|
||||||
|
|
||||||
|
this.lobbyManager.oldLobbys.push(tempLobby);
|
||||||
|
}
|
||||||
|
|
||||||
if(this.currentSecond === -2) {
|
if(this.currentSecond === -2) {
|
||||||
this.gameStart = false;
|
this.gameStart = false;
|
||||||
this.currentSecond = 5;
|
this.currentSecond = 5;
|
||||||
|
|||||||
@@ -1,8 +1,18 @@
|
|||||||
|
const LobbyManager = require("../LobbyManager");
|
||||||
|
|
||||||
class TemporaryLobby{
|
class TemporaryLobby{
|
||||||
/** @param { string } code @param {Array<SocketUser>} users */
|
/** @param { string } code @param {Array<SocketUser>} users @param {LobbyManager} lobbyManager*/
|
||||||
constructor(code, users){
|
constructor(code, users, lobbyManager){
|
||||||
this.code = code;
|
this.code = code;
|
||||||
this.users = users;
|
this.users = users;
|
||||||
|
this.lobbyManager = lobbyManager;
|
||||||
|
|
||||||
|
setTimeout(this.selfDestroy.bind(this), 5000);
|
||||||
|
}
|
||||||
|
|
||||||
|
selfDestroy(){
|
||||||
|
const i = this.lobbyManager.oldLobbys.findIndex(x => x.code == this.code);
|
||||||
|
this.lobbyManager.oldLobbys.splice(i, 1);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -16,7 +16,7 @@ class LobbyManager {
|
|||||||
|
|
||||||
// Zeigt die Anzahl der Lobbys an | Für Testing
|
// Zeigt die Anzahl der Lobbys an | Für Testing
|
||||||
// setInterval(() => {
|
// setInterval(() => {
|
||||||
// console.log(this.lobbys.size);
|
// console.log(this.oldLobbys.length);
|
||||||
// }, 1000);
|
// }, 1000);
|
||||||
|
|
||||||
}
|
}
|
||||||
@@ -24,7 +24,7 @@ class LobbyManager {
|
|||||||
/** @param {SocketUser} user */
|
/** @param {SocketUser} user */
|
||||||
createLobby(user){
|
createLobby(user){
|
||||||
const code = this.generateNonExistingCode();
|
const code = this.generateNonExistingCode();
|
||||||
const lobby = new Lobby(this.io, code);
|
const lobby = new Lobby(this.io, this ,code);
|
||||||
lobby.addUser(user);
|
lobby.addUser(user);
|
||||||
this.lobbys.set(code, lobby);
|
this.lobbys.set(code, lobby);
|
||||||
return code;
|
return code;
|
||||||
|
|||||||
17
frontend/game/index.html
Normal file
17
frontend/game/index.html
Normal file
@@ -0,0 +1,17 @@
|
|||||||
|
<!DOCTYPE html>
|
||||||
|
<html lang="en">
|
||||||
|
<head>
|
||||||
|
<meta charset="UTF-8">
|
||||||
|
<meta name="viewport" content="width=device-width, initial-scale=1.0">
|
||||||
|
<title>Doublesnake - Game</title>
|
||||||
|
<link rel="stylesheet" href="../style/generalStyle.css">
|
||||||
|
<link rel="stylesheet" href="./style/mainStyle.css">
|
||||||
|
</head>
|
||||||
|
<body>
|
||||||
|
|
||||||
|
</body>
|
||||||
|
|
||||||
|
<script src="https://cdn.socket.io/4.7.2/socket.io.min.js"></script>
|
||||||
|
<script type="module" src="./scripts/index.js"></script>
|
||||||
|
|
||||||
|
</html>
|
||||||
28
frontend/game/scripts/index.js
Normal file
28
frontend/game/scripts/index.js
Normal file
@@ -0,0 +1,28 @@
|
|||||||
|
class ServerConnectionManager {
|
||||||
|
constructor() {
|
||||||
|
/**@type {import("../../../backend/node_modules/socket.io-client".Socket} für Autocompletions VSC*/
|
||||||
|
this.socket = io(`${window.location.protocol}//${window.location.hostname}:${window.location.port}`);
|
||||||
|
|
||||||
|
this.basicSetup();
|
||||||
|
}
|
||||||
|
|
||||||
|
basicSetup() {
|
||||||
|
this.socket.emit("hereForGame");
|
||||||
|
|
||||||
|
this.socket.on("connect", () => {
|
||||||
|
console.log("Verbindung zum Server hergestellt!");
|
||||||
|
});
|
||||||
|
|
||||||
|
this.socket.on("connect_error", (error) => {
|
||||||
|
console.log(error);
|
||||||
|
window.location.pathname = "/dashboard";
|
||||||
|
});
|
||||||
|
|
||||||
|
this.socket.on("disconnect", () => {
|
||||||
|
console.log("Die verbindung wurde unterbrochen!");
|
||||||
|
window.location.pathname = "/dashboard";
|
||||||
|
});
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
new ServerConnectionManager();
|
||||||
0
frontend/game/style/mainStyle.css
Normal file
0
frontend/game/style/mainStyle.css
Normal file
Reference in New Issue
Block a user