import { Server } from 'socket.io' import { TSocket } from '../../../../utilities/types' import path from 'path' import fs from 'fs' import prisma from '../../../../utilities/prisma' type Payload = { id: string } /** * Handle game master remove tile event * @param socket * @param io */ export default function (socket: TSocket, io: Server) { socket.on('gm:tile:remove', async (data: Payload, callback: (response: boolean) => void) => { if (socket.character?.role !== 'gm') { return } try { await prisma.tile.delete({ where: { id: data.id } }) // get root path const public_folder = path.join(process.cwd(), 'public', 'tiles') // remove the tile from the disk const finalFilePath = path.join(public_folder, data.id + '.png') fs.unlink(finalFilePath, (err) => { if (err) { console.log(err) callback(false) return } callback(true) }) } catch (e) { console.log(e) callback(false) } }) }