forked from noxious/server
Mass replace parameter order (socket,io)>(io,socket), worked on queueing system
This commit is contained in:
28
src/socketEvents/chat/gameMaster/alertCommand.ts
Normal file
28
src/socketEvents/chat/gameMaster/alertCommand.ts
Normal file
@ -0,0 +1,28 @@
|
||||
import { Server } from 'socket.io'
|
||||
import { TSocket } from '../../../utilities/types'
|
||||
import { getArgs, isCommand } from '../../../utilities/chat'
|
||||
import CharacterRepository from '../../../repositories/characterRepository'
|
||||
|
||||
type TypePayload = {
|
||||
message: string
|
||||
}
|
||||
|
||||
export default function (io: Server, socket: TSocket) {
|
||||
socket.on('chat:send_message', async (data: TypePayload, callback: (response: boolean) => void) => {
|
||||
try {
|
||||
if (!isCommand(data.message, 'alert')) return
|
||||
|
||||
const args = getArgs('alert', data.message)
|
||||
|
||||
if (!args) return
|
||||
|
||||
const character = await CharacterRepository.getByUserAndId(socket.user?.id as number, socket.characterId as number)
|
||||
if (!character) return
|
||||
|
||||
io.emit('notification', { title: 'Message from GM', message: args.join(' ') })
|
||||
callback(true)
|
||||
} catch (error: any) {
|
||||
console.log(`---Error sending message: ${error.message}`)
|
||||
}
|
||||
})
|
||||
}
|
84
src/socketEvents/chat/gameMaster/teleportCommand.ts
Normal file
84
src/socketEvents/chat/gameMaster/teleportCommand.ts
Normal file
@ -0,0 +1,84 @@
|
||||
import { Server } from 'socket.io'
|
||||
import { TSocket } from '../../../utilities/types'
|
||||
import { getArgs, isCommand } from '../../../utilities/chat'
|
||||
import ZoneRepository from '../../../repositories/zoneRepository'
|
||||
import CharacterManager from '../../../managers/characterManager'
|
||||
import { gameMasterLogger } from '../../../utilities/logger'
|
||||
|
||||
type TypePayload = {
|
||||
message: string
|
||||
}
|
||||
|
||||
export default class TeleportCommandEvent {
|
||||
constructor(
|
||||
private readonly io: Server,
|
||||
private readonly socket: TSocket
|
||||
) {}
|
||||
|
||||
public listen(): void {
|
||||
this.socket.on('chat:send_message', this.handleTeleportCommand.bind(this))
|
||||
}
|
||||
|
||||
private async handleTeleportCommand(data: TypePayload, callback: (response: boolean) => void): Promise<void> {
|
||||
try {
|
||||
const character = CharacterManager.getCharacterFromSocket(this.socket)
|
||||
if (!character) {
|
||||
this.socket.emit('notification', { title: 'Server message', message: 'Character not found' })
|
||||
return
|
||||
}
|
||||
|
||||
if (!isCommand(data.message, 'teleport')) return
|
||||
|
||||
const args = getArgs('teleport', data.message)
|
||||
|
||||
if (!args || args.length !== 1) {
|
||||
this.socket.emit('notification', { title: 'Server message', message: 'Usage: /teleport <zoneId>' })
|
||||
return
|
||||
}
|
||||
|
||||
const zoneId = parseInt(args[0], 10)
|
||||
if (isNaN(zoneId)) {
|
||||
this.socket.emit('notification', { title: 'Server message', message: 'Invalid zone ID' })
|
||||
return
|
||||
}
|
||||
|
||||
const zone = await ZoneRepository.getById(zoneId)
|
||||
if (!zone) {
|
||||
this.socket.emit('notification', { title: 'Server message', message: 'Zone not found' })
|
||||
return
|
||||
}
|
||||
|
||||
if (character.zoneId === zone.id) {
|
||||
this.socket.emit('notification', { title: 'Server message', message: 'You are already in that zone' })
|
||||
return
|
||||
}
|
||||
|
||||
// Remove character from current zone
|
||||
this.io.to(character.zoneId.toString()).emit('zone:character:leave', character.id)
|
||||
this.socket.leave(character.zoneId.toString())
|
||||
|
||||
// Add character to new zone
|
||||
this.io.to(zone.id.toString()).emit('zone:character:join', character)
|
||||
this.socket.join(zone.id.toString())
|
||||
|
||||
character.zoneId = zone.id
|
||||
character.positionX = 0
|
||||
character.positionY = 0
|
||||
|
||||
character.resetMovement = true
|
||||
|
||||
this.socket.emit('zone:character:teleport', {
|
||||
zone,
|
||||
characters: CharacterManager.getCharactersInZone(zone)
|
||||
})
|
||||
|
||||
this.socket.emit('notification', { title: 'Server message', message: `You have been teleported to ${zone.name}` })
|
||||
gameMasterLogger.info('teleport', `Character ${character.id} teleported to zone ${zone.id}`)
|
||||
|
||||
callback(true)
|
||||
} catch (error: any) {
|
||||
gameMasterLogger.error(`Error in teleport command: ${error.message}`)
|
||||
this.socket.emit('notification', { title: 'Server message', message: 'An error occurred while teleporting' })
|
||||
}
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user