Added playernames to the scoreboard

This commit is contained in:
gohlkeMax
2021-07-01 18:39:23 +02:00
parent b22e3f4944
commit 173ee05cfe
3 changed files with 51 additions and 25 deletions

View File

@@ -19,6 +19,7 @@ class Game {
this.winnerIndex = 0;
this.round = 0;
this.hunter = new Hunter();
this.playerNames = [];
}
finish_turn() {
@@ -88,6 +89,18 @@ class Game {
this.winnerIndex = index;
}
}
getPlayerNames(){
return this.playerNames;
}
addPlayerName(playerName){
this.playerNames.push(playerName);
}
removePlayerName(playerName){
this.playerNames.splice(this.playerNames.indexOf(playerName), 1)
}
}
module.exports = Game;

View File

@@ -43,17 +43,19 @@ io.on('connection', socket => {
socket.on('add user', function (data) {
socket.username = data.username;
socket.room = data.room_name;
if (game[socket.room] === undefined) {
game[socket.room] = new Game();
}
if (game[socket.room].add_player(socket.username)) {
game[socket.room].addPlayerName(data.username);
addedUser = true;
socket.emit('login');
socket.join(socket.room);
io.in(socket.room).emit('updatePlayerNames', game[socket.room].getPlayerNames());
if (game[socket.room].players.length === 1) io.to(socket.id).emit('first player');
@@ -61,7 +63,7 @@ io.on('connection', socket => {
generate_log_message(socket.room, socket.username, "JOINED", "");
} else {
io.to(socket.id).emit('error', 'Game started already or room has two many members');
io.to(socket.id).emit('error', 'Game started already or room has too many members');
}
});
@@ -78,6 +80,10 @@ io.on('connection', socket => {
socket.on('disconnect', function () {
if (game[socket.room] !== undefined && addedUser) {
game[socket.room].removePlayerName(socket.username);
io.in(socket.room).emit('updatePlayerNames', game[socket.room].getPlayerNames());
socket.broadcast.to(socket.room).emit('user left', socket.username);
game[socket.room].remove_player(socket.username);