refactor frontend and fix debug mode

This commit is contained in:
jwinterm
2025-05-29 13:40:44 -04:00
parent de3eed792b
commit f02e16272e
28 changed files with 422 additions and 376 deletions
+18 -5
View File
@@ -1,5 +1,8 @@
const { Client } = require('pg');
// Environment-based console logging control
const CONSOLE_LOGGING = process.env.NODE_ENV === 'debug' || process.env.NODE_ENV === 'development';
const client = new Client({
user: 'postgres',
host: 'localhost',
@@ -21,7 +24,11 @@ function initializeDatabase() {
`;
client.query(createTableQuery)
.then(() => console.log("Database initialized"))
.then(() => {
if (CONSOLE_LOGGING) {
console.log("Database initialized");
}
})
.catch(err => console.error("Database initialization error:", err));
}
@@ -43,7 +50,9 @@ function insertVisitor(socketid, ip, cookie) {
// If table doesn't exist yet, try to initialize again
if (res.rows[0].to_regclass === null) {
console.log("Table 'visitors' doesn't exist yet, creating it now...");
if (CONSOLE_LOGGING) {
console.log("Table 'visitors' doesn't exist yet, creating it now...");
}
initializeDatabase();
return;
}
@@ -51,11 +60,15 @@ function insertVisitor(socketid, ip, cookie) {
// Insert the visitor
client.query(insertVisitorText, values, (err, res) => {
if (err) {
console.log(err.stack);
console.error("Database insert error:", err.stack);
} else if (res.rows && res.rows[0]) {
console.log("Visitor inserted:", res.rows[0]);
if (CONSOLE_LOGGING) {
console.log("Visitor inserted:", res.rows[0]);
}
} else {
console.log("Visitor inserted");
if (CONSOLE_LOGGING) {
console.log("Visitor inserted");
}
}
});
});
+12 -3
View File
@@ -1,5 +1,8 @@
const Game = require('../game/game'); // Ensure Game is required
// Environment-based console logging control
const CONSOLE_LOGGING = process.env.NODE_ENV === 'debug' || process.env.NODE_ENV === 'development';
// User class definition
class User {
constructor(id, address) {
@@ -22,7 +25,9 @@ class User {
// Add user to the registry upon creation
userRegistry.set(id, this);
console.log(`User created and registered: ${id}`);
if (CONSOLE_LOGGING) {
console.log(`User created and registered: ${id}`);
}
}
/**
@@ -32,7 +37,9 @@ class User {
joinGame(gameInstance) {
this.currentGame = gameInstance;
this.stats.gamesPlayed++;
console.log(`[User.joinGame] User ${this.id} joined game ${gameInstance.id}. Total games played: ${this.stats.gamesPlayed}`);
if (CONSOLE_LOGGING) {
console.log(`[User.joinGame] User ${this.id} joined game ${gameInstance.id}. Total games played: ${this.stats.gamesPlayed}`);
}
}
/**
@@ -64,7 +71,9 @@ class User {
this.stats.monstersDefeated += gameStats.monstersDefeated;
}
console.log(`[User.endGame] User ${this.id} game ended: ${result}. Score: ${score}. Stats: ${JSON.stringify(this.stats)}`);
if (CONSOLE_LOGGING) {
console.log(`[User.endGame] User ${this.id} game ended: ${result}. Score: ${score}. Stats: ${JSON.stringify(this.stats)}`);
}
}
}