mirror of
https://github.com/openfrontio/OpenFrontIO.git
synced 2026-06-21 15:20:43 +00:00
create consolex class for remote logging
This commit is contained in:
@@ -242,9 +242,9 @@
|
||||
* better error logging in server DONE 12/16/2024
|
||||
* store and archive player cookies DONE 12/17/2024
|
||||
* make ips less precise for user safety DONE 12/17/2024
|
||||
* send client logs back to server
|
||||
* seperate server config from client config
|
||||
* send client logs back to server DONE 12/17/2024
|
||||
* right click brings up player info menu
|
||||
* seperate server config from client config
|
||||
* give naval units health
|
||||
* bug: player names not updating sometimes
|
||||
* make player editeable configs
|
||||
|
||||
@@ -13,7 +13,7 @@ import { SendAttackIntentEvent, SendSpawnIntentEvent, Transport } from "./Transp
|
||||
import { createCanvas } from "./Utils";
|
||||
import { DisplayMessageEvent, MessageType } from "./graphics/layers/EventsDisplay";
|
||||
import { WorkerClient } from "../core/worker/WorkerClient";
|
||||
import { initializeLogSender } from "./LogSender";
|
||||
import { consolex, initRemoteSender } from "../core/Consolex";
|
||||
|
||||
export interface LobbyConfig {
|
||||
playerName: () => string
|
||||
@@ -27,9 +27,11 @@ export interface LobbyConfig {
|
||||
}
|
||||
|
||||
export function joinLobby(lobbyConfig: LobbyConfig, onjoin: () => void): () => void {
|
||||
console.log(`joinging lobby: gameID: ${lobbyConfig.gameID}, clientID: ${lobbyConfig.clientID}, persistentID: ${lobbyConfig.persistentID}`)
|
||||
const eventBus = new EventBus()
|
||||
initializeLogSender(eventBus)
|
||||
initRemoteSender(eventBus)
|
||||
|
||||
consolex.log(`joinging lobby: gameID: ${lobbyConfig.gameID}, clientID: ${lobbyConfig.clientID}, persistentID: ${lobbyConfig.persistentID}`)
|
||||
|
||||
const config = getConfig()
|
||||
|
||||
let gameConfig: GameConfig = null
|
||||
|
||||
@@ -1,51 +0,0 @@
|
||||
declare global {
|
||||
interface Console {
|
||||
localLog: typeof console.log;
|
||||
localWarn: typeof console.warn;
|
||||
localError: typeof console.error;
|
||||
}
|
||||
}
|
||||
|
||||
import { EventBus } from "../core/EventBus"
|
||||
import { LogSeverity } from "../core/Schemas"
|
||||
import { SendLogEvent } from "./Transport"
|
||||
|
||||
let inited = false
|
||||
|
||||
export function initializeLogSender(eventBus: EventBus) {
|
||||
if (inited) {
|
||||
return
|
||||
}
|
||||
inited = true
|
||||
|
||||
// Store original console methods
|
||||
const originalLog = console.log
|
||||
const originalWarn = console.warn
|
||||
const originalError = console.error
|
||||
|
||||
// Define networked logging functions (both local and remote)
|
||||
const log = (...args: any[]): void => {
|
||||
eventBus.emit(new SendLogEvent(LogSeverity.Info, args.join(' ')))
|
||||
originalLog.apply(console, args)
|
||||
}
|
||||
|
||||
const warn = (...args: any[]): void => {
|
||||
eventBus.emit(new SendLogEvent(LogSeverity.Warn, args.join(' ')))
|
||||
originalWarn.apply(console, args)
|
||||
}
|
||||
|
||||
const error = (...args: any[]): void => {
|
||||
eventBus.emit(new SendLogEvent(LogSeverity.Error, args.join(' ')))
|
||||
originalError.apply(console, args)
|
||||
}
|
||||
|
||||
// Store local-only logging functions
|
||||
console.localLog = originalLog.bind(console)
|
||||
console.localWarn = originalWarn.bind(console)
|
||||
console.localError = originalError.bind(console)
|
||||
|
||||
// Replace main console methods with networked versions
|
||||
console.log = log
|
||||
console.warn = warn
|
||||
console.error = error
|
||||
}
|
||||
@@ -10,12 +10,8 @@ import { SinglePlayerModal } from "./SinglePlayerModal";
|
||||
import { HostLobbyModal as HostPrivateLobbyModal } from "./HostLobbyModal";
|
||||
import { JoinPrivateLobbyModal } from "./JoinPrivateLobbyModal";
|
||||
import { generateID } from "../core/Util";
|
||||
import { initializeLogSender } from "./LogSender";
|
||||
import { generateCryptoRandomUUID } from "./Utils";
|
||||
|
||||
|
||||
|
||||
|
||||
class Client {
|
||||
private gameStop: () => void
|
||||
|
||||
|
||||
@@ -175,7 +175,7 @@ export class Transport {
|
||||
this.socket.onopen = () => {
|
||||
console.log('Connected to game server!');
|
||||
while (this.buffer.length > 0) {
|
||||
console.localLog('sending dropped message')
|
||||
console.log('sending dropped message')
|
||||
this.sendMsg(this.buffer.pop())
|
||||
}
|
||||
onconnect()
|
||||
@@ -184,13 +184,13 @@ export class Transport {
|
||||
onmessage(ServerMessageSchema.parse(JSON.parse(event.data)))
|
||||
};
|
||||
this.socket.onerror = (err) => {
|
||||
console.localError('Socket encountered error: ', err, 'Closing socket');
|
||||
console.error('Socket encountered error: ', err, 'Closing socket');
|
||||
this.socket.close();
|
||||
};
|
||||
this.socket.onclose = (event: CloseEvent) => {
|
||||
console.log(`WebSocket closed. Code: ${event.code}, Reason: ${event.reason}`);
|
||||
if (event.code != 1000) {
|
||||
console.localLog(`reconnecting`)
|
||||
console.log(`reconnecting`)
|
||||
this.connect(onconnect, onmessage)
|
||||
}
|
||||
};
|
||||
@@ -236,8 +236,8 @@ export class Transport {
|
||||
console.log('on stop: leaving game')
|
||||
this.socket.close()
|
||||
} else {
|
||||
console.localLog('WebSocket is not open. Current state:', this.socket.readyState);
|
||||
console.localError('attempting reconnect')
|
||||
console.log('WebSocket is not open. Current state:', this.socket.readyState);
|
||||
console.error('attempting reconnect')
|
||||
}
|
||||
this.socket.onclose = (event: CloseEvent) => { }
|
||||
}
|
||||
@@ -368,8 +368,8 @@ export class Transport {
|
||||
})
|
||||
this.sendMsg(JSON.stringify(msg))
|
||||
} else {
|
||||
console.localLog('WebSocket is not open. Current state:', this.socket.readyState);
|
||||
console.localLog('attempting reconnect')
|
||||
console.log('WebSocket is not open. Current state:', this.socket.readyState);
|
||||
console.log('attempting reconnect')
|
||||
}
|
||||
}
|
||||
|
||||
@@ -378,7 +378,7 @@ export class Transport {
|
||||
this.localServer.onMessage(msg)
|
||||
} else {
|
||||
if (this.socket.readyState == WebSocket.CLOSED || this.socket.readyState == WebSocket.CLOSED) {
|
||||
console.localWarn('socket not ready, closing and trying later')
|
||||
console.warn('socket not ready, closing and trying later')
|
||||
this.socket.close()
|
||||
this.socket = null
|
||||
this.connectRemote(this.onconnect, this.onmessage)
|
||||
|
||||
@@ -0,0 +1,34 @@
|
||||
import { EventBus } from "./EventBus"
|
||||
import { LogSeverity } from "./Schemas"
|
||||
import { SendLogEvent } from "../client/Transport"
|
||||
|
||||
export const consolex = {
|
||||
log: console.log,
|
||||
warn: console.warn,
|
||||
error: console.error
|
||||
}
|
||||
|
||||
let inited = false
|
||||
|
||||
// Only call this in client/browser!
|
||||
export function initRemoteSender(eventBus: EventBus) {
|
||||
if (inited) {
|
||||
return
|
||||
}
|
||||
inited = true
|
||||
|
||||
consolex.log = (...args: any[]): void => {
|
||||
console.log(...args);
|
||||
eventBus.emit(new SendLogEvent(LogSeverity.Info, args.join(' ')))
|
||||
}
|
||||
|
||||
consolex.warn = (...args: any[]): void => {
|
||||
console.warn(...args);
|
||||
eventBus.emit(new SendLogEvent(LogSeverity.Warn, args.join(' ')))
|
||||
}
|
||||
|
||||
consolex.error = (...args: any[]): void => {
|
||||
console.error(...args);
|
||||
eventBus.emit(new SendLogEvent(LogSeverity.Error, args.join(' ')))
|
||||
}
|
||||
}
|
||||
@@ -45,7 +45,7 @@ app.get('/lobbies', (req, res) => {
|
||||
|
||||
app.post('/private_lobby', (req, res) => {
|
||||
const id = gm.createPrivateGame()
|
||||
console.log('creating private lobby with id ${id}')
|
||||
console.logx('creating private lobby with id ${id}')
|
||||
res.json({
|
||||
id: id
|
||||
});
|
||||
@@ -56,6 +56,7 @@ app.post('/archive_singleplayer_game', (req, res) => {
|
||||
const gameRecord: GameRecord = req.body
|
||||
const clientIP = req.ip || req.socket.remoteAddress || 'unknown'; // Added this line
|
||||
|
||||
|
||||
if (!gameRecord) {
|
||||
console.log('game record not found in request')
|
||||
res.status(404).json({ error: 'Game record not found' });
|
||||
@@ -78,7 +79,7 @@ app.post('/archive_singleplayer_game', (req, res) => {
|
||||
})
|
||||
|
||||
app.post('/start_private_lobby/:id', (req, res) => {
|
||||
console.log(`starting private lobby with id ${req.params.id}`)
|
||||
console.logx(`starting private lobby with id ${req.params.id}`)
|
||||
gm.startPrivateGame(req.params.id)
|
||||
});
|
||||
|
||||
|
||||
Reference in New Issue
Block a user