forked from id-Software/DOOM
-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathindex.html
503 lines (433 loc) · 12.5 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
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
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
<html>
<div id="headeroverlay">
DOOM@EDGE
Player Name <input id="player_name" value="" />
<button onClick="showControlsOverlay();">Show Controls</button>
</div>
<div id="waiting">
Too Many Players; please wait.
</div>
<!--Player Index <input type="number" id="player_index" value="0" step="1" min="0" max="3" /> -->
<!--
Key bindings
{"key_right",&key_right, 'D'},
{"key_left",&key_left, 'A'},
{"key_up",&key_up, 'W'},
{"key_down",&key_down, 'S'},
{"key_strafeleft",&key_strafeleft, 'Q'},
{"key_straferight",&key_straferight, 'E'},
{"key_fire",&key_fire, 'F'},
{"key_use",&key_use, 'U'},
{"key_strafe",&key_strafe, KEY_RALT},
{"key_speed",&key_speed, KEY_RSHIFT},
-->
<div id="overlay" onclick="removeControlsOverlay()">
<div id="text">
W to move forward
S to move backward
A to turn left
D to turn right
Q to strafe left
E to strafe right
F to fire
U to use (and respawn)
</div>
</div>
<canvas id="framebuffer"></canvas>
<div>
<table id="sessions_table"></table>
</div>
<div id="version">
</div>
<style>
#version {
color: white;
}
body {
margin: 0;
}
div {
white-space: pre-wrap;
}
canvas {
width: 100%;
max-width: 960px;
margin: 0 auto;
}
table {
table-layout: fixed;
width: 40%;
border-collapse: collapse;
border: 2px solid purple;
}
thead th:nth-child(1) {
width: 10%;
}
thead th:nth-child(2) {
width: 70%;
}
thead th:nth-child(3) {
width: 20%;
}
th, td {
padding: 5px;
}
tbody td {
text-align: center;
}
thead td {
text-align: center;
}
table td, table th {
white-space: pre-wrap;
border: 1px solid black;
}
#overlay {
position: fixed;
display: none;
width: 100%;
height: 100%;
top: 0;
left: 0;
right: 0;
bottom: 0;
background-color: rgba(0,0,0,0.5);
z-index: 2;
cursor: pointer;
}
#text{
position: absolute;
top: 50%;
left: 50%;
font-size: 20px;
color: white;
transform: translate(-50%,-50%);
-ms-transform: translate(-50%,-50%);
}
</style>
<script>
var Game = {
stopMain: {},
started: false,
sessionId: -1,
playerId: -1,
playerIndex: 0,
globalpop: false,
pop: "",
host: "",
startpost: true,
keys: {},
frames: {},
numframes: 0,
lastTick: 0,
lastRender: 0,
countdown: 0,
version: 0,
maxposttimes: 100,
posttimes: [100],
numposttimes: 0,
postindex: 0,
postfull: false,
};
function removeControlsOverlay() {
document.getElementById("overlay").style.display = "none";
};
function showControlsOverlay() {
document.getElementById("overlay").style.display = "block";
}
function AddPostTime(time) {
if (!Game.postfull) {
Game.numposttimes++;
}
if (Game.numposttimes == Game.maxposttimes) {
Game.postfull = true;
}
Game.postindex = (Game.postindex + 1) % Game.numposttimes;
Game.posttimes[Game.postindex] = time;
}
function GetAveragePostTime() {
var total = 0;
for (i=0;i<Game.numposttimes;++i) {
total += Game.posttimes[i];
}
return total / Game.numposttimes;
}
function GeneratePostBody() {
var numevents = Object.keys(Game.keys).length;
var body = new ArrayBuffer(13 + numevents + 4);
var view = new DataView(body);
view.setUint32(0,Game.sessionId);
view.setUint8(4,Game.globalpop);
view.setUint32(5, Game.playerIndex);
view.setUint32(9, numevents);
var index=0;
for (var key in Game.keys) {
view.setUint8(13+index,key);
index++;
}
var num_requested_frames = 1; // adaptive frame interpolation isn't helping a lot yet.
view.setUint32(13+index,num_requested_frames);
return body;
}
// helper in case we need to download data for inspection
function download(filename, data) {
var element = document.createElement('a');
element.setAttribute('href', 'data:binary/octet-stream,' + data);
element.setAttribute('download', filename);
element.style.display = 'none';
document.body.appendChild(element);
element.click();
document.body.removeChild(element);
}
document.addEventListener('keydown', (event) => {
Game.keys[event.keyCode] = 1;
}, false);
document.addEventListener('keyup', (event) => {
delete Game.keys[event.keyCode];
}, false);
function RenderFrame() {
const rawcanvas = document.getElementById('framebuffer');
rawcanvas.width = 960;
rawcanvas.height = 600;
const ctx = rawcanvas.getContext('2d');
var imageData = ctx.getImageData(0, 0, 100, 100);
var data = new Uint8ClampedArray(4 * 320 * 200);
var palette = new Uint8ClampedArray(256*3);
for (var i = 0; i < 256 * 3; ++i) {
palette[i] = Game.frames[0][i];
}
//for (var i = 256*3; i < fb.length; i += 4) {
fb_index = 256*3;
for (var i = 0; i < data.length; i += 4) {
data[i] = palette[Game.frames[0][fb_index]*3];
data[i+1] = palette[Game.frames[0][fb_index]*3+1];
data[i+2] = palette[Game.frames[0][fb_index]*3+2];
data[i+3] = 255;
fb_index++;
}
var imageData = new ImageData(data, 320,200);
tempcanvas = document.createElement('canvas');
tempcanvas.width=320;
tempcanvas.height=200;
var tempctx = tempcanvas.getContext('2d');
tempctx.putImageData(imageData,0,0);
ctx.scale(3,3);
ctx.drawImage(tempcanvas,0,0);
Game.lastRender = performance.now();
}
function GetFrame() {
var xhttp = new XMLHttpRequest();
var startpost = performance.now();
xhttp.onload = function(oEvent) {
var posttime = performance.now() - startpost;
AddPostTime(posttime);
var arraybuffer = xhttp.response;
if (arraybuffer) {
var byteArray = new Uint8Array(arraybuffer);
var dv = new DataView(arraybuffer);
let num_frames = dv.getInt32(0, true); // number of frames
let frame_len = 320*200 + 768;
for (i=0;i<num_frames;++i) {
let fb = new Uint8Array(arraybuffer.slice(4+i*frame_len));
Game.frames[i] = fb;
}
Game.numframes = num_frames;
Game.sessionId = dv.getInt32(4+num_frames*frame_len,true);
var poplen = dv.getInt32(8+num_frames*frame_len,true);
var poparray = new Uint8Array(arraybuffer.slice(12+num_frames*frame_len,12+num_frames*frame_len+poplen));
var pop = new TextDecoder("utf-8").decode(poparray);
if (Game.pop != pop) {
Game.pop = pop;
var pop_update = new XMLHttpRequest();
pop_update.onload = function(oEvent) {
refresh_sessions();
}
pop_update.open("POST", "https://loudly-verified-elephant.edgecompute.app/update_pop_in_session", true);
pop_update.setRequestHeader("playerid",Game.playerId);
pop_update.setRequestHeader("sessionid",Game.sessionId);
pop_update.setRequestHeader("pop",Game.pop);
pop_update.send();
}
var hostlen = dv.getInt32(12+num_frames*frame_len + poplen,true);
var hostarray = new Uint8Array(arraybuffer.slice(16+num_frames*frame_len + poplen,16+num_frames*frame_len + poplen + hostlen));
Game.host = new TextDecoder("utf-8").decode(hostarray);
var version = dv.getInt32(16+num_frames*frame_len + poplen + hostlen,true);
Game.version = version;
document.getElementById('version').innerHTML = version;
Game.startpost = true;
}
};
xhttp.open("POST", "https://especially-cunning-bat.edgecompute.app/doomframe", true);
xhttp.responseType = "arraybuffer";
var body = GeneratePostBody();
xhttp.send(body);
}
function refresh_sessions() {
var session_req = new XMLHttpRequest();
session_req.onload = function(oEvent) {
var sessions = JSON.parse(session_req.response);
var table = document.getElementById("sessions_table");
var head = table.createTHead();
let row = head.insertRow();
let th = document.createElement("th");
let text = document.createTextNode("ID");
th.appendChild(text);
row.appendChild(th);
th = document.createElement("th");
text = document.createTextNode("Players");
th.appendChild(text);
row.appendChild(th);
th = document.createElement("th");
text = document.createTextNode("Join");
th.appendChild(text);
row.appendChild(th);
for(var i = table.rows.length; i > 1;i--)
{
document.getElementById("sessions_table").deleteRow(i -1);
}
sessions.forEach(session => {
var row = table.insertRow(-1);
var idcell = row.insertCell(0);
var playercell = row.insertCell(1);
var buttoncell = row.insertCell(2);
idcell.innerHTML = session.id;
session.players.forEach(player => {
if (player.id == Game.playerId) {
playercell.innerHTML += "<b>" + player.name + " (me)</b>";
} else {
playercell.innerHTML += player.name;
}
if (Game.pop != "") {
playercell.innerHTML += " (" + Game.pop + ")\\n";
} else {
playercell.innerHTML += "\\n";
}
});
buttoncell.innerHTML = "<button id="+session.id+" onClick=join_session(this.id);>Join</button>"
});
}
session_req.open("GET", "https://loudly-verified-elephant.edgecompute.app/sessions", true);
session_req.send();
}
function join_session(id) {
xhttp.open("GET", "https://loudly-verified-elephant.edgecompute.app/join_session", true);
xhttp.responseType = "text";
xhttp.setRequestHeader("playerid",Game.playerId);
xhttp.setRequestHeader("name",document.getElementById("player_name").value);
xhttp.setRequestHeader("sessionid",id);
xhttp.send();
}
;(function () {
function main( tFrame ) {
Game.stopMain = window.requestAnimationFrame( main );
var delta = tFrame-Game.lastTick;
Game.lastTick = tFrame;
if (Game.started) {
if (Game.numframes > 0)
RenderFrame();
if (Game.startpost)
{
GetFrame();
Game.startpost = false;
}
}
}
function get_value_from_cookie(key) {
var value;
var row = document.cookie
.split('; ')
.find(row => row.startsWith(key));
if (typeof row != "undefined") {
value = row.split('=')[1];
} else {
value = "";
}
return value;
}
function update_name_in_session()
{
var xhttp = new XMLHttpRequest();
xhttp.open("POST", "https://loudly-verified-elephant.edgecompute.app/update_name_in_session", true);
xhttp.setRequestHeader("sessionid",Game.sessionId);
xhttp.setRequestHeader("playerid",Game.playerId);
xhttp.setRequestHeader("name",document.getElementById("player_name").value);
xhttp.send();
}
function lobby_heartbeat() {
var heartbeat_req = new XMLHttpRequest();
heartbeat_req.onload = function(oEvent) {
Game.countdown = 30;
if (!Game.started && heartbeat_req.status == 200) {
join_best_session();
}
}
heartbeat_req.open("POST", "https://loudly-verified-elephant.edgecompute.app/heartbeat", true);
heartbeat_req.responseType = "text";
heartbeat_req.setRequestHeader("sessionid",Game.sessionId);
heartbeat_req.setRequestHeader("playerid",Game.playerId);
heartbeat_req.send();
refresh_sessions();
}
function lobby_counter() {
Game.countdown -= 1;
var el = document.getElementById('waiting');
waiting.innerText = "Too many players; retrying in " + Game.countdown + " seconds.";
}
function init_user() {
var playerid=get_value_from_cookie('playerid');
var playername = get_value_from_cookie('playername');
if (playerid == 0) {
playerid = Math.floor(Math.random() * 100000);
document.cookie = "playerid="+playerid;
playername="Player" + playerid;
document.cookie = "playername="+playername;
}
Game.playerId = playerid;
if (playername == "") {
playername="Player" + playerid;
document.cookie = "playername="+playername;
}
document.getElementById("player_name").value = playername;
document.getElementById("player_name").onchange = function() {
document.cookie = "playername="+document.getElementById("player_name").value;
update_name_in_session();
}
setInterval(lobby_heartbeat, 1000 * 30); // 30s interval
Game.countdown = 30;
setInterval(lobby_counter, 1000); // 1s interval
}
function join_best_session() {
var xhttp = new XMLHttpRequest();
xhttp.onload = function(oEvent) {
if (xhttp.status == 200) {
var parts = xhttp.response.split(',');
Game.sessionId = parts[0];
// document.getElementById('player_index').value = parts[1];
Game.playerIndex = parts[1];
Game.playerIndex = parts[1];
Game.lastTick = performance.now();
Game.started = true;
document.getElementById('headeroverlay').style.display = 'block';
document.getElementById('sessions_table').style.display = 'block';
document.getElementById('waiting').style.display = 'none';
main(performance.now());
} else {
Game.started = false;
document.getElementById('headeroverlay').style.display = 'none';
document.getElementById('sessions_table').style.display = 'none';
document.getElementById('waiting').style.display = 'block';
main(performance.now());
}
};
xhttp.open("GET", "https://loudly-verified-elephant.edgecompute.app/join_best_session", true);
xhttp.responseType = "text";
xhttp.setRequestHeader("id",Game.playerId);
xhttp.setRequestHeader("name",document.getElementById("player_name").value);
xhttp.send();
}
init_user();
join_best_session();
refresh_sessions();
})();
</script>
</html>