#244: Allow nickname changes

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

View File

@ -46,14 +46,15 @@ export abstract class BaseEvent {
}
protected sendNotificationAndLog(message: string): void {
console.log(message)
this.socket.emit(SocketEvent.NOTIFICATION, { title: 'Server message', message })
this.logger.error('Base event error', `Player ${this.socket.userId}: ${message}`)
this.logger.error('Base event error' + `Player ${this.socket.userId}: ${message}`)
}
protected handleError(context: string, error: unknown): void {
console.log(error)
const errorMessage = error instanceof Error ? error.message : error && typeof error === 'object' && 'toString' in error ? error.toString() : String(error)
this.socket.emit(SocketEvent.NOTIFICATION, { title: 'Server message', message: `Server error occured. Please contact the server administrator.` })
this.logger.error('Base event error', errorMessage)
this.logger.error('Base event error: ' + errorMessage)
}
}

View File

@ -58,3 +58,16 @@ export const ZCharacterCreate = z.object({
.max(255, { message: 'Name must be at most 255 characters long' })
.regex(/^[A-Za-z][A-Za-z0-9_-]*$/, { message: 'Name must start with a letter and can only contain letters, numbers, underscores, or dashes' })
})
export const ZCharacterConnect = z.object({
characterId: z.string(),
characterHairId: z.string().optional(),
newNickname: z
.string()
.min(3, { message: 'Name must be at least 3 characters long' })
.max(255, { message: 'Name must be at most 255 characters long' })
.regex(/^[A-Za-z][A-Za-z0-9_-]*$/, {
message: 'Name must start with a letter and can only contain letters, numbers, underscores, or dashes'
})
.optional()
})

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

View File

@ -32,7 +32,7 @@ export default class CharacterCreateEvent extends BaseEvent {
}
private async createCharacter(data: z.infer<typeof ZCharacterCreate>): Promise<void> {
const user = await this.userRepository.getById(this.socket.userId)
const user = await this.userRepository.getById(this.socket.userId!)
if (!user) {
throw new Error('You are not logged in')
}