Added more logging and some bug fixes

This commit is contained in:
Cam Spry 2026-03-01 05:54:48 -05:00
parent 6e55209159
commit 7bc9d67544
2 changed files with 29 additions and 8 deletions

View File

@ -332,7 +332,7 @@
<div id="layout"> <div id="layout">
<div id="header"> <div id="header">
YARN 2026 - Spin your story! <span id="room-display"></span> Y.A.R.N. 2026 - Spin a story! -&nbsp;<span id="room-display"></span>
</div> </div>
<div id="main-text"> <div id="main-text">
@ -370,7 +370,7 @@
// Get room name from URL // Get room name from URL
const pathParts = window.location.pathname.split('/').filter(p => p); const pathParts = window.location.pathname.split('/').filter(p => p);
const roomName = pathParts[0] || 'default'; const roomName = pathParts[0] || 'default';
document.getElementById('room-display').textContent = `Room: ${roomName}`; document.getElementById('room-display').textContent = `Room: ${roomName}`;
// Get or create player UUID // Get or create player UUID
let playerUUID = getCookie('yarn_player_uuid'); let playerUUID = getCookie('yarn_player_uuid');
@ -746,12 +746,12 @@
} }
function startGame() { function startGame() {
const scoreLimit = parseInt(document.getElementById('score-limit').value) || 25; const scoreLimit = parseInt(document.getElementById('score-limit').value) || 100;
const timeLimit = parseInt(document.getElementById('time-limit').value) || 60; const timeLimit = parseInt(document.getElementById('time-limit').value) || 60;
socket.emit('startGame', { socket.emit('startGame', {
scoreLimit: Math.max(10, Math.min(100, scoreLimit)), scoreLimit: Math.max(10, Math.min(9999, scoreLimit)),
timeLimit: Math.max(15, Math.min(180, timeLimit)) timeLimit: Math.max(15, Math.min(600, timeLimit))
}); });
} }

View File

@ -23,10 +23,12 @@ class YarnGame {
this.inProgress = false; this.inProgress = false;
this.gameHost = null; this.gameHost = null;
this.gameSettings = { this.gameSettings = {
scoreLimit: 25, scoreLimit: 50,
timeLimit: 60 timeLimit: 60,
botCount: 0
}; };
this.players = []; // { player_id, name, score, socketId } this.players = []; // { player_id, name, score, socketId }
this.disconnectedPlayers = []; // So people can keep their score when they reconnect
this.yarnStory = []; // { player: "", str: "" } this.yarnStory = []; // { player: "", str: "" }
this.chat = []; // { id, name, msg } this.chat = []; // { id, name, msg }
this.round_data = []; // { entry_text, player_id, votes: [] } this.round_data = []; // { entry_text, player_id, votes: [] }
@ -42,7 +44,19 @@ class YarnGame {
if (existingPlayer) { if (existingPlayer) {
existingPlayer.socketId = socketId; existingPlayer.socketId = socketId;
existingPlayer.name = name; existingPlayer.name = name;
console.log(`Existing player ${name} reconnected to game ${this.roomName}.`)
return existingPlayer; 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 = { const player = {
@ -64,13 +78,17 @@ class YarnGame {
removePlayer(playerId) { removePlayer(playerId) {
const index = this.players.findIndex(p => p.player_id === playerId); const index = this.players.findIndex(p => p.player_id === playerId);
if (index !== -1) { 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 // Assign new host if host left
if (this.gameHost === playerId && this.players.length > 0) { if (this.gameHost === playerId && this.players.length > 0) {
this.gameHost = this.players[0].player_id; this.gameHost = this.players[0].player_id;
} }
} }
if (this.players.length > 0) {
console.log(`Player ${this.players[index].name} dropped.`)
}
return this.players.length; return this.players.length;
} }
@ -241,6 +259,7 @@ class YarnGame {
// Get or create a game room // Get or create a game room
function getOrCreateGame(roomName) { function getOrCreateGame(roomName) {
if (!games[roomName]) { if (!games[roomName]) {
console.log(`Creating new room with name ${roomName}`)
games[roomName] = new YarnGame(roomName); games[roomName] = new YarnGame(roomName);
} }
return games[roomName]; return games[roomName];
@ -317,6 +336,7 @@ io.on('connection', (socket) => {
return; return;
} }
console.log(`Starting new game with settings ${JSON.stringify(settings)}`)
game.startGame(settings); game.startGame(settings);
io.to(currentRoom).emit('gameStarted', game.getState()); io.to(currentRoom).emit('gameStarted', game.getState());
@ -403,6 +423,7 @@ io.on('connection', (socket) => {
io.to(currentRoom).emit('gameState', game.getState()); io.to(currentRoom).emit('gameState', game.getState());
// Clean up empty rooms // Clean up empty rooms
console.log(`Removing empty room ${currentRoom}`)
if (remaining === 0) { if (remaining === 0) {
if (game.timer) clearInterval(game.timer); if (game.timer) clearInterval(game.timer);
delete games[currentRoom]; delete games[currentRoom];