-
Notifications
You must be signed in to change notification settings - Fork 6.7k
Expand file tree
/
Copy pathindex.html
More file actions
259 lines (229 loc) · 7.1 KB
/
index.html
File metadata and controls
259 lines (229 loc) · 7.1 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
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
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
<html>
<head>
<script src='/_ah/channel/jsapi'></script>
<style type='text/css'>
body {
font-family: 'Helvetica';
}
#board {
width:152px;
height: 152px;
margin: 20px auto;
}
#display-area {
text-align: center;
}
#this-game {
font-size: 9pt;
}
#winner {
}
table {
border-collapse: collapse;
}
td {
width: 50px;
height: 50px;
font-family: "Helvetica";
font-size: 16pt;
text-align: center;
vertical-align: middle;
margin:0px;
padding: 0px;
}
div.cell {
float: left;
width: 50px;
height: 50px;
border: none;
margin: 0px;
padding: 0px;
}
div.mark {
position: absolute;
top: 15px;
}
div.l {
border-right: 1pt solid black;
}
div.c {
}
div.r {
border-left: 1pt solid black;
}
div.t {
border-bottom: 1pt solid black;
}
div.m {
}
div.b {
border-top: 1pt solid black;
}
</style>
</head>
<body>
<script type='text/javascript'>
var state = {
game_key: '{{ game_key }}',
me: '{{ me }}'
};
updateGame = function() {
for (i = 0; i < 9; i++) {
var square = document.getElementById(i);
square.innerHTML = state.board[i];
if (state.winner != '' && state.winningBoard != '') {
if (state.winningBoard[i] == state.board[i]) {
if (state.winner == state.me) {
square.style.background = "green";
} else {
square.style.background = "red";
}
} else {
square.style.background = "white";
}
}
}
var display = {
'other-player': 'none',
'your-move': 'none',
'their-move': 'none',
'you-won': 'none',
'you-lost': 'none',
'board': 'block',
'this-game': 'block',
};
if (!state.userO || state.userO == '') {
display['other-player'] = 'block';
display['board'] = 'none';
display['this-game'] = 'none';
} else if (state.winner == state.me) {
display['you-won'] = 'block';
} else if (state.winner != '') {
display['you-lost'] = 'block';
} else if (isMyMove()) {
display['your-move'] = 'block';
} else {
display['their-move'] = 'block';
}
for (var label in display) {
document.getElementById(label).style.display = display[label];
}
};
isMyMove = function() {
return (state.winner == "") &&
(state.moveX == (state.userX == state.me));
};
myPiece = function() {
return state.userX == state.me ? 'X' : 'O';
};
// [START open_socket]
sendMessage = function(path, opt_param) {
path += '?g=' + state.game_key;
if (opt_param) {
path += '&' + opt_param;
}
var xhr = new XMLHttpRequest();
xhr.open('POST', path, true);
xhr.send();
};
onOpened = function() {
sendMessage('/opened');
};
// [END open_socket]
// [START update_state]
moveInSquare = function(id) {
if (isMyMove() && state.board[id] == ' ') {
sendMessage('/move', 'i=' + id);
}
};
// [END update_state]
highlightSquare = function(id) {
if (state.winner != "") {
return;
}
for (i = 0; i < 9; i++) {
if (i == id && isMyMove()) {
if (state.board[i] = ' ') {
color = 'lightBlue';
} else {
color = 'lightGrey';
}
} else {
color = 'white';
}
document.getElementById(i).style['background'] = color;
}
};
onMessage = function(m) {
newState = JSON.parse(m.data);
state.board = newState.board || state.board;
state.userX = newState.userX || state.userX;
state.userO = newState.userO || state.userO;
state.moveX = newState.moveX;
state.winner = newState.winner || "";
state.winningBoard = newState.winningBoard || "";
updateGame();
};
openChannel = function() {
var token = '{{ token }}';
// [START create_channel_2]
var channel = new goog.appengine.Channel('{{ token }}');
var handler = {
'onopen': onOpened,
'onmessage': onMessage,
'onerror': function() {},
'onclose': function() {}
};
var socket = channel.open(handler);
// [END create_channel_2]
// socket.onopen = onOpened;
// socket.onmessage = onMessage;
};
initialize = function() {
openChannel();
var i;
for (i = 0; i < 9; i++) {
var square = document.getElementById(i);
square.onmouseover = new Function('highlightSquare(' + i + ')');
square.onclick = new Function('moveInSquare(' + i + ')');
}
onMessage({data: '{{ initial_message }}'});
};
setTimeout(initialize, 100);
</script>
<div id='display-area'>
<h2>Channel-based Tic Tac Toe</h2>
<div id='other-player' style='display:none'>
Waiting for another player to join.<br>
Send them this link to play:<br>
<div id='game-link'><a href='{{ game_link }}'>{{ game_link }}</a></div>
</div>
<div id='your-move' style='display:none'>
Your move! Click a square to place your piece.
</div>
<div id='their-move' style='display:none'>
Waiting for other player to move...
</div>
<div id='you-won'>
You won this game!
</div>
<div id='you-lost'>
You lost this game.
</div>
<div id='board'>
<div class='t l cell'><table><tr><td id='0'></td></tr></td></table></div>
<div class='t c cell'><table><tr><td id='1'></td></tr></td></table></div>
<div class='t r cell'><table><tr><td id='2'></td></tr></td></table></div>
<div class='m l cell'><table><tr><td id='3'></td></tr></td></table></div>
<div class='m c cell'><table><tr><td id='4'></td></tr></td></table></div>
<div class='m r cell'><table><tr><td id='5'></td></tr></td></table></div>
<div class='b l cell'><table><tr><td id='6'></td></tr></td></table></div>
<div class='b c cell'><table><tr><td id='7'></td></tr></td></table></div>
<div class='b r cell'><table><tr><td id='8'></td></tr></td></table></div>
</div>
<div id='this-game' float='top'>
Quick link to this game: <span id='this-game-link'><a href='{{ game_link }}'>{{ game_link }}</a></span>
</div>
</div>
</body>
</html>