forked from noxious/server
Moved service logic from repo to service, minor improvements, working hair customisation proof of concept
This commit is contained in:
104
src/services/characterService.ts
Normal file
104
src/services/characterService.ts
Normal file
@ -0,0 +1,104 @@
|
||||
import { AStar } from '../utilities/character/aStar'
|
||||
import ZoneManager from '../managers/zoneManager'
|
||||
import prisma from '../utilities/prisma'
|
||||
import Rotation from '../utilities/character/rotation'
|
||||
import { appLogger, gameLogger } from '../utilities/logger'
|
||||
import { Character } from '@prisma/client'
|
||||
|
||||
interface Position {
|
||||
x: number
|
||||
y: number
|
||||
}
|
||||
|
||||
export class CharacterService {
|
||||
private readonly MOVEMENT_DELAY_MS = 250
|
||||
|
||||
async create(name: string, userId: number) {
|
||||
return prisma.character.create({
|
||||
data: {
|
||||
name,
|
||||
userId
|
||||
// characterTypeId: 1 // @TODO set to chosen character type
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
async updateHair(characterId: number, hairId: number | null) {
|
||||
await prisma.character.update({
|
||||
where: { id: characterId },
|
||||
data: {
|
||||
hairId
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
async deleteByUserIdAndId(userId: number, characterId: number): Promise<Character | null> {
|
||||
try {
|
||||
return await prisma.character.delete({
|
||||
where: {
|
||||
userId,
|
||||
id: characterId
|
||||
}
|
||||
})
|
||||
} catch (error: any) {
|
||||
// Handle error
|
||||
appLogger.error(`Failed to delete character by user ID and character ID: ${error instanceof Error ? error.message : String(error)}`)
|
||||
return null
|
||||
}
|
||||
}
|
||||
|
||||
async updateCharacterPosition(id: number, positionX: number, positionY: number, rotation: number, zoneId: number) {
|
||||
await prisma.character.update({
|
||||
where: { id },
|
||||
data: {
|
||||
positionX,
|
||||
positionY,
|
||||
rotation,
|
||||
zoneId
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
public updatePosition(character: Character, position: Position, newZoneId?: number): void {
|
||||
if (!this.isValidPosition(position)) {
|
||||
gameLogger.error(`Invalid position coordinates: ${position.x}, ${position.y}`)
|
||||
}
|
||||
|
||||
Object.assign(character, {
|
||||
positionX: position.x,
|
||||
positionY: position.y,
|
||||
rotation: Rotation.calculate(character.positionX, character.positionY, position.x, position.y),
|
||||
zoneId: newZoneId ?? character.zoneId
|
||||
})
|
||||
}
|
||||
|
||||
public async calculatePath(character: Character, targetX: number, targetY: number): Promise<Position[] | null> {
|
||||
const zone = ZoneManager.getZoneById(character.zoneId)
|
||||
const grid = await zone?.getGrid()
|
||||
|
||||
if (!grid?.length) {
|
||||
gameLogger.error('character:move error', 'Grid not found or empty')
|
||||
return null
|
||||
}
|
||||
|
||||
const start: Position = {
|
||||
x: Math.floor(character.positionX),
|
||||
y: Math.floor(character.positionY)
|
||||
}
|
||||
|
||||
const end: Position = {
|
||||
x: Math.floor(targetX),
|
||||
y: Math.floor(targetY)
|
||||
}
|
||||
|
||||
return AStar.findPath(start, end, grid)
|
||||
}
|
||||
|
||||
public async applyMovementDelay(): Promise<void> {
|
||||
await new Promise((resolve) => setTimeout(resolve, this.MOVEMENT_DELAY_MS))
|
||||
}
|
||||
|
||||
private isValidPosition(position: Position): boolean {
|
||||
return Number.isFinite(position.x) && Number.isFinite(position.y) && position.x >= 0 && position.y >= 0
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user