-
Notifications
You must be signed in to change notification settings - Fork 0
/
index-template
221 lines (195 loc) · 5.84 KB
/
index-template
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
<html>
<head>
<script src='/_ah/channel/jsapi'></script>
<style type='text/css'>
.row {
float: top;
}
.board {
margin: 20px;
}
td {
width: 50px;
height: 50px;
font-family: "Helvetica";
font-size: 16pt;
border: none;
margin: 0px;
padding: 0px;
text-align: center;
vertical-align: middle;
}
td.ul {
border-bottom: 1pt solid black;
border-right: 1pt solid black;
}
td.um {
border-bottom: 1pt solid black;
}
td.ur {
border-bottom: 1pt solid black;
border-left: 1pt solid black;
}
td.ml {
border-right: 1px solid black;
}
td.mr {
border-left: 1px solid black;
}
td.bl {
border-top: 1pt solid black;
border-right: 1pt solid black;
}
td.bm {
border-top: 1pt solid black;
}
td.br {
border-top: 1pt solid black;
border-left: 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]) {
square.style.background = "red";
}
}
}
var display = {
'other-player': 'none',
'your-move': 'none',
'their-move': 'none'
};
if (!state.userO || state.userO == '') {
display['other-player'] = 'block';
var path = window.location.href + '?g={{ game_key }}';
var link = '<a href="' + path + '">' + path + '</a">';
document.getElementById('other-player').innerHTML = link;
} 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';
}
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();
};
moveInSquare = function(id) {
if (isMyMove() && state.board[id] == ' ') {
sendMessage('/move', 'i=' + id);
}
}
highlightSquare = function(id) {
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'] = color;
}
}
onOpened = function() {
sendMessage('/opened');
};
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 == 'true';
state.winner = newState.winner || "";
state.winningBoard = newState.winningBoard || "";
updateGame();
}
openChannel = function() {
var token = '{{ token }}';
var channel = new goog.appengine.Channel(token);
var handler = {
'onopen': onOpened,
'onmessage': onMessage,
'onerror': function() {},
'onclose': function() {}
};
var socket = channel.open(handler);
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'>
</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='board'>
<table style='border-spacing: 0px 0px'>
<tr>
<td id='0' class='ul'></td>
<td id='1' class='um'></td>
<td id='2' class='ur'></td>
</tr>
<tr>
<td id='3' class='ml'></td>
<td id='4' class='mm'></td>
<td id='5' class='mr'></td>
</tr>
<tr>
<td id='6' class='bl'></td>
<td id='7' class='bm'></td>
<td id='8' class='br'></td>
</tr>
</table>
</div>
</div>
</body>
</html>