GM event fixes

This commit is contained in:
2024-09-21 00:42:08 +02:00
parent 0a629bf80f
commit 6b2433f814
20 changed files with 105 additions and 72 deletions

View File

@ -3,6 +3,7 @@ import { TSocket } from '../../../../utilities/types'
import { Object } from '@prisma/client'
import ObjectRepository from '../../../../repositories/objectRepository'
import CharacterManager from '../../../../managers/characterManager'
import characterRepository from '../../../../repositories/characterRepository'
interface IPayload {}
@ -13,10 +14,11 @@ interface IPayload {}
*/
export default function (socket: TSocket, io: Server) {
socket.on('gm:object:list', async (data: any, callback: (response: Object[]) => void) => {
const character = CharacterManager.getCharacterFromSocket(socket);
if (character?.role !== 'gm') {
console.log(`---Character #${character?.id} is not a game master.`)
return
const character = await characterRepository.getById(socket.characterId as number);
if (!character) return callback([])
if (character.role !== 'gm') {
return callback([])
}
// get all objects

View File

@ -4,6 +4,7 @@ import path from 'path'
import fs from 'fs'
import prisma from '../../../../utilities/prisma'
import CharacterManager from '../../../../managers/characterManager'
import characterRepository from '../../../../repositories/characterRepository'
interface IPayload {
object: string
@ -16,9 +17,11 @@ interface IPayload {
*/
export default function (socket: TSocket, io: Server) {
socket.on('gm:object:remove', async (data: IPayload, callback: (response: boolean) => void) => {
const character = CharacterManager.getCharacterFromSocket(socket);
if (character?.role !== 'gm') {
return
const character = await characterRepository.getById(socket.characterId as number);
if (!character) return callback(false)
if (character.role !== 'gm') {
return callback(false)
}
try {

View File

@ -2,6 +2,7 @@ import { Server } from 'socket.io'
import { TSocket } from '../../../../utilities/types'
import prisma from '../../../../utilities/prisma'
import CharacterManager from '../../../../managers/characterManager'
import characterRepository from '../../../../repositories/characterRepository'
type Payload = {
id: string
@ -22,9 +23,11 @@ type Payload = {
*/
export default function (socket: TSocket, io: Server) {
socket.on('gm:object:update', async (data: Payload, callback: (success: boolean) => void) => {
const character = CharacterManager.getCharacterFromSocket(socket);
if (character?.role !== 'gm') {
return
const character = await characterRepository.getById(socket.characterId as number);
if (!character) return callback(false)
if (character.role !== 'gm') {
return callback(false)
}
try {

View File

@ -7,6 +7,7 @@ import prisma from '../../../../utilities/prisma'
import sharp from 'sharp'
import logger from '../../../../utilities/logger'
import CharacterManager from '../../../../managers/characterManager'
import characterRepository from '../../../../repositories/characterRepository'
interface IObjectData {
[key: string]: Buffer
@ -24,12 +25,12 @@ export default class ObjectUploadEvent {
private async handleObjectUpload(data: IObjectData, callback: (response: boolean) => void): Promise<void> {
try {
const character = CharacterManager.getCharacterFromSocket(this.socket);
if (character?.role !== 'gm') {
callback(false)
return
}
const character = await characterRepository.getById(this.socket.characterId as number);
if (!character) return callback(false)
if (character.role !== 'gm') {
return callback(false)
}
const public_folder = path.join(process.cwd(), 'public', 'objects')
// Ensure the folder exists