forked from noxious/server
GM event fixes
This commit is contained in:
parent
0a629bf80f
commit
6b2433f814
@ -1,6 +1,5 @@
|
||||
import { Server } from 'socket.io'
|
||||
import { TSocket } from '../utilities/types'
|
||||
import ZoneManager from '../managers/zoneManager'
|
||||
import CharacterManager from '../managers/characterManager'
|
||||
|
||||
export default function (socket: TSocket, io: Server) {
|
||||
|
@ -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
|
||||
|
@ -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 {
|
||||
|
@ -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 {
|
||||
|
@ -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
|
||||
|
@ -4,6 +4,7 @@ import path from 'path'
|
||||
import fs from 'fs/promises'
|
||||
import prisma from '../../../../utilities/prisma'
|
||||
import CharacterManager from '../../../../managers/characterManager'
|
||||
import characterRepository from '../../../../repositories/characterRepository'
|
||||
|
||||
/**
|
||||
* Handle game master new sprite event
|
||||
@ -13,13 +14,11 @@ import CharacterManager from '../../../../managers/characterManager'
|
||||
export default function (socket: TSocket, io: Server) {
|
||||
socket.on('gm:sprite:create', async (data: undefined, callback: (response: boolean) => void) => {
|
||||
try {
|
||||
if (!socket.characterId) return
|
||||
const character = CharacterManager.getCharacterFromSocket(socket);
|
||||
if(!character) return
|
||||
const character = await characterRepository.getById(socket.characterId as number);
|
||||
if (!character) return callback(false)
|
||||
|
||||
if (character.role !== 'gm') {
|
||||
callback(false)
|
||||
return
|
||||
return callback(false)
|
||||
}
|
||||
|
||||
const public_folder = path.join(process.cwd(), 'public', 'sprites')
|
||||
|
@ -27,8 +27,7 @@ export default class GMSpriteDeleteEvent {
|
||||
private async handleSpriteDelete(data: Payload, callback: (response: boolean) => void): Promise<void> {
|
||||
const character = CharacterManager.getCharacterFromSocket(this.socket);
|
||||
if (character?.role !== 'gm') {
|
||||
callback(false)
|
||||
return
|
||||
return callback(false)
|
||||
}
|
||||
|
||||
try {
|
||||
|
@ -3,6 +3,7 @@ import { TSocket } from '../../../../utilities/types'
|
||||
import { Sprite } from '@prisma/client'
|
||||
import SpriteRepository from '../../../../repositories/spriteRepository'
|
||||
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:sprite:list', async (data: any, callback: (response: Sprite[]) => 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 sprites
|
||||
|
@ -31,8 +31,7 @@ export default function (socket: TSocket, io: Server) {
|
||||
socket.on('gm:sprite:update', async (data: Payload, callback: (success: boolean) => void) => {
|
||||
const character = CharacterManager.getCharacterFromSocket(socket);
|
||||
if (character?.role !== 'gm') {
|
||||
callback(false)
|
||||
return
|
||||
return callback(false)
|
||||
}
|
||||
|
||||
try {
|
||||
|
@ -5,6 +5,7 @@ import { TSocket } from '../../../../utilities/types'
|
||||
import prisma from '../../../../utilities/prisma'
|
||||
import logger from '../../../../utilities/logger'
|
||||
import CharacterManager from '../../../../managers/characterManager'
|
||||
import characterRepository from '../../../../repositories/characterRepository'
|
||||
|
||||
type Payload = {
|
||||
id: string
|
||||
@ -25,9 +26,10 @@ export default class GMTileDeleteEvent {
|
||||
}
|
||||
|
||||
private async handleTileDelete(data: Payload, callback: (response: boolean) => void): Promise<void> {
|
||||
const character = CharacterManager.getCharacterFromSocket(this.socket);
|
||||
if (character?.role !== 'gm') {
|
||||
callback(false)
|
||||
const character = await characterRepository.getById(this.socket.characterId as number);
|
||||
if (!character) return callback(false)
|
||||
|
||||
if (character.role !== 'gm') {
|
||||
return
|
||||
}
|
||||
|
||||
|
@ -3,6 +3,7 @@ import { TSocket } from '../../../../utilities/types'
|
||||
import { Tile } from '@prisma/client'
|
||||
import TileRepository from '../../../../repositories/tileRepository'
|
||||
import CharacterManager from '../../../../managers/characterManager'
|
||||
import characterRepository from '../../../../repositories/characterRepository'
|
||||
|
||||
interface IPayload {}
|
||||
|
||||
@ -13,9 +14,10 @@ interface IPayload {}
|
||||
*/
|
||||
export default function (socket: TSocket, io: Server) {
|
||||
socket.on('gm:tile:list', async (data: any, callback: (response: Tile[]) => void) => {
|
||||
const character = CharacterManager.getCharacterFromSocket(socket);
|
||||
if (character?.role !== 'gm') {
|
||||
console.log(`---Character #${character?.id} is not a game master.`)
|
||||
const character = await characterRepository.getById(socket.characterId as number);
|
||||
if (!character) return
|
||||
|
||||
if (character.role !== 'gm') {
|
||||
return
|
||||
}
|
||||
|
||||
|
@ -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
|
||||
@ -16,8 +17,10 @@ type Payload = {
|
||||
*/
|
||||
export default function (socket: TSocket, io: Server) {
|
||||
socket.on('gm:tile:update', async (data: Payload, callback: (success: boolean) => void) => {
|
||||
const character = CharacterManager.getCharacterFromSocket(socket);
|
||||
if (character?.role !== 'gm') {
|
||||
const character = await characterRepository.getById(socket.characterId as number);
|
||||
if (!character) return callback(false)
|
||||
|
||||
if (character.role !== 'gm') {
|
||||
return
|
||||
}
|
||||
|
||||
|
@ -5,6 +5,8 @@ import path from 'path'
|
||||
import fs from 'fs/promises'
|
||||
import prisma from '../../../../utilities/prisma'
|
||||
import CharacterManager from '../../../../managers/characterManager'
|
||||
import characterRepository from '../../../../repositories/characterRepository'
|
||||
import logger from '../../../../utilities/logger'
|
||||
|
||||
interface ITileData {
|
||||
[key: string]: Buffer
|
||||
@ -18,9 +20,10 @@ interface ITileData {
|
||||
export default function (socket: TSocket, io: Server) {
|
||||
socket.on('gm:tile:upload', async (data: ITileData, callback: (response: boolean) => void) => {
|
||||
try {
|
||||
const character = CharacterManager.getCharacterFromSocket(socket);
|
||||
if (character?.role !== 'gm') {
|
||||
callback(false)
|
||||
const character = await characterRepository.getById(socket.characterId as number);
|
||||
if (!character) return callback(false)
|
||||
|
||||
if (character.role !== 'gm') {
|
||||
return
|
||||
}
|
||||
|
||||
|
@ -4,6 +4,8 @@ import ZoneRepository from '../../../repositories/zoneRepository'
|
||||
import { Zone } from '@prisma/client'
|
||||
import prisma from '../../../utilities/prisma'
|
||||
import CharacterManager from '../../../managers/characterManager'
|
||||
import characterRepository from '../../../repositories/characterRepository'
|
||||
import logger from '../../../utilities/logger'
|
||||
|
||||
type Payload = {
|
||||
name: string
|
||||
@ -18,13 +20,16 @@ type Payload = {
|
||||
*/
|
||||
export default function (socket: TSocket, io: Server) {
|
||||
socket.on('gm:zone_editor:zone:create', async (data: Payload, callback: (response: Zone[]) => void) => {
|
||||
const character = CharacterManager.getCharacterFromSocket(socket);
|
||||
if (character?.role !== 'gm') {
|
||||
console.log(`---Character #${character?.id} is not a game master.`)
|
||||
const character = await characterRepository.getById(socket.characterId as number);
|
||||
if (!character) return
|
||||
|
||||
if (character.role !== 'gm') {
|
||||
logger.info(`User ${character.id} tried to create zone but is not a game master.`)
|
||||
return
|
||||
}
|
||||
|
||||
console.log(`---GM ${character?.id} has created a new zone via zone editor.`)
|
||||
logger.info(`User ${character.id} has created a new zone via zone editor.`)
|
||||
|
||||
let zoneList: Zone[] = []
|
||||
try {
|
||||
const zone = await prisma.zone.create({
|
||||
|
@ -3,6 +3,8 @@ import { TSocket } from '../../../utilities/types'
|
||||
import ZoneRepository from '../../../repositories/zoneRepository'
|
||||
import prisma from '../../../utilities/prisma'
|
||||
import CharacterManager from '../../../managers/characterManager'
|
||||
import characterRepository from '../../../repositories/characterRepository'
|
||||
import logger from '../../../utilities/logger'
|
||||
|
||||
type Payload = {
|
||||
zoneId: number
|
||||
@ -15,14 +17,15 @@ type Payload = {
|
||||
*/
|
||||
export default function (socket: TSocket, io: Server) {
|
||||
socket.on('gm:zone_editor:zone:delete', async (data: Payload, callback: (response: boolean) => void) => {
|
||||
const character = CharacterManager.getCharacterFromSocket(socket);
|
||||
const character = await characterRepository.getById(socket.characterId as number);
|
||||
if (!character) return
|
||||
|
||||
if (character?.role !== 'gm') {
|
||||
console.log(`---Character #${character?.id} is not a game master.`)
|
||||
if (character.role !== 'gm') {
|
||||
logger.info(`User ${character.id} tried to delete zone but is not a game master.`)
|
||||
return
|
||||
}
|
||||
|
||||
console.log(`---GM ${character?.id} has deleted a zone via zone editor.`)
|
||||
logger.info(`User ${character.id} has deleted a zone via zone editor.`)
|
||||
|
||||
try {
|
||||
const zone = await ZoneRepository.getById(data.zoneId)
|
||||
|
@ -3,6 +3,8 @@ import { TSocket } from '../../../utilities/types'
|
||||
import { Zone } from '@prisma/client'
|
||||
import ZoneRepository from '../../../repositories/zoneRepository'
|
||||
import CharacterManager from '../../../managers/characterManager'
|
||||
import characterRepository from '../../../repositories/characterRepository'
|
||||
import logger from '../../../utilities/logger'
|
||||
|
||||
interface IPayload {}
|
||||
|
||||
@ -13,13 +15,15 @@ interface IPayload {}
|
||||
*/
|
||||
export default function (socket: TSocket, io: Server) {
|
||||
socket.on('gm:zone_editor:zone:list', async (data: IPayload, callback: (response: Zone[]) => void) => {
|
||||
const character = CharacterManager.getCharacterFromSocket(socket);
|
||||
if (character?.role !== 'gm') {
|
||||
console.log(`---Character #${character?.id} is not a game master.`)
|
||||
const character = await characterRepository.getById(socket.characterId as number);
|
||||
if (!character) return
|
||||
|
||||
if (character.role !== 'gm') {
|
||||
logger.info(`User ${character.id} tried to list zones but is not a game master.`)
|
||||
return
|
||||
}
|
||||
|
||||
console.log(`---GM ${character?.id} has requested zone list via zone editor.`)
|
||||
logger.info(`User ${character.id} has requested zone list via zone editor.`)
|
||||
|
||||
try {
|
||||
const zones = await ZoneRepository.getAll()
|
||||
|
@ -1,8 +1,10 @@
|
||||
import { Server } from 'socket.io'
|
||||
import { TSocket } from '../../../utilities/types'
|
||||
import ZoneRepository from '../../../repositories/zoneRepository'
|
||||
import { Zone } from '@prisma/client'
|
||||
import { Character, Zone } from '@prisma/client'
|
||||
import CharacterManager from '../../../managers/characterManager'
|
||||
import characterRepository from '../../../repositories/characterRepository'
|
||||
import logger from '../../../utilities/logger'
|
||||
|
||||
interface IPayload {
|
||||
zoneId: number
|
||||
@ -15,15 +17,18 @@ interface IPayload {
|
||||
*/
|
||||
export default function (socket: TSocket, io: Server) {
|
||||
socket.on('gm:zone_editor:zone:request', async (data: IPayload, callback: (response: Zone) => void) => {
|
||||
const character = CharacterManager.getCharacterFromSocket(socket);
|
||||
if (character?.role !== 'gm') {
|
||||
const character = await characterRepository.getById(socket.characterId as number);
|
||||
if (!character) return
|
||||
|
||||
if (character.role !== 'gm') {
|
||||
logger.info(`User ${character!.id} tried to request zone but is not a game master.`)
|
||||
return
|
||||
}
|
||||
|
||||
console.log(`---GM ${character?.id} has requested zone via zone editor.`)
|
||||
logger.info(`User ${character.id} has requested zone via zone editor.`)
|
||||
|
||||
if (!data.zoneId) {
|
||||
console.log(`---Zone id not provided.`)
|
||||
logger.info(`User ${character.id} tried to request zone but did not provide a zone id.`)
|
||||
return
|
||||
}
|
||||
|
||||
@ -31,7 +36,7 @@ export default function (socket: TSocket, io: Server) {
|
||||
const zone = await ZoneRepository.getById(data.zoneId)
|
||||
|
||||
if (!zone) {
|
||||
console.log(`---Zone not found.`)
|
||||
logger.info(`User ${character.id} tried to request zone ${data.zoneId} but it does not exist.`)
|
||||
return
|
||||
}
|
||||
|
||||
|
@ -6,6 +6,7 @@ import prisma from '../../../utilities/prisma'
|
||||
import zoneManager from '../../../managers/zoneManager'
|
||||
import logger from '../../../utilities/logger'
|
||||
import CharacterManager from '../../../managers/characterManager'
|
||||
import characterRepository from '../../../repositories/characterRepository'
|
||||
|
||||
interface IPayload {
|
||||
zoneId: number
|
||||
@ -34,16 +35,18 @@ interface IPayload {
|
||||
*/
|
||||
export default function (socket: TSocket, io: Server) {
|
||||
socket.on('gm:zone_editor:zone:update', async (data: IPayload, callback: (response: Zone) => void) => {
|
||||
const character = CharacterManager.getCharacterFromSocket(socket);
|
||||
if (character?.role !== 'gm') {
|
||||
logger.info(`User ${character?.id} tried to update zone but is not a game master.`)
|
||||
const character = await characterRepository.getById(socket.characterId as number);
|
||||
if (!character) return
|
||||
|
||||
if (character.role !== 'gm') {
|
||||
logger.info(`User ${character.id} tried to update zone but is not a game master.`)
|
||||
return
|
||||
}
|
||||
|
||||
logger.info(`User ${character?.id} has updated zone via zone editor.`)
|
||||
logger.info(`User ${character.id} has updated zone via zone editor.`)
|
||||
|
||||
if (!data.zoneId) {
|
||||
logger.info(`User ${character?.id} tried to update zone but did not provide a zone id.`)
|
||||
logger.info(`User ${character.id} tried to update zone but did not provide a zone id.`)
|
||||
return
|
||||
}
|
||||
|
||||
@ -51,7 +54,7 @@ export default function (socket: TSocket, io: Server) {
|
||||
let zone = await ZoneRepository.getById(data.zoneId)
|
||||
|
||||
if (!zone) {
|
||||
logger.info(`User ${character?.id} tried to update zone ${data.zoneId} but it does not exist.`)
|
||||
logger.info(`User ${character.id} tried to update zone ${data.zoneId} but it does not exist.`)
|
||||
return
|
||||
}
|
||||
|
||||
@ -105,7 +108,7 @@ export default function (socket: TSocket, io: Server) {
|
||||
zone = await ZoneRepository.getById(data.zoneId)
|
||||
|
||||
if (!zone) {
|
||||
logger.info(`User ${character?.id} tried to update zone ${data.zoneId} but it does not exist.`)
|
||||
logger.info(`User ${character.id} tried to update zone ${data.zoneId} but it does not exist.`)
|
||||
return
|
||||
}
|
||||
|
||||
|
@ -3,7 +3,7 @@ import { TSocket } from '../utilities/types'
|
||||
|
||||
export default function (socket: TSocket, io: Server) {
|
||||
socket.on('login', () => {
|
||||
// return user data
|
||||
if (!socket.user) return
|
||||
socket.emit('logged_in', { user: socket.user })
|
||||
})
|
||||
}
|
||||
|
@ -26,10 +26,6 @@ class CharacterManager {
|
||||
this.characters = this.characters.filter((x) => x.id !== character.id)
|
||||
}
|
||||
|
||||
public getCharacter(characterId: number) {
|
||||
return this.characters.find((x) => x.id === characterId)
|
||||
}
|
||||
|
||||
public getCharacterFromSocket(socket: TSocket) {
|
||||
return this.characters.find((x) => x.id === socket?.characterId)
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user