-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgame.js
315 lines (245 loc) · 8.28 KB
/
game.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
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
var EventEmitter = require('events').EventEmitter;
var util = require('util');
var Words = require('./dictionary');
//var shared = require('./public/js/shared');
var Game = function (io, clients) {
this.io = io;
this.dict = new Words(clients[0].info.native_lang,clients[0].info.learning_lang);
this.clients = clients;
init.call(this);
this.isTeamReady = function () {
var _is_team_ready = 1;
this.clients.forEach(function (v) {
_is_team_ready &= v.is_ready;
});
return _is_team_ready;
}
};
Game.INIT_SNAKE_SIZE = 2;
util.inherits(Game, EventEmitter);
function init() {
var self = this;
this.room = this.clients.map(function (v) {
return v.id
}).join('_');
for (var i = 0, l = this.clients.length; i < l; i++) {
this.clients[i].join(this.room);
this.clients[i].game = this;
this.clients[i].on('disconnect', function () {
self.end(this.info.nickname + " disconnected");
});
}
var self = this;
this.dict.getWordAndTr(function (res) {
this.data={};
var data = {};
//get word to learn
data.w = res.word;
data.wt = res.tr;
//size of map and coordinates of letters
var size = self.clients.length * 15;
data.s = size;
data.ss = Game.INIT_SNAKE_SIZE;
//get letter coordinates
data.ws = getLetterCoordinates(data.w, size, self.clients.length);
//init player's snakes coorinates
data.p = getPlayersInitCoordinates(self.clients, size);
self.data = data;
console.log('game.init' + " " + JSON.stringify(data));
self.io.in(self.room).emit('game.init', data);
//init client's letters
self.data.pl = {};
for (var i = 0, l = self.clients.length; i < l; i++) {
self.data.pl[self.clients[i].info.nickname] = "";
}
});
}
Game.prototype.checkGameState = function (position) {
//check collisions with other snakes
if (!this.data)
return false;
this.data.p[position.client] = position.coords;
var collisions = [];
//need rewrite
for (var key in this.data.p) {
if (key == position.client)
continue;
for (var i = 0; i < this.data.p[key].length; i++) {
for (var j = 0; j < position.coords.length; j++) {
if (this.data.p[key][i].x == position.coords[j].x && this.data.p[key][i].y == position.coords[j].y) {
//reduce one letter
var w1 = this.data.pl[position.client];
var w2 = this.data.pl[key];
var _ls = {};
if (w1.length) {
var l1 = w1.substring(w1.length - 1, w1.length);
this.data.pl[position.client] = w1.substring(0, w1.length - 1);
var _c1 = getCoordHash(getRandomCoordinate(this.data.s, Game.INIT_SNAKE_SIZE));
this.data.ws[_c1] = l1;
_ls[_c1] = l1;
}
if (w2.length) {
var l2 = w2.substring(w2.length - 1, w2.length);
this.data.pl[key] = w2.substring(0, w2.length - 1);
var _c2 = getCoordHash(getRandomCoordinate(this.data.s, Game.INIT_SNAKE_SIZE));
this.data.ws[_c2] = l2;
_ls[_c2] = l2;
}
collisions.push({
clients: [
{client: position.client, word: this.data.pl[position.client]},
{client: key, word: this.data.pl[key]}
],
letters: _ls
});
}
}
}
}
//send collisions
if (collisions.length) {
console.log('game.collision' + " " + JSON.stringify(collisions));
this.io.in(this.room).emit('game.collision', collisions);
return false;
}
//check collisions with letters
var coods_hash = [];
for (var j = 0; j < position.coords.length; j++) {
coods_hash.push(getCoordHash(position.coords[j]));
}
var consumes = [];
for (var key in this.data.ws) {
if (coods_hash.indexOf(key) == -1) {
continue;
}
var l = this.data.ws[key];
// check if letter is in correct order
var consumed = this.data.pl[position.client];
var word = this.data.w;
var nex_l = word[consumed.length];
if (nex_l != l) {
//choosed wrong letter, reset
console.log('game.reset' + " " + JSON.stringify({client: position.client}));
this.io.in(this.room).emit('game.reset', {client: position.client});
return false;
}
else {
//var _c = {};
//_c[key] = l;
delete this.data.ws[key];
consumes.push(key);
this.data.pl[position.client] += l;
}
}
//send consumes
if (consumes.length) {
var r = {};
r['client'] = position.client;
r['letters'] = consumes;
r['word'] = this.data.pl[position.client];
console.log('game.consume' + " " + JSON.stringify(r));
this.io.in(this.room).emit('game.consume', r);
}
//check end game
for (var c in this.data.pl) {
if (this.data.pl[c] == this.data.w) {
//c have von
this.end(c + " has won");
//init.call(this);
return false;
}
}
return true;
}
Game.prototype.end = function (reason) {
console.log('game.over' + " " + reason);
this.io.in(this.room).emit('game.over', {
reason: reason
});
this.emit('ended');
};
function getPlayersInitCoordinates(clients, size) {
var ps = {};
for (var i = 0, l = clients.length; i < l; i++) {
//ps[clients[i].info.nickname] = getSnakeInitCoordinates(i, size, Game.INIT_SNAKE_SIZE);
ps[clients[i].info.nickname] = getSnakeInit(i, size, Game.INIT_SNAKE_SIZE);
}
return ps;
}
function getLetterCoordinates(word, size, players) {
var last = word[word.length - 1];
var letters = '';
for (var i = 0; i < players; i++)
letters += word.substring(0, word.length - 1);
letters += last;
var coords = {};
for (i = 0; i < letters.length; i++) {
var _c = getCoordHash(getRandomCoordinate(size, Game.INIT_SNAKE_SIZE));
//probability of getting non-unique coordinate low enough, so getting new coordinate until it's unique
while (coords.hasOwnProperty(_c))
_c = getCoordHash(getRandomCoordinate(size, Game.INIT_SNAKE_SIZE));
coords[_c] = letters[i];
}
return coords;
}
function getCoordHash(c) {
return c.x + ' ' + c.y;
}
function getRandomCoordinate(map_size, init_snake_size) {
var max = map_size - init_snake_size;
var min = init_snake_size;
return {
x: Math.ceil(Math.random() * (max - min) + min),
y: Math.ceil(Math.random() * (max - min) + min)
};
}
function getSnakeInit(pos, map_size, init_snake_size) {
var start;
switch (pos) {
case 0:
start = {x: 0, y: 0};
break;
case 1:
start = {x: map_size - 1, y: map_size - 1};
break;
case 2:
start = {x: map_size - 1, y: 0};
break;
case 3:
start = {x: 0, y: map_size - 1};
break;
}
return start;
}
function getSnakeInitCoordinates(pos, map_size, init_snake_size) {
var c, start, end, coords = [];
switch (pos) {
case 0:
start = [0, 0];
end = [
0, init_snake_size
];
break;
case 1:
start = [map_size - 1, map_size - 1];
end = [
map_size - init_snake_size - 1, map_size - 1
];
break;
}
if (start[0] == end[0])
c = 0;
else
c = 1;
var nc = (c - 1) * -1;
var _s = Math.min(start[nc], end[nc]);
var _e = Math.max(start[nc], end[nc]);
for (var i = _s; i <= _e; i++) {
var _c = [];
_c[c] = start[c];
_c[nc] = i;
coords.push({x: _c[0], y: _c[1]});
}
return coords;
}
module.exports = Game;