import { Server } from 'socket.io' import { TSocket } from '../../utilities/types' import CharacterRepository from '../../repositories/characterRepository' import { gameLogger } from '../../utilities/logger' import ZoneManager from '../../managers/zoneManager' interface CharacterConnectPayload { characterId: 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 }: 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 } const character = await this.connectCharacter(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) // Optionally notify other players or perform additional connection logic this.io.emit('character:joined', { characterId: character.id }) } catch (error) { this.handleError('Failed to connect character', error) } } private async hasActiveCharacter(): Promise { const characters = await CharacterRepository.getByUserId(this.socket.userId!) return characters?.some(char => ZoneManager.getCharacter(char.id)) ?? false } private async connectCharacter(characterId: number) { return CharacterRepository.getByUserAndId(this.socket.userId!, characterId) } private emitError(message: string): void { this.socket.emit('notification', { message, type: 'error' }) 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) } }