code update
This commit is contained in:
parent
8356a83431
commit
a2f21229d8
6
package-lock.json
generated
6
package-lock.json
generated
@ -1269,9 +1269,9 @@
|
|||||||
"integrity": "sha512-YZo3K82SD7Riyi0E1EQPojLz7kpepnSQI9IyPbHHg1XXXevb5dJI7tpyN2ADxGcQbHG7vcyRHk0cbwqcQriUtg=="
|
"integrity": "sha512-YZo3K82SD7Riyi0E1EQPojLz7kpepnSQI9IyPbHHg1XXXevb5dJI7tpyN2ADxGcQbHG7vcyRHk0cbwqcQriUtg=="
|
||||||
},
|
},
|
||||||
"node_modules/semver": {
|
"node_modules/semver": {
|
||||||
"version": "7.6.1",
|
"version": "7.6.2",
|
||||||
"resolved": "https://registry.npmjs.org/semver/-/semver-7.6.1.tgz",
|
"resolved": "https://registry.npmjs.org/semver/-/semver-7.6.2.tgz",
|
||||||
"integrity": "sha512-f/vbBsu+fOiYt+lmwZV0rVwJScl46HppnOA1ZvIuBWKOTlllpyJ3bfVax76/OrhCH38dyxoDIA8K7uB963IYgA==",
|
"integrity": "sha512-FNAIBWCx9qcRhoHcgcJ0gvU7SN1lYU2ZXuSfl04bSC5OpvDHFyJCjdNHomPXxjQlCBU67YW64PzY7/VIEH7F2w==",
|
||||||
"dev": true,
|
"dev": true,
|
||||||
"bin": {
|
"bin": {
|
||||||
"semver": "bin/semver.js"
|
"semver": "bin/semver.js"
|
||||||
|
@ -1,7 +1,7 @@
|
|||||||
{
|
{
|
||||||
"scripts": {
|
"scripts": {
|
||||||
"start": "node dist/index.js",
|
"start": "node dist/index.js",
|
||||||
"dev": "nodemon --exec ts-node src/application.ts",
|
"dev": "nodemon --exec ts-node src/server.ts",
|
||||||
"build": "tsc"
|
"build": "tsc"
|
||||||
},
|
},
|
||||||
"dependencies": {
|
"dependencies": {
|
||||||
|
46
prisma/migrations/20240509150033_init/migration.sql
Normal file
46
prisma/migrations/20240509150033_init/migration.sql
Normal file
@ -0,0 +1,46 @@
|
|||||||
|
/*
|
||||||
|
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;
|
@ -26,12 +26,12 @@ model User {
|
|||||||
position_x Int?
|
position_x Int?
|
||||||
position_y Int?
|
position_y Int?
|
||||||
rotation Int?
|
rotation Int?
|
||||||
mapId Int? @unique
|
zoneId Int? @unique
|
||||||
map Map? @relation(fields: [mapId], references: [id])
|
zone Zone? @relation(fields: [zoneId], references: [id])
|
||||||
chatlogs Chatlogs[]
|
chatlogs Chatlogs[]
|
||||||
}
|
}
|
||||||
|
|
||||||
model Map {
|
model Zone {
|
||||||
id Int @id @default(autoincrement())
|
id Int @id @default(autoincrement())
|
||||||
name String
|
name String
|
||||||
width Int
|
width Int
|
||||||
@ -45,7 +45,7 @@ model Chatlogs {
|
|||||||
id Int @id @default(autoincrement())
|
id Int @id @default(autoincrement())
|
||||||
userId Int
|
userId Int
|
||||||
message String
|
message String
|
||||||
mapId Int
|
zoneId Int
|
||||||
map Map @relation(fields: [mapId], references: [id])
|
zone Zone @relation(fields: [zoneId], references: [id])
|
||||||
user User @relation(fields: [userId], references: [id])
|
user User @relation(fields: [userId], references: [id])
|
||||||
}
|
}
|
53
src/app/ZoneManager.ts
Normal file
53
src/app/ZoneManager.ts
Normal file
@ -0,0 +1,53 @@
|
|||||||
|
import { Zone } from "@prisma/client";
|
||||||
|
import { LoadedZoneEntity } from "./entities/LoadedZoneEntity";
|
||||||
|
import ZoneRepository from "./repositories/zone";
|
||||||
|
|
||||||
|
class ZoneManager {
|
||||||
|
private static _instance: ZoneManager;
|
||||||
|
private _loadedZones: LoadedZoneEntity[] = [];
|
||||||
|
|
||||||
|
private constructor() {}
|
||||||
|
|
||||||
|
// Singleton instance getter
|
||||||
|
public static getInstance(): ZoneManager {
|
||||||
|
if (!ZoneManager._instance) {
|
||||||
|
ZoneManager._instance = new ZoneManager();
|
||||||
|
}
|
||||||
|
return ZoneManager._instance;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Method to initialize zone loading
|
||||||
|
public async boot() {
|
||||||
|
const zoneRepository = new ZoneRepository();
|
||||||
|
const zones = await zoneRepository.getAll();
|
||||||
|
|
||||||
|
for (const zone of zones) {
|
||||||
|
this.loadZone(zone);
|
||||||
|
}
|
||||||
|
|
||||||
|
console.log('[✅] ZoneManager loaded');
|
||||||
|
}
|
||||||
|
|
||||||
|
// Method to handle individual zone loading
|
||||||
|
public loadZone(zone: Zone) {
|
||||||
|
const loadedZone = new LoadedZoneEntity(zone);
|
||||||
|
this._loadedZones.push(loadedZone);
|
||||||
|
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`);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Getter for loaded zones
|
||||||
|
public getLoadedZones(): LoadedZoneEntity[] {
|
||||||
|
return this._loadedZones;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
export default ZoneManager;
|
37
src/app/entities/LoadedZoneEntity.ts
Normal file
37
src/app/entities/LoadedZoneEntity.ts
Normal file
@ -0,0 +1,37 @@
|
|||||||
|
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;
|
||||||
|
}
|
||||||
|
}
|
17
src/app/entities/PlayerEntity.ts
Normal file
17
src/app/entities/PlayerEntity.ts
Normal file
@ -0,0 +1,17 @@
|
|||||||
|
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;
|
||||||
|
}
|
||||||
|
}
|
@ -1,6 +1,6 @@
|
|||||||
import { Socket, Server } from "socket.io";
|
import { Socket, Server } from "socket.io";
|
||||||
|
|
||||||
export default function player_map_load(socket: Socket, io: Server) {
|
export default function player_zone_load(socket: Socket, io: Server) {
|
||||||
socket.on('player:map:load', (data) => {
|
socket.on('player:map:load', (data) => {
|
||||||
console.log(`---User ${socket.id} has requested map.`);
|
console.log(`---User ${socket.id} has requested map.`);
|
||||||
});
|
});
|
@ -1,15 +0,0 @@
|
|||||||
import { Map } from '@prisma/client';
|
|
||||||
import prisma from '../utilities/prisma'; // Import the global Prisma instance
|
|
||||||
|
|
||||||
class MapRepository {
|
|
||||||
async getFirst(): Promise<Map | null> {
|
|
||||||
try {
|
|
||||||
return await prisma.map.findFirst();
|
|
||||||
} catch (error: any) {
|
|
||||||
// Handle error
|
|
||||||
throw new Error(`Failed to get first map: ${error.message}`);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
export default MapRepository;
|
|
24
src/app/repositories/zone.ts
Normal file
24
src/app/repositories/zone.ts
Normal file
@ -0,0 +1,24 @@
|
|||||||
|
import { Zone } from '@prisma/client';
|
||||||
|
import prisma from '../utilities/prisma'; // Import the global Prisma instance
|
||||||
|
|
||||||
|
class ZoneRepository {
|
||||||
|
async getFirst(): Promise<Zone | null> {
|
||||||
|
try {
|
||||||
|
return await prisma.zone.findFirst();
|
||||||
|
} catch (error: any) {
|
||||||
|
// Handle error
|
||||||
|
throw new Error(`Failed to get first zone: ${error.message}`);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
async getAll(): Promise<Zone[]> {
|
||||||
|
try {
|
||||||
|
return await prisma.zone.findMany();
|
||||||
|
} catch (error: any) {
|
||||||
|
// Handle error
|
||||||
|
throw new Error(`Failed to get all zone: ${error.message}`);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
export default ZoneRepository;
|
13
src/app/services/zone.ts
Normal file
13
src/app/services/zone.ts
Normal file
@ -0,0 +1,13 @@
|
|||||||
|
import {LoadedZoneEntity} from "../entities/LoadedZoneEntity";
|
||||||
|
import {Zone} from "@prisma/client";
|
||||||
|
|
||||||
|
class ZoneService
|
||||||
|
{
|
||||||
|
public loaded: LoadedZoneEntity[] = [];
|
||||||
|
|
||||||
|
public boot() {
|
||||||
|
// Load all zones
|
||||||
|
// const zones = await new ZoneRepository().getAll();
|
||||||
|
// this.loadedZones = zones;
|
||||||
|
}
|
||||||
|
}
|
@ -26,7 +26,7 @@ async function addAuthRoutes(app: any) {
|
|||||||
return res.status(400).json({ message: 'Failed to register user' });
|
return res.status(400).json({ message: 'Failed to register user' });
|
||||||
});
|
});
|
||||||
|
|
||||||
console.log('[🌎] Auth routes added');
|
console.log('[🕸️] Auth routes added');
|
||||||
}
|
}
|
||||||
|
|
||||||
export default { addAuthRoutes };
|
export default { addAuthRoutes };
|
@ -1,9 +0,0 @@
|
|||||||
/**
|
|
||||||
* Entry point of the application
|
|
||||||
* Made by Dennis Postma
|
|
||||||
* for New Quest
|
|
||||||
*/
|
|
||||||
|
|
||||||
import { Server } from './app/server';
|
|
||||||
|
|
||||||
new Server().start();
|
|
@ -4,9 +4,10 @@ import express from 'express';
|
|||||||
import http from 'http';
|
import http from 'http';
|
||||||
import cors from 'cors';
|
import cors from 'cors';
|
||||||
import {Server as HttpServer, Socket} from 'socket.io';
|
import {Server as HttpServer, Socket} from 'socket.io';
|
||||||
import config from './utilities/config';
|
import config from './app/utilities/config';
|
||||||
import prisma from './utilities/prisma';
|
import prisma from './app/utilities/prisma';
|
||||||
import api from "./utilities/api";
|
import api from "./app/utilities/api";
|
||||||
|
import ZoneManager from "./app/ZoneManager";
|
||||||
|
|
||||||
export class Server
|
export class Server
|
||||||
{
|
{
|
||||||
@ -31,7 +32,7 @@ export class Server
|
|||||||
*/
|
*/
|
||||||
public async start() {
|
public async start() {
|
||||||
// Print logo
|
// Print logo
|
||||||
const art = fs.readFileSync(path.join(__dirname, 'logo.txt'), 'utf8');
|
const art = fs.readFileSync(path.join(__dirname, 'app', 'logo.txt'), 'utf8');
|
||||||
console.log('\x1b[31m%s\x1b[0m', art + '\n');
|
console.log('\x1b[31m%s\x1b[0m', art + '\n');
|
||||||
|
|
||||||
// Check prisma connection
|
// Check prisma connection
|
||||||
@ -53,7 +54,11 @@ export class Server
|
|||||||
// Add API routes
|
// Add API routes
|
||||||
await api.addAuthRoutes(this.app);
|
await api.addAuthRoutes(this.app);
|
||||||
|
|
||||||
// Add socket events
|
// Load zone manager
|
||||||
|
const zoneManager = ZoneManager.getInstance();
|
||||||
|
await zoneManager.boot();
|
||||||
|
|
||||||
|
// Listen for socket connections
|
||||||
this.io.on('connection', this.handleConnection.bind(this));
|
this.io.on('connection', this.handleConnection.bind(this));
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -65,12 +70,8 @@ export class Server
|
|||||||
private async handleConnection(socket: Socket) {
|
private async handleConnection(socket: Socket) {
|
||||||
const eventsPath = path.join(__dirname, 'events');
|
const eventsPath = path.join(__dirname, 'events');
|
||||||
try {
|
try {
|
||||||
const files = await fs.promises.readdir(eventsPath);
|
const files: string[] = await fs.promises.readdir(eventsPath);
|
||||||
for (const file of files) {
|
for (const file of files) {
|
||||||
if (!file.endsWith('.ts')) {
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
|
|
||||||
const module = await import(path.join(eventsPath, file));
|
const module = await import(path.join(eventsPath, file));
|
||||||
module.default(socket, this.io);
|
module.default(socket, this.io);
|
||||||
}
|
}
|
||||||
@ -79,3 +80,7 @@ export class Server
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Start the server
|
||||||
|
const server = new Server();
|
||||||
|
server.start();
|
Loading…
x
Reference in New Issue
Block a user