-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathmultiplayer.js
193 lines (183 loc) · 5 KB
/
multiplayer.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
var waitingRef = new Firebase('https://chess.firebaseio.com/waitinglist');
var playingRef = new Firebase('https://chess.firebaseio.com/playinglist');
var gameRef;
var game;
var freeze = false;
var connected = false;
function nullToNegOne(mypieces) {
for (a in mypieces)
for (b in mypieces) {
if (mypieces[a][b].color == null)
mypieces[a][b].color = -1;
if (mypieces[a][b].piece == null)
mypieces[a][b].piece = -1;
}
return mypieces;
}
function negOneToNull(mypieces) {
for (a in mypieces)
for (b in mypieces) {
if (mypieces[a][b].color == -1)
mypieces[a][b].color = null;
if (mypieces[a][b].piece == -1)
mypieces[a][b].piece = null;
}
return mypieces;
}
function blinkTitle(alttitle) {
var timeoutid;
timeoutid = setInterval("document.title == 'Play Chess' ? document.title = '"+alttitle+"' : document.title = 'Play Chess';", 750);
window.addEventListener('mousemove', function(e) {
clearInterval(timeoutid);
document.title = 'Play Chess';
});
}
function endGame() {
game = undefined;
gameRef = undefined;
freeze = false;
connected = false;
resetBoard();
document.getElementById('connectednow').style.visibility = "hidden";
document.getElementById('notconnected').style.visibility = "";
waitingRef.on('child_added', addToGameList);
waitingRef.on('child_removed', removeFromGameList);
document.getElementById('gamelist').style.visibility = "";
}
function changeGame(snapshot) {
var data = snapshot.val();
if (!data) {
if (game.checkmate)
alert("Game Over.")
else
alert("Something went wrong. Maybe your partner disconnected.");
endGame();
return;
}
var notfrozen = true;
if (freeze && stack.length != 0)
freeze = false;
else
notfrozen = false;
if (data.pieces != pieces) {
pieces = negOneToNull(data.pieces);
stack.push(pieces);
}
if (data.enpassant != enpassant)
enpassant = data.enpassant;
game = data;
loadCanvas();
if (notfrozen) {
checkAlerts(data.oppcolor, data.checkmate, data.stalemate, data.checkvar);
blinkTitle('Opponent Moved');
}
if (game.checkmate && game.checkvar[game.oppcolor]) {
gameRef.off('value', changeGame);
gameRef.remove();
endGame();
}
}
function playerSetup(playwhite) {
waitingRef.off('child_added', addToGameList);
waitingRef.off('child_removed', removeFromGameList);
document.getElementById('gamelist').style.visibility = "hidden";
document.getElementById('notconnected').style.visibility = "hidden";
if (!playwhite) {
document.getElementById("colour").innerHTML = "black";
white = false;
freeze = true;
} else {
document.getElementById("colour").innerHTML = "white";
white = true;
freeze = false;
stack.push(pieces);
}
document.getElementById("connectednow").style.display = "";
}
function playerTwoMove(snapshot) {
var data = snapshot.val();
if (!data) {
alert("Seems "+game.name+" got bored and left");
endGame();
return;
}
if (data.newloc) {
gameRef.off('value', playerTwoMove);
gameRef.remove();
gameRef = new Firebase(data.newloc);
connected = true;
gameRef.on('value', changeGame);
gameRef.onDisconnect().remove();
playerSetup(!game.playwhite);
}
}
function playerOneMove(snapshot) {
var data = snapshot.val();
if (game.players == 1)
if (data.players == 2) {
game = data;
var playingGameRef = playingRef.push(game);
data.newloc = playingGameRef.toString();
gameRef.off('value', playerOneMove);
gameRef.set(data);
gameRef = playingGameRef;
connected = true;
gameRef.on('value', changeGame);
gameRef.onDisconnect().remove();
playerSetup(game.playwhite);
blinkTitle('Opponent joined');
}
}
function startGame(playwhite) {
var name = document.getElementById('playername').value;
if (!name) {
alert("You need to give a name");
return;
}
if (stack.length != 0) {
resetBoard();
}
game = new Object();
white = playwhite
game.checkvar = new Object();
game.checkvar["white"] = false;
game.checkvar["black"] = false;
game.checkmate = false;
game.playwhite = white;
nullToNegOne(pieces);
game.pieces = pieces;
negOneToNull(pieces);
game.enpassant = enpassant;
game.players = 1;
game.name = name;
game.newloc = false;
if (white)
game.oppcolor = "black";
else
game.oppcolor = "white";
game.stalemate = false;
gameRef = waitingRef.push(game);
gameRef.on('value', playerOneMove);
gameRef.onDisconnect().remove();
}
function connectGame(refstr) {
if (gameRef) {
gameRef.off('value', playerOneMove);
gameRef.remove();
}
gameRef = new Firebase(refstr);
var gamereq = new XMLHttpRequest();
gamereq.open("GET", refstr+".json");
gamereq.onreadystatechange = function(e) {
if (gamereq.readyState == 4) {
game = JSON.parse(gamereq.response);
game.players = 2;
if (game.playwhite)
white = false;
gameRef.set(game);
gameRef.on('value', playerTwoMove);
gameRef.onDisconnect().remove();
}
}
gamereq.send()
}