1
0
forked from noxious/server

asset mngr stuff

This commit is contained in:
2024-06-29 22:39:14 +02:00
parent 93adb85253
commit 6f6fddd861
10 changed files with 661 additions and 15 deletions

View File

@ -14,6 +14,11 @@ interface IPayload {
export default function (socket: TSocket, io: Server) {
socket.on('gm:tile:list', async (data: any, callback: (response: string[]) => 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');

View File

@ -0,0 +1,39 @@
import { Server } from "socket.io";
import {TSocket} from "../../utilities/Types";
import {writeFile} from "node:fs";
import {randomUUID} from "node:crypto";
import path from "path";
import fs from "fs";
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;
}
// 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);
fs.unlink(finalFilePath, (err) => {
if (err) {
console.log(err);
callback(false);
return;
}
callback(true);
});
});
}

View File

@ -17,7 +17,9 @@ interface IPayload {
export default function (socket: TSocket, io: Server) {
socket.on('gm:tile:upload', async (data: any, callback: (response: boolean) => void) => {
// @TODO : check if socket is gm
if (socket.character?.role !== 'gm') {
return;
}
// get root path
const public_folder = path.join(process.cwd(), 'public', 'tiles');
@ -28,13 +30,18 @@ export default function (socket: TSocket, io: Server) {
}
for (const key in data) {
const filename = randomUUID();
// the files in the folder are named 0.png, 1.png, 2.png etc... check the last file name and add 1
const files = fs.readdirSync(public_folder);
const lastFile = files[files.length - 1];
const lastFileName = lastFile?.split('.')[0];
const filename = `${parseInt(lastFileName ?? 0) + 1}.png`;
const finalFilePath = path.join(public_folder, filename);
const tile = data[key];
// save the tile to the disk, for example
writeFile(finalFilePath, tile, (err) => {});
// return true to the client
callback(true);
}
});

View File

@ -15,6 +15,11 @@ interface IPayload {
*/
export default function (socket: TSocket, io: Server) {
socket.on('gm:zone_editor:zone:request', async (data: IPayload) => {
if (socket.character?.role !== 'gm') {
return;
}
console.log(`---GM ${socket.character?.id} has requested zone via zone editor.`);
if (!data.zoneId) {

View File

@ -20,6 +20,12 @@ interface IPayload {
*/
export default function (socket: TSocket, io: Server) {
socket.on('gm:zone_editor:zone:save', async (data: IPayload) => {
if (socket.character?.role !== 'gm') {
console.log(`---Character #${socket.character?.id} is not a game master.`);
return;
}
console.log(`---GM ${socket.character?.id} has saved zone via zone editor.`);
console.log(data);