forked from zheplusplus/sgs
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.html
165 lines (149 loc) · 4.25 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
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
<html>
<head><title>SGS</title></head>
<meta http-equiv='Content-type' content='text/html; charset=UTF-8'>
<script src='/static/event_parse.js'></script>
<script src='/static/hint_parse.js'></script>
<script src='/static/strings.js'></script>
<script src='/static/views.js'></script>
<script>
var BEAT_INTERVAL = 2000;
var status_heartbeat = null;
var hint_heartbeat = null;
var events_heartbeat = null;
function post_data(data, url, on_sent) {
var req = null;
try {
if (window.XMLHttpRequest) {
req = new XMLHttpRequest();
} else {
req = new ActiveXObject("Microsoft.XMLHTTP");
}
} catch (e) {
alert('Browser not support AJAX!');
return;
}
req.open('POST', url, true);
req.onreadystatechange = function() {
if (req.readyState == 4) {
on_sent(eval('(' + req.responseText + ')'));
}
};
req.send(data);
}
function post_act(data) {
data['token'] = token;
return post_data(JSON.stringify(data), '/act', function(result) {
if (result['code'] != 200) {
alert('SEND:' + JSON.stringify(data));
alert('REASON:' + result['reason']);
} else {
clearTimeout(events_heartbeat);
clearTimeout(hint_heartbeat);
getEvents();
getHint();
}
});
}
var eventlist = new SGS_EventParser();
var game = null;
var token = localStorage.getItem('token');
function joinGame() {
post_data('', '/ctrl/add', function(result) {
if (result['code'] == 200) {
token = result['token'];
localStorage.setItem('token', token);
enter();
} else {
msgPane.innerHTML = result['reason'];
}
});
}
function updateRoomPlayers(count, is_host) {
document.getElementById('players_count').innerHTML = count;
document.getElementById('start_button').disabled = (0 == is_host);
}
function enter() {
joinPane.style.visibility = 'hidden';
startPane.style.visibility = 'visible';
getStatus();
}
function exitGame() {
joinPane.style.visibility = 'visible';
startPane.style.visibility = 'hidden';
post_data(token, '/ctrl/exit', function(r) {});
token = null;
clearTimeout(status_heartbeat);
clearTimeout(hint_heartbeat);
clearTimeout(events_heartbeat);
}
function startGame() {
post_data(token, '/ctrl/start', function(result) {
if (result['code'] == 200) {
started();
} else {
msgPane.innerHTML = result['reason'];
}
});
}
function getStatus() {
post_data(token, '/info/status', function(result) {
if (result['started'] == 0) {
updateRoomPlayers(result['players'], result['host']);
status_heartbeat = setTimeout(enter, BEAT_INTERVAL);
} else {
started();
}
});
}
function getEvents() {
var request_body = {
'token': token,
'previous event id': eventlist.prevId(),
};
post_data(JSON.stringify(request_body), '/info/events', function(result) {
eventlist.append(result, game);
events_heartbeat = setTimeout(getEvents, BEAT_INTERVAL);
});
}
function getHint() {
post_data(token, '/info/hint', function(result) {
game.hint(result);
hint_heartbeat = setTimeout(getHint, BEAT_INTERVAL);
});
}
function started() {
startPane.style.visibility = 'hidden';
gamePane.style.visibility = 'visible';
clearTimeout(status_heartbeat);
getEvents();
getHint();
}
var joinPane = null;
var startPane = null;
var gamePane = null;
var msgPane = null;
function init() {
paras = document.getElementsByTagName('p');
joinPane = paras[0];
startPane = paras[1];
gamePane = paras[2];
msgPane = paras[3];
startPane.style.visibility = 'hidden';
gamePane.style.visibility = 'hidden';
game = new Game(gamePane);
post_data(token, '/info/checktoken', function(result) {
if (result['in'] == 1) {
enter();
}
});
}
</script>
<body onload='init()'>
<p><button onclick='joinGame()'>Join</button></p>
<p>
<button id='start_button' onclick='startGame()'>Start</button>
<button onclick='exitGame()'>Exit</button>
<span id='players_count'></span> player(s) in the room now.
</p>
<p></p>
<p></p>