48 lines
1.1 KiB
TypeScript
48 lines
1.1 KiB
TypeScript
import { Server } from 'socket.io'
|
|
import { TSocket } from '../../../../utilities/types'
|
|
import prisma from '../../../../utilities/prisma'
|
|
import characterRepository from '../../../../repositories/characterRepository'
|
|
|
|
type Payload = {
|
|
id: string
|
|
name: string
|
|
tags: string[]
|
|
}
|
|
|
|
export default class TileUpdateEvent {
|
|
constructor(
|
|
private readonly io: Server,
|
|
private readonly socket: TSocket
|
|
) {}
|
|
|
|
public listen(): void {
|
|
this.socket.on('gm:tile:update', this.handleTileUpdate.bind(this))
|
|
}
|
|
|
|
private async handleTileUpdate(data: Payload, callback: (success: boolean) => void): Promise<void> {
|
|
const character = await characterRepository.getById(this.socket.characterId as number)
|
|
if (!character) return callback(false)
|
|
|
|
if (character.role !== 'gm') {
|
|
return
|
|
}
|
|
|
|
try {
|
|
const Tile = await prisma.tile.update({
|
|
where: {
|
|
id: data.id
|
|
},
|
|
data: {
|
|
name: data.name,
|
|
tags: data.tags
|
|
}
|
|
})
|
|
|
|
callback(true)
|
|
} catch (error) {
|
|
console.error(error)
|
|
callback(false)
|
|
}
|
|
}
|
|
}
|