diff options
| author | caleb.mabry.15@cnu.edu <caleb.mabry.15@cnu.edu> | 2022-03-12 22:06:52 -0500 |
|---|---|---|
| committer | caleb.mabry.15@cnu.edu <caleb.mabry.15@cnu.edu> | 2022-03-12 22:06:52 -0500 |
| commit | 1296cecb0690bbe70c109693625cb424297a0b40 (patch) | |
| tree | 43f9a690934b19d847daa6d1b1eb49800595ae30 | |
| parent | c84825118e2c9f60d05e685ea33e7c3e27fa6e2c (diff) | |
Removing console logs adding function for different char icons
| -rw-r--r-- | webAO/client.js | 29 | ||||
| -rw-r--r-- | webAO/utils/calculateApngLength.js | 1 | ||||
| -rw-r--r-- | webAO/utils/fileExists.js | 25 |
3 files changed, 43 insertions, 12 deletions
diff --git a/webAO/client.js b/webAO/client.js index 2c1a831..0c7c4b6 100644 --- a/webAO/client.js +++ b/webAO/client.js @@ -780,13 +780,28 @@ class Client extends EventEmitter { async handleCharacterInfo(chargs, charid) { if (chargs[0]) { let cini = {}; - const cswap = {}; - const icon = `${AO_HOST}characters/${encodeURI(chargs[0].toLowerCase())}/char_icon.png`; + const getCharIcon = async () => { + const extensions = [ + '.png', + '.webp', + ]; + const charIconBaseUrl = `${AO_HOST}characters/${encodeURI(chargs[0].toLowerCase())}/char_icon`; + for (let i = 0; i < extensions.length; i++) { + const fileUrl = charIconBaseUrl + extensions[i]; + const exists = await fileExists(fileUrl); + if (exists) { + return fileUrl + } + } + return transparentPNG + }; + const charIconUrlResponse = await getCharIcon(); + const icon = charIconUrlResponse || transparentPNG; const img = document.getElementById(`demo_${charid}`); img.alt = chargs[0]; img.src = icon; // seems like a good time to load the icon - + // If the ini doesn't exist on the server this will throw an error try { const cinidata = await request(`${AO_HOST}characters/${encodeURI(chargs[0].toLowerCase())}/char.ini`); @@ -853,12 +868,13 @@ class Client extends EventEmitter { */ handleCI(args) { // Loop through the 10 characters that were sent + for (let i = 2; i <= args.length - 2; i++) { if (i % 2 === 0) { document.getElementById('client_loadingtext').innerHTML = `Loading Character ${args[1]}/${this.char_list_length}`; const chargs = args[i].split('&'); const charid = args[i - 1]; - setTimeout(() => this.handleCharacterInfo(chargs, charid), charid * 10); + setTimeout(() => this.handleCharacterInfo(chargs, charid), 500) } } // Request the next pack @@ -870,13 +886,14 @@ class Client extends EventEmitter { * in one packet. * @param {Array} args packet arguments */ - handleSC(args) { + async handleSC(args) { document.getElementById('client_loadingtext').innerHTML = 'Loading Characters'; for (let i = 1; i < args.length; i++) { document.getElementById('client_loadingtext').innerHTML = `Loading Character ${i}/${this.char_list_length}`; const chargs = args[i].split('&'); const charid = i - 1; - setTimeout(() => this.handleCharacterInfo(chargs, charid), charid * 10); + + this.handleCharacterInfo(chargs, charid) } // We're done with the characters, request the music this.sendServer('RM#%'); diff --git a/webAO/utils/calculateApngLength.js b/webAO/utils/calculateApngLength.js index bc6b3fa..86c2073 100644 --- a/webAO/utils/calculateApngLength.js +++ b/webAO/utils/calculateApngLength.js @@ -12,7 +12,6 @@ const calculateApngLength = (apngFile) => { && d[i + 1] === 0x63 && d[i + 2] === 0x54 && d[i + 3] === 0x4C) { - console.log("found apng header"); // numerator and denominator let delay = ((d[i + 21] / d[i + 23]) * 1000) diff --git a/webAO/utils/fileExists.js b/webAO/utils/fileExists.js index 6d32a1e..3065112 100644 --- a/webAO/utils/fileExists.js +++ b/webAO/utils/fileExists.js @@ -1,8 +1,23 @@ -const fileExists = async (url) => { - const xhr = new XMLHttpRequest(); - xhr.open('HEAD', url, false); - xhr.send(); - return xhr.status === 200; +const fileExists = async (url) => { + return new Promise((resolve, reject) => { + const xhr = new XMLHttpRequest(); + xhr.open('HEAD', url); + xhr.onload = function (e) { + if (xhr.readyState === 4) { + if (xhr.status === 200) { + resolve(true) + } else { + reject(false) + } + } + }; + xhr.onerror = function (e) { + resolve(false) + }; + xhr.send(null); + }) + + }; export default fileExists; |
