Skip to content

Commit

Permalink
the inevitable utils module
Browse files Browse the repository at this point in the history
  • Loading branch information
Hans Acker committed Jul 18, 2024
1 parent 91861fc commit 287c8cc
Show file tree
Hide file tree
Showing 7 changed files with 38 additions and 40 deletions.
1 change: 1 addition & 0 deletions html/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
<link rel="modulepreload" href="js/activity_icon.min.js">
<link rel="modulepreload" href="js/statsbox.min.js">
<link rel="modulepreload" href="js/infobox.min.js">
<link rel="modulepreload" href="js/utils.min.js">
<link rel="modulepreload" href="js/board.min.js">
<link rel="stylesheet" href="css/style.css">

Expand Down
35 changes: 1 addition & 34 deletions html/js/board.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,46 +2,13 @@ import { ReconnectingWebSocket } from "./reconnecting-websocket.min.js";
import { ActivityIcon } from "./activity_icon.min.js";
import { StatsBox } from "./statsbox.min.js";
import { InfoBox } from "./infobox.min.js";
import { distance3, trimPrefix, makeTd, addRow, whatGame } from "./utils.min.js";

/* https://github.com/HansAcker/EDDN-RealTime */

// TODO: modularize


// TODO: move to inevitable utility module

// const distanceN = (v0, v1) => Math.hypot.apply(null, v0.map((v, i) => v - v1[i]));
const distance3 = (v0, v1) => Math.hypot(v0[0] - v1[0], v0[1] - v1[1], v0[2] - v1[2]); // subtract vectors, return length
const trimPrefix = (str, prefix) => (str.startsWith(prefix) ? str.slice(prefix.length) : str).trim();
const makeTd = (textContent) => { const td = document.createElement("td"); td.textContent = td.title = textContent; return td; };

function addRow(tbody, tr) {
while (tbody.childElementCount >= listLength) {
tbody.lastElementChild.remove();
}

tbody.prepend(tr);
}

function whatGame(data) {
try {
// no decision if gameversion is not set or set to CAPI-<endpoint>
// https://github.com/EDCD/EDDN/blob/live/docs/Developers.md#gameversions-and-gamebuild
const gameversion = data.header.gameversion;
if (gameversion && (gameversion.startsWith("CAPI-Legacy-") || parseInt(gameversion) < 4)) {
return "Legacy";
}

// https://github.com/EDCD/EDDN/blob/live/docs/Developers.md#horizons-and-odyssey-flags
const msg = data.message;
return msg.odyssey ? "Odyssey" : msg.horizons ? "Horizons" : msg.horizons === false ? "Base" : "Unknown";
} catch(error) {
console.log("gameversion error:", error);
return "Unknown";
}
}


const gameStats = new StatsBox(statstable.querySelector("tbody"), {
"Total": 0,
"Old": 0,
Expand Down
2 changes: 1 addition & 1 deletion html/js/board.min.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

7 changes: 3 additions & 4 deletions html/js/statsbox.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import { makeTd } from "./utils.min.js";

class StatsBox {
#statsbody;
#stats;
Expand Down Expand Up @@ -37,13 +39,10 @@ class StatsBox {
this.#rows[stat].textContent = this.#stats[stat];
} else {
const row = document.createElement("tr");
row.append(StatsBox.#makeTd(stat), this.#rows[stat] = StatsBox.#makeTd(this.#stats[stat]));
row.append(makeTd(stat), this.#rows[stat] = makeTd(this.#stats[stat]));
this.#statsbody.append(row);
}
}

// TODO: move to inevitable utility module
static #makeTd = (textContent) => { const td = document.createElement("td"); td.textContent = textContent; return td; };
}

export { StatsBox };
2 changes: 1 addition & 1 deletion html/js/statsbox.min.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

30 changes: 30 additions & 0 deletions html/js/utils.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
// const distanceN = (v0, v1) => Math.hypot.apply(null, v0.map((v, i) => v - v1[i]));
export const distance3 = (v0, v1) => Math.hypot(v0[0] - v1[0], v0[1] - v1[1], v0[2] - v1[2]); // subtract vectors, return length
export const trimPrefix = (str, prefix) => (str.startsWith(prefix) ? str.slice(prefix.length) : str).trim();
export const makeTd = (textContent) => { const td = document.createElement("td"); td.textContent = td.title = textContent; return td; };

export function addRow(tbody, tr) {
while (tbody.childElementCount >= listLength) {
tbody.lastElementChild.remove();
}

tbody.prepend(tr);
}

export function whatGame(data) {
try {
// no decision if gameversion is not set or set to CAPI-<endpoint>
// https://github.com/EDCD/EDDN/blob/live/docs/Developers.md#gameversions-and-gamebuild
const gameversion = data.header.gameversion;
if (gameversion && (gameversion.startsWith("CAPI-Legacy-") || parseInt(gameversion) < 4)) {
return "Legacy";
}

// https://github.com/EDCD/EDDN/blob/live/docs/Developers.md#horizons-and-odyssey-flags
const msg = data.message;
return msg.odyssey ? "Odyssey" : msg.horizons ? "Horizons" : msg.horizons === false ? "Base" : "Unknown";
} catch(error) {
console.log("gameversion error:", error);
return "Unknown";
}
}
1 change: 1 addition & 0 deletions html/js/utils.min.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 287c8cc

Please sign in to comment.