Continuation of refactor

This commit is contained in:
2024-12-28 20:40:05 +01:00
parent 6dda79f8b2
commit e1a6f650fb
27 changed files with 158 additions and 236 deletions

View File

@ -4,10 +4,11 @@ import * as readline from 'readline'
import { Server } from 'socket.io'
import { commandLogger } from '#application/logger'
import Logger, { LoggerType } from '#application/logger'
import { getAppPath } from '#application/storage'
class CommandManager {
private logger = Logger.type(LoggerType.COMMAND)
private commands: Map<string, any> = new Map()
private rl: readline.Interface
private io: Server | null = null
@ -27,7 +28,7 @@ class CommandManager {
public async boot(io: Server) {
this.io = io
await this.loadCommands()
commandLogger.info('Command manager loaded')
this.logger.info('Command manager loaded')
this.startPrompt()
}
@ -64,7 +65,7 @@ class CommandManager {
private async loadCommands() {
const directory = getAppPath('commands')
commandLogger.info(`Loading commands from: ${directory}`)
this.logger.info(`Loading commands from: ${directory}`)
try {
const files = await fs.promises.readdir(directory, { withFileTypes: true })
@ -80,26 +81,26 @@ class CommandManager {
try {
const module = await import(fullPath)
if (typeof module.default !== 'function') {
commandLogger.warn(`Unrecognized export in ${file.name}`)
this.logger.warn(`Unrecognized export in ${file.name}`)
continue
}
this.registerCommand(commandName, module.default)
} catch (error) {
commandLogger.error(`Error loading command ${file.name}: ${error instanceof Error ? error.message : String(error)}`)
this.logger.error(`Error loading command ${file.name}: ${error instanceof Error ? error.message : String(error)}`)
}
}
} catch (error) {
commandLogger.error(`Failed to read commands directory: ${error instanceof Error ? error.message : String(error)}`)
this.logger.error(`Failed to read commands directory: ${error instanceof Error ? error.message : String(error)}`)
}
}
private registerCommand(name: string, CommandClass: any) {
if (this.commands.has(name)) {
commandLogger.warn(`Command '${name}' is already registered. Overwriting...`)
this.logger.warn(`Command '${name}' is already registered. Overwriting...`)
}
this.commands.set(name, CommandClass)
commandLogger.info(`Registered command: ${name}`)
this.logger.info(`Registered command: ${name}`)
}
}

View File

@ -1,6 +1,6 @@
import { Server } from 'socket.io'
import { appLogger } from '#application/logger'
import Logger, { LoggerType } from '#application/logger'
import worldRepository from '#repositories/worldRepository'
import worldService from '#services/worldService'
@ -11,12 +11,13 @@ class DateManager {
private io: Server | null = null
private intervalId: NodeJS.Timeout | null = null
private currentDate: Date = new Date()
private logger = Logger.type(LoggerType.APP)
public async boot(io: Server): Promise<void> {
this.io = io
await this.loadDate()
this.startDateLoop()
appLogger.info('Date manager loaded')
this.logger.info('Date manager loaded')
}
public async setTime(time: string): Promise<void> {
@ -38,7 +39,7 @@ class DateManager {
this.emitDate()
await this.saveDate()
} catch (error) {
appLogger.error(`Failed to set time: ${error instanceof Error ? error.message : String(error)}`)
this.logger.error(`Failed to set time: ${error instanceof Error ? error.message : String(error)}`)
throw error
}
}
@ -51,7 +52,7 @@ class DateManager {
this.currentDate = world.date
}
} catch (error) {
appLogger.error(`Failed to load date: ${error instanceof Error ? error.message : String(error)}`)
this.logger.error(`Failed to load date: ${error instanceof Error ? error.message : String(error)}`)
this.currentDate = new Date() // Use current date as fallback
}
}
@ -82,7 +83,7 @@ class DateManager {
date: this.currentDate
})
} catch (error) {
appLogger.error(`Failed to save date: ${error instanceof Error ? error.message : String(error)}`)
this.logger.error(`Failed to save date: ${error instanceof Error ? error.message : String(error)}`)
}
}

View File

