From f3361c299996ddad052fe9b10c4424c3e8544280 Mon Sep 17 00:00:00 2001 From: David Skoland Date: Sat, 18 Nov 2023 14:00:20 +0100 Subject: [PATCH 1/9] Refactor master.ts --- webAO/master.ts | 136 ++++++++++++++++++++++++++++++++++++++---------- 1 file changed, 108 insertions(+), 28 deletions(-) diff --git a/webAO/master.ts b/webAO/master.ts index e596406b..2e56495d 100644 --- a/webAO/master.ts +++ b/webAO/master.ts @@ -8,12 +8,25 @@ declare global { } } +interface AOServer { + name: string, + description: string, + ip: string, + port?: number, + ws_port?: number, + wss_port?: number, + assets?: string, + onlineStatus?: string, +} + const myStorage = window.localStorage; const version = process.env.npm_package_version; const MASTERSERVER_IP = 'master.aceattorneyonline.com:27014'; +const serverlist_cache_key = 'masterlist'; + let hdid: string; let selectedServer: number = -1; @@ -29,15 +42,24 @@ servers[-1] = { const fpPromise = FingerprintJS.load(); fpPromise .then((fp) => fp.get()) - .then((result) => { + .then(async (result) => { hdid = result.visitorId; check_https(); - fetch('http://servers.aceattorneyonline.com/servers') - .then(cachedServerlist) - .then((response) => loadServerlist(response)) - .catch(cachedServerlist); + const serverlist = await (async () => { + try { + return await getServerlist(); + } catch (err) { + console.error(err); + // Something went wrong, try to use the cached serverlist + document.getElementById('ms_error').style.display = 'block'; + // This returns an empty list if there is no cached serverlist, which is the best we can do at this point + return getCachedServerlist(); + } + })(); + + processServerlist(serverlist); fetch('http://servers.aceattorneyonline.com/version') .then((response) => response.text()) @@ -114,37 +136,95 @@ function checkOnline(serverID: number, coIP: string) { }; } -function loadServerlist(thelist: { name: string, description: string, ip: string, port: number, ws_port: number, assets: string, online: string }[]) { - localStorage.setItem('masterlist', JSON.stringify(thelist)); - processServerlist(thelist); -} +// Fetches the serverlist from the masterserver +// Returns a properly typed list of servers +async function getServerlist(): Promise { + // get if we're on http or https + const protocol = window.location.protocol; + const response = await fetch(protocol + '//servers.aceattorneyonline.com/servers'); -function cachedServerlist(response: Response) { if (!response.ok) { - document.getElementById('ms_error').style.display = 'block'; - processServerlist(JSON.parse(localStorage.getItem('masterlist'))); - return; + throw new Error(`Bad status code from masterserver. status: ${response.status}, body: ${response.body}`); } - return response.json(); -} -function processServerlist(thelist: { name: string, description: string, ip: string, port: number, ws_port: number, assets: string, online: string }[]) { - const myURL: string = window.location.href.replace('https://', 'http://').replace('index.html', ''); - for (let i = 0; i < thelist.length - 1; i++) { - const serverEntry: { name: string, description: string, ip: string, port: number, ws_port: number, assets: string, online: string } = thelist[i]; + const data = await response.json(); + const serverlist: AOServer[] = []; + + for (const item of data) { + if (!item.name) { + console.warn(`Server ${item} has no name, skipping`); + continue; + } + if (!item.ip) { + console.warn(`Server ${item.name} has no ip, skipping`); + continue; + } + if (!item.description) { + console.warn(`Server ${item.name} has no description, skipping`); + continue; + } - servers[i] = serverEntry; - servers[i].online = "Offline"; + const newServer: AOServer = { + name: item.name, + description: item.description, + ip: item.id, + } - const ipport = `${serverEntry.ip}:${serverEntry.ws_port}`; - const serverName = serverEntry.name; + if (item.ws_port) { + newServer.ws_port = item.ws_port; + } + if (item.wss_port) { + newServer.wss_port = item.wss_port; + } - if (serverEntry.ws_port) { - document.getElementById('masterlist').innerHTML - += `
  • ${safeTags(serverEntry.name)}

    ` - + `Watch` - + `Join
  • `; + // if none of ws_port or wss_port are defined, skip + // Note that this is not an error condition, as many servers only has port (TCP) enabled + // Which means they don't support webAO + if (!newServer.ws_port && !newServer.wss_port) { + continue; } + + serverlist.push(newServer); + } + + // Always cache the result when we get it + localStorage.setItem(serverlist_cache_key, JSON.stringify(serverlist)); + + return serverlist; +} + +function getCachedServerlist(): AOServer[] { + // If it's not in the cache, return an empty list + const cached = localStorage.getItem(serverlist_cache_key) || '[]'; + return JSON.parse(cached) as AOServer[]; +} + +function processServerlist(serverlist: AOServer[]) { + const protocol = window.location.protocol; + const domain = window.location.hostname; + const clientURL: string = `${protocol}//${domain}/client.html`; + for (let i = 0; i < serverlist.length - 1; i++) { + const server = serverlist[i]; + let port = 0; + + if (server.ws_port) { + port = server.ws_port; + } + if (server.wss_port) { + port = server.wss_port; + } + if (port === 0) { + continue; + } + + const ipport = `${server.ip}:${port}`; + const serverName = server.name; + servers[i].onlineStatus = 'Offline'; + + document.getElementById('masterlist').innerHTML + += `
  • ${safeTags(server.name)}

    ` + + `Watch` + + `Join
  • `; } } From d8010fef613c4aa2add282092768aaa631dcaf88 Mon Sep 17 00:00:00 2001 From: David Skoland Date: Sat, 18 Nov 2023 14:17:29 +0100 Subject: [PATCH 2/9] Slimmer code in main function --- webAO/master.ts | 51 ++++++++++++++++++++++++++++--------------------- 1 file changed, 29 insertions(+), 22 deletions(-) diff --git a/webAO/master.ts b/webAO/master.ts index 2e56495d..fa291b50 100644 --- a/webAO/master.ts +++ b/webAO/master.ts @@ -21,9 +21,11 @@ interface AOServer { const myStorage = window.localStorage; -const version = process.env.npm_package_version; +const clientVersion = process.env.npm_package_version; const MASTERSERVER_IP = 'master.aceattorneyonline.com:27014'; +const serverlist_domain = 'servers.aceattorneyonline.com'; +const protocol = window.location.protocol; const serverlist_cache_key = 'masterlist'; @@ -47,30 +49,20 @@ fpPromise check_https(); - const serverlist = await (async () => { - try { - return await getServerlist(); - } catch (err) { - console.error(err); - // Something went wrong, try to use the cached serverlist - document.getElementById('ms_error').style.display = 'block'; - // This returns an empty list if there is no cached serverlist, which is the best we can do at this point - return getCachedServerlist(); - } - })(); - + const serverlist = await getServerlist(); processServerlist(serverlist); - fetch('http://servers.aceattorneyonline.com/version') - .then((response) => response.text()) - .then((response) => processVersion(response)); + processClientVersion(clientVersion); + + const masterVersion = await getMasterVersion(); + processMasterVersion(masterVersion); // i don't need the ms to play alone setTimeout(() => checkOnline(-1, '127.0.0.1:50001'), 0); }); export function check_https() { - if (document.location.protocol === 'https:') { + if (protocol === 'https:') { document.getElementById('https_error').style.display = ''; setTimeout(() => window.location.replace("http://web.aceattorneyonline.com/"), 5000); } @@ -140,11 +132,13 @@ function checkOnline(serverID: number, coIP: string) { // Returns a properly typed list of servers async function getServerlist(): Promise { // get if we're on http or https - const protocol = window.location.protocol; const response = await fetch(protocol + '//servers.aceattorneyonline.com/servers'); if (!response.ok) { - throw new Error(`Bad status code from masterserver. status: ${response.status}, body: ${response.body}`); + console.error(`Bad status code from masterserver. status: ${response.status}, body: ${response.body}`); + document.getElementById('ms_error').style.display = 'block'; + // If we get a bad status code, try to use the cached serverlist + return getCachedServerlist(); } const data = await response.json(); @@ -200,7 +194,6 @@ function getCachedServerlist(): AOServer[] { } function processServerlist(serverlist: AOServer[]) { - const protocol = window.location.protocol; const domain = window.location.hostname; const clientURL: string = `${protocol}//${domain}/client.html`; for (let i = 0; i < serverlist.length - 1; i++) { @@ -228,7 +221,21 @@ function processServerlist(serverlist: AOServer[]) { } } -function processVersion(data: string) { - document.getElementById('clientinfo').innerHTML = `Client version: ${version}`; +async function getMasterVersion(): Promise { + const url = `${protocol}//${serverlist_domain}/version`; + const response = await fetch(url); + if (!response.ok) { + console.error(`Bad status code from masterserver version check. status: ${response.status}, body: ${response.body}`); + return 'Unknown'; + } + + return await response.text(); +} + +function processClientVersion(data: string) { + document.getElementById('clientinfo').innerHTML = `Client version: ${data}`; +} + +function processMasterVersion(data: string) { document.getElementById('serverinfo').innerHTML = `Master server version: ${data}`; } From a00962c9580fd98faa2861ab73d3a73150432288 Mon Sep 17 00:00:00 2001 From: David Skoland Date: Sat, 18 Nov 2023 14:18:27 +0100 Subject: [PATCH 3/9] Remove unused variables --- webAO/master.ts | 8 +++----- 1 file changed, 3 insertions(+), 5 deletions(-) diff --git a/webAO/master.ts b/webAO/master.ts index fa291b50..96999a0a 100644 --- a/webAO/master.ts +++ b/webAO/master.ts @@ -1,10 +1,10 @@ import FingerprintJS from '@fingerprintjs/fingerprintjs'; -import { unescapeChat, safeTags } from './encoding'; +import { safeTags } from './encoding'; declare global { interface Window { - setServ: (ID: any) => void; + setServ: (ID: number) => void; } } @@ -19,11 +19,9 @@ interface AOServer { onlineStatus?: string, } -const myStorage = window.localStorage; - const clientVersion = process.env.npm_package_version; -const MASTERSERVER_IP = 'master.aceattorneyonline.com:27014'; +// const MASTERSERVER_IP = 'master.aceattorneyonline.com:27014'; const serverlist_domain = 'servers.aceattorneyonline.com'; const protocol = window.location.protocol; From 8847311bd5f57d60bd05403e3ebe3b6a2acbb414 Mon Sep 17 00:00:00 2001 From: David Skoland Date: Sat, 18 Nov 2023 14:31:01 +0100 Subject: [PATCH 4/9] Remember to populate servers as well --- webAO/master.ts | 13 +++++++++---- 1 file changed, 9 insertions(+), 4 deletions(-) diff --git a/webAO/master.ts b/webAO/master.ts index 96999a0a..d0855a93 100644 --- a/webAO/master.ts +++ b/webAO/master.ts @@ -16,7 +16,7 @@ interface AOServer { ws_port?: number, wss_port?: number, assets?: string, - onlineStatus?: string, + online?: string, } const clientVersion = process.env.npm_package_version; @@ -31,7 +31,7 @@ let hdid: string; let selectedServer: number = -1; -const servers: { name: string, description: string, ip: string, port: number, ws_port: number, assets: string, online: string }[] = []; +const servers: AOServer[] = []; servers[-2] = { name: 'Singleplayer', description: 'Build cases, try out new things', ip: '127.0.0.1', port: 50001, ws_port: 50001, assets: '', online: 'Online: 0/1', }; @@ -123,6 +123,8 @@ function checkOnline(serverID: number, coIP: string) { serverConnection.onerror = function (_evt: Event) { document.getElementById(`server${serverID}`).className = 'unavailable'; + console.error(`Error connecting to ${coIP}`); + console.error(_evt); }; } @@ -159,7 +161,7 @@ async function getServerlist(): Promise { const newServer: AOServer = { name: item.name, description: item.description, - ip: item.id, + ip: item.ip, } if (item.ws_port) { @@ -176,7 +178,10 @@ async function getServerlist(): Promise { continue; } + console.log(newServer) + serverlist.push(newServer); + servers.push(newServer); } // Always cache the result when we get it @@ -210,7 +215,7 @@ function processServerlist(serverlist: AOServer[]) { const ipport = `${server.ip}:${port}`; const serverName = server.name; - servers[i].onlineStatus = 'Offline'; + servers[i].online = 'Offline'; document.getElementById('masterlist').innerHTML += `
  • ${safeTags(server.name)}

    ` From bc8fb9800bf9244aa5c5654bd469c2d0b104bbee Mon Sep 17 00:00:00 2001 From: David Skoland Date: Sat, 18 Nov 2023 14:36:13 +0100 Subject: [PATCH 5/9] Add players field --- webAO/master.ts | 22 ++++++++++++++++++---- 1 file changed, 18 insertions(+), 4 deletions(-) diff --git a/webAO/master.ts b/webAO/master.ts index d0855a93..d87c10d7 100644 --- a/webAO/master.ts +++ b/webAO/master.ts @@ -12,6 +12,7 @@ interface AOServer { name: string, description: string, ip: string, + players: number, port?: number, ws_port?: number, wss_port?: number, @@ -33,10 +34,22 @@ let selectedServer: number = -1; const servers: AOServer[] = []; servers[-2] = { - name: 'Singleplayer', description: 'Build cases, try out new things', ip: '127.0.0.1', port: 50001, ws_port: 50001, assets: '', online: 'Online: 0/1', + name: 'Singleplayer', + description: 'Build cases, try out new things', + ip: '127.0.0.1', + port: 50001, + assets: '', + online: 'Online: 0/1', + players: 0, }; servers[-1] = { - name: 'Localhost', description: 'This is your computer on port 50001', ip: '127.0.0.1', port: 50001, ws_port: 50001, assets: '', online: 'Offline', + name: 'Localhost', + description: 'This is your computer on port 50001', + ip: '127.0.0.1', + port: 50001, + assets: '', + online: 'Offline', + players: 0, }; const fpPromise = FingerprintJS.load(); @@ -162,6 +175,7 @@ async function getServerlist(): Promise { name: item.name, description: item.description, ip: item.ip, + players: item.players || 0, } if (item.ws_port) { @@ -181,7 +195,6 @@ async function getServerlist(): Promise { console.log(newServer) serverlist.push(newServer); - servers.push(newServer); } // Always cache the result when we get it @@ -215,7 +228,8 @@ function processServerlist(serverlist: AOServer[]) { const ipport = `${server.ip}:${port}`; const serverName = server.name; - servers[i].online = 'Offline'; + server.online = 'Offline'; + servers.push(server); document.getElementById('masterlist').innerHTML += `
  • ${safeTags(server.name)}

    ` From 2ef78433802a434ded890c952849c2e92264ddef Mon Sep 17 00:00:00 2001 From: David Skoland Date: Sat, 18 Nov 2023 14:40:53 +0100 Subject: [PATCH 6/9] Fix host --- webAO/master.ts | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/webAO/master.ts b/webAO/master.ts index d87c10d7..ae0165af 100644 --- a/webAO/master.ts +++ b/webAO/master.ts @@ -144,8 +144,8 @@ function checkOnline(serverID: number, coIP: string) { // Fetches the serverlist from the masterserver // Returns a properly typed list of servers async function getServerlist(): Promise { - // get if we're on http or https - const response = await fetch(protocol + '//servers.aceattorneyonline.com/servers'); + const url = `${protocol}//${serverlist_domain}/servers`; + const response = await fetch(url); if (!response.ok) { console.error(`Bad status code from masterserver. status: ${response.status}, body: ${response.body}`); @@ -210,8 +210,8 @@ function getCachedServerlist(): AOServer[] { } function processServerlist(serverlist: AOServer[]) { - const domain = window.location.hostname; - const clientURL: string = `${protocol}//${domain}/client.html`; + const host = window.location.host; + const clientURL: string = `${protocol}//${host}/client.html`; for (let i = 0; i < serverlist.length - 1; i++) { const server = serverlist[i]; let port = 0; From 9ab2c3ca84aca8e70807a0b65a25ee70e02a16e4 Mon Sep 17 00:00:00 2001 From: David Skoland Date: Sat, 18 Nov 2023 14:42:57 +0100 Subject: [PATCH 7/9] remove debug log --- webAO/master.ts | 2 -- 1 file changed, 2 deletions(-) diff --git a/webAO/master.ts b/webAO/master.ts index ae0165af..c1ce92fd 100644 --- a/webAO/master.ts +++ b/webAO/master.ts @@ -192,8 +192,6 @@ async function getServerlist(): Promise { continue; } - console.log(newServer) - serverlist.push(newServer); } From 5012d56be354ad543e78933a018a16af109f25ee Mon Sep 17 00:00:00 2001 From: David Skoland Date: Sat, 18 Nov 2023 14:48:40 +0100 Subject: [PATCH 8/9] Add playercount to serverlist --- webAO/master.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/webAO/master.ts b/webAO/master.ts index c1ce92fd..9348feb1 100644 --- a/webAO/master.ts +++ b/webAO/master.ts @@ -230,7 +230,7 @@ function processServerlist(serverlist: AOServer[]) { servers.push(server); document.getElementById('masterlist').innerHTML - += `
  • ${safeTags(server.name)}

    ` + += `
  • ${safeTags(server.name)} (${server.players})

    ` + `Watch` + `Join
  • `; } From 9c816d82a5b4c5adf812405815896b2a3fab657a Mon Sep 17 00:00:00 2001 From: David Skoland Date: Sat, 18 Nov 2023 15:04:44 +0100 Subject: [PATCH 9/9] Real async --- webAO/master.ts | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/webAO/master.ts b/webAO/master.ts index 9348feb1..336fc5ff 100644 --- a/webAO/master.ts +++ b/webAO/master.ts @@ -60,13 +60,15 @@ fpPromise check_https(); - const serverlist = await getServerlist(); - processServerlist(serverlist); + getServerlist().then((serverlist) => { + processServerlist(serverlist); + }); processClientVersion(clientVersion); - const masterVersion = await getMasterVersion(); - processMasterVersion(masterVersion); + getMasterVersion().then((masterVersion) => { + processMasterVersion(masterVersion); + }); // i don't need the ms to play alone setTimeout(() => checkOnline(-1, '127.0.0.1:50001'), 0);