Added more logging and some bug fixes
This commit is contained in:
1 parent
6e55209159
commit
7bc9d67544
2 files changed
+29
-8
No files matched your search
@@ -23,10 +23,12 @@ class YarnGame {
|
||||
this.inProgress = false;
|
||||
this.gameHost = null;
|
||||
this.gameSettings = {
|
||||
scoreLimit: 25,
|
||||
timeLimit: 60
|
||||
scoreLimit: 50,
|
||||
timeLimit: 60,
|
||||
botCount: 0
|
||||
};
|
||||
this.players = []; // { player_id, name, score, socketId }
|
||||
this.disconnectedPlayers = []; // So people can keep their score when they reconnect
|
||||
this.yarnStory = []; // { player: "", str: "" }
|
||||
this.chat = []; // { id, name, msg }
|
||||
this.round_data = []; // { entry_text, player_id, votes: [] }
|
||||
@@ -42,7 +44,19 @@ class YarnGame {
|
||||
if (existingPlayer) {
|
||||
existingPlayer.socketId = socketId;
|
||||
existingPlayer.name = name;
|
||||
console.log(`Existing player ${name} reconnected to game ${this.roomName}.`)
|
||||
return existingPlayer;
|
||||
} else {
|
||||
console.log(`New player ${name} joined game ${this.roomName}.`)
|
||||
}
|
||||
|
||||
const existingDisconnectedPlayer = this.disconnectedPlayers.find(p => p.player_id === playerId);
|
||||
if (existingDisconnectedPlayer) {
|
||||
existingDisconnectedPlayer.socketId = socketId;
|
||||
existingDisconnectedPlayer.name = name;
|
||||
this.players.push(existingDisconnectedPlayer);
|
||||
console.log(`Existing recently-disconnected player ${name} reconnected to game ${this.roomName}.`)
|
||||
return existingDisconnectedPlayer;
|
||||
}
|
||||
|
||||
const player = {
|
||||
@@ -64,13 +78,17 @@ class YarnGame {
|
||||
removePlayer(playerId) {
|
||||
const index = this.players.findIndex(p => p.player_id === playerId);
|
||||
if (index !== -1) {
|
||||
this.players.splice(index, 1);
|
||||
const removed = this.players.splice(index, 1);
|
||||
this.disconnectedPlayers.push(removed[0]);
|
||||
|
||||
// Assign new host if host left
|
||||
if (this.gameHost === playerId && this.players.length > 0) {
|
||||
this.gameHost = this.players[0].player_id;
|
||||
}
|
||||
}
|
||||
if (this.players.length > 0) {
|
||||
console.log(`Player ${this.players[index].name} dropped.`)
|
||||
}
|
||||
return this.players.length;
|
||||
}
|
||||
|
||||
@@ -241,6 +259,7 @@ class YarnGame {
|
||||
// Get or create a game room
|
||||
function getOrCreateGame(roomName) {
|
||||
if (!games[roomName]) {
|
||||
console.log(`Creating new room with name ${roomName}`)
|
||||
games[roomName] = new YarnGame(roomName);
|
||||
}
|
||||
return games[roomName];
|
||||
@@ -317,6 +336,7 @@ io.on('connection', (socket) => {
|
||||
return;
|
||||
}
|
||||
|
||||
console.log(`Starting new game with settings ${JSON.stringify(settings)}`)
|
||||
game.startGame(settings);
|
||||
io.to(currentRoom).emit('gameStarted', game.getState());
|
||||
|
||||
@@ -403,6 +423,7 @@ io.on('connection', (socket) => {
|
||||
io.to(currentRoom).emit('gameState', game.getState());
|
||||
|
||||
// Clean up empty rooms
|
||||
console.log(`Removing empty room ${currentRoom}`)
|
||||
if (remaining === 0) {
|
||||
if (game.timer) clearInterval(game.timer);
|
||||
delete games[currentRoom];
|
||||
|
||||
Reference in new issue
Block a user