import { Server } from 'socket.io' import { TSocket } from '#application/types' import CharacterRepository from '#repositories/characterRepository' import { gameLogger } from '#application/logger' import ZoneManager from '#managers/zoneManager' import { CharacterService } from '#services/characterService' interface CharacterConnectPayload { characterId: number characterHairId?: number } export default class CharacterConnectEvent { constructor( private readonly io: Server, private readonly socket: TSocket ) {} public listen(): void { this.socket.on('character:connect', this.handleCharacterConnect.bind(this)) } private async handleCharacterConnect({ characterId, characterHairId }: CharacterConnectPayload): Promise { if (!this.socket.userId) { this.emitError('User not authenticated') return } try { if (await this.hasActiveCharacter()) { this.emitError('You are already connected to another character') return } // Update hair const characterService = new CharacterService() await characterService.updateHair(characterId, characterHairId ?? null) const character = await CharacterRepository.getByUserAndId(this.socket.userId!, characterId) if (!character) { this.emitError('Character not found or does not belong to this user') return } this.socket.characterId = character.id this.socket.emit('character:connect', character) } catch (error) { this.handleError('Failed to connect character', error) // @TODO : Make global error handler } } private async hasActiveCharacter(): Promise { const characters = await CharacterRepository.getByUserId(this.socket.userId!) return characters?.some((char) => ZoneManager.getCharacter(char.id)) ?? false } private emitError(message: string): void { this.socket.emit('notification', { title: 'Server message', message }) gameLogger.error('character:connect error', `Player ${this.socket.userId}: ${message}`) } private handleError(context: string, error: unknown): void { const errorMessage = error instanceof Error ? error.message : String(error) this.emitError(`${context}: ${errorMessage}`) gameLogger.error('character:connect error', errorMessage) } }