forked from noxious/server
refractor file to unfuck the logic
This commit is contained in:
parent
39e8f52595
commit
d59608174d
@ -1,12 +1,11 @@
|
||||
import { Server } from "socket.io";
|
||||
import {TSocket} from "../../utilities/Types";
|
||||
import {writeFile} from "node:fs";
|
||||
import {randomUUID} from "node:crypto";
|
||||
import { TSocket } from "../../utilities/Types";
|
||||
import { writeFile } from "node:fs/promises";
|
||||
import path from "path";
|
||||
import fs from "fs";
|
||||
import fs from "fs/promises";
|
||||
|
||||
|
||||
interface IPayload {
|
||||
interface ITileData {
|
||||
[key: string]: Buffer;
|
||||
}
|
||||
|
||||
/**
|
||||
@ -15,34 +14,44 @@ interface IPayload {
|
||||
* @param io
|
||||
*/
|
||||
export default function (socket: TSocket, io: Server) {
|
||||
socket.on('gm:tile:upload', async (data: any, callback: (response: boolean) => void) => {
|
||||
socket.on('gm:tile:upload', async (data: ITileData, callback: (response: boolean) => void) => {
|
||||
try {
|
||||
if (socket.character?.role !== 'gm') {
|
||||
callback(false);
|
||||
return;
|
||||
}
|
||||
|
||||
if (socket.character?.role !== 'gm') {
|
||||
return;
|
||||
}
|
||||
const public_folder = path.join(process.cwd(), 'public', 'tiles');
|
||||
|
||||
// get root path
|
||||
const public_folder = path.join(process.cwd(), 'public', 'tiles');
|
||||
// Ensure the folder exists
|
||||
await fs.mkdir(public_folder, { recursive: true });
|
||||
|
||||
// check if folder exists or create it
|
||||
if (!fs.existsSync(public_folder)) {
|
||||
fs.mkdirSync(public_folder, { recursive: true });
|
||||
}
|
||||
const files = await fs.readdir(public_folder);
|
||||
let highestNumber = -1;
|
||||
|
||||
for (const key in data) {
|
||||
// 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.reduce((a, b) => a > b ? a : b, '0.png');
|
||||
const lastFileName = lastFile?.split('.')[0];
|
||||
const filename = `${parseInt(lastFileName ?? '0') + 1}.png`;
|
||||
const finalFilePath = path.join(public_folder, filename);
|
||||
const tile = data[key];
|
||||
// Find the highest number in existing filenames
|
||||
for (const file of files) {
|
||||
const match = file.match(/^(\d+)\.png$/);
|
||||
if (match) {
|
||||
const number = parseInt(match[1], 10);
|
||||
if (number > highestNumber) {
|
||||
highestNumber = number;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// save the tile to the disk, for example
|
||||
writeFile(finalFilePath, tile, (err) => {});
|
||||
const uploadPromises = Object.entries(data).map(async ([key, tileData], index) => {
|
||||
const filename = `${highestNumber + index + 1}.png`;
|
||||
const finalFilePath = path.join(public_folder, filename);
|
||||
await writeFile(finalFilePath, tileData);
|
||||
});
|
||||
|
||||
await Promise.all(uploadPromises);
|
||||
|
||||
// return true to the client
|
||||
callback(true);
|
||||
} catch (error) {
|
||||
console.error('Error uploading tiles:', error);
|
||||
callback(false);
|
||||
}
|
||||
});
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user