1
0
forked from noxious/server
Files
noxious_server/src/events/character/connect.ts
2025-02-21 22:05:46 +01:00

90 lines
3.2 KiB
TypeScript

import { BaseEvent } from '@/application/base/baseEvent'
import { SocketEvent } from '@/application/enums'
import type { UUID } from '@/application/types'
import { ZCharacterConnect } from '@/application/zodTypes'
import MapManager from '@/managers/mapManager'
import CharacterHairRepository from '@/repositories/characterHairRepository'
import CharacterRepository from '@/repositories/characterRepository'
import TeleportService from '@/services/characterTeleportService'
interface CharacterConnectPayload {
characterId: UUID
characterHairId: UUID | null
newNickname?: string
}
export default class CharacterConnectEvent extends BaseEvent {
private readonly characterHairRepository = new CharacterHairRepository()
private readonly characterRepository = new CharacterRepository()
public listen(): void {
this.socket.on(SocketEvent.CHARACTER_CONNECT, this.handleEvent.bind(this))
}
private async handleEvent(data: CharacterConnectPayload, callback: (response: any) => void): Promise<void> {
try {
if (data.newNickname === '') data.newNickname = undefined
const result = ZCharacterConnect.safeParse(data)
if (!result.success) {
this.sendNotificationAndLog(result.error?.errors[0]?.message ?? 'Invalid data')
return
}
if (await this.checkForActiveCharacters()) {
this.sendNotificationAndLog('You are already connected to another character')
return
}
let character = await this.characterRepository.getByUserAndId(this.socket.userId!, data.characterId)
if (!character) {
this.sendNotificationAndLog('Character not found or does not belong to this user')
return
}
if (data.newNickname) {
const existingCharacter = await this.characterRepository.getByName(data.newNickname)
if (existingCharacter) {
this.sendNotificationAndLog('Nickname already in use: ' + data.newNickname)
return
}
await character.setName(data.newNickname).save()
}
// Set character id
this.socket.characterId = character.id
// Set character hair
if (data.characterHairId !== undefined && data.characterHairId !== null) {
const characterHair = await this.characterHairRepository.getById(data.characterHairId)
await character.setCharacterHair(characterHair).save()
} else {
await character.setCharacterHair(null).save()
}
// Emit character connect event
callback({ character })
// wait 300 ms, @TODO: Find a better way to do this, race condition
await new Promise((resolve) => setTimeout(resolve, 500))
await TeleportService.teleportCharacter(character.id, {
targetMapId: character.map.id,
targetX: character.positionX,
targetY: character.positionY,
rotation: character.rotation,
isInitialJoin: true,
character
})
} catch (error) {
this.handleError('Failed to connect character', error)
}
}
private async checkForActiveCharacters(): Promise<boolean> {
const characters = await this.characterRepository.getByUserId(this.socket.userId!)
return characters?.some((char) => MapManager.getCharacterById(char.id)) ?? false
}
}