forked from yogstation13/demo-viewer
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
80 lines (79 loc) · 2.98 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
'use strict';
const Demo = require('./lib/loader.js');
const DemoPlayer = require('./lib/player.js');
document.addEventListener("DOMContentLoaded", async function() {
let url = null;
let querystring = new URLSearchParams(window.location.search);
let status_holder = document.createElement("h1");
if(querystring.has("demo_url")) {
url = querystring.get("demo_url");
} else if(querystring.has("roundid")) {
url = window.demo_url_template.replace(/\{roundid}/g, querystring.get("roundid"));
}
if(url) {
document.body.appendChild(status_holder);
status_holder.textContent = "Fetching demo file...";
try {
let response = await fetch(url, {credentials: +querystring.get('send_credentials') ? 'include' : 'same-origin'});
if(response.status != 200) {
status_holder.textContent = "Error when fetching: " + response.status + " " + response.statusText;
} else {
await run_demo(response, status_holder);
}
} catch(e) {
status_holder.textContent = `${e}`;
}
} else {
let running = false;
let fileselect = document.createElement("input");
fileselect.type = "file";
let button = document.createElement("input");
button.type = "button";
button.value = "Open demo from file";
button.addEventListener("click", () => {
if(!fileselect.files[0]) return;
if(running) return;
document.body.appendChild(status_holder);
run_demo(fileselect.files[0], status_holder).catch(e => {
status_holder.textContent = `${e}`;
});
});
document.body.appendChild(fileselect);
document.body.appendChild(button);
}
});
async function run_demo(source, status_holder) {
status_holder.textContent = "Parsing demo file...";
let demo = new Demo(source);
console.log(demo);
await demo.initialized_promise;
/*let turfs = new Map();
let icons = new Map();
let icon_promises = [];
let completed = 0;
for(let icon of demo.icons_used) {
icon_promises.push((async () => {
let url = "https://cdn.jsdelivr.net/gh/" + window.repository + "@" + demo.commit + "/" + icon;
console.log(url);
try {
let icon_obj = await read_icon(url);
icons.set(icon, icon_obj);
} catch(e) {
console.error(e);
} finally {
completed++;
status_holder.textContent = "Downloading icons..." + (completed * 100 / demo.icons_used.length).toFixed(1) + "%";
}
})());
}
await Promise.all(icon_promises);*/
let chat_css = await (await fetch("https://cdn.jsdelivr.net/gh/" + window.repository + "@" + demo.commit + "/code/modules/goonchat/browserassets/css/browserOutput.css")).text();
chat_css = chat_css.replace(/((?:^|[},])[^\@\{]*?)([a-zA-Z.#\[\]":=\-_][a-zA-Z0-9.# \[\]":=\-_]*)(?=.+\{)/g, "$1.chat_window $2");
chat_css = chat_css.replace(/height: [^;]+%;/g, "");
chat_css = chat_css.replace(/ ?html| ?body/g, "");
let style = document.createElement("style");
style.innerHTML = chat_css;
document.head.appendChild(style);
//console.log(icons);
window.demo_player = new DemoPlayer(demo, demo.loaded_icons);
}