#213: Prevent character:connect if user is already logged in from another character
This commit is contained in:
parent
86ed3ae4b0
commit
344ddbaf39
@ -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<void> {
|
||||
console.log('character:connect requested', data)
|
||||
private async handleCharacterConnect({ characterId }: CharacterConnectPayload): Promise<void> {
|
||||
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<boolean> {
|
||||
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)
|
||||
}
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user