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);
|
||||
}
|
||||
});
|
||||
}
|
14
src/app/repositories/ObjectRepository.ts
Normal file
14
src/app/repositories/ObjectRepository.ts
Normal file
@ -0,0 +1,14 @@
|
||||
import prisma from '../utilities/Prisma'; // Import the global Prisma instance
|
||||
import { Object } from '@prisma/client'
|
||||
|
||||
class ObjectRepository {
|
||||
getById(id: string): Promise<Object | null> {
|
||||
return prisma.object.findUnique({
|
||||
where: {
|
||||
id,
|
||||
},
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
export default new ObjectRepository();
|
45
src/app/repositories/TileTagRepository.ts
Normal file
45
src/app/repositories/TileTagRepository.ts
Normal file
@ -0,0 +1,45 @@
|
||||
import prisma from '../utilities/Prisma'; // Import the global Prisma instance
|
||||
import { TileTag } from '@prisma/client'
|
||||
|
||||
class TileTagRepository {
|
||||
async upsertTileTag(tile: string, tags: string[]): Promise<TileTag> {
|
||||
return prisma.tileTag.upsert({
|
||||
where: { tile },
|
||||
create: {
|
||||
tile,
|
||||
tags: tags,
|
||||
},
|
||||
update: {
|
||||
tags: tags,
|
||||
},
|
||||
});
|
||||
}
|
||||
|
||||
async getTileTag(tile: string): Promise<TileTag | null> {
|
||||
return prisma.tileTag.findUnique({
|
||||
where: { tile },
|
||||
});
|
||||
}
|
||||
|
||||
async deleteTileTag(tile: string): Promise<TileTag> {
|
||||
return prisma.tileTag.delete({
|
||||
where: { tile },
|
||||
});
|
||||
}
|
||||
|
||||
async searchTilesByTags(tags: string[]): Promise<TileTag[]> {
|
||||
return prisma.tileTag.findMany({
|
||||
where: {
|
||||
tags: {
|
||||
array_contains: tags,
|
||||
} as any, // Type assertion needed due to Json field
|
||||
},
|
||||
});
|
||||
}
|
||||
|
||||
async getAllTileTags(): Promise<TileTag[]> {
|
||||
return prisma.tileTag.findMany();
|
||||
}
|
||||
}
|
||||
|
||||
export default new TileTagRepository();
|
@ -1,9 +1,6 @@
|
||||
|
||||
class AssetService
|
||||
{
|
||||
static generateTileset() {
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
export default AssetService;
|
@ -18,6 +18,12 @@ async function addHttpRoutes(app: Application) {
|
||||
tiles.forEach(tile => {
|
||||
assets.push({key: tile, value: '/tiles/' + tile, group: 'tiles', type: 'link'});
|
||||
});
|
||||
|
||||
const objects = listObjects();
|
||||
objects.forEach(object => {
|
||||
assets.push({key: object, value: '/objects/' + object, group: 'objects', type: 'link'});
|
||||
});
|
||||
|
||||
res.json(assets);
|
||||
});
|
||||
app.get('/assets/:type/:file', (req: Request, res: Response) => {
|
||||
@ -104,4 +110,26 @@ function listTiles(): string[] {
|
||||
console.log(tiles);
|
||||
|
||||
return tiles;
|
||||
}
|
||||
|
||||
function listObjects(): string[] {
|
||||
// get root path
|
||||
const folder = path.join(process.cwd(), 'public', 'objects');
|
||||
|
||||
// list the files in the folder
|
||||
let objects: string[] = [];
|
||||
|
||||
try {
|
||||
const files = fs.readdirSync(folder);
|
||||
|
||||
files.forEach(file => {
|
||||
objects.push(file.replace('.png', ''));
|
||||
});
|
||||
} catch (err) {
|
||||
console.log(err);
|
||||
}
|
||||
|
||||
console.log(objects);
|
||||
|
||||
return objects;
|
||||
}
|
Reference in New Issue
Block a user