Zone updating stuff

This commit is contained in:
Dennis Postma 2024-07-10 17:31:07 +02:00
parent d001d3bd23
commit 45b4817ac4
4 changed files with 64 additions and 42 deletions

View File

@ -84,7 +84,7 @@ CREATE TABLE `Zone` (
-- CreateTable -- CreateTable
CREATE TABLE `ZoneObject` ( CREATE TABLE `ZoneObject` (
`id` INTEGER NOT NULL AUTO_INCREMENT, `id` VARCHAR(191) NOT NULL,
`zoneId` INTEGER NOT NULL, `zoneId` INTEGER NOT NULL,
`objectId` VARCHAR(191) NOT NULL, `objectId` VARCHAR(191) NOT NULL,
`depth` INTEGER NOT NULL, `depth` INTEGER NOT NULL,
@ -96,7 +96,7 @@ CREATE TABLE `ZoneObject` (
-- CreateTable -- CreateTable
CREATE TABLE `ZoneEventTile` ( CREATE TABLE `ZoneEventTile` (
`id` INTEGER NOT NULL AUTO_INCREMENT, `id` VARCHAR(191) NOT NULL,
`zoneId` INTEGER NOT NULL, `zoneId` INTEGER NOT NULL,
`type` ENUM('BLOCK', 'WARP', 'NPC', 'ITEM') NOT NULL, `type` ENUM('BLOCK', 'WARP', 'NPC', 'ITEM') NOT NULL,
`position_x` INTEGER NOT NULL, `position_x` INTEGER NOT NULL,

View File

@ -94,7 +94,7 @@ model Zone {
} }
model ZoneObject { model ZoneObject {
id Int @id @default(autoincrement()) id String @id @default(uuid())
zoneId Int zoneId Int
zone Zone @relation(fields: [zoneId], references: [id], onDelete: Cascade) zone Zone @relation(fields: [zoneId], references: [id], onDelete: Cascade)
objectId String objectId String
@ -112,7 +112,7 @@ enum ZoneEventTileType {
} }
model ZoneEventTile { model ZoneEventTile {
id Int @id @default(autoincrement()) id String @id @default(uuid())
zoneId Int zoneId Int
zone Zone @relation(fields: [zoneId], references: [id], onDelete: Cascade) zone Zone @relation(fields: [zoneId], references: [id], onDelete: Cascade)
type ZoneEventTileType type ZoneEventTileType

View File

@ -2,7 +2,7 @@ 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, ZoneObject } from '@prisma/client' import { Character, Zone, ZoneEventTile, ZoneObject } from '@prisma/client'
interface IPayload { interface IPayload {
zoneId: number; zoneId: number;
@ -10,7 +10,8 @@ interface IPayload {
width: number; width: number;
height: number; height: number;
tiles: string[][]; tiles: string[][];
objects: ZoneObject[]; zoneEventTiles: ZoneEventTile[];
zoneObjects: ZoneObject[];
} }
/** /**
@ -33,6 +34,7 @@ export default function (socket: TSocket, io: Server) {
return; return;
} }
try {
let zone = await ZoneRepository.getById(data.zoneId); let zone = await ZoneRepository.getById(data.zoneId);
if (!zone) { if (!zone) {
@ -42,16 +44,20 @@ export default function (socket: TSocket, io: Server) {
await ZoneRepository.update( await ZoneRepository.update(
data.zoneId, data.zoneId,
zone.name, data.name,
zone.width, data.width,
zone.height, data.height,
data.tiles, data.tiles,
data.objects data.zoneEventTiles,
data.zoneObjects
); );
zone = await ZoneRepository.getById(data.zoneId); zone = await ZoneRepository.getById(data.zoneId);
// send over zone and characters to socket // send over zone and characters to socket
socket.emit('gm:zone_editor:zone:load', zone); socket.emit('gm:zone_editor:zone:load', zone);
} catch (error: any) {
console.log(`---Error updating zone: ${error.message}`);
}
}); });
} }

View File

@ -1,4 +1,4 @@
import { Zone, ZoneObject } from '@prisma/client' import { Zone, ZoneEventTile, ZoneObject } from '@prisma/client'
import prisma from '../utilities/Prisma'; // Import the global Prisma instance import prisma from '../utilities/Prisma'; // Import the global Prisma instance
class ZoneRepository { class ZoneRepository {
@ -27,6 +27,11 @@ class ZoneRepository {
id: id id: id
}, },
include: { include: {
zoneEventTiles: {
include: {
zone: true
}
},
zoneObjects: { zoneObjects: {
include: { include: {
object: true object: true
@ -56,7 +61,7 @@ class ZoneRepository {
} }
} }
async update(id: number, name: string, width: number, height: number, tiles: string[][], objects: ZoneObject[]): Promise<Zone> { async update(id: number, name: string, width: number, height: number, tiles: string[][], zoneEventTiles: ZoneEventTile[], zoneObjects: ZoneObject[]): Promise<Zone> {
try { try {
return await prisma.zone.update({ return await prisma.zone.update({
where: { where: {
@ -67,16 +72,27 @@ class ZoneRepository {
width, width,
height, height,
tiles, tiles,
zoneEventTiles: {
deleteMany: {
zoneId: id // Ensure only event tiles related to the zone are deleted
},
// Save new zone event tiles
create: zoneEventTiles.map(zoneEventTile => ({
type: zoneEventTile.type,
position_x: zoneEventTile.position_x,
position_y: zoneEventTile.position_y
}))
},
zoneObjects: { zoneObjects: {
deleteMany: { deleteMany: {
zoneId: id // Ensure only objects related to the zone are deleted zoneId: id // Ensure only objects related to the zone are deleted
}, },
// Save new zone objects // Save new zone objects
create: objects.map(obj => ({ create: zoneObjects.map(zoneObject => ({
objectId: obj.objectId, objectId: zoneObject.objectId,
depth: obj.depth, depth: zoneObject.depth,
position_x: obj.position_x, position_x: zoneObject.position_x,
position_y: obj.position_y position_y: zoneObject.position_y
})) }))
} }
} }