-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgame.html
322 lines (277 loc) · 9.88 KB
/
game.html
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
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1">
<!-- Latest compiled and minified CSS -->
<link rel="stylesheet" href="http://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css">
<!-- jQuery library -->
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.1.0/jquery.min.js"></script>
<!-- Latest compiled JavaScript -->
<script src="http://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>
<script type="text/javascript">
var width = 100;
var height = 100;
var bsize = 20;
var midsize = bsize/2;
var gameboard = null;
var play = false;
var curr_players = [];
function drawLine(ctx, x1, y1, x2, y2) {
ctx.moveTo(x1, y1);
ctx.lineTo(x2, y2);
}
function drawCallback(data) {
gameboard = data;
drawGameboard(data);
}
function drawGameboard(data) {
var canvas = document.getElementById('game_board');
width = canvas.width = data[0]*bsize;
height = canvas.height = data[1]*bsize;
if (canvas.getContext){
var ctx = canvas.getContext('2d');
ctx.clearRect(0,0,width*bsize,height*bsize);
ctx.beginPath()
for(x = 0; x < data[2].length; x++)
{
for(y = 0; y < data[2][x].length; y++)
{
// north
if ( !data[2][x][y]['n'] )
{
drawLine(ctx, x*bsize, y*bsize, (x+1)*bsize, y*bsize);
}
// south
if ( !data[2][x][y]['s'] )
{
drawLine(ctx, x*bsize, (y+1)*bsize, (x+1)*bsize, (y+1)*bsize);
}
// east
if ( !data[2][x][y]['e'] )
{
drawLine(ctx, (x+1)*bsize, y*bsize, (x+1)*bsize, (y+1)*bsize);
}
// west
if ( !data[2][x][y]['w'] )
{
drawLine(ctx, x*bsize, y*bsize, x*bsize, (y+1)*bsize);
}
}
}
ctx.stroke();
ctx.closePath();
}
}
function getRandomColor() {
var letters = '0123456789ABCDEF';
var color = '#';
for (var i = 0; i < 6; i++ ) {
color += letters[Math.floor(Math.random() * 16)];
}
return color;
}
function playersCallback(data) {
if(0 == width || 0 == height) return;
var scores = document.getElementById('scorerow');
for (i = 0; i < data.length; i++)
{
var player = data[i];
var div = document.getElementById(player.name+"text");
if ( !div )
{
// Create the div to show the data
var row = document.createElement('div');
row.className = 'row playerdata';
row.id = player.name;
row.style.color = getRandomColor();
scores.appendChild(row);
div = document.createElement('div');
row.appendChild(div);
div.className = "col-sm-9";
div.id = player.name+"text";
div.appendChild(document.createTextNode(player.name));
// add the code button
var div2 = document.createElement('div');
row.appendChild(div2);
div2.className = "col-sm-3";
var button = document.createElement('button');
button.className = "btn btn-info";
button.innerHTML = "Code";
button.onclick = function(){
var data = {'player':player.name};
$.post("/code", JSON.stringify(data), show_code);
};
div2.appendChild(button);
}
player_str = player.name + ":" + player.distance + ":" + player.hitpoints + ":" + player.kills;
div.replaceChild(document.createTextNode(player_str), div.childNodes[0]);
}
// Delete any non-existing players
for (i = 0; i < $('.playerdata').length; i++)
{
var found = false;
for (j = 0; j < data.length; j++)
{
if ( $('.playerdata')[i].id == data[j].name )
{
found = true;
break;
}
}
if ( !found )
{
$('.playerdata')[i].parentElement.removeChild($('.playerdata')[i]);
}
}
var canvas = document.getElementById('game_board');
if (canvas.getContext) {
var ctx = canvas.getContext('2d');
ctx.clearRect(0,0,width*bsize,height*bsize);
drawGameboard(gameboard);
ctx.beginPath()
for (i = 0; i < data.length; i++)
{
player = data[i];
x = player['x']*bsize;
y = player['y']*bsize;
var player_div = document.getElementById(player.name);
ctx.fillStyle = player_div.style.color;
ctx.fillRect(x + (bsize/10), y + (bsize/10), bsize * .7, bsize * .7);
ctx.fillStyle = "#000000";
switch(player['dir'])
{
case 'n':
drawLine(ctx,
x + midsize, y + midsize,
x + midsize, y);
drawLine(ctx,
x + midsize + 1, y + midsize,
x + midsize + 1, y);
break;
case 'e':
drawLine(ctx,
x + midsize, y + midsize,
x + bsize, y + midsize);
drawLine(ctx,
x + midsize, y + midsize + 1,
x + bsize, y + midsize + 1);
break;
case 'w':
drawLine(ctx,
x + midsize, y + midsize,
x, y + midsize);
drawLine(ctx,
x + midsize, y + midsize + 1,
x, y + midsize + 1);
break;
case 's':
drawLine(ctx,
x + midsize, y + midsize,
x + midsize, y + bsize);
drawLine(ctx,
x + midsize + 1, y + midsize,
x + midsize + 1, y + bsize);
break;
}
}
ctx.stroke();
ctx.closePath();
}
// Set up for the next call
if (play)
{
setTimeoutCallback();
}
}
function reset_game() {
$.get("/create_game", drawCallback);
var scores = document.getElementById('scorerow');
scores.innerHTML = "";
curr_players = [];
play = false;
}
function add_player() {
$.get("/add_player", playersCallback);
}
function add_rand_player() {
$.get("/add_rand_player", playersCallback);
}
function show_code(data) {
var code = document.getElementById("robo-code");
code.innerHTML = data;
}
var timeout = null;
function setTimeoutCallback() {
if ( timeout )
{
// This is so we cant have multiple timeout going at one. It gets
// confusing otherwise. Things speed up drastically.
clearTimeout(timeout);
}
timeout = setTimeout(function() {
$.get("/step");
$.getJSON("/players", playersCallback);
}, 100);
}
$(document).ready(function(){
// Configure button callbacks. Really? I can't just do this with
// an 'onclick' param?
$("#reset_game").click(reset_game);
$("#add_player").click(add_player);
$("#add_rand_player").click(add_rand_player);
$('#playButton').click(function () {
if ( !play )
{
play = true;
setTimeoutCallback();
}
});
$('#pauseButton').click(function () {
play = false;
});
});
</script>
<style>
.pValue
{
border: 2px solid #a0a0a0;
border-radius: 10px;
-moz-box-sizing: border-box;
-webkit-box-sizing: border-box;
box-sizing: border-box;
text-align: center;
font: bold 28pt Calibri;
}
</style>
</head>
<body>
<div class="container">
<div class="row">
<div class="col-sm-8">
<canvas id="game_board"></canvas>
</div>
<div class="col-sm-4" >
<div id="scorerow"></div>
</div>
</div>
<div class="row">
<button id="playButton" type="button" class="btn btn-default">
<span class="glyphicon glyphicon-play"></span>
</button>
<button id="pauseButton" type="button" class="btn btn-default">
<span class="glyphicon glyphicon-pause"></span>
</button>
</div>
<div class="row">
<button id="reset_game" type="button" class="btn btn-primary">Reset game</button>
<button id="add_player" type="button" class="btn btn-primary">Add Player</button>
<button id="add_rand_player" type="button" class="btn btn-primary">Add Random Player</button>
</div>
<div class="row">
<pre id="robo-code" class="pre-scrollable"> </pre>
</div>
</div>
</body>
</html>