forked from noxious/server
Renamed folder
This commit is contained in:
63
src/events/gameMaster/zoneEditor/create.ts
Normal file
63
src/events/gameMaster/zoneEditor/create.ts
Normal file
@ -0,0 +1,63 @@
|
||||
import { Zone } from '@prisma/client'
|
||||
import { Server } from 'socket.io'
|
||||
|
||||
import { gameMasterLogger } from '#application/logger'
|
||||
import prisma from '#application/prisma'
|
||||
import { TSocket } from '#application/types'
|
||||
import CharacterRepository from '#repositories/characterRepository'
|
||||
import ZoneRepository from '#repositories/zoneRepository'
|
||||
|
||||
type Payload = {
|
||||
name: string
|
||||
width: number
|
||||
height: number
|
||||
}
|
||||
|
||||
export default class ZoneCreateEvent {
|
||||
constructor(
|
||||
private readonly io: Server,
|
||||
private readonly socket: TSocket
|
||||
) {}
|
||||
|
||||
public listen(): void {
|
||||
this.socket.on('gm:zone_editor:zone:create', this.handleEvent.bind(this))
|
||||
}
|
||||
|
||||
private async handleEvent(data: Payload, callback: (response: Zone[]) => void): Promise<void> {
|
||||
try {
|
||||
const character = await CharacterRepository.getById(this.socket.characterId as number)
|
||||
if (!character) {
|
||||
gameMasterLogger.error('gm:zone_editor:zone:create error', 'Character not found')
|
||||
callback([])
|
||||
return
|
||||
}
|
||||
|
||||
if (character.role !== 'gm') {
|
||||
gameMasterLogger.info(`User ${character.id} tried to create zone but is not a game master.`)
|
||||
callback([])
|
||||
return
|
||||
}
|
||||
|
||||
gameMasterLogger.info(`User ${character.id} has created a new zone via zone editor.`)
|
||||
|
||||
const zone = await prisma.zone.create({
|
||||
data: {
|
||||
name: data.name,
|
||||
width: data.width,
|
||||
height: data.height,
|
||||
tiles: Array.from({ length: data.height }, () => Array.from({ length: data.width }, () => 'blank_tile'))
|
||||
}
|
||||
})
|
||||
|
||||
const zoneList = await ZoneRepository.getAll()
|
||||
callback(zoneList)
|
||||
|
||||
// You might want to emit an event to notify other clients about the new zone
|
||||
// this.io.emit('gm:zone_created', zone);
|
||||
} catch (error: any) {
|
||||
gameMasterLogger.error('gm:zone_editor:zone:create error', error.message)
|
||||
this.socket.emit('notification', { message: 'Failed to create zone.' })
|
||||
callback([])
|
||||
}
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user