forked from noxious/server
Moved service logic from repo to service, minor improvements, working hair customisation proof of concept
This commit is contained in:
@ -1,9 +1,9 @@
|
||||
import { AStar } from '../../utilities/character/aStar'
|
||||
import ZoneManager from '../../managers/zoneManager'
|
||||
import prisma from '../../utilities/prisma'
|
||||
import Rotation from '../../utilities/character/rotation'
|
||||
import { gameLogger } from '../../utilities/logger'
|
||||
import { Character, CharacterGender, CharacterRace } from '@prisma/client'
|
||||
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
|
||||
@ -13,6 +13,52 @@ interface Position {
|
||||
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}`)
|
||||
@ -55,26 +101,4 @@ export class CharacterService {
|
||||
private isValidPosition(position: Position): boolean {
|
||||
return Number.isFinite(position.x) && Number.isFinite(position.y) && position.x >= 0 && position.y >= 0
|
||||
}
|
||||
|
||||
async create(name: string, userId: number) {
|
||||
return prisma.character.create({
|
||||
data: {
|
||||
name,
|
||||
userId
|
||||
// characterTypeId: 1 // @TODO set to chosen character type
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
async updateCharacterPosition(id: number, positionX: number, positionY: number, rotation: number, zoneId: number) {
|
||||
await prisma.character.update({
|
||||
where: { id },
|
||||
data: {
|
||||
positionX,
|
||||
positionY,
|
||||
rotation,
|
||||
zoneId
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user