forked from noxious/server
Improved folder structures for better maintainability
This commit is contained in:
26
src/events/gm/tile/GmTileList.ts
Normal file
26
src/events/gm/tile/GmTileList.ts
Normal file
@ -0,0 +1,26 @@
|
||||
import { Server } from "socket.io";
|
||||
import {TSocket} from "../../../utilities/Types";
|
||||
import { Tile } from '@prisma/client'
|
||||
import TileRepository from '../../../repositories/TileRepository'
|
||||
|
||||
interface IPayload {
|
||||
}
|
||||
|
||||
/**
|
||||
* 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: Tile[]) => void) => {
|
||||
|
||||
if (socket.character?.role !== 'gm') {
|
||||
console.log(`---Character #${socket.character?.id} is not a game master.`);
|
||||
return;
|
||||
}
|
||||
|
||||
// get all tiles
|
||||
const tiles = await TileRepository.getAll();
|
||||
callback(tiles);
|
||||
});
|
||||
}
|
45
src/events/gm/tile/GmTileRemove.ts
Normal file
45
src/events/gm/tile/GmTileRemove.ts
Normal file
@ -0,0 +1,45 @@
|
||||
import { Server } from "socket.io";
|
||||
import {TSocket} from "../../../utilities/Types";
|
||||
import path from "path";
|
||||
import fs from "fs";
|
||||
import TileRepository from '../../../repositories/TileRepository'
|
||||
|
||||
interface IPayload {
|
||||
tile: 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: IPayload, callback: (response: boolean) => void) => {
|
||||
|
||||
if (socket.character?.role !== 'gm') {
|
||||
return;
|
||||
}
|
||||
|
||||
try {
|
||||
await TileRepository.delete(data.tile);
|
||||
|
||||
// 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.tile + '.png');
|
||||
fs.unlink(finalFilePath, (err) => {
|
||||
if (err) {
|
||||
console.log(err);
|
||||
callback(false);
|
||||
return;
|
||||
}
|
||||
|
||||
callback(true);
|
||||
});
|
||||
} catch (e) {
|
||||
console.log(e);
|
||||
callback(false);
|
||||
}
|
||||
});
|
||||
}
|
32
src/events/gm/tile/GmTileUpdate.ts
Normal file
32
src/events/gm/tile/GmTileUpdate.ts
Normal file
@ -0,0 +1,32 @@
|
||||
import { Server } from "socket.io";
|
||||
import {TSocket} from "../../../utilities/Types";
|
||||
import TileRepository from '../../../repositories/TileRepository'
|
||||
|
||||
interface IPayload {
|
||||
id: string;
|
||||
name: string;
|
||||
tags: string[];
|
||||
}
|
||||
|
||||
/**
|
||||
* Handle game master tile update event
|
||||
* @param socket
|
||||
* @param io
|
||||
*/
|
||||
export default function (socket: TSocket, io: Server) {
|
||||
socket.on('gm:tile:update', async (data: IPayload, callback: (success: boolean) => void) => {
|
||||
|
||||
if (socket.character?.role !== 'gm') {
|
||||
return;
|
||||
}
|
||||
|
||||
try {
|
||||
const Tile = await TileRepository.update(data.id, data.name, data.tags);
|
||||
|
||||
callback(true);
|
||||
} catch (error) {
|
||||
console.error(error);
|
||||
callback(false);
|
||||
}
|
||||
});
|
||||
}
|
46
src/events/gm/tile/GmTileUpload.ts
Normal file
46
src/events/gm/tile/GmTileUpload.ts
Normal file
@ -0,0 +1,46 @@
|
||||
import { Server } from "socket.io";
|
||||
import { TSocket } from "../../../utilities/Types";
|
||||
import { writeFile } from "node:fs/promises";
|
||||
import path from "path";
|
||||
import fs from "fs/promises";
|
||||
import tileRepository from '../../../repositories/TileRepository'
|
||||
|
||||
interface ITileData {
|
||||
[key: string]: Buffer;
|
||||
}
|
||||
|
||||
/**
|
||||
* Handle game master upload tile event
|
||||
* @param socket
|
||||
* @param io
|
||||
*/
|
||||
export default function (socket: TSocket, io: Server) {
|
||||
socket.on('gm:tile:upload', async (data: ITileData, callback: (response: boolean) => void) => {
|
||||
try {
|
||||
if (socket.character?.role !== 'gm') {
|
||||
callback(false);
|
||||
return;
|
||||
}
|
||||
|
||||
const public_folder = path.join(process.cwd(), 'public', 'tiles');
|
||||
|
||||
// Ensure the folder exists
|
||||
await fs.mkdir(public_folder, { recursive: true });
|
||||
|
||||
const uploadPromises = Object.entries(data).map(async ([key, tileData]) => {
|
||||
const tile = await tileRepository.create('New tile');
|
||||
const uuid = tile.id;
|
||||
const filename = `${uuid}.png`;
|
||||
const finalFilePath = path.join(public_folder, filename);
|
||||
await writeFile(finalFilePath, tileData);
|
||||
});
|
||||
|
||||
await Promise.all(uploadPromises);
|
||||
|
||||
callback(true);
|
||||
} catch (error) {
|
||||
console.error('Error uploading tile:', error);
|
||||
callback(false);
|
||||
}
|
||||
});
|
||||
}
|
Reference in New Issue
Block a user