1
0
forked from noxious/server

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

View File

@ -80,21 +80,21 @@ model TileTag {
}
model Zone {
id Int @id @default(autoincrement())
name String
width Int
height Int
tiles Json
id Int @id @default(autoincrement())
name String
width Int
height Int
tiles Json
zoneEventTiles ZoneEventTile[]
zoneObjects ZoneObject[]
characters Character[]
chats Chat[]
createdAt DateTime @default(now())
updatedAt DateTime @updatedAt
zoneObjects ZoneObject[]
characters Character[]
chats Chat[]
createdAt DateTime @default(now())
updatedAt DateTime @updatedAt
}
model ZoneObject {
id Int @id @default(autoincrement())
id String @id @default(uuid())
zoneId Int
zone Zone @relation(fields: [zoneId], references: [id], onDelete: Cascade)
objectId String
@ -112,9 +112,9 @@ enum ZoneEventTileType {
}
model ZoneEventTile {
id Int @id @default(autoincrement())
id String @id @default(uuid())
zoneId Int
zone Zone @relation(fields: [zoneId], references: [id], onDelete: Cascade)
zone Zone @relation(fields: [zoneId], references: [id], onDelete: Cascade)
type ZoneEventTileType
position_x Int
position_y Int

View File

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

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
class ZoneRepository {
@ -27,6 +27,11 @@ class ZoneRepository {
id: id
},
include: {
zoneEventTiles: {
include: {
zone: true
}
},
zoneObjects: {
include: {
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 {
return await prisma.zone.update({
where: {
@ -67,16 +72,27 @@ class ZoneRepository {
width,
height,
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: {
deleteMany: {
zoneId: id // Ensure only objects related to the zone are deleted
},
// Save new zone objects
create: objects.map(obj => ({
objectId: obj.objectId,
depth: obj.depth,
position_x: obj.position_x,
position_y: obj.position_y
create: zoneObjects.map(zoneObject => ({
objectId: zoneObject.objectId,
depth: zoneObject.depth,
position_x: zoneObject.position_x,
position_y: zoneObject.position_y
}))
}
}