improved zone manager

This commit is contained in:
Dennis Postma 2024-05-11 18:50:51 +02:00
parent a2f21229d8
commit ebfbae864b
8 changed files with 77 additions and 202 deletions

View File

@ -1,9 +0,0 @@
-- CreateTable
CREATE TABLE `User` (
`id` INTEGER NOT NULL AUTO_INCREMENT,
`email` VARCHAR(191) NOT NULL,
`name` VARCHAR(191) NULL,
UNIQUE INDEX `User_email_key`(`email`),
PRIMARY KEY (`id`)
) DEFAULT CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci;

View File

@ -1,59 +0,0 @@
/*
Warnings:
- You are about to drop the column `email` on the `User` table. All the data in the column will be lost.
- You are about to drop the column `name` on the `User` table. All the data in the column will be lost.
- A unique constraint covering the columns `[username]` on the table `User` will be added. If there are existing duplicate values, this will fail.
- A unique constraint covering the columns `[mapId]` on the table `User` will be added. If there are existing duplicate values, this will fail.
- Added the required column `password` to the `User` table without a default value. This is not possible if the table is not empty.
- Added the required column `username` to the `User` table without a default value. This is not possible if the table is not empty.
*/
-- DropIndex
DROP INDEX `User_email_key` ON `User`;
-- AlterTable
ALTER TABLE `User` DROP COLUMN `email`,
DROP COLUMN `name`,
ADD COLUMN `mapId` INTEGER NULL,
ADD COLUMN `password` VARCHAR(191) NOT NULL,
ADD COLUMN `position_x` INTEGER NULL,
ADD COLUMN `position_y` INTEGER NULL,
ADD COLUMN `rotation` INTEGER NULL,
ADD COLUMN `username` VARCHAR(191) NOT NULL;
-- CreateTable
CREATE TABLE `Map` (
`id` INTEGER NOT NULL AUTO_INCREMENT,
`name` VARCHAR(191) NOT NULL,
`width` INTEGER NOT NULL,
`height` INTEGER NOT NULL,
`tiles` JSON NOT NULL,
PRIMARY KEY (`id`)
) DEFAULT CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci;
-- CreateTable
CREATE TABLE `Chatlogs` (
`id` INTEGER NOT NULL AUTO_INCREMENT,
`userId` INTEGER NOT NULL,
`message` VARCHAR(191) NOT NULL,
`mapId` INTEGER NOT NULL,
PRIMARY KEY (`id`)
) DEFAULT CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci;
-- CreateIndex
CREATE UNIQUE INDEX `User_username_key` ON `User`(`username`);
-- CreateIndex
CREATE UNIQUE INDEX `User_mapId_key` ON `User`(`mapId`);
-- AddForeignKey
ALTER TABLE `User` ADD CONSTRAINT `User_mapId_fkey` FOREIGN KEY (`mapId`) REFERENCES `Map`(`id`) ON DELETE SET NULL ON UPDATE CASCADE;
-- AddForeignKey
ALTER TABLE `Chatlogs` ADD CONSTRAINT `Chatlogs_mapId_fkey` FOREIGN KEY (`mapId`) REFERENCES `Map`(`id`) ON DELETE RESTRICT ON UPDATE CASCADE;
-- AddForeignKey
ALTER TABLE `Chatlogs` ADD CONSTRAINT `Chatlogs_userId_fkey` FOREIGN KEY (`userId`) REFERENCES `User`(`id`) ON DELETE RESTRICT ON UPDATE CASCADE;

View File

