Added ORM entities

This commit is contained in:
Dennis Postma 2024-12-24 22:08:08 +01:00
parent 40c24cee10
commit 8980691409
15 changed files with 403 additions and 1 deletions

84
src/entities/character.ts Normal file
View File

@ -0,0 +1,84 @@
import { Collection, Entity, ManyToOne, OneToMany, PrimaryKey, Property } from '@mikro-orm/core';
import { User } from './user';
import { Zone } from './zone';
import { CharacterType } from './characterType';
import { CharacterHair } from './characterHair';
import { CharacterItem } from './characterItem';
import { CharacterEquipment } from './characterEquipment';
import { Chat } from './chat';
@Entity()
export class Character {
@PrimaryKey()
id!: number;
@ManyToOne(() => User)
user!: User;
@Property({ unique: true })
name!: string;
@Property()
online = false;
@Property()
role = 'player';
@OneToMany(() => Chat, chat => chat.character)
chats = new Collection<Chat>(this);
// Position
@ManyToOne(() => Zone)
zone!: Zone;
@Property()
positionX = 0;
@Property()
positionY = 0;
@Property()
rotation = 0;
// Customization
@ManyToOne(() => CharacterType, { nullable: true })
characterType?: CharacterType;
@ManyToOne(() => CharacterHair, { nullable: true })
characterHair?: CharacterHair;
// Inventory
@OneToMany(() => CharacterItem, item => item.character)
items = new Collection<CharacterItem>(this);
@OneToMany(() => CharacterEquipment, equipment => equipment.character)
equipment = new Collection<CharacterEquipment>(this);
// Stats
@Property()
alignment = 50;
@Property()
hitpoints = 100;
@Property()
mana = 100;
@Property()
level = 1;
@Property()
experience = 0;
@Property()
strength = 10;
@Property()
dexterity = 10;
@Property()
intelligence = 10;
@Property()
wisdom = 10;
}

View File

@ -0,0 +1,19 @@
import { Entity, ManyToOne, PrimaryKey, Property } from '@mikro-orm/core';
import { Character } from './character';
import { CharacterItem } from './characterItem';
import { CharacterEquipmentSlotType } from '../utilities/enums';
@Entity()
export class CharacterEquipment {
@PrimaryKey()
id!: number;
@Property()
slot!: CharacterEquipmentSlotType;
@ManyToOne(() => Character)
character!: Character;
@ManyToOne(() => CharacterItem)
characterItem!: CharacterItem;
}

View File

@ -0,0 +1,25 @@
import { Collection, Entity, ManyToOne, OneToMany, PrimaryKey, Property } from '@mikro-orm/core';
import { Character } from './character';
import { Sprite } from './sprite';
import { CharacterGender } from '../utilities/enums';
@Entity()
export class CharacterHair {
@PrimaryKey()
id!: number;
@Property()
name!: string;
@Property()
gender: CharacterGender = CharacterGender.MALE;
@Property()
isSelectable = false;
@ManyToOne(() => Sprite, { nullable: true })
sprite?: Sprite;
@OneToMany(() => Character, character => character.characterHair)
characters = new Collection<Character>(this);
}

View File

@ -0,0 +1,22 @@
import { Collection, Entity, ManyToOne, OneToMany, PrimaryKey, Property } from '@mikro-orm/core';
import { Character } from './character';
import { Item } from './item';
import { CharacterEquipment } from './characterEquipment';
@Entity()
export class CharacterItem {
@PrimaryKey()
id!: number;
@ManyToOne(() => Character)
character!: Character;
@ManyToOne(() => Item)
item!: Item;
@Property()
quantity!: number;
@OneToMany(() => CharacterEquipment, equipment => equipment.characterItem)
characterEquipment = new Collection<CharacterEquipment>(this);
}

View File

@ -0,0 +1,34 @@
import { Collection, Entity, ManyToOne, OneToMany, PrimaryKey, Property } from '@mikro-orm/core';
import { Character } from './character';
import { Sprite } from './sprite';
import { CharacterGender, CharacterRace } from '../utilities/enums';
@Entity()
export class CharacterType {
@PrimaryKey()
id!: number;
@Property()
name!: string;
@Property()
gender!: CharacterGender;
@Property()
race!: CharacterRace;
@Property()
isSelectable = false;
@OneToMany(() => Character, character => character.characterType)
characters = new Collection<Character>(this);
@ManyToOne(() => Sprite, { nullable: true })
sprite?: Sprite;
@Property()
createdAt = new Date();
@Property()
updatedAt = new Date();
}

21
src/entities/chat.ts Normal file
View File

@ -0,0 +1,21 @@
import { Entity, ManyToOne, PrimaryKey, Property } from '@mikro-orm/core';
import { Character } from './character';
import { Zone } from './zone';
@Entity()
export class Chat {
@PrimaryKey()
id!: number;
@ManyToOne(() => Character)
character!: Character;
@ManyToOne(() => Zone)
zone!: Zone;
@Property()
message!: string;
@Property()
createdAt = new Date();
}

37
src/entities/item.ts Normal file
View File

@ -0,0 +1,37 @@
import { Collection, Entity, ManyToOne, OneToMany, PrimaryKey, Property } from '@mikro-orm/core';
import { Sprite } from './sprite';
import { CharacterItem } from './characterItem';
import { ItemType, ItemRarity } from '../utilities/enums';
@Entity()
export class Item {
@PrimaryKey()
id!: string;
@Property()
name!: string;
@Property({ nullable: true })
description?: string;
@Property()
itemType!: ItemType;
@Property()
stackable = false;
@Property()
rarity: ItemRarity = ItemRarity.COMMON;
@ManyToOne(() => Sprite, { nullable: true })
sprite?: Sprite;
@Property()
createdAt = new Date();
@Property()
updatedAt = new Date();
@OneToMany(() => CharacterItem, characterItem => characterItem.item)
characters = new Collection<CharacterItem>(this);
}

