forked from noxious/server
More event progress
This commit is contained in:
@ -1,79 +1,35 @@
|
||||
import { Server } from 'socket.io'
|
||||
|
||||
import type { Prisma } from '@prisma/client'
|
||||
|
||||
import { gameMasterLogger } from '#application/logger'
|
||||
import prisma from '#application/prisma'
|
||||
import { TSocket } from '#application/types'
|
||||
import { BaseEvent } from '#application/base/baseEvent'
|
||||
import { UUID } from '#application/types'
|
||||
import { Sprite } from '#entities/sprite'
|
||||
import CharacterRepository from '#repositories/characterRepository'
|
||||
import SpriteRepository from '#repositories/spriteRepository'
|
||||
|
||||
interface CopyPayload {
|
||||
id: string
|
||||
id: UUID
|
||||
}
|
||||
|
||||
export default class SpriteCopyEvent {
|
||||
constructor(
|
||||
private readonly io: Server,
|
||||
private readonly socket: TSocket
|
||||
) {}
|
||||
|
||||
export default class SpriteCopyEvent extends BaseEvent {
|
||||
public listen(): void {
|
||||
this.socket.on('gm:sprite:copy', this.handleEvent.bind(this))
|
||||
}
|
||||
|
||||
private async handleEvent(payload: CopyPayload, callback: (success: boolean) => void): Promise<void> {
|
||||
try {
|
||||
if (!(await this.validateGameMasterAccess())) {
|
||||
return callback(false)
|
||||
}
|
||||
if (!(await this.isCharacterGM())) return
|
||||
|
||||
const sourceSprite = await prisma.sprite.findUnique({
|
||||
where: { id: payload.id },
|
||||
include: {
|
||||
spriteActions: true
|
||||
}
|
||||
})
|
||||
const sourceSprite = await SpriteRepository.getById(payload.id)
|
||||
|
||||
if (!sourceSprite) {
|
||||
throw new Error('Source sprite not found')
|
||||
}
|
||||
|
||||
const newSprite = await prisma.sprite.create({
|
||||
data: {
|
||||
name: `${sourceSprite.name} (Copy)`,
|
||||
spriteActions: {
|
||||
create: sourceSprite.spriteActions.map((action) => ({
|
||||
action: action.action,
|
||||
sprites: action.sprites as Prisma.InputJsonValue,
|
||||
originX: action.originX,
|
||||
originY: action.originY,
|
||||
isAnimated: action.isAnimated,
|
||||
isLooping: action.isLooping,
|
||||
frameWidth: action.frameWidth,
|
||||
frameHeight: action.frameHeight,
|
||||
frameRate: action.frameRate
|
||||
}))
|
||||
}
|
||||
}
|
||||
})
|
||||
const newSprite = new Sprite()
|
||||
await newSprite.setName(`${sourceSprite.getName()} (Copy)`).setSpriteActions(sourceSprite.getSpriteActions()).save()
|
||||
|
||||
callback(true)
|
||||
} catch (error) {
|
||||
this.handleError(error, payload.id, callback)
|
||||
this.logger.error(`Error copying sprite:`, String(error))
|
||||
callback(false)
|
||||
}
|
||||
}
|
||||
|
||||
private async validateGameMasterAccess(): Promise<boolean> {
|
||||
const character = await CharacterRepository.getById(this.socket.characterId!)
|
||||
return character?.role === 'gm'
|
||||
}
|
||||
|
||||
private handleError(error: unknown, spriteId: string, callback: (success: boolean) => void): void {
|
||||
gameMasterLogger.error(`Error copying sprite ${spriteId}: ${this.getErrorMessage(error)}`)
|
||||
callback(false)
|
||||
}
|
||||
|
||||
private getErrorMessage(error: unknown): string {
|
||||
return error instanceof Error ? error.message : String(error)
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user