47 lines
1.3 KiB
TypeScript
47 lines
1.3 KiB
TypeScript
import { Server } from 'socket.io'
|
|
import { TSocket } from '../../../utilities/Types'
|
|
import ZoneRepository from '../../../repositories/ZoneRepository'
|
|
import { Zone } from '@prisma/client'
|
|
import prisma from '../../../utilities/Prisma'
|
|
|
|
type Payload = {
|
|
name: string
|
|
width: number
|
|
height: number
|
|
}
|
|
|
|
/**
|
|
* Handle game master zone create event
|
|
* @param socket
|
|
* @param io
|
|
*/
|
|
export default function (socket: TSocket, io: Server) {
|
|
socket.on('gm:zone_editor:zone:create', async (data: Payload, callback: (response: Zone[]) => void) => {
|
|
if (socket.character?.role !== 'gm') {
|
|
console.log(`---Character #${socket.character?.id} is not a game master.`)
|
|
return
|
|
}
|
|
|
|
console.log(`---GM ${socket.character?.id} has created a new zone via zone editor.`)
|
|
let zoneList: Zone[] = []
|
|
try {
|
|
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'))
|
|
}
|
|
})
|
|
|
|
zoneList = await ZoneRepository.getAll()
|
|
callback(zoneList)
|
|
// send over zone and characters to socket
|
|
} catch (e) {
|
|
console.error(e)
|
|
socket.emit('notification', { message: 'Failed to create zone.' })
|
|
callback(zoneList)
|
|
}
|
|
})
|
|
}
|