Skip to content

Commit

Permalink
change serverlist to use detail tag
Browse files Browse the repository at this point in the history
prep for onhover description
  • Loading branch information
stonedDiscord committed Jan 30, 2025
1 parent a3a8e7d commit 9be77db
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 81 deletions.
24 changes: 5 additions & 19 deletions public/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -136,27 +136,13 @@ <h2>Server List</h2>
</div>
</noscript>
<ul class="serverlist">
<li id="server-2" onmouseover="setServ(-2)">
<p>Singleplayer (beta)</p>
<a class="button" href="client.html?mode=replay">Try</a>
</li>
<details name="servers">
<summary><p>Singleplayer</p>
<a class="button" href="client.html?mode=replay">Join</a></summary>
<p>This is your computer</p>
</details>
</ul>
<ul class="serverlist" id="masterlist"></ul>
<ul class="serverlist">
<li id="server-1" onmouseover="setServ(-1)">
<p>Localhost</p>
<a
class="button"
href="client.html?mode=watch&connect=ws://127.0.0.1:50001"
>Watch</a
>
<a
class="button"
href="client.html?mode=join&connect=ws://127.0.0.1:50001"
>Join</a
>
</li>
</ul>
<div id="info_container">
<p id="serverinfo">Masterserver version - ...</p>
<p id="clientinfo">Client version - ...</p>
Expand Down
99 changes: 41 additions & 58 deletions webAO/master.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,5 @@
import { safeTags } from "./encoding";

declare global {
interface Window {
setServ: (ID: number) => void;
}
}

interface AOServer {
name: string;
description: string;
Expand All @@ -27,29 +21,22 @@ const protocol = window.location.protocol;
const serverlist_cache_key = "masterlist";

const servers: AOServer[] = [];
servers[-2] = {
name: "Singleplayer",
description: "Build cases, try out new things",
ip: "127.0.0.1",
players: 0,
online: "Singleplayer",
port: 50001,
} as AOServer;

servers[-1] = {
name: "Localhost",
description: "This is your computer on port 50001",
ip: "127.0.0.1",
players: 0,
online: "Localhost",
port: 50001,
ws_port: 50001,
} as AOServer;

function main() {
getServerlist().then((serverlist) => {
processServerlist(serverlist);
});

addServer(servers[-1]);

processClientVersion(clientVersion);

getMasterVersion().then((masterVersion) => {
Expand All @@ -59,17 +46,6 @@ function main() {

main();

export function setServ(ID: number) {
const server = servers[ID];
const onlineStr = server.online;
document.getElementById("serverdescription_content").innerHTML =
`<b>${onlineStr}</b><br>`;
document
.getElementById("serverdescription_content")
.appendChild(document.createTextNode(server.description));
}
window.setServ = setServ;

// Fetches the serverlist from the masterserver
// Returns a properly typed list of servers
async function getServerlist(): Promise<AOServer[]> {
Expand Down Expand Up @@ -164,41 +140,48 @@ function constructClientURL(protocol: string): string {
return clientURL.href;
}

function processServerlist(serverlist: AOServer[]) {
for (let i = 0; i < serverlist.length; i++) {
const server = serverlist[i];
let ws_port = 0;
let ws_protocol = "";
let http_protocol = "";

if (server.ws_port) {
ws_port = server.ws_port;
ws_protocol = "ws";
http_protocol = "http";
}
if (server.wss_port && !window.navigator.userAgent.includes("Nintendo")) {
ws_port = server.wss_port;
ws_protocol = "wss";
http_protocol = "https";
}
function addServer(server: AOServer) {
let ws_port = 0;
let ws_protocol = "";
let http_protocol = "";

if (ws_port === 0 || ws_protocol === "" || http_protocol === "") {
console.warn(`Server ${server.name} has no websocket port, skipping`);
continue;
}
if (server.ws_port) {
ws_port = server.ws_port;
ws_protocol = "ws";
http_protocol = "http";
}
if (server.wss_port && !window.navigator.userAgent.includes("Nintendo")) {
ws_port = server.wss_port;
ws_protocol = "wss";
http_protocol = "https";
}

if (ws_port === 0 || ws_protocol === "" || http_protocol === "") {
console.warn(`Server ${server.name} has no websocket port, skipping`);
return;
}

const clientURL = constructClientURL(http_protocol);
const connect = `${ws_protocol}://${server.ip}:${ws_port}`;
const serverName = server.name;
const fullClientWatchURL = `${clientURL}?mode=watch&connect=${connect}&serverName=${serverName}`;
const fullClientJoinURL = `${clientURL}?mode=join&connect=${connect}&serverName=${serverName}`;

servers.push(server);

const clientURL = constructClientURL(http_protocol);
const connect = `${ws_protocol}://${server.ip}:${ws_port}`;
const serverName = server.name;
const fullClientWatchURL = `${clientURL}?mode=watch&connect=${connect}&serverName=${serverName}`;
const fullClientJoinURL = `${clientURL}?mode=join&connect=${connect}&serverName=${serverName}`;
document.getElementById("masterlist").innerHTML +=
`<details name="servers">` +
`<summary><p>${safeTags(server.name)} (${server.players})</p>` +
`<a class="button" href="${fullClientJoinURL}" target="_blank">Join</a>` +
`<a class="button" href="${fullClientWatchURL}" target="_blank">Watch</a></summary>` +
`<p>${safeTags(server.description)}</p>` +
`</details>`

servers.push(server);
}

document.getElementById("masterlist").innerHTML +=
`<li id="server${i}" onmouseover="setServ(${i})"><p>${safeTags(server.name)} (${server.players})</p>` +
`<a class="button" href="${fullClientWatchURL}" target="_blank">Watch</a>` +
`<a class="button" href="${fullClientJoinURL}" target="_blank">Join</a></li>`;
function processServerlist(serverlist: AOServer[]) {
for (let i = 0; i < serverlist.length; i++) {
addServer(serverlist[i]);
}
}

Expand Down
8 changes: 4 additions & 4 deletions webAO/styles/master.css
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@
margin: auto;
}

.serverlist li {
.serverlist details {
position: relative;
right: 0;
background-color: #fff;
Expand All @@ -110,16 +110,16 @@
cursor: default;
}

.serverlist li:hover {
.serverlist details:hover {
background-color: #ffff6b;
border-color: #efad21;
}

.serverlist li p {
.serverlist details p {
display: inline-block !important;
}

.serverlist li a {
.serverlist details a {
float: right;
}

Expand Down

0 comments on commit 9be77db

Please sign in to comment.