use structed logging

This commit is contained in:
evanpelle
2024-09-01 12:51:20 -07:00
parent 735a2ae57b
commit 026a0cddbe
10 changed files with 292 additions and 31 deletions
+54 -15
View File
@@ -27,6 +27,8 @@ class Client {
private random = new PseudoRandom(1234)
private ip: Promise<string | null> = null
constructor() {
this.lobbiesContainer = document.getElementById('lobbies-container');
}
@@ -35,6 +37,7 @@ class Client {
setFavicon()
this.terrainMap = loadTerrainMap()
this.startLobbyPolling()
this.ip = getClientIP()
setupUsernameCallback((username) => {
console.log('Username updated:', username);
if (this.game != null) {
@@ -100,7 +103,7 @@ class Client {
}
}
private joinLobby(lobby: Lobby) {
private async joinLobby(lobby: Lobby) {
const lobbyButton = document.getElementById('lobby-button');
if (lobbyButton) {
this.isLobbyHighlighted = !this.isLobbyHighlighted;
@@ -115,21 +118,26 @@ class Client {
if (this.game != null) {
return;
}
this.terrainMap.then(tm => {
this.game = createClientGame(getUsername(), new PseudoRandom(Date.now()).nextID(), lobby.id, getConfig(), tm);
this.game.join();
const g = this.game;
window.addEventListener('beforeunload', function (event) {
console.log('Browser is closing');
g.stop();
});
})
const [terrainMap, clientIP] = await Promise.all([
this.terrainMap,
this.ip
]);
console.log(`got ip ${clientIP}`)
this.game = createClientGame(
getUsername(),
new PseudoRandom(Date.now()).nextID(), // TODO this can cause dup ids
clientIP,
lobby.id,
getConfig(),
terrainMap
);
this.game.join();
const g = this.game;
window.addEventListener('beforeunload', function (event) {
console.log('Browser is closing');
g.stop();
});
}
}
function getUsername(): string {
@@ -154,6 +162,37 @@ function setupUsernameCallback(callback: (username: string) => void): void {
}
async function getClientIP(timeoutMs: number = 1000): Promise<string | null> {
const controller = new AbortController();
const timeoutId = setTimeout(() => controller.abort(), timeoutMs);
try {
const response: Response = await fetch('https://api.ipify.org?format=json', {
signal: controller.signal
});
if (!response.ok) {
throw new Error(`HTTP error! status: ${response.status}`);
}
const data: {ip: string} = await response.json();
return data.ip;
} catch (error) {
if (error instanceof Error) {
if (error.name === 'AbortError') {
console.error('Request timed out');
} else {
console.error('Error fetching IP:', error.message);
}
} else {
console.error('An unknown error occurred');
}
return null;
} finally {
clearTimeout(timeoutId);
}
}
// Initialize the client when the DOM is loaded
+4 -1
View File
@@ -12,7 +12,7 @@ import {TerrainRenderer} from "./graphics/TerrainRenderer";
export function createClientGame(name: string, clientID: ClientID, gameID: GameID, config: Config, terrainMap: TerrainMap): ClientGame {
export function createClientGame(name: string, clientID: ClientID, ip: string | null, gameID: GameID, config: Config, terrainMap: TerrainMap): ClientGame {
let eventBus = new EventBus()
let game = createGame(terrainMap, eventBus, config)
let terrainRenderer = new TerrainRenderer(game)
@@ -21,6 +21,7 @@ export function createClientGame(name: string, clientID: ClientID, gameID: GameI
return new ClientGame(
name,
clientID,
ip,
gameID,
eventBus,
game,
@@ -47,6 +48,7 @@ export class ClientGame {
constructor(
public playerName: string,
private id: ClientID,
private clientIP: string | null,
private gameID: GameID,
private eventBus: EventBus,
private gs: Game,
@@ -67,6 +69,7 @@ export class ClientGame {
type: "join",
gameID: this.gameID,
clientID: this.id,
clientIP: this.clientIP,
lastTurn: this.turns.length
})
)