forked from noxious/server
Major refractor, cleaning and improvements.
This commit is contained in:
24
src/events/gameMaster/assetManager/tile/list.ts
Normal file
24
src/events/gameMaster/assetManager/tile/list.ts
Normal file
@ -0,0 +1,24 @@
|
||||
import { Server } from 'socket.io'
|
||||
import { TSocket } from '../../../../utilities/types'
|
||||
import { Tile } from '@prisma/client'
|
||||
import TileRepository from '../../../../repositories/tileRepository'
|
||||
|
||||
interface IPayload {}
|
||||
|
||||
/**
|
||||
* Handle game master list tile event
|
||||
* @param socket
|
||||
* @param io
|
||||
*/
|
||||
export default function (socket: TSocket, io: Server) {
|
||||
socket.on('gm:tile:list', async (data: any, callback: (response: Tile[]) => void) => {
|
||||
if (socket.character?.role !== 'gm') {
|
||||
console.log(`---Character #${socket.character?.id} is not a game master.`)
|
||||
return
|
||||
}
|
||||
|
||||
// get all tiles
|
||||
const tiles = await TileRepository.getAll()
|
||||
callback(tiles)
|
||||
})
|
||||
}
|
48
src/events/gameMaster/assetManager/tile/remove.ts
Normal file
48
src/events/gameMaster/assetManager/tile/remove.ts
Normal file
@ -0,0 +1,48 @@
|
||||
import { Server } from 'socket.io'
|
||||
import { TSocket } from '../../../../utilities/types'
|
||||
import path from 'path'
|
||||
import fs from 'fs'
|
||||
import prisma from '../../../../utilities/prisma'
|
||||
|
||||
type Payload = {
|
||||
id: string
|
||||
}
|
||||
|
||||
/**
|
||||
* Handle game master remove tile event
|
||||
* @param socket
|
||||
* @param io
|
||||
*/
|
||||
export default function (socket: TSocket, io: Server) {
|
||||
socket.on('gm:tile:remove', async (data: Payload, callback: (response: boolean) => void) => {
|
||||
if (socket.character?.role !== 'gm') {
|
||||
return
|
||||
}
|
||||
|
||||
try {
|
||||
await prisma.tile.delete({
|
||||
where: {
|
||||
id: data.id
|
||||
}
|
||||
})
|
||||
|
||||
// get root path
|
||||
const public_folder = path.join(process.cwd(), 'public', 'tiles')
|
||||
|
||||
// remove the tile from the disk
|
||||
const finalFilePath = path.join(public_folder, data.id + '.png')
|
||||
fs.unlink(finalFilePath, (err) => {
|
||||
if (err) {
|
||||
console.log(err)
|
||||
callback(false)
|
||||
return
|
||||
}
|
||||
|
||||
callback(true)
|
||||
})
|
||||
} catch (e) {
|
||||
console.log(e)
|
||||
callback(false)
|
||||
}
|
||||
})
|
||||
}
|
39
src/events/gameMaster/assetManager/tile/update.ts
Normal file
39
src/events/gameMaster/assetManager/tile/update.ts
Normal file
@ -0,0 +1,39 @@
|
||||
import { Server } from 'socket.io'
|
||||
import { TSocket } from '../../../../utilities/types'
|
||||
import prisma from '../../../../utilities/prisma'
|
||||
|
||||
type Payload = {
|
||||
id: string
|
||||
name: string
|
||||
tags: string[]
|
||||
}
|
||||
|
||||
/**
|
||||
* Handle game master tile update event
|
||||
* @param socket
|
||||
* @param io
|
||||
*/
|
||||
export default function (socket: TSocket, io: Server) {
|
||||
socket.on('gm:tile:update', async (data: Payload, callback: (success: boolean) => void) => {
|
||||
if (socket.character?.role !== 'gm') {
|
||||
return
|
||||
}
|
||||
|
||||
try {
|
||||
const Tile = await prisma.tile.update({
|
||||
where: {
|
||||
id: data.id
|
||||
},
|
||||
data: {
|
||||
name: data.name,
|
||||
tags: data.tags
|
||||
}
|
||||
})
|
||||
|
||||
callback(true)
|
||||
} catch (error) {
|
||||
console.error(error)
|
||||
callback(false)
|
||||
}
|
||||
})
|
||||
}
|
50
src/events/gameMaster/assetManager/tile/upload.ts
Normal file
50
src/events/gameMaster/assetManager/tile/upload.ts
Normal file
@ -0,0 +1,50 @@
|
||||
import { Server } from 'socket.io'
|
||||
import { TSocket } from '../../../../utilities/types'
|
||||
import { writeFile } from 'node:fs/promises'
|
||||
import path from 'path'
|
||||
import fs from 'fs/promises'
|
||||
import prisma from '../../../../utilities/prisma'
|
||||
|
||||
interface ITileData {
|
||||
[key: string]: Buffer
|
||||
}
|
||||
|
||||
/**
|
||||
* Handle game master upload tile event
|
||||
* @param socket
|
||||
* @param io
|
||||
*/
|
||||
export default function (socket: TSocket, io: Server) {
|
||||
socket.on('gm:tile:upload', async (data: ITileData, callback: (response: boolean) => void) => {
|
||||
try {
|
||||
if (socket.character?.role !== 'gm') {
|
||||
callback(false)
|
||||
return
|
||||
}
|
||||
|
||||
const public_folder = path.join(process.cwd(), 'public', 'tiles')
|
||||
|
||||
// Ensure the folder exists
|
||||
await fs.mkdir(public_folder, { recursive: true })
|
||||
|
||||
const uploadPromises = Object.entries(data).map(async ([key, tileData]) => {
|
||||
const tile = await prisma.tile.create({
|
||||
data: {
|
||||
name: 'New tile'
|
||||
}
|
||||
})
|
||||
const uuid = tile.id
|
||||
const filename = `${uuid}.png`
|
||||
const finalFilePath = path.join(public_folder, filename)
|
||||
await writeFile(finalFilePath, tileData)
|
||||
})
|
||||
|
||||
await Promise.all(uploadPromises)
|
||||
|
||||
callback(true)
|
||||
} catch (error) {
|
||||
console.error('Error uploading tile:', error)
|
||||
callback(false)
|
||||
}
|
||||
})
|
||||
}
|
Reference in New Issue
Block a user