forked from noxious/server
improved zone manager
This commit is contained in:
parent
a2f21229d8
commit
ebfbae864b
@ -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;
|
@ -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;
|
@ -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;
|
39
prisma/migrations/20240511164426_init/migration.sql
Normal file
39
prisma/migrations/20240511164426_init/migration.sql
Normal 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;
|
@ -23,12 +23,19 @@ model User {
|
||||
id Int @id @default(autoincrement())
|
||||
username String @unique
|
||||
password String
|
||||
position_x Int?
|
||||
position_y Int?
|
||||
rotation Int?
|
||||
zoneId Int? @unique
|
||||
zone Zone? @relation(fields: [zoneId], references: [id])
|
||||
chatlogs Chatlogs[]
|
||||
characters Character[]
|
||||
}
|
||||
|
||||
model Character {
|
||||
id Int @id @default(autoincrement())
|
||||
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 {
|
||||
@ -37,15 +44,5 @@ model Zone {
|
||||
width Int
|
||||
height Int
|
||||
tiles Json
|
||||
users User[] // One-to-many relation: A map can have multiple users
|
||||
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])
|
||||
characters Character[]
|
||||
}
|
@ -1,19 +1,23 @@
|
||||
import { Zone } from "@prisma/client";
|
||||
import { LoadedZoneEntity } from "./entities/LoadedZoneEntity";
|
||||
import {Character, Zone} from "@prisma/client";
|
||||
import ZoneRepository from "./repositories/zone";
|
||||
|
||||
interface ILoadedZone {
|
||||
zone: Zone;
|
||||
characters: Character[];
|
||||
}
|
||||
|
||||
class ZoneManager {
|
||||
private static _instance: ZoneManager;
|
||||
private _loadedZones: LoadedZoneEntity[] = [];
|
||||
private static instance: ZoneManager;
|
||||
private loadedZones: ILoadedZone[] = [];
|
||||
|
||||
private constructor() {}
|
||||
|
||||
// Singleton instance getter
|
||||
public static getInstance(): ZoneManager {
|
||||
if (!ZoneManager._instance) {
|
||||
ZoneManager._instance = new ZoneManager();
|
||||
if (!ZoneManager.instance) {
|
||||
ZoneManager.instance = new ZoneManager();
|
||||
}
|
||||
return ZoneManager._instance;
|
||||
return ZoneManager.instance;
|
||||
}
|
||||
|
||||
// Method to initialize zone loading
|
||||
@ -25,28 +29,31 @@ class ZoneManager {
|
||||
this.loadZone(zone);
|
||||
}
|
||||
|
||||
console.log('[✅] ZoneManager loaded');
|
||||
console.log('[✅] Zone manager loaded');
|
||||
}
|
||||
|
||||
// Method to handle individual zone loading
|
||||
public loadZone(zone: Zone) {
|
||||
const loadedZone = new LoadedZoneEntity(zone);
|
||||
this._loadedZones.push(loadedZone);
|
||||
this.loadedZones.push({
|
||||
zone,
|
||||
characters: []
|
||||
});
|
||||
console.log(`[✅] Zone ID ${zone.id} loaded`);
|
||||
}
|
||||
|
||||
// Method to handle individual zone unloading
|
||||
public unloadZone(zoneId: number) {
|
||||
const index = this._loadedZones.findIndex(loadedZone => loadedZone.getZone().id === zoneId);
|
||||
if (index > -1) {
|
||||
this._loadedZones.splice(index, 1);
|
||||
console.log(`[❌] Zone ID ${zoneId} unloaded`);
|
||||
}
|
||||
this.loadedZones = this.loadedZones.filter((loadedZone) => {
|
||||
return loadedZone.zone.id !== zoneId;
|
||||
});
|
||||
console.log(`[❌] Zone ID ${zoneId} unloaded`);
|
||||
}
|
||||
|
||||
// Getter for loaded zones
|
||||
public getLoadedZones(): LoadedZoneEntity[] {
|
||||
return this._loadedZones;
|
||||
public getLoadedZones(): Zone[] {
|
||||
return this.loadedZones.map((loadedZone) => {
|
||||
return loadedZone.zone;
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -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;
|
||||
}
|
||||
}
|
@ -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;
|
||||
}
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user