forked from noxious/server
26 lines
863 B
TypeScript
26 lines
863 B
TypeScript
import { Socket, Server } from 'socket.io'
|
|
import { TSocket } from '../../utilities/types'
|
|
import { Character } from '@prisma/client'
|
|
import CharacterRepository from '../../repositories/characterRepository'
|
|
|
|
export default class CharacterListEvent {
|
|
constructor(
|
|
private readonly io: Server,
|
|
private readonly socket: TSocket
|
|
) {}
|
|
|
|
public listen(): void {
|
|
this.socket.on('character:list', this.handleCharacterList.bind(this))
|
|
}
|
|
|
|
private async handleCharacterList(data: any): Promise<void> {
|
|
try {
|
|
console.log('character:list requested')
|
|
const user_id = this.socket.user?.id as number
|
|
const characters: Character[] = (await CharacterRepository.getByUserId(user_id)) as Character[]
|
|
this.socket.emit('character:list', characters)
|
|
} catch (error: any) {
|
|
console.log('character:list error', error)
|
|
}
|
|
}
|
|
} |