@ -1,46 +0,0 @@
/*
Warnings:
- You are about to drop the column `mapId` on the `Chatlogs` table. All the data in the column will be lost.
- You are about to drop the column `mapId` on the `User` table. All the data in the column will be lost.
- You are about to drop the `Map` table. If the table is not empty, all the data it contains will be lost.
- A unique constraint covering the columns `[zoneId]` on the table `User` will be added. If there are existing duplicate values, this will fail.
- Added the required column `zoneId` to the `Chatlogs` table without a default value. This is not possible if the table is not empty.
*/
-- DropForeignKey
ALTER TABLE `Chatlogs` DROP FOREIGN KEY `Chatlogs_mapId_fkey`;
-- DropForeignKey
ALTER TABLE `User` DROP FOREIGN KEY `User_mapId_fkey`;
-- AlterTable
ALTER TABLE `Chatlogs` DROP COLUMN `mapId`,
ADD COLUMN `zoneId` INTEGER NOT NULL;
-- AlterTable
ALTER TABLE `User` DROP COLUMN `mapId`,
ADD COLUMN `zoneId` INTEGER NULL;
-- DropTable
DROP TABLE `Map`;
-- CreateTable
CREATE TABLE `Zone` (
`id` INTEGER NOT NULL AUTO_INCREMENT,
`name` VARCHAR(191) NOT NULL,
`width` INTEGER NOT NULL,
`height` INTEGER NOT NULL,
`tiles` JSON NOT NULL,
PRIMARY KEY (`id`)
) DEFAULT CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci;
-- CreateIndex
CREATE UNIQUE INDEX `User_zoneId_key` ON `User`(`zoneId`);
-- AddForeignKey
ALTER TABLE `User` ADD CONSTRAINT `User_zoneId_fkey` FOREIGN KEY (`zoneId`) REFERENCES `Zone`(`id`) ON DELETE SET NULL ON UPDATE CASCADE;
-- AddForeignKey
ALTER TABLE `Chatlogs` ADD CONSTRAINT `Chatlogs_zoneId_fkey` FOREIGN KEY (`zoneId`) REFERENCES `Zone`(`id`) ON DELETE RESTRICT ON UPDATE CASCADE;

View File

@ -0,0 +1,39 @@
-- CreateTable
CREATE TABLE `User` (
`id` INTEGER NOT NULL AUTO_INCREMENT,
`username` VARCHAR(191) NOT NULL,
`password` VARCHAR(191) NOT NULL,
UNIQUE INDEX `User_username_key`(`username`),
PRIMARY KEY (`id`)
) DEFAULT CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci;
-- CreateTable
CREATE TABLE `Character` (
`id` INTEGER NOT NULL AUTO_INCREMENT,
`name` VARCHAR(191) NOT NULL,
`position_x` INTEGER NOT NULL,
`position_y` INTEGER NOT NULL,
`rotation` INTEGER NOT NULL,
`userId` INTEGER NOT NULL,
`zoneId` INTEGER NOT NULL,
PRIMARY KEY (`id`)
) DEFAULT CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci;
-- CreateTable
CREATE TABLE `Zone` (
`id` INTEGER NOT NULL AUTO_INCREMENT,
`name` VARCHAR(191) NOT NULL,
`width` INTEGER NOT NULL,
`height` INTEGER NOT NULL,
`tiles` JSON NOT NULL,
PRIMARY KEY (`id`)
) DEFAULT CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci;
-- AddForeignKey
ALTER TABLE `Character` ADD CONSTRAINT `Character_userId_fkey` FOREIGN KEY (`userId`) REFERENCES `User`(`id`) ON DELETE RESTRICT ON UPDATE CASCADE;
-- AddForeignKey
ALTER TABLE `Character` ADD CONSTRAINT `Character_zoneId_fkey` FOREIGN KEY (`zoneId`) REFERENCES `Zone`(`id`) ON DELETE RESTRICT ON UPDATE CASCADE;

View File

