forked from noxious/server
Renamed folder utilities > application, added baseEntity class, updated baseRepo class, removed prisma helper
This commit is contained in:
@ -1,9 +1,14 @@
|
||||
import { AStar } from '#utilities/character/aStar'
|
||||
import { AStar } from '#application/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'
|
||||
import Rotation from '#application/character/rotation'
|
||||
import { appLogger, gameLogger } from '#application/logger'
|
||||
import { Database } from '#application/database'
|
||||
import { Character } from '#entities/character'
|
||||
import UserRepository from '#repositories/userRepository'
|
||||
import CharacterRepository from '#repositories/characterRepository'
|
||||
import CharacterHairRepository from '#repositories/characterHairRepository'
|
||||
import ZoneRepository from '#repositories/zoneRepository'
|
||||
import { Zone } from '#entities/zone'
|
||||
|
||||
interface Position {
|
||||
x: number
|
||||
@ -14,32 +19,41 @@ 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
|
||||
}
|
||||
})
|
||||
const user = await UserRepository.getById(userId)
|
||||
if (!user) return null
|
||||
|
||||
const character = new Character()
|
||||
character.name = name
|
||||
character.user = user
|
||||
await Database.save(character)
|
||||
|
||||
return character
|
||||
}
|
||||
|
||||
async updateHair(characterId: number, characterHairId: number | null) {
|
||||
await prisma.character.update({
|
||||
where: { id: characterId },
|
||||
data: {
|
||||
characterHairId
|
||||
}
|
||||
})
|
||||
const character = await CharacterRepository.getById(characterId)
|
||||
if (!character) return null
|
||||
|
||||
if (characterHairId === null) {
|
||||
character.characterHair = undefined
|
||||
await Database.save(character)
|
||||
return character
|
||||
}
|
||||
|
||||
const characterHair = await CharacterHairRepository.getById(characterHairId)
|
||||
character.characterHair = characterHair ?? undefined
|
||||
|
||||
await Database.save(character)
|
||||
return character
|
||||
}
|
||||
|
||||
async deleteByUserIdAndId(userId: number, characterId: number): Promise<Character | null> {
|
||||
try {
|
||||
return await prisma.character.delete({
|
||||
where: {
|
||||
userId,
|
||||
id: characterId
|
||||
}
|
||||
})
|
||||
const character = await CharacterRepository.getByUserAndId(userId, characterId)
|
||||
if (!character) return null
|
||||
|
||||
await Database.delete(character)
|
||||
return character
|
||||
} catch (error: any) {
|
||||
// Handle error
|
||||
appLogger.error(`Failed to delete character by user ID and character ID: ${error instanceof Error ? error.message : String(error)}`)
|
||||
@ -48,15 +62,16 @@ export class CharacterService {
|
||||
}
|
||||
|
||||
async updateCharacterPosition(id: number, positionX: number, positionY: number, rotation: number, zoneId: number) {
|
||||
await prisma.character.update({
|
||||
where: { id },
|
||||
data: {
|
||||
positionX,
|
||||
positionY,
|
||||
rotation,
|
||||
zoneId
|
||||
}
|
||||
})
|
||||
const character = await CharacterRepository.getById(id)
|
||||
if (!character) return null
|
||||
|
||||
character.positionX = positionX
|
||||
character.positionY = positionY
|
||||
character.rotation = rotation
|
||||
character.zone = await ZoneRepository.getById(zoneId) as Zone
|
||||
|
||||
await Database.save(character)
|
||||
return character
|
||||
}
|
||||
|
||||
public updatePosition(character: Character, position: Position, newZoneId?: number): void {
|
||||
@ -68,12 +83,12 @@ export class CharacterService {
|
||||
positionX: position.x,
|
||||
positionY: position.y,
|
||||
rotation: Rotation.calculate(character.positionX, character.positionY, position.x, position.y),
|
||||
zoneId: newZoneId ?? character.zoneId
|
||||
zoneId: newZoneId ?? character.zone.id
|
||||
})
|
||||
}
|
||||
|
||||
public async calculatePath(character: Character, targetX: number, targetY: number): Promise<Position[] | null> {
|
||||
const zone = ZoneManager.getZoneById(character.zoneId)
|
||||
const zone = ZoneManager.getZoneById(character.zone.id)
|
||||
const grid = await zone?.getGrid()
|
||||
|
||||
if (!grid?.length) {
|
||||
|
Reference in New Issue
Block a user