Added README.md, started refactoring init. command

This commit is contained in:
Dennis Postma 2024-12-25 19:07:03 +01:00
parent bf64a6df70
commit 2de2bec705
8 changed files with 250 additions and 274 deletions

40
README.md Normal file
View File

@ -0,0 +1,40 @@
# Noxious game server
This is the server for the Noxious game.
## Installation
1. Clone the repository
2. Install dependencies with `npm install`
3. Copy the `.env.example` file to `.env` and fill in the required variables
4. Run the server with `npm run dev`
## Commands
### `npm run dev`
Starts the server in development mode.
### `npm run build`
Builds the server for production.
### `npm run format`
Formats the code using Prettier.
## MikroORM
MikroORM is used as the ORM for the server.
### Create init. migrations
Run `npx mikro-orm migration:create --initial` to create a new initial migration.
### Create migrations
Run `npx mikro-orm migration:create` to create a new migration.
### Apply migrations
Run `npx mikro-orm migration:up` to apply all pending migrations.

View File

@ -1,6 +1,6 @@
import { Migration } from '@mikro-orm/migrations'; import { Migration } from '@mikro-orm/migrations';
export class Migration20241225124810 extends Migration { export class Migration20241225180201 extends Migration {
override async up(): Promise<void> { override async up(): Promise<void> {
this.addSql(`create table \`map_object\` (\`id\` varchar(255) not null, \`name\` varchar(255) not null, \`tags\` json null, \`origin_x\` int not null default 0, \`origin_y\` int not null default 0, \`is_animated\` tinyint(1) not null default false, \`frame_rate\` int not null default 0, \`frame_width\` int not null default 0, \`frame_height\` int not null default 0, \`created_at\` datetime not null, \`updated_at\` datetime not null, primary key (\`id\`)) default character set utf8mb4 engine = InnoDB;`); this.addSql(`create table \`map_object\` (\`id\` varchar(255) not null, \`name\` varchar(255) not null, \`tags\` json null, \`origin_x\` int not null default 0, \`origin_y\` int not null default 0, \`is_animated\` tinyint(1) not null default false, \`frame_rate\` int not null default 0, \`frame_width\` int not null default 0, \`frame_height\` int not null default 0, \`created_at\` datetime not null, \`updated_at\` datetime not null, primary key (\`id\`)) default character set utf8mb4 engine = InnoDB;`);

File diff suppressed because one or more lines are too long

View File

@ -1,3 +1,4 @@
import { randomUUID } from 'node:crypto'
import { Entity, ManyToOne, PrimaryKey, Property } from '@mikro-orm/core' import { Entity, ManyToOne, PrimaryKey, Property } from '@mikro-orm/core'
import { BaseEntity } from '#application/bases/baseEntity' import { BaseEntity } from '#application/bases/baseEntity'
import { Zone } from './zone' import { Zone } from './zone'
@ -5,7 +6,7 @@ import { Zone } from './zone'
@Entity() @Entity()
export class ZoneEffect extends BaseEntity { export class ZoneEffect extends BaseEntity {
@PrimaryKey() @PrimaryKey()
id!: string id = randomUUID()
@ManyToOne(() => Zone) @ManyToOne(() => Zone)
zone!: Zone zone!: Zone

View File

@ -3,6 +3,16 @@ import { BaseRepository } from '#application/bases/baseRepository'
import { CharacterHair } from '#entities/characterHair' import { CharacterHair } from '#entities/characterHair'
class CharacterHairRepository extends BaseRepository { class CharacterHairRepository extends BaseRepository {
async getFirst() {
try {
const repository = this.em.getRepository(CharacterHair)
return await repository.findOne({ id: { $exists: true } })
} catch (error: any) {
appLogger.error(`Failed to get first character hair: ${error instanceof Error ? error.message : String(error)}`)
return null
}
}
async getAll() { async getAll() {
try { try {
const repository = this.em.getRepository(CharacterHair) const repository = this.em.getRepository(CharacterHair)

View File

@ -3,6 +3,16 @@ import { BaseRepository } from '#application/bases/baseRepository'
import { CharacterType } from '#entities/characterType' import { CharacterType } from '#entities/characterType'
class CharacterTypeRepository extends BaseRepository { class CharacterTypeRepository extends BaseRepository {
async getFirst() {
try {
const repository = this.em.getRepository(CharacterType)
return await repository.findOne({ id: { $exists: true } })
} catch (error: any) {
appLogger.error(`Failed to get first character type: ${error instanceof Error ? error.message : String(error)}`)
return null
}
}
async getAll() { async getAll() {
try { try {
const repository = this.em.getRepository(CharacterType) const repository = this.em.getRepository(CharacterType)

View File

@ -5,6 +5,16 @@ import { ZoneObject } from '#entities/zoneObject'
import { Zone } from '#entities/zone' import { Zone } from '#entities/zone'
class ZoneRepository extends BaseRepository { class ZoneRepository extends BaseRepository {
async getFirst(): Promise<Zone | null> {
try {
const repository = this.em.getRepository(Zone)
return await repository.findOne({ id: { $exists: true } })
} catch (error: any) {
appLogger.error(`Failed to get first zone: ${error instanceof Error ? error.message : String(error)}`)
return null
}
}
async getAll(): Promise<Zone[]> { async getAll(): Promise<Zone[]> {
try { try {
const repository = this.em.getRepository(Zone) const repository = this.em.getRepository(Zone)

View File

@ -38,7 +38,9 @@ export default class CharacterCreateEvent {
} }
const characterService = new CharacterService() const characterService = new CharacterService()
const character: Character = await characterService.create(data.name, user_id) const character = await characterService.create(data.name, user_id)
if (!character) return this.socket.emit('notification', { message: 'Failed to create character. Please try again (later).' })
characters = [...characters, character] characters = [...characters, character]