1
0
forked from noxious/server

Updated tiles logics

This commit is contained in:
2024-07-11 19:52:44 +02:00
parent 8682ce01cd
commit c290443742
10 changed files with 114 additions and 86 deletions

View File

@ -1,42 +1,26 @@
import { Server } from "socket.io";
import {TSocket} from "../../../utilities/Types";
import fs from 'fs';
import path from "path";
import { Tile } from '@prisma/client'
import TileRepository from '../../../repositories/TileRepository'
interface IPayload {
}
/**
* Handle game master list object event
* Handle game master list tile event
* @param socket
* @param io
*/
export default function (socket: TSocket, io: Server) {
socket.on('gm:tile:list', async (data: any, callback: (response: string[]) => void) => {
socket.on('gm:tile:list', async (data: any, callback: (response: Tile[]) => void) => {
if (socket.character?.role !== 'gm') {
console.log(`---Character #${socket.character?.id} is not a game master.`);
return;
}
// get root path
const folder = path.join(process.cwd(), 'public', 'tiles');
// list the files in the folder
let tiles: string[] = [];
fs.readdir(folder, (err, files) => {
if (err) {
console.log(err);
return;
}
files.forEach(file => {
tiles.push(file.replace('.png', ''));
});
// send over the list of object to the socket
callback(tiles);
});
// get all tiles
const tiles = await TileRepository.getAll();
callback(tiles);
});
}