-
Notifications
You must be signed in to change notification settings - Fork 0
/
GameController.js
58 lines (52 loc) · 1.89 KB
/
GameController.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
var GameController = function(socket, environment) {
socket.on('repopulate', function (gameName) {
withPlayerAndGame(gameName, function (playerField, game) {
if (playerField.isReady())
return environment.handleError('Cannot repopulate after you\'re ready.');
playerField.repopulate();
environment.refreshGame(socket, game, playerField);
});
});
socket.on('ready', function (gameName) {
withPlayerAndGame(gameName, function (playerField, game) {
playerField.confirmReady();
refreshAndBroadcastGame(game, playerField);
});
});
socket.on('shoot', function (data) {
withPlayerAndGame(data.name, function (playerField, game) {
if (!game.getCreator().isReady() || game.getVisitor() === null || !game.getVisitor().isReady())
return environment.handleError('The game hasn\'t started.');
if (!game.isActive(playerField))
return environment.handleError('It\'s not your turn.');
try {
game.shoot(data.x, data.y);
} catch (e) {
return environment.handleError(e);
}
handleGameOver(game);
refreshAndBroadcastGame(game, playerField);
});
function handleGameOver(game) {
if (game.isGameOver()) {
environment.removeGame(game);
environment.listGames(socket.broadcast);
}
}
});
function refreshAndBroadcastGame(game, playerField) {
environment.refreshGame(socket, game, playerField);
environment.broadcastGameRoom(game, game.getOpponent(playerField));
}
function withPlayerAndGame(gameName, func) {
environment.withPlayer(function (player) {
var game = environment.getGame(gameName);
if (!game) return;
var playerField = game.getPlayerField(player.name);
if (!playerField)
return environment.handleError('You are not part of this game.');
func(playerField, game);
});
}
};
module.exports = GameController;