@ -23,12 +23,19 @@ model User {
id Int @id @default(autoincrement()) id Int @id @default(autoincrement())
username String @unique username String @unique
password String password String
position_x Int? characters Character[]
position_y Int? }
rotation Int?
zoneId Int? @unique model Character {
zone Zone? @relation(fields: [zoneId], references: [id]) id Int @id @default(autoincrement())
chatlogs Chatlogs[] name String
position_x Int
position_y Int
rotation Int
userId Int
user User @relation(fields: [userId], references: [id])
zoneId Int
zone Zone @relation(fields: [zoneId], references: [id])
} }
model Zone { model Zone {
@ -37,15 +44,5 @@ model Zone {
width Int width Int
height Int height Int
tiles Json tiles Json
users User[] // One-to-many relation: A map can have multiple users characters Character[]
chatlogs Chatlogs[]
}
model Chatlogs {
id Int @id @default(autoincrement())
userId Int
message String
zoneId Int
zone Zone @relation(fields: [zoneId], references: [id])
user User @relation(fields: [userId], references: [id])
} }

View File

@ -1,19 +1,23 @@
import { Zone } from "@prisma/client"; import {Character, Zone} from "@prisma/client";
import { LoadedZoneEntity } from "./entities/LoadedZoneEntity";
import ZoneRepository from "./repositories/zone"; import ZoneRepository from "./repositories/zone";
interface ILoadedZone {
zone: Zone;
characters: Character[];
}
class ZoneManager { class ZoneManager {
private static _instance: ZoneManager; private static instance: ZoneManager;
private _loadedZones: LoadedZoneEntity[] = []; private loadedZones: ILoadedZone[] = [];
private constructor() {} private constructor() {}
// Singleton instance getter // Singleton instance getter
public static getInstance(): ZoneManager { public static getInstance(): ZoneManager {
if (!ZoneManager._instance) { if (!ZoneManager.instance) {
ZoneManager._instance = new ZoneManager(); ZoneManager.instance = new ZoneManager();
} }
return ZoneManager._instance; return ZoneManager.instance;
} }
// Method to initialize zone loading // Method to initialize zone loading
@ -25,28 +29,31 @@ class ZoneManager {
this.loadZone(zone); this.loadZone(zone);
} }
console.log('[✅] ZoneManager loaded'); console.log('[✅] Zone manager loaded');
} }
// Method to handle individual zone loading // Method to handle individual zone loading
public loadZone(zone: Zone) { public loadZone(zone: Zone) {
const loadedZone = new LoadedZoneEntity(zone); this.loadedZones.push({
this._loadedZones.push(loadedZone); zone,
characters: []
});
console.log(`[✅] Zone ID ${zone.id} loaded`); console.log(`[✅] Zone ID ${zone.id} loaded`);
} }
// Method to handle individual zone unloading // Method to handle individual zone unloading
public unloadZone(zoneId: number) { public unloadZone(zoneId: number) {
const index = this._loadedZones.findIndex(loadedZone => loadedZone.getZone().id === zoneId); this.loadedZones = this.loadedZones.filter((loadedZone) => {
if (index > -1) { return loadedZone.zone.id !== zoneId;
this._loadedZones.splice(index, 1); });
console.log(`[❌] Zone ID ${zoneId} unloaded`); console.log(`[❌] Zone ID ${zoneId} unloaded`);
}
} }
// Getter for loaded zones // Getter for loaded zones
public getLoadedZones(): LoadedZoneEntity[] { public getLoadedZones(): Zone[] {
return this._loadedZones; return this.loadedZones.map((loadedZone) => {
return loadedZone.zone;
});
} }
} }

View File

@ -1,37 +0,0 @@
import { PlayerEntity } from "./PlayerEntity";
import {Zone} from "@prisma/client";
export class LoadedZoneEntity
{
zone: Zone;
players: Map<string, PlayerEntity>;
constructor(zone: Zone) {
this.zone = zone;
this.players = new Map();
}
getZone() {
return this.zone;
}
addPlayer(player: PlayerEntity) {
this.players.set(player.id, player);
}
removePlayer(player: PlayerEntity) {
this.players.delete(player.id);
}
getPlayer(playerId: string) {
return this.players.get(playerId);
}
getPlayers() {
return Array.from(this.players.values());
}
getPlayersCount() {
return this.players.size;
}
}

View File

@ -1,17 +0,0 @@
export class PlayerEntity
{
id: string;
name: string;
position: { x: number, y: number };
constructor(id: string, name: string, position: { x: number, y: number }) {
this.id = id;
this.name = name;
this.position = position;
}
move(x: number, y: number) {
this.position.x = x;
this.position.y = y;
}
}