Skip to content

Commit

Permalink
feat: Czar can draw the next question by clicking
Browse files Browse the repository at this point in the history
- Add a card deck modal
- Create a new game state
- Update game service and controller
- Add tests for new game state
[Finishes #158457043]
  • Loading branch information
ikenjoku committed Jul 19, 2018
1 parent 3dd0ebb commit ae28d05
Show file tree
Hide file tree
Showing 9 changed files with 3,693 additions and 3,501 deletions.
84 changes: 74 additions & 10 deletions backend-test/game/game.js
Original file line number Diff line number Diff line change
Expand Up @@ -148,13 +148,13 @@ describe('Game Server', () => {
const expectStartGame = () => {
client1.emit('startGame');
client1.on('gameUpdate', (data) => {
data.state.should.equal('waiting for players to pick');
data.state.should.equal('czar picks card');
});
client2.on('gameUpdate', (data) => {
data.state.should.equal('waiting for players to pick');
data.state.should.equal('czar picks card');
});
client3.on('gameUpdate', (data) => {
data.state.should.equal('waiting for players to pick');
data.state.should.equal('czar picks card');
});
setTimeout(disconnect, 200);
};
Expand All @@ -172,7 +172,35 @@ describe('Game Server', () => {
});
});

it('Should automatically start game when 6 players are in a game', (done) => {
it('Should not start game when startGame event is sent with less than 3 players', (done) => {
let client2;
const client1 = io.connect(socketURL, options);
const disconnect = () => {
client1.disconnect();
client2.disconnect();
done();
};
const expectStartGame = () => {
client1.emit('startGame');
client1.on('gameUpdate', (data) => {
data.state.should.equal('waiting for players to pick');
});
client2.on('gameUpdate', (data) => {
data.state.should.equal('waiting for players to pick');
});
setTimeout(disconnect, 200);
};
client1.on('connect', () => {
client1.emit('joinGame', { userID: 'unauthenticated', room: '', createPrivate: false });
client2 = io.connect(socketURL, options);
client2.on('connect', () => {
client2.emit('joinGame', { userID: 'unauthenticated', room: '', createPrivate: false });
setTimeout(expectStartGame, 100);
});
});
});

it('Should start only when game has more than 3 players are in a game', (done) => {
let client2, client3, client4, client5, client6;
const client1 = io.connect(socketURL, options);
const disconnect = () => {
Expand All @@ -187,22 +215,22 @@ describe('Game Server', () => {
const expectStartGame = () => {
client1.emit('startGame');
client1.on('gameUpdate', (data) => {
data.state.should.equal('waiting for players to pick');
data.state.should.equal('czar picks card');
});
client2.on('gameUpdate', (data) => {
data.state.should.equal('waiting for players to pick');
data.state.should.equal('czar picks card');
});
client3.on('gameUpdate', (data) => {
data.state.should.equal('waiting for players to pick');
data.state.should.equal('czar picks card');
});
client4.on('gameUpdate', (data) => {
data.state.should.equal('waiting for players to pick');
data.state.should.equal('czar picks card');
});
client5.on('gameUpdate', (data) => {
data.state.should.equal('waiting for players to pick');
data.state.should.equal('czar picks card');
});
client6.on('gameUpdate', (data) => {
data.state.should.equal('waiting for players to pick');
data.state.should.equal('czar picks card');
});
setTimeout(disconnect, 200);
};
Expand Down Expand Up @@ -238,4 +266,40 @@ describe('Game Server', () => {
});
});
});

it('Should have a new game state for czar to pick card', (done) => {
let client2, client3;
const client1 = io.connect(socketURL, options);
const disconnect = () => {
client1.disconnect();
client2.disconnect();
client3.disconnect();
done();
};
const expectStartGame = () => {
client1.emit('startGame');
client1.on('gameUpdate', (data) => {
data.state.should.equal('czar picks card');
});
client2.on('gameUpdate', (data) => {
data.state.should.equal('czar picks card');
});
client3.on('gameUpdate', (data) => {
data.state.should.equal('czar picks card');
});
setTimeout(disconnect, 200);
};
client1.on('connect', () => {
client1.emit('joinGame', { userID: 'unauthenticated', room: '', createPrivate: false });
client2 = io.connect(socketURL, options);
client2.on('connect', () => {
client2.emit('joinGame', { userID: 'unauthenticated', room: '', createPrivate: false });
client3 = io.connect(socketURL, options);
client3.on('connect', () => {
client3.emit('joinGame', { userID: 'unauthenticated', room: '', createPrivate: false });
setTimeout(expectStartGame, 100);
});
});
});
});
});
30 changes: 22 additions & 8 deletions config/socket/game.js
Original file line number Diff line number Diff line change
Expand Up @@ -133,11 +133,31 @@ Game.prototype.prepareGame = function() {
});
};

Game.prototype.beginRound = function (self) {
if (this.state === 'czar picks card') {
this.stateChoosing(self);
} else if (this.state === 'czar has left') {
this.selectCzar(self);
}
};

Game.prototype.selectCzar = function (self) {
self.state = 'czar picks card';
self.table = [];
if (self.czar >= self.players.length - 1) {
self.czar = 0;
} else {
self.czar += 1;
}
self.sendUpdate();
};

Game.prototype.startGame = function() {
console.log(this.gameID,this.state);
this.shuffleCards(this.questions);
this.shuffleCards(this.answers);
this.stateChoosing(this);
this.selectCzar(this);
this.sendUpdate();
};

Game.prototype.sendUpdate = function() {
Expand All @@ -159,12 +179,6 @@ Game.prototype.stateChoosing = function(self) {
}
self.round++;
self.dealAnswers();
// Rotate card czar
if (self.czar >= self.players.length - 1) {
self.czar = 0;
} else {
self.czar++;
}
self.sendUpdate();

self.choosingTimeout = setTimeout(function() {
Expand Down Expand Up @@ -217,7 +231,7 @@ Game.prototype.stateResults = function(self) {
if (winner !== -1) {
self.stateEndGame(winner);
} else {
self.stateChoosing(self);
self.selectCzar(self);
}
}, self.timeLimits.stateResults*1000);
};
Expand Down
4 changes: 4 additions & 0 deletions config/socket/socket.js
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,10 @@ module.exports = function (io) {
joinGame(socket, data);
});

socket.on('czarSelectCard', function () {
allGames[socket.gameID].beginRound(allGames[socket.gameID]);
});

socket.on('startGame', function () {
if (allGames[socket.gameID]) {
var thisGame = allGames[socket.gameID];
Expand Down
Loading

0 comments on commit ae28d05

Please sign in to comment.