1
0
forked from noxious/server

Creating and deleting zones now works, added try() to certain functions, tiny general improvements

This commit is contained in:
Dennis Postma 2024-07-07 04:06:09 +02:00
parent 3c82f4f06b
commit c7069c9e98
9 changed files with 163 additions and 42 deletions

View File

@ -76,7 +76,6 @@ CREATE TABLE `Zone` (
`width` INTEGER NOT NULL, `width` INTEGER NOT NULL,
`height` INTEGER NOT NULL, `height` INTEGER NOT NULL,
`tiles` JSON NOT NULL, `tiles` JSON NOT NULL,
`walls` JSON NOT NULL,
`createdAt` DATETIME(3) NOT NULL DEFAULT CURRENT_TIMESTAMP(3), `createdAt` DATETIME(3) NOT NULL DEFAULT CURRENT_TIMESTAMP(3),
`updatedAt` DATETIME(3) NOT NULL, `updatedAt` DATETIME(3) NOT NULL,

View File

@ -0,0 +1,44 @@
import { Server } from "socket.io";
import {TSocket} from "../../../utilities/Types";
import ZoneRepository from "../../../repositories/ZoneRepository";
import ZoneManager from "../../../ZoneManager";
import {Character, Zone} from "@prisma/client";
interface IPayload {
name: string;
width: number;
height: number;
}
/**
* Handle game master zone create event
* @param socket
* @param io
*/
export default function (socket: TSocket, io: Server) {
socket.on('gm:zone_editor:zone:create', async (data: IPayload, callback: (response: boolean) => void) => {
if (socket.character?.role !== 'gm') {
console.log(`---Character #${socket.character?.id} is not a game master.`);
return;
}
console.log(`---GM ${socket.character?.id} has created a new zone via zone editor.`);
try {
const zone = await ZoneRepository.create(
data.name,
data.width,
data.height,
Array.from({length: data.height}, () => Array.from({length: data.width}, () => 'blank_tile')),
);
// send over zone and characters to socket
socket.emit('gm:zone_editor:zone:load', zone);
callback(true);
} catch (e) {
console.error(e);
callback(false);
}
});
}

View File

@ -0,0 +1,42 @@
import { Server } from "socket.io";
import {TSocket} from "../../../utilities/Types";
import ZoneRepository from "../../../repositories/ZoneRepository";
import ZoneManager from "../../../ZoneManager";
import {Character, Zone} from "@prisma/client";
interface IPayload {
zoneId: number;
}
/**
* Handle game master zone delete event
* @param socket
* @param io
*/
export default function (socket: TSocket, io: Server) {
socket.on('gm:zone_editor:zone:delete', async (data: IPayload, callback: (response: boolean) => void) => {
if (socket.character?.role !== 'gm') {
console.log(`---Character #${socket.character?.id} is not a game master.`);
return;
}
console.log(`---GM ${socket.character?.id} has deleted a zone via zone editor.`);
try {
const zone = await ZoneRepository.getById(data.zoneId);
if (!zone) {
console.log(`---Zone not found.`);
return;
}
await ZoneRepository.delete(data.zoneId);
callback(true);
} catch (e) {
console.error(e);
callback(false);
}
});
}

View File

@ -0,0 +1,32 @@
import { Server } from "socket.io";
import {TSocket} from '../../../utilities/Types'
import { Zone } from '@prisma/client'
import ZoneRepository from '../../../repositories/ZoneRepository'
interface IPayload {
}
/**
* Handle game master list zones event
* @param socket
* @param io
*/
export default function (socket: TSocket, io: Server) {
socket.on('gm:zone_editor:zone:list', async (data: any, callback: (response: Zone[]) => void) => {
if (socket.character?.role !== 'gm') {
console.log(`---Character #${socket.character?.id} is not a game master.`);
return;
}
console.log(`---GM ${socket.character?.id} has requested zone list via zone editor.`);
try {
const zones = await ZoneRepository.getAll();
callback(zones);
} catch (e) {
console.error(e);
callback([]);
}
});
}

View File

@ -1,7 +1,7 @@
import { Server } from "socket.io"; import { Server } from "socket.io";
import {TSocket} from "../../utilities/Types"; import {TSocket} from "../../../utilities/Types";
import ZoneRepository from "../../repositories/ZoneRepository"; import ZoneRepository from "../../../repositories/ZoneRepository";
import ZoneManager from "../../ZoneManager"; import ZoneManager from "../../../ZoneManager";
import {Character, Zone} from "@prisma/client"; import {Character, Zone} from "@prisma/client";
interface IPayload { interface IPayload {

View File

@ -1,7 +1,7 @@
import { Server } from "socket.io"; import { Server } from "socket.io";
import {TSocket} from "../../utilities/Types"; import {TSocket} from "../../../utilities/Types";
import ZoneRepository from "../../repositories/ZoneRepository"; import ZoneRepository from "../../../repositories/ZoneRepository";
import ZoneManager from "../../ZoneManager"; import ZoneManager from "../../../ZoneManager";
import {Character, Zone} from "@prisma/client"; import {Character, Zone} from "@prisma/client";
interface IPayload { interface IPayload {
@ -9,24 +9,23 @@ interface IPayload {
name: string; name: string;
width: number; width: number;
height: number; height: number;
tiles: number[][]; tiles: string[][];
walls: number[][];
} }
/** /**
* Handle game master zone save event * Handle game master zone update event
* @param socket * @param socket
* @param io * @param io
*/ */
export default function (socket: TSocket, io: Server) { export default function (socket: TSocket, io: Server) {
socket.on('gm:zone_editor:zone:save', async (data: IPayload) => { socket.on('gm:zone_editor:zone:update', async (data: IPayload) => {
if (socket.character?.role !== 'gm') { if (socket.character?.role !== 'gm') {
console.log(`---Character #${socket.character?.id} is not a game master.`); console.log(`---Character #${socket.character?.id} is not a game master.`);
return; return;
} }
console.log(`---GM ${socket.character?.id} has saved zone via zone editor.`); console.log(`---GM ${socket.character?.id} has updated zone via zone editor.`);
console.log(data); console.log(data);
@ -48,7 +47,6 @@ export default function (socket: TSocket, io: Server) {
data.width, data.width,
data.height, data.height,
data.tiles, data.tiles,
data.walls
); );
zone = await ZoneRepository.getById(data.zoneId); zone = await ZoneRepository.getById(data.zoneId);

View File

@ -33,7 +33,7 @@ class ZoneRepository {
} }
} }
async create(name: string, width: number, height: number, tiles: number[][], walls: number[][]): Promise<Zone> { async create(name: string, width: number, height: number, tiles: string[][]): Promise<Zone> {
try { try {
return await prisma.zone.create({ return await prisma.zone.create({
data: { data: {
@ -41,7 +41,6 @@ class ZoneRepository {
width: width, width: width,
height: height, height: height,
tiles: tiles, tiles: tiles,
walls: walls,
} }
}); });
} catch (error: any) { } catch (error: any) {
@ -50,7 +49,7 @@ class ZoneRepository {
} }
} }
async update(id: number, name: string, width: number, height: number, tiles: number[][], walls: number[][]): Promise<Zone> { async update(id: number, name: string, width: number, height: number, tiles: string[][]): Promise<Zone> {
try { try {
return await prisma.zone.update({ return await prisma.zone.update({
where: { where: {
@ -61,7 +60,6 @@ class ZoneRepository {
width: width, width: width,
height: height, height: height,
tiles: tiles, tiles: tiles,
walls: walls,
} }
}); });
} catch (error: any) { } catch (error: any) {
@ -69,6 +67,19 @@ class ZoneRepository {
throw new Error(`Failed to update zone: ${error.message}`); throw new Error(`Failed to update zone: ${error.message}`);
} }
} }
async delete(id: number): Promise<Zone> {
try {
return await prisma.zone.delete({
where: {
id: id
}
});
} catch (error: any) {
// Handle error
throw new Error(`Failed to delete zone: ${error.message}`);
}
}
} }
export default new ZoneRepository; export default new ZoneRepository;

View File

@ -6,27 +6,16 @@ class ZoneService
async createDemoZone(): Promise<boolean> async createDemoZone(): Promise<boolean>
{ {
await ZoneRepository.create("Demo Zone", 10, 10, [ await ZoneRepository.create("Demo Zone", 10, 10, [
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0], ['blank_tile', 'blank_tile', 'blank_tile', 'blank_tile', 'blank_tile', 'blank_tile', 'blank_tile', 'blank_tile', 'blank_tile', 'blank_tile'],
[0, 1, 1, 1, 1, 1, 1, 1, 1, 0], ['blank_tile', 'blank_tile', 'blank_tile', 'blank_tile', 'blank_tile', 'blank_tile', 'blank_tile', 'blank_tile', 'blank_tile', 'blank_tile'],
[0, 1, 1, 1, 1, 1, 1, 1, 1, 0], ['blank_tile', 'blank_tile', 'blank_tile', 'blank_tile', 'blank_tile', 'blank_tile', 'blank_tile', 'blank_tile', 'blank_tile', 'blank_tile'],
[0, 1, 1, 1, 1, 1, 1, 1, 1, 0], ['blank_tile', 'blank_tile', 'blank_tile', 'blank_tile', 'blank_tile', 'blank_tile', 'blank_tile', 'blank_tile', 'blank_tile', 'blank_tile'],
[0, 1, 1, 1, 1, 1, 1, 1, 1, 0], ['blank_tile', 'blank_tile', 'blank_tile', 'blank_tile', 'blank_tile', 'blank_tile', 'blank_tile', 'blank_tile', 'blank_tile', 'blank_tile'],
[0, 1, 1, 1, 1, 1, 1, 1, 1, 0], ['blank_tile', 'blank_tile', 'blank_tile', 'blank_tile', 'blank_tile', 'blank_tile', 'blank_tile', 'blank_tile', 'blank_tile', 'blank_tile'],
[0, 1, 1, 1, 1, 1, 1, 1, 1, 0], ['blank_tile', 'blank_tile', 'blank_tile', 'blank_tile', 'blank_tile', 'blank_tile', 'blank_tile', 'blank_tile', 'blank_tile', 'blank_tile'],
[0, 1, 1, 1, 1, 1, 1, 1, 1, 0], ['blank_tile', 'blank_tile', 'blank_tile', 'blank_tile', 'blank_tile', 'blank_tile', 'blank_tile', 'blank_tile', 'blank_tile', 'blank_tile'],
[0, 1, 1, 1, 1, 1, 1, 1, 1, 0], ['blank_tile', 'blank_tile', 'blank_tile', 'blank_tile', 'blank_tile', 'blank_tile', 'blank_tile', 'blank_tile', 'blank_tile', 'blank_tile'],
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0], ['blank_tile', 'blank_tile', 'blank_tile', 'blank_tile', 'blank_tile', 'blank_tile', 'blank_tile', 'blank_tile', 'blank_tile', 'blank_tile'],
], [
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
[0, 1, 1, 1, 1, 1, 1, 1, 1, 0],
[0, 1, 1, 1, 1, 1, 1, 1, 1, 0],
[0, 1, 1, 1, 1, 1, 1, 1, 1, 0],
[0, 1, 1, 1, 1, 1, 1, 1, 1, 0],
[0, 1, 1, 1, 1, 1, 1, 1, 1, 0],
[0, 1, 1, 1, 1, 1, 1, 1, 1, 0],
[0, 1, 1, 1, 1, 1, 1, 1, 1, 0],
[0, 1, 1, 1, 1, 1, 1, 1, 1, 0],
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
]) ])
console.log("Demo zone created."); console.log("Demo zone created.");
return true; return true;

View File

@ -94,6 +94,11 @@ function listTiles(): string[] {
// get root path // get root path
const folder = path.join(process.cwd(), 'public', 'tiles'); const folder = path.join(process.cwd(), 'public', 'tiles');
// if folder does not exist, create it
if (!fs.existsSync(folder)) {
fs.mkdirSync(folder);
}
// list the files in the folder // list the files in the folder
let tiles: string[] = []; let tiles: string[] = [];
@ -107,8 +112,6 @@ function listTiles(): string[] {
console.log(err); console.log(err);
} }
console.log(tiles);
return tiles; return tiles;
} }
@ -116,6 +119,11 @@ function listObjects(): string[] {
// get root path // get root path
const folder = path.join(process.cwd(), 'public', 'objects'); const folder = path.join(process.cwd(), 'public', 'objects');
// if folder does not exist, create it
if (!fs.existsSync(folder)) {
fs.mkdirSync(folder);
}
// list the files in the folder // list the files in the folder
let objects: string[] = []; let objects: string[] = [];
@ -129,7 +137,5 @@ function listObjects(): string[] {
console.log(err); console.log(err);
} }
console.log(objects);
return objects; return objects;
} }