mirror of
https://github.com/openfrontio/OpenFrontIO.git
synced 2026-06-21 16:10:53 +00:00
send all logs from client to server and log them
This commit is contained in:
@@ -13,6 +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";
|
||||
|
||||
export interface LobbyConfig {
|
||||
playerName: () => string
|
||||
@@ -27,6 +28,7 @@ export interface LobbyConfig {
|
||||
|
||||
export function joinLobby(lobbyConfig: LobbyConfig, onjoin: () => void): () => void {
|
||||
const eventBus = new EventBus()
|
||||
initializeLogSender(eventBus)
|
||||
const config = getConfig()
|
||||
|
||||
let gameConfig: GameConfig = null
|
||||
@@ -186,7 +188,6 @@ export class GameRunner {
|
||||
}
|
||||
|
||||
private playerEvent(event: PlayerEvent) {
|
||||
console.log('received new player event!')
|
||||
if (event.player.clientID() == this.clientID) {
|
||||
console.log('setting name')
|
||||
this.myPlayer = event.player
|
||||
|
||||
+27
-6
@@ -1,16 +1,37 @@
|
||||
import { EventBus } from "../core/EventBus"
|
||||
import { LogSeverity } from "../core/Schemas"
|
||||
import { SendLogEvent } from "./Transport"
|
||||
|
||||
export enum LogSeverity {
|
||||
Info,
|
||||
Warn,
|
||||
Error
|
||||
}
|
||||
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
|
||||
|
||||
const log = (msg: string): void => {
|
||||
eventBus.emit(new SendLogEvent(LogSeverity.Info, msg))
|
||||
console.log(msg)
|
||||
originalLog.call(console, msg) // Use the original method
|
||||
}
|
||||
|
||||
const warn = (msg: string): void => {
|
||||
eventBus.emit(new SendLogEvent(LogSeverity.Warn, msg))
|
||||
originalWarn.call(console, msg) // Use the original method
|
||||
}
|
||||
|
||||
const error = (msg: string): void => {
|
||||
eventBus.emit(new SendLogEvent(LogSeverity.Error, msg))
|
||||
originalError.call(console, msg) // Use the original method
|
||||
}
|
||||
|
||||
// Replace console methods
|
||||
console.log = log
|
||||
console.warn = warn
|
||||
console.error = error
|
||||
}
|
||||
@@ -10,6 +10,7 @@ import { SinglePlayerModal } from "./SinglePlayerModal";
|
||||
import { HostLobbyModal as HostPrivateLobbyModal } from "./HostLobbyModal";
|
||||
import { JoinPrivateLobbyModal } from "./JoinPrivateLobbyModal";
|
||||
import { generateID } from "../core/Util";
|
||||
import { initializeLogSender } from "./LogSender";
|
||||
|
||||
|
||||
|
||||
|
||||
@@ -1,10 +1,9 @@
|
||||
import { Config } from "../core/configuration/Config"
|
||||
import { EventBus, GameEvent } from "../core/EventBus"
|
||||
import { AllianceRequest, AllPlayers, Cell, GameType, Player, PlayerID, PlayerType, Tile, UnitType } from "../core/game/Game"
|
||||
import { ClientID, ClientIntentMessageSchema, ClientJoinMessageSchema, GameID, Intent, ServerMessage, ServerMessageSchema, ClientPingMessageSchema, GameConfig, ClientLogMessageSchema } from "../core/Schemas"
|
||||
import { ClientID, ClientIntentMessageSchema, ClientJoinMessageSchema, GameID, Intent, ServerMessage, ServerMessageSchema, ClientPingMessageSchema, GameConfig, ClientLogMessageSchema, LogSeverity } from "../core/Schemas"
|
||||
import { LobbyConfig } from "./GameRunner"
|
||||
import { LocalServer } from "./LocalServer"
|
||||
import { LogSeverity } from "./LogSender"
|
||||
|
||||
|
||||
export class SendAllianceRequestIntentEvent implements GameEvent {
|
||||
@@ -206,6 +205,7 @@ export class Transport {
|
||||
clientID: this.lobbyConfig.clientID,
|
||||
persistentID: this.lobbyConfig.persistentID,
|
||||
log: event.log,
|
||||
severity: event.severity,
|
||||
})
|
||||
)
|
||||
)
|
||||
|
||||
+9
-1
@@ -48,6 +48,14 @@ export type GameRecord = z.infer<typeof GameRecordSchema>
|
||||
|
||||
const PlayerTypeSchema = z.nativeEnum(PlayerType);
|
||||
|
||||
export enum LogSeverity {
|
||||
Debug = 'DEBUG',
|
||||
Info = 'INFO',
|
||||
Warn = 'WARN',
|
||||
Error = 'ERROR',
|
||||
Fatal = 'FATAL'
|
||||
}
|
||||
|
||||
// TODO: create Cell schema
|
||||
|
||||
export interface Lobby {
|
||||
@@ -216,7 +224,7 @@ const ClientBaseMessageSchema = z.object({
|
||||
|
||||
export const ClientLogMessageSchema = ClientBaseMessageSchema.extend({
|
||||
type: z.literal('log'),
|
||||
severity: z.enum([''])
|
||||
severity: z.nativeEnum(LogSeverity),
|
||||
log: z.string(),
|
||||
})
|
||||
|
||||
|
||||
@@ -4,9 +4,9 @@ import { WebSocketServer } from 'ws';
|
||||
import path from 'path';
|
||||
import { fileURLToPath } from 'url';
|
||||
import { GameManager } from './GameManager';
|
||||
import { ClientMessage, ClientMessageSchema, GameRecord, GameRecordSchema, Turn, TurnSchema } from '../core/Schemas';
|
||||
import { ClientMessage, ClientMessageSchema, GameRecord, GameRecordSchema, LogSeverity } from '../core/Schemas';
|
||||
import { getConfig } from '../core/configuration/Config';
|
||||
import { LogSeverity, slog } from './StructuredLog';
|
||||
import { slog } from './StructuredLog';
|
||||
import { Client } from './Client';
|
||||
import { GamePhase, GameServer } from './GameServer';
|
||||
import { archive } from './Archive';
|
||||
@@ -68,7 +68,7 @@ app.post('/archive_singleplayer_game', (req, res) => {
|
||||
success: true,
|
||||
});
|
||||
} catch (error) {
|
||||
slog('complete_single_player_game_record', 'Failed to complete game record', { error }, LogSeverity.ERROR);
|
||||
slog('complete_single_player_game_record', 'Failed to complete game record', { error }, LogSeverity.Error);
|
||||
res.status(400).json({ error: 'Invalid game record format' });
|
||||
}
|
||||
})
|
||||
@@ -103,7 +103,7 @@ app.get('/private_lobby/:id', (req, res) => {
|
||||
wss.on('connection', (ws, req) => {
|
||||
ws.on('message', (message: string) => {
|
||||
const clientMsg: ClientMessage = ClientMessageSchema.parse(JSON.parse(message))
|
||||
slog('websocket_msg', 'server received websocket message', clientMsg, LogSeverity.DEBUG)
|
||||
slog('websocket_msg', 'server received websocket message', clientMsg, LogSeverity.Debug)
|
||||
if (clientMsg.type == "join") {
|
||||
const forwarded = req.headers['x-forwarded-for']
|
||||
const ip = Array.isArray(forwarded)
|
||||
@@ -122,8 +122,8 @@ wss.on('connection', (ws, req) => {
|
||||
clientMsg.lastTurn
|
||||
)
|
||||
}
|
||||
if(clientMsg.type == "log") {
|
||||
console.log()
|
||||
if (clientMsg.type == "log") {
|
||||
console.log(clientMsg.log)
|
||||
}
|
||||
})
|
||||
});
|
||||
|
||||
@@ -1,12 +1,6 @@
|
||||
export enum LogSeverity {
|
||||
DEBUG = 'DEBUG',
|
||||
INFO = 'INFO',
|
||||
WARN = 'WARN',
|
||||
ERROR = 'ERROR',
|
||||
FATAL = 'FATAL'
|
||||
}
|
||||
import { LogSeverity } from "../core/Schemas";
|
||||
|
||||
export function slog(eventType: string, description, data: any, severity = LogSeverity.INFO): void {
|
||||
export function slog(eventType: string, description, data: any, severity = LogSeverity.Info): void {
|
||||
const logEntry = {
|
||||
eventType: eventType,
|
||||
description: description,
|
||||
@@ -14,7 +8,7 @@ export function slog(eventType: string, description, data: any, severity = LogSe
|
||||
data: data
|
||||
};
|
||||
if (process.env.GAME_ENV == 'dev') {
|
||||
if (severity != LogSeverity.DEBUG) {
|
||||
if (severity != LogSeverity.Debug) {
|
||||
console.log(description)
|
||||
}
|
||||
} else {
|
||||
|
||||
Reference in New Issue
Block a user