-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathtictac.js
67 lines (52 loc) · 1.55 KB
/
tictac.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
"use strict";
var Player = require('./player').Player;
var Game = require('./game').Game;
var tictactoe = (function() {
var games = [];
function getGameInstance() {
for (var i = 0; i < games.length; i++) {
if (games[i].hasFreeSlot()) {
return games[i];
}
}
var game = new Game();
games.push(game);
return game;
}
var tictactoe = function() {
};
tictactoe.prototype.initConnection = function(connection) {
console.log('client connection');
connection.on('text', manageMessage);
connection.on('close', function(){manageClose(this);});
var player = new Player(connection);
var game = getGameInstance();
var accepted = game.newPlayer(player);
if (!accepted) {
connection.close();
}
};
function manageMessage(message) {
console.log(message);
message = JSON.parse(message);
var game = null;
switch (message.type) {
case 'move':
game = this.player.game;
game.processMove(message.coords, this.player);
break;
case 'newgame':
game = this.player.game;
game.start();
break;
default:
break;
}
}
function manageClose(connection) {
console.log('connection close');
connection.player.game.onPlayerQuit(connection.player);
}
return tictactoe;
})();
exports.tictactoe = tictactoe;