-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.js
191 lines (151 loc) · 4.28 KB
/
server.js
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
var express = require('express');
var app = express()
, server = require('http').createServer(app)
, io = require('socket.io').listen(server);
app.use("/js", express.static(__dirname + '/js'));
server.listen(8080);
var game ={
team1: {
players: [],
score: 0
},
team2: {
players: [],
score: 0
},
word: "",
turn1: true,
drawer: "",
picture: [[]],
drawPile: ["test", "words", "here"],
started: false,
drawTime: 60000
};
function fisherYates ( myArray ) {
var i = myArray.length, j, tempi, tempj;
if ( i == 0 ) return false;
while ( --i ) {
j = Math.floor( Math.random() * ( i + 1 ) );
tempi = myArray[i];
tempj = myArray[j];
myArray[i] = tempj;
myArray[j] = tempi;
}
}
app.get('/', function(req, res) {
res.sendFile(__dirname + '/pict.html');
});
function start_game() {
//randomize words
fisherYates(game.drawPile);
//randomize players
fisherYates(game.team1.players);
fisherYates(game.team2.players);
//randomly assign first team to draw
game.turn1 = (Math.floor(Math.random() * 2) == 0);
//assign drawer
(game.turn1) ? game.drawer = game.team1.players[0] : game.drawer = game.team2.players[0];
//assign word
game.word = game.drawPile.shift();
//start game (for people joining later)
game.start = true;
//let the players know the game is starting
io.sockets.emit('start_game', game);
}
function start_round(socket) {
//let other players know the round is started
socket.broadcast.emit('start_round', game);
//the server is the final word on timing, so it keeps track itself
game.timeout = setTimeout(end_round, game.drawTime);
}
function end_round() {
//flip turn
game.turn1 = !game.turn1;
//assign new drawer
if (game.turn1) {
game.team1.players.push(game.team1.players.shift());
game.drawer = game.team1[0];
} else {
game.team2.players.push(game.team2.players.shift());
game.drawer = game.team2[0];
}
//assign new word after checking we have one
if (game.drawPile.length == 0) {
end_game('out of words');
return;
}
game.word = game.drawPile.shift();
//let everyone know the round is over and a new one is starting
io.sockets.emit('end_round', game);
}
function new_word(socket) {
//make sure we have a word to send, if not end the game
if (game.drawPile.length == 0) {
end_game('out of words');
return;
}
//assign new word
game.word = game.drawPile.shift();
//send it out to the drawer, since the others won't know until the round starts
socket.emit('new_word', game);
}
function win_round() {
//increment the team's score
(game.turn == 1) ? game.team1.score++ : game.team2.score++;
//clear the end_round timeout
clearTimeout(game.timeout);
//then call end_round manually
end_round();
}
function end_game(reason) {
}
io.sockets.on('connection', function (socket) {
//let the client know we're connected
socket.emit('handshake', game);
socket.on('join', function(player) {
//if the player is new
if (game.team1.players.indexOf(player) < 0 && game.team2.players.indexOf(player) < 0) {
console.log("New player " + player + " joined");
//add the new person to the smallest team
(game.team1.players.length > game.team2.players.length) ? game.team2.players.push(player) : game.team1.players.push(player);
console.dir(game.team1);
console.dir(game.team2);
//check to start the game
if (!game.started && game.team1.players.length >= 2 && game.team2.players.length >= 2) {
console.log("starting game");
start_game();
} else if (game.started) {
socket.emit('join_game', game);
} else {
socket.emit('wait');
}
}
});
socket.on('update_stroke', function(pos) {
game.picture[game.picture.length - 1].push(pos);
socket.broadcast.emit('update_stroke', pos);
});
socket.on('start_stroke', function() {
game.picture.push([]);
socket.broadcast.emit('start_stroke');
});
socket.on('end_stroke', function() {
socket.broadcast.emit('end_stroke');
});
socket.on('clear', function() {
game.picture = [[]];
socket.broadcast.emit('clear');
});
socket.on('new_word', function() {
//the drawer wanted a new word
new_word(socket);
});
socket.on('start_round', function() {
//got the go-ahead from the drawer
start_round(socket);
});
socket.on('end_round', function() {
//the drawer ended the round early, meaning the team won
win_round();
});
});