1
0
forked from noxious/server

object stuff

This commit is contained in:
Dennis Postma 2024-07-04 13:38:12 +02:00
parent 829a2ef726
commit 9c80efbb51
13 changed files with 67 additions and 52 deletions

View File

@ -0,0 +1,26 @@
import { Server } from "socket.io";
import {TSocket} from "../../../utilities/Types";
import { Object } from '@prisma/client'
import ObjectRepository from '../../../repositories/ObjectRepository'
interface IPayload {
}
/**
* Handle game master list object event
* @param socket
* @param io
*/
export default function (socket: TSocket, io: Server) {
socket.on('gm:object:list', async (data: any, callback: (response: Object[]) => void) => {
if (socket.character?.role !== 'gm') {
console.log(`---Character #${socket.character?.id} is not a game master.`);
return;
}
// get all objects
const objects = await ObjectRepository.getAll();
callback(objects);
});
}

View File

@ -4,6 +4,7 @@ import { writeFile } from "node:fs/promises";
import path from "path";
import fs from "fs/promises";
import { randomUUID } from 'node:crypto';
import objectRepository from '../../../repositories/ObjectRepository'
interface IObjectData {
[key: string]: Buffer;
@ -28,7 +29,8 @@ export default function (socket: TSocket, io: Server) {
await fs.mkdir(public_folder, { recursive: true });
const uploadPromises = Object.entries(data).map(async ([key, objectData]) => {
const uuid = randomUUID();
const object = await objectRepository.create('New object', 0, 0);
const uuid = object.id;
const filename = `${uuid}.png`;
const finalFilePath = path.join(public_folder, filename);
await writeFile(finalFilePath, objectData);
@ -38,7 +40,7 @@ export default function (socket: TSocket, io: Server) {
callback(true);
} catch (error) {
console.error('Error uploading objects:', error);
console.error('Error uploading tile:', error);
callback(false);
}
});

View File

@ -7,7 +7,7 @@ interface IPayload {
}
/**
* Handle game master list tiles event
* Handle game master list object event
* @param socket
* @param io
*/
@ -35,7 +35,7 @@ export default function (socket: TSocket, io: Server) {
tiles.push(file.replace('.png', ''));
});
// send over the list of tiles to the socket
// send over the list of object to the socket
callback(tiles);
});
});

View File

@ -38,7 +38,7 @@ export default function (socket: TSocket, io: Server) {
callback(true);
} catch (error) {
console.error('Error uploading tiles:', error);
console.error('Error uploading object:', error);
callback(false);
}
});

View File

@ -1,42 +0,0 @@
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);
});
});
}

View File

@ -2,13 +2,42 @@ import prisma from '../utilities/Prisma'; // Import the global Prisma instance
import { Object } from '@prisma/client'
class ObjectRepository {
getById(id: string): Promise<Object | null> {
async getById(id: string): Promise<Object | null> {
return prisma.object.findUnique({
where: {
id,
where: { id },
});
}
async getAll(): Promise<Object[]> {
return prisma.object.findMany();
}
async create(name: string, origin_x: number, origin_y: number): Promise<Object> {
return prisma.object.create({
data: {
name,
origin_x,
origin_y
},
});
}
async update(id: string, name: string, origin_x: number, origin_y: number): Promise<Object> {
return prisma.object.update({
where: { id },
data: {
name,
origin_x,
origin_y
},
});
}
async delete(id: string): Promise<Object> {
return prisma.object.delete({
where: { id },
});
}
}
export default new ObjectRepository();

View File

@ -16,12 +16,12 @@ async function addHttpRoutes(app: Application) {
let assets: TAsset[] = [];
const tiles = listTiles();
tiles.forEach(tile => {
assets.push({key: tile, value: '/tiles/' + tile, group: 'tiles', type: 'link'});
assets.push({key: tile, value: '/object/' + tile, group: 'tiles', type: 'link'});
});
const objects = listObjects();
objects.forEach(object => {
assets.push({key: object, value: '/objects/' + object, group: 'objects', type: 'link'});
assets.push({key: object, value: '/tile/' + object, group: 'objects', type: 'link'});
});
res.json(assets);