1
0
forked from noxious/server

#244: Allow nickname changes

This commit is contained in:
2025-02-19 11:45:43 +01:00
parent 2cbc951816
commit 39d793570d
4 changed files with 36 additions and 4 deletions

View File

@ -1,6 +1,7 @@
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'
@ -9,6 +10,7 @@ import TeleportService from '@/services/characterTeleportService'
interface CharacterConnectPayload {
characterId: UUID
characterHairId?: UUID
newNickname?: string
}
export default class CharacterConnectEvent extends BaseEvent {
@ -21,18 +23,34 @@ export default class CharacterConnectEvent extends BaseEvent {
private async handleEvent(data: CharacterConnectPayload, callback: (response: any) => void): Promise<void> {
try {
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
}
const character = await this.characterRepository.getByUserAndId(this.socket.userId!, data.characterId)
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