forked from morozd/blk-game
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
412 lines (371 loc) · 11 KB
/
index.js
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
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
/**
* Copyright 2012 Google, Inc. All Rights Reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
var hasLocalSaving = false;
function setupIndexPage() {
setupSupportList();
setupUserName();
var localLink = document.getElementById('local-link');
localLink.onclick = function(e) {
e.preventDefault();
if (!hasLocalSaving) {
alert('NOTE: your browser does not support the File System API or IndexedDB: your map will not be saved!');
}
connectToServer('local://blk-server-0');
};
var joinLink = document.getElementById('server-connect-join');
joinLink.onclick = function(e) {
e.preventDefault();
var address = document.getElementById('server-connect-input');
if (!address.value.length) {
return;
}
connectToServer(address.value);
};
window.setInterval(function() {
updateServers();
}, 30 * 1000);
updateServers();
};
function testWebGL() {
var canvas = document.createElement('canvas');
if (canvas) {
var names = ['webgl', 'experimental-webgl'];
for (var n = 0; n < names.length; n++) {
var gl = canvas.getContext(names[n]);
if (gl) {
return true;
}
}
}
return false;
}
/**
* Gets a value indicating, roughly, whether the game is playable.
* @return {boolean} True if the game is (probably) playable.
*/
function isPlayable() {
return testWebGL();
}
/**
* Generates a list of supported features and warnings.
*/
function setupSupportList() {
var isChrome = window.navigator.userAgent.indexOf('Chrome') != -1;
var isFirefox = window.navigator.userAgent.indexOf('Firefox') != -1;
function testWebAudio() {
return !!window.webkitAudioContext || !!window.AudioContext;
}
function testMouseLock() {
return (
document.pointerLockElement !== undefined ||
document.mozPointerLockElement !== undefined ||
document.webkitPointerLockElement !== undefined);
}
function testWebSockets() {
return !!window.WebSocket;
}
function testFileSystem() {
return !!window.webkitStorageInfo && !!window.webkitRequestFileSystem;
}
function testIndexedDb() {
var indexedDB =
window.indexedDB ||
window.mozIndexedDB ||
window.webkitIndexedDB ||
window.msIndexedDB;
if (isFirefox) {
// Not supported in workers yet.
return false;
}
return !!indexedDB;
}
function testWebWorker() {
return !!window.Worker;
}
function testSharedWorker() {
// Only supported in Chrome
if (isChrome) {
return !!window.SharedWorker;
}
return false;
}
function addStatusLine(ul, feature, critical, supportFunc,
failureInfo, successInfo) {
var supported = false;
try {
supported = supportFunc();
} catch (e) {
if (window.console) {
window.console.log(e);
}
}
var li = document.createElement('li');
li.appendChild(document.createTextNode(feature + ': '));
var statusSpan = document.createElement('span');
if (supported) {
statusSpan.className = 'status-info';
statusSpan.appendChild(document.createTextNode('supported'));
li.appendChild(statusSpan);
if (successInfo) {
var infoSpan = document.createElement('span');
infoSpan.className = 'info';
infoSpan.appendChild(document.createTextNode(
' (' + successInfo + ')'));
li.appendChild(infoSpan);
}
} else {
if (critical) {
var statusSpan = document.createElement('span');
statusSpan.className = 'status-error';
statusSpan.appendChild(document.createTextNode('unsupported'));
li.appendChild(statusSpan);
} else {
var statusSpan = document.createElement('span');
statusSpan.className = 'status-warning';
statusSpan.appendChild(document.createTextNode('unsupported'));
li.appendChild(statusSpan);
}
if (failureInfo) {
var infoSpan = document.createElement('span');
infoSpan.className = 'info';
infoSpan.appendChild(document.createTextNode(
' (' + failureInfo + ')'));
li.appendChild(infoSpan);
}
}
ul.appendChild(li);
}
var parentElement = document.getElementById('supportList');
var ul = document.createElement('ul');
parentElement.appendChild(ul);
addStatusLine(ul,
'WebGL', true,
testWebGL,
'nothing will work!');
addStatusLine(ul,
'WebSockets', false,
testWebSockets,
'no multiplayer');
addStatusLine(ul,
'Web Workers', false,
testWebWorker,
'no singleplayer');
// addStatusLine(ul,
// 'SharedWorker', false,
// testSharedWorker,
// 'no local multiplayer');
if (testIndexedDb()) {
addStatusLine(ul,
'IndexedDB', false,
testIndexedDb,
'no local saving');
hasLocalSaving = true;
} else if (testFileSystem()) {
addStatusLine(ul,
'File System', false,
testFileSystem,
'no local saving');
hasLocalSaving = true;
} else if (isFirefox) {
// https://bugzilla.mozilla.org/show_bug.cgi?id=701634
addStatusLine(ul,
'IndexedDB', false,
testIndexedDb,
'no local saving');
} else {
addStatusLine(ul,
'FileSystem/IndexedDB', false,
function() { return false; },
'no local saving');
}
addStatusLine(ul,
'Web Audio API', false,
testWebAudio,
'no sounds');
addStatusLine(ul,
'Mouse Lock', false,
testMouseLock,
undefined,
document.mozPointerLockElement !== undefined ?
'fullscreen only' : null);
}
/**
* Sets up the user name box.
*/
function setupUserName() {
// This will ensure that the name is valid and clear bad cookies.
loadUserName();
saveUserName();
// Save on change.
var nameBox = document.getElementById('name');
nameBox.addEventListener('change', function() {
saveUserName();
}, false);
};
/**
* Loads teh user name from the cookie.
*/
function loadUserName() {
var nameBox = document.getElementById('name');
var value = null;
var parts = document.cookie.split(';');
for (var n = 0; n < parts.length; n++) {
var part = parts[n];
part = part.replace(/^[\s\xa0]+|[\s\xa0]+$/g, '');
if (part.indexOf('s_un=') == 0) {
value = part.substr(part.indexOf('=') + 1);
value = decodeURIComponent(value.replace(/\+/g, ' '));
break;
}
}
if (value && value.length) {
nameBox.value = value;
} else {
nameBox.value = 'Some Player ' + Math.floor(Math.random() * 100);
}
}
/**
* Saves the user name to the cookie.
*/
function saveUserName() {
var nameBox = document.getElementById('name');
// Not very strong verification here - the game will do it.
var value = nameBox.value;
if (!value || !value.length) {
value = 'User';
}
var maxAge = 60 * 60 * 24 * 30 * 12;
var futureDate = new Date(+(new Date()) + maxAge * 1000);
document.cookie =
's_un=' + encodeURIComponent(value) +
'; path=/; expires=' + futureDate.toUTCString() + ';';
}
/**
* Resets the local filesystem for the current domain, clearing the maps.
*/
function resetLocalStorage() {
// Clear IndexedDB.
var indexedDB =
window.indexedDB ||
window.mozIndexedDB ||
window.webkitIndexedDB ||
window.msIndexedDB;
if (indexedDB) {
// Always hardcoded.
indexedDB.deleteDatabase('/maps/map01/');
}
// Clear filesystem.
function resetFS(fs) {
var reader = fs.root.createReader();
function removeMore(entries) {
for (var n = 0; n < entries.length; n++) {
var entry = entries[n];
if (entry.isDirectory) {
entry.removeRecursively(function() {});
}
}
if (entries.length) {
reader.readEntries(removeMore);
} else {
alert('Done!');
}
};
reader.readEntries(removeMore);
};
var requestFileSystem =
window.requestFileSystem || window.webkitRequestFileSystem;
if (requestFileSystem) {
requestFileSystem.call(
window, window.PERSISTENT, 1, resetFS);
}
}
/**
* Updates the server list.
*/
function updateServers() {
var serverList = document.getElementById('server-list');
var xhr = new XMLHttpRequest();
xhr.addEventListener('readystatechange', function() {
if (xhr.readyState == 4) {
if (xhr.status == 200) {
// Succeeded
while (serverList.firstChild) {
serverList.removeChild(serverList.firstChild);
}
var table = document.createElement('table');
var json = JSON.parse(xhr.responseText);
if (DEV_MODE) {
addServerRow(table, {
'endpoint': 'ws://localhost:1337',
'server_name': '<localhost>',
'user_count': '?',
'user_max': '?'
});
}
for (var n = 0; n < json.length; n++) {
var result = json[n];
var serverInfo = result['server_info'];
addServerRow(table, serverInfo);
}
if (!json.length) {
serverList.innerText = '\nNo public servers online! Try again later!'//' or ';
// var startLink = document.createElement('a');
// startLink.href = 'a';
// startLink.innerText = 'start your own!';
// serverList.appendChild(startLink);
} else {
serverList.appendChild(table);
}
} else {
// Failed
serverList.innerText = '\nUnable to fetch server list!'
}
}
}, true);
xhr.open('GET', SERVER_QUERY_URL);
xhr.send();
function addServerRow(table, serverInfo) {
var endpoint = serverInfo['endpoint'];
var row = document.createElement('tr');
var c1 = document.createElement('td');
c1.innerHTML = '<li><a href="" target="_blank">' + serverInfo['server_name'] + '</a>';
row.appendChild(c1);
var c2 = document.createElement('td');
c2.innerText = serverInfo['user_count'] + '/' + serverInfo['user_max'];
row.appendChild(c2);
// // TODO(benvanik): flag
// var c3 = document.createElement('td');
// c3.innerText = serverInfo['server_location'];
// row.appendChild(c3);
table.appendChild(row);
c1.firstChild.onclick = function(e) {
e.preventDefault();
// Disclaimer
alert('Warning! This server is not run by or affiliated with Google and may contain scary things like trolls!');
connectToServer(endpoint);
};
}
}
/**
* Launches the game by connecting to the given server.
* @param {string} address Server IP.
*/
function connectToServer(address) {
_gaq.push(['_trackEvent', 'index',
'connect_to_server', address]);
var url = GAME_URL + '?host=' + address;
window.open(url, '_blank');
}