forked from MattCollins84/sns-naughts-crosses
-
Notifications
You must be signed in to change notification settings - Fork 0
/
sns.js
73 lines (57 loc) · 1.41 KB
/
sns.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
var sns = new SNSClient("demokey", {
userData: {
gameID: app.gameID
},
userQuery: {
gameID: app.gameID
}
});
sns.on('connected', function() {
app.getAvailablePlayer(sns.id);
})
sns.on('notification', function(n) {
app.logs.unshift(JSON.stringify(n, null, 2));
switch(n.action) {
case "playersync":
app.players = n.data;
app.whoAmI(sns.id);
break;
case "turn":
// mark this square as played
app.squares[n.square][n.player] = true;
app.check.play();
// check for winning combos for this player
var winner = app.checkWinners(app.turn);
if (winner) {
app.status = app.turn;
$('#gameOver').modal('show');
return;
}
var draw = app.checkDraw();
if (draw) {
app.status = "d";
app.turn = (app.turn === "o" ? "x" : "o");
$('#gameOver').modal('show');
return;
}
app.turn = (app.turn === "o" ? "x" : "o");
break;
case "reset":
for(var i in app.squares) {
app.squares[i].x = false;
app.squares[i].o = false;
app.squares[i].win = false;
app.status = "";
}
app.turn = (app.turn === "o" ? "x" : "o");
$('#gameOver').modal('hide');
$('#playerLeft').modal('hide');
break;
case "cancel":
window.location = "/";
break;
case "playerquit":
app.quit(n.id)
break;
}
})