-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgame.js
More file actions
200 lines (151 loc) · 7.6 KB
/
game.js
File metadata and controls
200 lines (151 loc) · 7.6 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
/**
* User: gamaroff
* Date: 2012/08/16
* Time: 8:00 PM
*/
var io = require('socket.io');
var uuid = require('node-uuid');
var _ = require('underscore');
var gameModel = require('./domain/game');
function Game() {
"use strict";
var self = this;
self.init = function (server) {
// Express 3 requires a server object
io = io.listen(server);
io.sockets.on('connection', function (socket) {
socket.on('new_game', function (data) {
if (parseInt(data.players, 10) === 2) {
twoPlayerGame(socket, data);
}
else {
singlePlayerGame(socket, data);
}
});
socket.on('disconnect', function (data) {
socket.get('room', function (derr, room) {
socket.broadcast.to(room).emit('opponent_disconnected');
});
});
// when a client answers a question
socket.on('answer', function (data) {
// tell the other player in the room that this player is still playing
socket.broadcast.to(data.gameRoom).emit('opponent_playing');
// grab the question the player answered
gameModel.getQuestion(data.game, data.question, function (question) {
var answer = _.find(question.answers, function (answer) {
return answer.id === parseInt(data.answer);
});
// if the answer is correct then send the player point down to the opponent to update the UI
if (answer) {
var point = 0;
if (answer.correct) {
point = 1;
if (data.gameRoom)
socket.broadcast.to(data.gameRoom).emit('opponent_score', {score:point});
}
}
// get the next question
gameModel.getQuestions(data.game, function (result) {
// if there is another question send it
if (result[data.currentQuestion])
socket.emit('new_question', {question:result[data.currentQuestion], score:point});
else { // else finish the player game and tell the opponent the player has finished
socket.emit('finish', {score:point});
socket.broadcast.to(data.gameRoom).emit('opponent_finished');
}
});
});
});
// if the player runs out of time, finish the game
socket.on('time_up', function (data) {
socket.emit('finish', {});
socket.broadcast.to(data.gameRoom).emit('opponent_finished');
});
});
};
var singlePlayerGame = function (socket, data) {
var room = uuid.v4();
socket.join(room);
socket.set('game', data.game);
socket.set('duration', data.duration);
socket.emit('in_room', {gameRoom:room});
gameModel.getQuestions(data.game, function (result) {
io.sockets.in(room).emit('start',
{
// send the first random question
question:result[Math.floor(Math.random() * result.length) + 1],
// and the number of questions
questionCount:result.length
});
});
};
var twoPlayerGame = function (socket, data) {
// look through all the rooms to find one with only one player
var inRoom = false;
var rooms = io.sockets.manager.rooms;
for (var room in rooms) {
var clients = rooms[room];
// If the room has one player in it and it is not the default room
if (room !== '' && clients.length === 1) {
var clientSocketId = clients[0];
// go through all the clients
_.each(io.sockets.clients(), function (clientSocket) {
if (clientSocket.id === clientSocketId) {
// get the game data on the client already in the room for comparison with the newly joining client
clientSocket.get('game', function (err, game) {
// get the duration data of the client already in the room
clientSocket.get('duration', function (derr, duration) {
// Game and Duration must be the same so players are playing same game and duration
if (parseInt(game) === parseInt(data.game) && parseInt(duration) === parseInt(data.duration)) {
// Join the client to the room with a waiting client playing the same game and duration
socket.join(room.slice(1));
socket.set('room', room.slice(1));
socket.set('game', data.game);
socket.set('duration', data.duration);
socket.emit('in_room', {gameRoom:room.slice(1)});
inRoom = true;
//var clientSocketId = clients[0];
socket.set('playerName', data.playerName);
// go through all the clients
_.each(io.sockets.clients(), function (clientSocket) {
if (clientSocket.id === socket.id) {
// clientSocket.get('playerName', function (err, playerName) {
socket.broadcast.emit('opponent_name', {name:data.playerName});
//});
}
else {
clientSocket.get('playerName', function (err, playerName) {
clientSocket.broadcast.emit('opponent_name', {name:playerName});
});
}
});
gameModel.getQuestions(data.game, function (result) {
io.sockets.in(room.slice(1)).emit('start',
{
// send the first random question
question:result[Math.floor(Math.random() * result.length) + 1],
// and the number of questions
questionCount:result.length,
});
});
}
});
});
}
});
}
}
// There are no rooms to join the create a new room for the client
if (!inRoom) {
var room = uuid.v4();
socket.join(room);
socket.set('room', room);
socket.set('game', data.game);
socket.set('duration', data.duration);
socket.emit('in_room', {gameRoom:room});
socket.set('playerName', data.playerName);
}
};
}
module.exports = new Game();