Skip to content

Commit

Permalink
added game sounds
Browse files Browse the repository at this point in the history
  • Loading branch information
minhyeong-joe committed Aug 11, 2020
1 parent 3165813 commit e855083
Show file tree
Hide file tree
Showing 7 changed files with 33 additions and 6 deletions.
Binary file added public/assets/agents_win.wav
Binary file not shown.
Binary file added public/assets/clock.wav
Binary file not shown.
Binary file added public/assets/gunshot.wav
Binary file not shown.
Binary file added public/assets/spy_wins.wav
Binary file not shown.
Binary file added public/assets/start.wav
Binary file not shown.
28 changes: 27 additions & 1 deletion public/scripts/chatroom.js
Original file line number Diff line number Diff line change
Expand Up @@ -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!");
Expand Down Expand Up @@ -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");
Expand Down Expand Up @@ -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(() => {
Expand All @@ -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;
Expand All @@ -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(() => {
Expand All @@ -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');
Expand All @@ -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
Expand Down
11 changes: 6 additions & 5 deletions server.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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);
Expand All @@ -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');
});

});
Expand Down Expand Up @@ -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
Expand All @@ -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;
}
}
Expand Down

0 comments on commit e855083

Please sign in to comment.