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