-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.html
136 lines (117 loc) · 2.65 KB
/
index.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
<html>
<head>
<link rel="stylesheet" href="style.css" type="text/css">
<script type='text/javascript' src='http://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.min.js'></script>
<script type="text/javascript" charset="utf-8">
var ID = 'guest' + Math.floor(Math.random()*101);
var GRID = new Array();
function initGrid(grid) {
if(grid == null) {
return;
}
$('#grid').html('');
for(row=0; row<grid.length; row++) {
for(col=0; col<grid.length;col++) {
if(grid[row][col] == null) {
$('#grid').append('<div class="vacant"></div>');
} else if(grid[row][col] == ID){
$('#grid').append('<div class="self">' + 'YOU' + '</div>');
} else {
$('#grid').append('<div class="occupied">' + grid[row][col] + '</div>');
}
}
}
}
function register() {
jQuery.get(
"/register",
{ id : ID },
function (data) {
GRID = data.grid;
initGrid(GRID);
},
"json"
);
}
function move(direction) {
if(direction) {
jQuery.get(
"/move",
{
id : ID,
direction : direction
},
function (data) {
GRID = data.grid;
initGrid(GRID);
},
"json"
);
}
}
function longPoll(data) {
if(data != undefined) {
GRID = data.grid;
initGrid(GRID);
}
$.ajax({
cache: false,
type: "GET",
url: "/recv",
dataType: "json",
error: function() {
alert('long poll error...trying again');
setTimeout(longPoll, 30*1000);
},
success: function (data) {
//if everything went well, begin another request immediately
//the server will take a long time to respond
//how long? well, it will wait until there is another message
//and then it will return it to us and close the connection.
//since the connection is closed when we get data, we longPoll again
longPoll(data);
}
});
}
//handlers in here
$(document).ready(function() {
$('#start').click(function() {
register();
//bind keystrokes
$(document).keydown(function(event) {
var direction = '';
if (event.keyCode == '37') {
direction = 'left';
}
else if (event.keyCode == '38') {
direction = 'up';
}
else if (event.keyCode == '39') {
direction = 'right';
}
else if (event.keyCode == '40') {
direction = 'down';
}
if(direction != '') {
move(direction);
}
});
$('#join-control').hide();
longPoll();
});
});
$(window).unload(function () {
jQuery.get("/part", {id: ID}, function (data) { }, "json");
});
</script>
</head>
<body>
<div id="wrapper">
<div id="join-control">
<a href="#" id="start">start</a>
</div>
<div id="grid">
</div>
</div>
</body>
</html>