View File

@ -0,0 +1,17 @@
import { Entity, ManyToOne, PrimaryKey, Property } from '@mikro-orm/core';
import { User } from './user';
@Entity()
export class PasswordResetToken {
@PrimaryKey()
id!: number;
@ManyToOne(() => User)
user!: User;
@Property({ unique: true })
token!: string;
@Property()
createdAt = new Date();
}

33
src/entities/sprite.ts Normal file
View File

@ -0,0 +1,33 @@
import { randomUUID } from 'node:crypto';
import { Collection, Entity, OneToMany, PrimaryKey, Property } from '@mikro-orm/core';
import { SpriteAction } from './spriteAction';
import { CharacterType } from './characterType';
import { CharacterHair } from './characterHair';
import { Item } from './item';
@Entity()
export class Sprite {
@PrimaryKey()
id = randomUUID();
@Property()
name!: string;
@Property()
createdAt = new Date();
@Property()
updatedAt = new Date();
@OneToMany(() => SpriteAction, action => action.sprite)
spriteActions = new Collection<SpriteAction>(this);
@OneToMany(() => CharacterType, type => type.sprite)
characterTypes = new Collection<CharacterType>(this);
@OneToMany(() => CharacterHair, hair => hair.sprite)
characterHairs = new Collection<CharacterHair>(this);
@OneToMany(() => Item, item => item.sprite)
items = new Collection<Item>(this);
}

View File

@ -0,0 +1,39 @@
import { randomUUID } from 'node:crypto'
import { Entity, ManyToOne, PrimaryKey, Property } from '@mikro-orm/core';
import { Sprite } from './sprite';
@Entity()
export class SpriteAction {
@PrimaryKey()
id = randomUUID();
@ManyToOne(() => Sprite)
sprite!: Sprite;
@Property()
action!: string;
@Property({ type: 'json', nullable: true })
sprites?: any;
@Property()
originX = 0;
@Property()
originY = 0;
@Property()
isAnimated = false;
@Property()
isLooping = false;
@Property()
frameWidth = 0;
@Property()
frameHeight = 0;
@Property()
frameRate = 0;
}

27
src/entities/user.ts Normal file
View File

@ -0,0 +1,27 @@
import { Collection, Entity, OneToMany, PrimaryKey, Property } from '@mikro-orm/core';
import { Character } from './character';
import { PasswordResetToken } from './passwordResetToken';
@Entity()
export class User {
@PrimaryKey()
id!: number;
@Property({ unique: true })
username!: string;
@Property({ unique: true })
email!: string;
@Property()
password!: string;
@Property()
online = false;
@OneToMany(() => Character, character => character.user)
characters = new Collection<Character>(this);
@OneToMany(() => PasswordResetToken, token => token.user)
passwordResetTokens = new Collection<PasswordResetToken>(this);
}

19
src/entities/world.ts Normal file
View File

@ -0,0 +1,19 @@
import { Entity, PrimaryKey, Property } from '@mikro-orm/core';
@Entity()
export class World {
@PrimaryKey()
date = new Date();
@Property()
isRainEnabled = false;
@Property()
rainPercentage = 0;
@Property()
isFogEnabled = false;
@Property()
fogDensity = 0;
}

15
src/entities/zone.ts Normal file
View File

@ -0,0 +1,15 @@
import { Collection, Entity, OneToMany, PrimaryKey, Property } from '@mikro-orm/core';
import { Character } from './character';
import { Chat } from './chat';
@Entity()
export class Zone {
@PrimaryKey()
id!: number;
@OneToMany(() => Character, character => character.zone)
characters = new Collection<Character>(this);
@OneToMany(() => Chat, chat => chat.zone)
chats = new Collection<Chat>(this);
}

View File

@ -8,6 +8,7 @@ import cors from 'cors'
import { Server as SocketServer } from 'socket.io' import { Server as SocketServer } from 'socket.io'
import { Authentication } from './middleware/authentication' import { Authentication } from './middleware/authentication'
import { TSocket } from './utilities/types' import { TSocket } from './utilities/types'
import { MikroORM } from '@mikro-orm/mariadb'
import prisma from './utilities/prisma' import prisma from './utilities/prisma'
import { appLogger, watchLogs } from './utilities/logger' import { appLogger, watchLogs } from './utilities/logger'
import ZoneManager from './managers/zoneManager' import ZoneManager from './managers/zoneManager'
@ -57,6 +58,14 @@ export class Server {
appLogger.error(`Database connection failed: ${error.message}`) appLogger.error(`Database connection failed: ${error.message}`)
} }
// MikroORM
try {
const orm = await MikroORM.init();
appLogger.info('Database 2 connected')
} catch (error: any) {
appLogger.error(`Database 2 connection failed: ${error.message}`)
}
// Start the server // Start the server
try { try {
this.http.listen(config.PORT, config.HOST) this.http.listen(config.PORT, config.HOST)

View File

@ -3,7 +3,8 @@
// Enable latest features // Enable latest features
"lib": ["ESNext"], "lib": ["ESNext"],
"target": "ESNext", "target": "ESNext",
"module": "ESNext", "module": "NodeNext",
"moduleResolution": "NodeNext",
"moduleDetection": "force", "moduleDetection": "force",
"allowJs": true, "allowJs": true,
"declaration": true, "declaration": true,