@ -5,7 +5,7 @@ import IORedis from 'ioredis'
import { Server as SocketServer } from 'socket.io'
import config from '#application/config'
import { queueLogger } from '#application/logger'
import Logger, { LoggerType } from '#application/logger'
import { getAppPath } from '#application/storage'
import { TSocket } from '#application/types'
@ -14,6 +14,7 @@ class QueueManager {
private queue!: Queue
private worker!: Worker
private io!: SocketServer
private logger = Logger.type(LoggerType.QUEUE)
public async boot(io: SocketServer) {
this.io = io
@ -24,9 +25,9 @@ class QueueManager {
try {
await this.connection.ping()
queueLogger.info('Successfully connected to Redis')
this.logger.info('Successfully connected to Redis')
} catch (error) {
queueLogger.error('Failed to connect to Redis:', error)
this.logger.error('Failed to connect to Redis:', error)
process.exit(1)
}
@ -40,14 +41,14 @@ class QueueManager {
})
this.worker.on('completed', (job) => {
queueLogger.info(`Job ${job?.id} has completed`)
this.logger.info(`Job ${job?.id} has completed`)
})
this.worker.on('failed', (job, err) => {
queueLogger.error(`Job ${job?.id} failed with error: ${err}`)
this.logger.error(`Job ${job?.id} failed with error: ${err}`)
})
queueLogger.info('Queue manager loaded')
this.logger.info('Queue manager loaded')
}
private async processJob(job: Job) {
@ -59,7 +60,7 @@ class QueueManager {
const jobPath = getAppPath('jobs', `${jobName}${extension}`)
if (!fs.existsSync(jobPath)) {
queueLogger.warn(`Job file not found: ${jobPath}`)
this.logger.warn(`Job file not found: ${jobPath}`)
return
}
@ -67,7 +68,7 @@ class QueueManager {
const JobClass = JobModule.default
if (!JobClass || typeof JobClass !== 'function') {
queueLogger.warn(`Invalid job class in file: ${jobPath}`)
this.logger.warn(`Invalid job class in file: ${jobPath}`)
return
}
@ -78,14 +79,14 @@ class QueueManager {
if (socket) {
await jobInstance.execute(this.io, socket)
} else {
queueLogger.warn(`Socket not found for job: ${socketId}`)
this.logger.warn(`Socket not found for job: ${socketId}`)
await jobInstance.execute(this.io)
}
} else {
await jobInstance.execute(this.io)
}
} catch (error: any) {
queueLogger.error(`Error processing job ${jobName}: ${error.message}`)
this.logger.error(`Error processing job ${jobName}: ${error.message}`)
}
}

View File

@ -1,6 +1,6 @@
import { User } from '@prisma/client'
import { appLogger } from '#application/logger'
import Logger, { LoggerType } from '#application/logger'
type TLoggedInUsers = {
users: User[]
@ -8,10 +8,11 @@ type TLoggedInUsers = {
class UserManager {
private loggedInUsers: TLoggedInUsers[] = []
private logger = Logger.type(LoggerType.APP)
// Method to initialize user manager
public async boot() {
appLogger.info('User manager loaded')
this.logger.info('User manager loaded')
}
// Function that adds user to logged in users

View File

@ -1,4 +1,4 @@
import { gameLogger } from '#application/logger'
import Logger, { LoggerType } from '#application/logger'
import { Zone } from '#entities/zone'
import LoadedZone from '#models/loadedZone'
import ZoneCharacter from '#models/zoneCharacter'
@ -6,23 +6,24 @@ import ZoneRepository from '#repositories/zoneRepository'
class ZoneManager {
private readonly zones = new Map<number, LoadedZone>()
private logger = Logger.type(LoggerType.GAME)
public async boot(): Promise<void> {
const zones = await ZoneRepository.getAll()
await Promise.all(zones.map((zone) => this.loadZone(zone)))
gameLogger.info(`Zone manager loaded with ${this.zones.size} zones`)
this.logger.info(`Zone manager loaded with ${this.zones.size} zones`)
}
public async loadZone(zone: Zone): Promise<void> {
const loadedZone = new LoadedZone(zone)
this.zones.set(zone.id, loadedZone)
gameLogger.info(`Zone ID ${zone.id} loaded`)
this.logger.info(`Zone ID ${zone.id} loaded`)
}
public unloadZone(zoneId: number): void {
this.zones.delete(zoneId)
gameLogger.info(`Zone ID ${zoneId} unloaded`)
this.logger.info(`Zone ID ${zoneId} unloaded`)
}
public getLoadedZones(): LoadedZone[] {