Worked on zone objects, tile tags and searching
This commit is contained in:
@ -1,5 +1,5 @@
|
||||
import { Server } from "socket.io";
|
||||
import {TSocket} from "../../utilities/Types";
|
||||
import {TSocket} from "../../../utilities/Types";
|
||||
import fs from 'fs';
|
||||
import path from "path";
|
||||
|
@ -1,5 +1,5 @@
|
||||
import { Server } from "socket.io";
|
||||
import {TSocket} from "../../utilities/Types";
|
||||
import {TSocket} from "../../../utilities/Types";
|
||||
import {writeFile} from "node:fs";
|
||||
import {randomUUID} from "node:crypto";
|
||||
import path from "path";
|
30
src/app/events/gm/objects/GmTileTags.ts
Normal file
30
src/app/events/gm/objects/GmTileTags.ts
Normal file
@ -0,0 +1,30 @@
|
||||
import { Server } from "socket.io";
|
||||
import {TSocket} from "../../../utilities/Types";
|
||||
import TileTagRepository from "../../../repositories/TileTagRepository";
|
||||
|
||||
interface IPayload {
|
||||
tile: string;
|
||||
}
|
||||
|
||||
/**
|
||||
* Handle game master tile tags update event
|
||||
* @param socket
|
||||
* @param io
|
||||
*/
|
||||
export default function (socket: TSocket, io: Server) {
|
||||
socket.on('gm:tile:tags', async (data: IPayload, callback: (response: string[]) => void) => {
|
||||
|
||||
if (socket.character?.role !== 'gm') {
|
||||
return;
|
||||
}
|
||||
|
||||
// update the tile tags
|
||||
try {
|
||||
const tileTag = await TileTagRepository.getTileTag(data.tile);
|
||||
callback(tileTag ? tileTag.tags as string[] : []);
|
||||
} catch (error) {
|
||||
console.log(error);
|
||||
callback([]);
|
||||
}
|
||||
});
|
||||
}
|
31
src/app/events/gm/objects/GmTileTagsUpdate.ts
Normal file
31
src/app/events/gm/objects/GmTileTagsUpdate.ts
Normal file
@ -0,0 +1,31 @@
|
||||
import { Server } from "socket.io";
|
||||
import {TSocket} from "../../../utilities/Types";
|
||||
import TileTagRepository from "../../../repositories/TileTagRepository";
|
||||
|
||||
interface IPayload {
|
||||
tile: string;
|
||||
tags: string[];
|
||||
}
|
||||
|
||||
/**
|
||||
* Handle game master tile tags update event
|
||||
* @param socket
|
||||
* @param io
|
||||
*/
|
||||
export default function (socket: TSocket, io: Server) {
|
||||
socket.on('gm:tile:tags:update', async (data: IPayload, callback: (response: boolean) => void) => {
|
||||
|
||||
if (socket.character?.role !== 'gm') {
|
||||
return;
|
||||
}
|
||||
|
||||
// update the tile tags
|
||||
try {
|
||||
await TileTagRepository.upsertTileTag(data.tile, data.tags);
|
||||
callback(true);
|
||||
} catch (error) {
|
||||
console.log(error);
|
||||
callback(false);
|
||||
}
|
||||
});
|
||||
}
|
@ -1,5 +1,5 @@
|
||||
import { Server } from "socket.io";
|
||||
import { TSocket } from "../../utilities/Types";
|
||||
import { TSocket } from "../../../utilities/Types";
|
||||
import { writeFile } from "node:fs/promises";
|
||||
import path from "path";
|
||||
import fs from "fs/promises";
|
33
src/app/events/gm/tiles/GmObjectDetails.ts
Normal file
33
src/app/events/gm/tiles/GmObjectDetails.ts
Normal file
@ -0,0 +1,33 @@
|
||||
import { Server } from "socket.io";
|
||||
import {TSocket} from "../../../utilities/Types";
|
||||
import ObjectRepository from '../../../repositories/ObjectRepository'
|
||||
import { Object } from '@prisma/client'
|
||||
|
||||
interface IPayload {
|
||||
object: string;
|
||||
}
|
||||
|
||||
// callback will return Object from Prisma
|
||||
type TCallback = (object: Object | null) => void;
|
||||
|
||||
/**
|
||||
* Handle game master object details fetch event
|
||||
* @param socket
|
||||
* @param io
|
||||
*/
|
||||
export default function (socket: TSocket, io: Server) {
|
||||
socket.on('gm:object:details', async (data: IPayload, callback: TCallback) => {
|
||||
|
||||
if (socket.character?.role !== 'gm') {
|
||||
return;
|
||||
}
|
||||
|
||||
try {
|
||||
const object = await ObjectRepository.getById(data.object);
|
||||
callback(object);
|
||||
} catch (error) {
|
||||
console.error(error);
|
||||
callback(null);
|
||||
}
|
||||
});
|
||||
}
|
42
src/app/events/gm/tiles/GmObjectList.ts
Normal file
42
src/app/events/gm/tiles/GmObjectList.ts
Normal file
@ -0,0 +1,42 @@
|
||||
import { Server } from "socket.io";
|
||||
import {TSocket} from "../../../utilities/Types";
|
||||
import fs from 'fs';
|
||||
import path from "path";
|
||||
|
||||
interface IPayload {
|
||||
}
|
||||
|
||||
/**
|
||||
* Handle game master list objects event
|
||||
* @param socket
|
||||
* @param io
|
||||
*/
|
||||
export default function (socket: TSocket, io: Server) {
|
||||
socket.on('gm:object: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', 'objects');
|
||||
|
||||
// list the files in the folder
|
||||
let objects: string[] = [];
|
||||
|
||||
fs.readdir(folder, (err, files) => {
|
||||
if (err) {
|
||||
console.log(err);
|
||||
return;
|
||||
}
|
||||
|
||||
files.forEach(file => {
|
||||
objects.push(file.replace('.png', ''));
|
||||
});
|
||||
|
||||
// send over the list of objects to the socket
|
||||
callback(objects);
|
||||
});
|
||||
});
|
||||
}
|
39
src/app/events/gm/tiles/GmObjectRemove.ts
Normal file
39
src/app/events/gm/tiles/GmObjectRemove.ts
Normal 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 {
|
||||
object: string;
|
||||
}
|
||||
|
||||
/**
|
||||
* Handle game master remove object event
|
||||
* @param socket
|
||||
* @param io
|
||||
*/
|
||||
export default function (socket: TSocket, io: Server) {
|
||||
socket.on('gm:object: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', 'objects');
|
||||
|
||||
// remove the tile from the disk
|
||||
const finalFilePath = path.join(public_folder, data.object);
|
||||
fs.unlink(finalFilePath, (err) => {
|
||||
if (err) {
|
||||
console.log(err);
|
||||
callback(false);
|
||||
return;
|
||||
}
|
||||
|
||||
callback(true);
|
||||
});
|
||||
});
|
||||
}
|
45
src/app/events/gm/tiles/GmObjectUpload.ts
Normal file
45
src/app/events/gm/tiles/GmObjectUpload.ts
Normal file
@ -0,0 +1,45 @@
|
||||
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 { randomUUID } from 'node:crypto';
|
||||
|
||||
interface IObjectData {
|
||||
[key: string]: Buffer;
|
||||
}
|
||||
|
||||
/**
|
||||
* Handle game master upload object event
|
||||
* @param socket
|
||||
* @param io
|
||||
*/
|
||||
export default function (socket: TSocket, io: Server) {
|
||||
socket.on('gm:object:upload', async (data: IObjectData, callback: (response: boolean) => void) => {
|
||||
try {
|
||||
if (socket.character?.role !== 'gm') {
|
||||
callback(false);
|
||||
return;
|
||||
}
|
||||
|
||||
const public_folder = path.join(process.cwd(), 'public', 'objects');
|
||||
|
||||
// Ensure the folder exists
|
||||
await fs.mkdir(public_folder, { recursive: true });
|
||||
|
||||
const uploadPromises = Object.entries(data).map(async ([key, objectData]) => {
|
||||
const uuid = randomUUID();
|
||||
const filename = `${uuid}.png`;
|
||||
const finalFilePath = path.join(public_folder, filename);
|
||||
await writeFile(finalFilePath, objectData);
|
||||
});
|
||||
|
||||
await Promise.all(uploadPromises);
|
||||
|
||||
callback(true);
|
||||
} catch (error) {
|
||||
console.error('Error uploading objects:', error);
|
||||
callback(false);
|
||||
}
|
||||
});
|
||||
}
|
Reference in New Issue
Block a user