Greatly improved server code base

This commit is contained in:
2024-12-28 17:26:17 +01:00
parent bd85908014
commit bd3bf6f580
39 changed files with 227 additions and 246 deletions

View File

@ -0,0 +1,5 @@
import { Server } from 'socket.io'
export abstract class BaseCommand {
constructor(readonly io: Server) {}
}

View File

@ -1,4 +1,5 @@
import { EntityManager } from '@mikro-orm/core'
import Database from '#application/database'
import { appLogger } from '#application/logger'
@ -8,18 +9,18 @@ export abstract class BaseEntity {
}
async save(): Promise<this> {
return this.performDbOperation('persist', 'save entity')
return this.execute('persist', 'save entity')
}
async update(): Promise<this> {
return this.performDbOperation('merge', 'update entity')
return this.execute('merge', 'update entity')
}
async delete(): Promise<this> {
return this.performDbOperation('remove', 'remove entity')
return this.execute('remove', 'remove entity')
}
private async performDbOperation(method: 'persist' | 'merge' | 'remove', actionDescription: string): Promise<this> {
private async execute(method: 'persist' | 'merge' | 'remove', actionDescription: string): Promise<this> {
try {
const em = this.getEntityManager()
@ -38,4 +39,4 @@ export abstract class BaseEntity {
throw error
}
}
}
}

View File

@ -1,4 +1,10 @@
import { Server } from 'socket.io'
import { TSocket } from '#application/types'
export abstract class BaseEvent {
}
constructor(
readonly io: Server,
readonly socket: TSocket
) {}
}

View File

@ -1,12 +1,9 @@
import { EntityManager, MikroORM } from '@mikro-orm/core'
import { EntityManager } from '@mikro-orm/core'
import Database from '../database'
export abstract class BaseRepository {
protected get orm(): MikroORM {
return Database.getORM()
}
protected get em(): EntityManager {
return Database.getEntityManager()
}
}
}

View File

@ -1,70 +0,0 @@
import config from '../config'
type Position = { x: number; y: number }
export type Node = Position & { parent?: Node; g: number; h: number; f: number }
export class AStar {
private static readonly DIRECTIONS = [
{ x: 0, y: -1 }, // Up
{ x: 0, y: 1 }, // Down
{ x: -1, y: 0 }, // Left
{ x: 1, y: 0 }, // Right
{ x: -1, y: -1 },
{ x: -1, y: 1 },
{ x: 1, y: -1 },
{ x: 1, y: 1 }
]
static findPath(start: Position, end: Position, grid: number[][]): Node[] {
const openList: Node[] = [{ ...start, g: 0, h: 0, f: 0 }]
const closedSet = new Set<string>()
const getKey = (p: Position) => `${p.x},${p.y}`
while (openList.length > 0) {
const current = openList.reduce((min, node) => (node.f < min.f ? node : min))
if (current.x === end.x && current.y === end.y) return this.reconstructPath(current)
openList.splice(openList.indexOf(current), 1)
closedSet.add(getKey(current))
const neighbors = this.DIRECTIONS.slice(0, config.ALLOW_DIAGONAL_MOVEMENT ? 8 : 4)
.map((dir) => ({ x: current.x + dir.x, y: current.y + dir.y }))
.filter((pos) => this.isValidPosition(pos, grid, end))
for (const neighbor of neighbors) {
if (closedSet.has(getKey(neighbor))) continue
const g = current.g + this.getDistance(current, neighbor)
const existing = openList.find((node) => node.x === neighbor.x && node.y === neighbor.y)
if (!existing || g < existing.g) {
const h = this.getDistance(neighbor, end)
const node: Node = { ...neighbor, g, h, f: g + h, parent: current }
if (!existing) openList.push(node)
else Object.assign(existing, node)
}
}
}
return [] // No path found
}
private static isValidPosition(pos: Position, grid: number[][], end: Position): boolean {
return pos.x >= 0 && pos.y >= 0 && pos.x < grid[0].length && pos.y < grid.length && (grid[pos.y][pos.x] === 0 || (pos.x === end.x && pos.y === end.y))
}
private static getDistance(a: Position, b: Position): number {
const dx = Math.abs(a.x - b.x),
dy = Math.abs(a.y - b.y)
// Manhattan distance for straight paths, then Euclidean for diagonals
return dx + dy + (Math.sqrt(2) - 2) * Math.min(dx, dy)
}
private static reconstructPath(endNode: Node): Node[] {
const path: Node[] = []
for (let current: Node | undefined = endNode; current; current = current.parent) {
path.unshift(current)
}
return path
}
}

View File

@ -1,33 +0,0 @@
import config from '../config'
class Rotation {
static calculate(X1: number, Y1: number, X2: number, Y2: number): number {
if (config.ALLOW_DIAGONAL_MOVEMENT) {
// Check diagonal movements
if (X1 > X2 && Y1 > Y2) {
return 7
} else if (X1 < X2 && Y1 < Y2) {
return 3
} else if (X1 > X2 && Y1 < Y2) {
return 5
} else if (X1 < X2 && Y1 > Y2) {
return 1
}
}
// Non-diagonal movements
if (X1 > X2) {
return 6
} else if (X1 < X2) {
return 2
} else if (Y1 < Y2) {
return 4
} else if (Y1 > Y2) {
return 0
}
return 0 // Default case
}
}
export default Rotation

View File

@ -1,11 +0,0 @@
export function isCommand(message: string, command?: string) {
if (command) {
return message === `/${command}` || message.startsWith(`/${command} `)
}
return message.startsWith('/')
}
export function getArgs(command: string, message: string): string[] | undefined {
if (!isCommand(message, command)) return
return message.split(`/${command} `)[1].split(' ')
}

View File

@ -1,5 +1,6 @@
import { MikroORM } from '@mikro-orm/mysql'
import { EntityManager } from '@mikro-orm/core'
import { MikroORM } from '@mikro-orm/mysql'
import { appLogger } from './logger'
import config from '../../mikro-orm.config'
@ -33,4 +34,4 @@ class Database {
}
}
export default Database
export default Database

View File

@ -41,7 +41,7 @@ export type AssetData = {
frameRate?: number
frameWidth?: number
frameHeight?: number
frameRate?: number
frameCount?: number
}
export type WorldSettings = {

View File

@ -1,9 +0,0 @@
export function FlattenZoneArray(tiles: string[][]) {
const normalArray = []
for (const row of tiles) {
normalArray.push(...row)
}
return normalArray
}