diff --git a/src/socketEvents/character/connect.ts b/src/socketEvents/character/connect.ts index 2beab2c..30b5767 100644 --- a/src/socketEvents/character/connect.ts +++ b/src/socketEvents/character/connect.ts @@ -1,8 +1,10 @@ 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' -type SocketResponseT = { +interface CharacterConnectPayload { characterId: number } @@ -16,16 +18,51 @@ export default class CharacterConnectEvent { this.socket.on('character:connect', this.handleCharacterConnect.bind(this)) } - private async handleCharacterConnect(data: SocketResponseT): Promise { - console.log('character:connect requested', data) + private async handleCharacterConnect({ characterId }: CharacterConnectPayload): Promise { + if (!this.socket.userId) { + this.emitError('User not authenticated') + return + } + try { - const character = await CharacterRepository.getByUserAndId(this.socket?.userId!, data.characterId!) - if (!character) return + 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) - } catch (error: any) { - console.log('character:connect error', error) + + // 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) + } +} \ No newline at end of file