forked from noxious/server
Moved service logic from repo to service, minor improvements, working hair customisation proof of concept
This commit is contained in:
@ -0,0 +1,22 @@
|
||||
import { Server } from 'socket.io'
|
||||
import { CharacterHair } from '@prisma/client'
|
||||
import { TSocket } from '../../../utilities/types'
|
||||
import characterHairRepository from '../../../repositories/characterHairRepository'
|
||||
|
||||
interface IPayload {}
|
||||
|
||||
export default class characterHairListEvent {
|
||||
constructor(
|
||||
private readonly io: Server,
|
||||
private readonly socket: TSocket
|
||||
) {}
|
||||
|
||||
public listen(): void {
|
||||
this.socket.on('character:hair:list', this.handleEvent.bind(this))
|
||||
}
|
||||
|
||||
private async handleEvent(data: IPayload, callback: (response: CharacterHair[]) => void): Promise<void> {
|
||||
const items = await characterHairRepository.getIsEnabledForCharCreationHair()
|
||||
callback(items)
|
||||
}
|
||||
}
|
@ -3,9 +3,11 @@ import { TSocket } from '../../utilities/types'
|
||||
import CharacterRepository from '../../repositories/characterRepository'
|
||||
import { gameLogger } from '../../utilities/logger'
|
||||
import ZoneManager from '../../managers/zoneManager'
|
||||
import { CharacterService } from '../../services/characterService'
|
||||
|
||||
interface CharacterConnectPayload {
|
||||
characterId: number
|
||||
hairId?: number
|
||||
}
|
||||
|
||||
export default class CharacterConnectEvent {
|
||||
@ -18,7 +20,7 @@ export default class CharacterConnectEvent {
|
||||
this.socket.on('character:connect', this.handleCharacterConnect.bind(this))
|
||||
}
|
||||
|
||||
private async handleCharacterConnect({ characterId }: CharacterConnectPayload): Promise<void> {
|
||||
private async handleCharacterConnect({ characterId, hairId }: CharacterConnectPayload): Promise<void> {
|
||||
if (!this.socket.userId) {
|
||||
this.emitError('User not authenticated')
|
||||
return
|
||||
@ -30,6 +32,10 @@ export default class CharacterConnectEvent {
|
||||
return
|
||||
}
|
||||
|
||||
// Update hair
|
||||
const characterService = new CharacterService()
|
||||
await characterService.updateHair(characterId, hairId ?? null)
|
||||
|
||||
const character = await this.connectCharacter(characterId)
|
||||
if (!character) {
|
||||
this.emitError('Character not found or does not belong to this user')
|
||||
|
@ -2,7 +2,7 @@ import { Server } from 'socket.io'
|
||||
import { TSocket } from '../../utilities/types'
|
||||
import { Character } from '@prisma/client'
|
||||
import CharacterRepository from '../../repositories/characterRepository'
|
||||
import { CharacterService } from '../../services/character/characterService'
|
||||
import { CharacterService } from '../../services/characterService'
|
||||
import { ZCharacterCreate } from '../../utilities/zodTypes'
|
||||
import { gameLogger } from '../../utilities/logger'
|
||||
import { ZodError } from 'zod'
|
||||
|
@ -2,6 +2,7 @@ import { Server } from 'socket.io'
|
||||
import { TSocket } from '../../utilities/types'
|
||||
import { Character, Zone } from '@prisma/client'
|
||||
import CharacterRepository from '../../repositories/characterRepository'
|
||||
import { CharacterService } from '../../services/characterService'
|
||||
|
||||
type TypePayload = {
|
||||
characterId: number
|
||||
@ -24,7 +25,8 @@ export default class CharacterDeleteEvent {
|
||||
|
||||
private async handleCharacterDelete(data: TypePayload, callback: (response: TypeResponse) => void): Promise<any> {
|
||||
try {
|
||||
await CharacterRepository.deleteByUserIdAndId(this.socket.userId!, data.characterId!)
|
||||
const characterService = new CharacterService()
|
||||
await characterService.deleteByUserIdAndId(this.socket.userId!, data.characterId!)
|
||||
|
||||
const characters: Character[] = (await CharacterRepository.getByUserId(this.socket.userId!)) as Character[]
|
||||
|
||||
|
Reference in New Issue
Block a user