-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathserver.js
More file actions
161 lines (142 loc) · 4.99 KB
/
server.js
File metadata and controls
161 lines (142 loc) · 4.99 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
var events = require('events');
var uuid = require('node-uuid');
var mongojs = require('mongojs');
var server = exports;
STARTING_POSITIONS = {
'CLASSIC': 'dF1,dG1,dJ1,dK1,dE2,dL2,dD3,dM3,dC4,dN4,dB5,dO5,dA6,dP6,dA7,dP7,dA9,dP9,dA10,dP10,dB11,dO11,dC12,dN12,dD13,dM13,dE14,dL14,dF15,dG15,dJ15,dK15,TG7,TH7,TJ7,TG8,TJ8,TG9,TH9,TJ9,RH8'
}
server.backend = function(socket_emitter) {
var self = this;
self.db = mongojs('gamesdb', ['games']);
self.front_end = socket_emitter || new events.EventEmitter();
self.create_game = function(dwarf_controller, troll_controller, callback) {
// callback returns function(game_id)
var game_id = uuid.v1();
self.db.games.save({
game_id: game_id,
dwarf_controller: dwarf_controller || 'human',
troll_controller: troll_controller || 'human',
moves: [],
starting_positions: STARTING_POSITIONS['CLASSIC'],
complete: false
}, function(err, saved) {
if (err) {
callback(null);
} else {
callback(saved);
}
})
}
self.find_game = function(game_id, callback) {
// callback returns function(game)
self.db.games.findOne({game_id: game_id}, function(err, game) {
if (!err)
callback(game);
else
console.error('No game found with', criteria);
})
}
self.turn_to_act = function(moves) {
return (moves.length % 2 == 0 ? 'dwarf' : 'troll')
}
self.query = function(moves, type, callback) {
var start = new Date().getTime();
if (['validate', 'next_move', 'captures'].indexOf(type) < 0)
throw 'query type not permitted.'
var exec = require('child_process').exec;
var child = exec(__dirname + '/console.py ' + type);
child.stdout.on('data', function(data) {
var retval = data.toString('ascii').trim();
console.info(type, moves[moves.length-1], new Date().getTime() - start, 'ms');
if (type == 'validate')
callback(JSON.parse(retval.toLowerCase()));
else
callback(retval);
})
child.stdin.write(moves.join('\n'));
child.stdin.end();
}
self.validate_move = function(data, callback) {
self.find_game(data.game_id, function(game) {
var recorded_moves = game.moves;
recorded_moves.push(data.move);
self.query(recorded_moves, 'validate', function(is_valid) {
if (!is_valid) {
callback(null);
} else {
self.query(recorded_moves, 'captures', function(full_capstring) {
if (full_capstring.indexOf('x') >= 0) {
recorded_moves.pop();
recorded_moves.push(full_capstring)
}
callback(full_capstring || data.move);
})
}
})
})
}
self.front_end.on('connection', function(socket) {
var ip = socket['client']['conn']['remoteAddress'];
console.log('New client connected:', ip);
socket.on('create_game', function(data) {
self.create_game(data.dwarf_controller, data.troll_controller, function(game) {
if (!game) {
console.log('Failed to provision a new gameboard for', ip);
} else {
console.log('Request to provision a new gameboard from', ip);
console.log('Game ID:', game.game_id)
socket.emit('new_game_created', {
game_id: game.game_id,
positions: game.starting_positions
})
}
})
})
socket.on('attempt_move', function(data) {
self.validate_move(data, function(validated_move) {
if (validated_move) {
self.db.games.findAndModify({
query: {game_id: data.game_id},
update: { $push: {moves: validated_move}}
})
console.log('Game:', data.game_id, 'accepted move', validated_move, 'from', ip);
socket.emit('move_accepted', {
game_id: data.game_id,
requested: validated_move
})
} else {
console.log('Game:', data.game_id, 'rejected move', data.move, 'from', ip);
}
})
});
socket.on('wait_for_cpu', function(game_id) {
self.find_game(game_id, function(game) {
if (game[self.turn_to_act(game.moves) + '_controller'] == 'cpu') {
self.query(game.moves, 'next_move', function(next_move) {
self.db.games.findAndModify({
query: {game_id: game_id},
update: { $push: {moves: next_move}}
})
console.log('Game:', game_id, 'responded with', next_move, 'to', ip);
socket.emit('cpu_response', {
game_id: game_id,
responded: next_move
})
})
}
})
})
socket.on('replay_game', function(game_id) {
console.log('Received game replay from', ip, 'for game', game_id);
self.find_game(game_id, function(game) {
console.log('Found game:', game_id, '- Sending game replay.');
socket.emit('replay_game', {
game_id: game_id,
positions: game.starting_positions,
moves: game.moves
})
})
})
})
return self;
}