diff --git a/public/assets/agents_win.wav b/public/assets/agents_win.wav new file mode 100644 index 0000000..68c6d6e Binary files /dev/null and b/public/assets/agents_win.wav differ diff --git a/public/assets/clock.wav b/public/assets/clock.wav new file mode 100644 index 0000000..344d796 Binary files /dev/null and b/public/assets/clock.wav differ diff --git a/public/assets/gunshot.wav b/public/assets/gunshot.wav new file mode 100644 index 0000000..12f945f Binary files /dev/null and b/public/assets/gunshot.wav differ diff --git a/public/assets/spy_wins.wav b/public/assets/spy_wins.wav new file mode 100644 index 0000000..8718ca9 Binary files /dev/null and b/public/assets/spy_wins.wav differ diff --git a/public/assets/start.wav b/public/assets/start.wav new file mode 100644 index 0000000..431f98c Binary files /dev/null and b/public/assets/start.wav differ diff --git a/public/scripts/chatroom.js b/public/scripts/chatroom.js index a5d19ac..c773e76 100644 --- a/public/scripts/chatroom.js +++ b/public/scripts/chatroom.js @@ -40,6 +40,13 @@ document.addEventListener('DOMContentLoaded', () => { let sabotageInterval let sabotageTimeout; + // game sounds + const startSound = new Audio('../assets/start.wav'); + const timerSound = new Audio('../assets/clock.wav'); + const gunshotSound = new Audio('../assets/gunshot.wav'); + const spyWinSound = new Audio('../assets/spy_wins.wav'); + const agentWinSound = new Audio('../assets/agents_win.wav'); + if (!username || challenge != sessionStorage.getItem('token')) { // TODO: make error feedback look better alert("Invalid access!"); @@ -79,6 +86,10 @@ document.addEventListener('DOMContentLoaded', () => { // listens for game start socket.on('gameStart', game => { + spyWinSound.pause(); + agentWinSound.pause(); + startSound.currentTime = 0; + startSound.play(); chatWindow.innerHTML = ""; renderChatMessage(socket.id, {id: null, username: "ChatBot", message: "The Game has started!", time: new Date().toLocaleTimeString()}); document.querySelector('.game-start-container').classList.add("d-none"); @@ -119,6 +130,8 @@ document.addEventListener('DOMContentLoaded', () => { keyboard: false }); // start vote timer + timerSound.currentTime = 0; + timerSound.play(); voteTime = VOTE_TIME_LIMIT; voteTimer.innerText = voteTime; voteInterval = setInterval(() => { @@ -135,6 +148,9 @@ document.addEventListener('DOMContentLoaded', () => { // listens for the vote done socket.on('voteDone', deadId => { + timerSound.pause(); + gunshotSound.currentTime = 0; + gunshotSound.play(); if (deadId == socket.id) { isAlive = false; @@ -160,6 +176,8 @@ document.addEventListener('DOMContentLoaded', () => { keyboard: false }); // start the sabotage timer + timerSound.currentTime = 0; + timerSound.play(); sabotageTime = SABOTAGE_TIME_LIMIT; sabotageTimer.innerText = sabotageTime; sabotageInterval = setInterval(() => { @@ -177,7 +195,8 @@ document.addEventListener('DOMContentLoaded', () => { }); // listens for game end - socket.on('endGame', () => { + socket.on('endGame', winner => { + timerSound.pause(); // un-initialize the game and disable game buttons document.querySelector('.game-start-container').classList.remove("d-none"); document.querySelector('.list-of-words-toggle').classList.add('d-none'); @@ -189,6 +208,13 @@ document.addEventListener('DOMContentLoaded', () => { document.querySelector('.dead-view').classList.add('d-none'); isSpy = false; isAlive = false; + if (winner == 'agents') { + agentWinSound.currentTime = 0; + agentWinSound.play(); + } else { + spyWinSound.currentTime = 0; + spyWinSound.play(); + } }); // listens for a message diff --git a/server.js b/server.js index de335a2..bd1fdb9 100644 --- a/server.js +++ b/server.js @@ -120,7 +120,7 @@ io.on('connection', socket => { // spy dies // tell client game is over io.to(user.room).emit('message', message(null, bot, `Agents Won!\n${spy.username} was the spy`)); - io.to(user.room).emit('endGame'); + io.to(user.room).emit('endGame', 'agents'); // reset in-server game stat endGame(user.room); return; @@ -136,7 +136,7 @@ io.on('connection', socket => { endGame(user.room); // tell client game is over io.to(user.room).emit('message', message(null, bot, `Spy Won!\n${spy.username} is the spy`)); - io.to(user.room).emit('endGame'); + io.to(user.room).emit('endGame', 'spy'); } } resetVotes(user.room); @@ -156,11 +156,12 @@ io.on('connection', socket => { if (guess == getAnswer(user.room)) { io.to(user.room).emit('message', message(null, bot, `Spy has correctly sabotaged the secret word: ${guess}`)); io.to(user.room).emit('message', message(null, bot, `Spy Won!\n${getSpy(user.room).username} was the spy`)); + io.to(user.room).emit('endGame', 'spy'); } else { io.to(user.room).emit('message', message(null, bot, `Agents Won!\nThe secret key was ${getAnswer(user.room)}`)); + io.to(user.room).emit('endGame', 'agents'); } endGame(user.room); - io.to(user.room).emit('endGame'); }); }); @@ -193,7 +194,7 @@ io.on('connection', socket => { endGame(user.room); // tell client game is over io.to(user.room).emit('message', message(null, bot, `Agents Won!\n${spy.username} is the spy`)); - io.to(user.room).emit('endGame'); + io.to(user.room).emit('endGame', 'agents'); return; } // if 1v1, spy wins @@ -202,7 +203,7 @@ io.on('connection', socket => { endGame(user.room); // tell client game is over io.to(user.room).emit('message', message(null, bot, `Spy Won!\n${spy.username} is the spy`)); - io.to(user.room).emit('endGame'); + io.to(user.room).emit('endGame', 'spy'); return; } }