diff --git a/package-lock.json b/package-lock.json index 7f4a425..5ab691b 100644 --- a/package-lock.json +++ b/package-lock.json @@ -2335,9 +2335,9 @@ "license": "MIT" }, "node_modules/undici-types": { - "version": "6.19.6", - "resolved": "https://registry.npmjs.org/undici-types/-/undici-types-6.19.6.tgz", - "integrity": "sha512-e/vggGopEfTKSvj4ihnOLTsqhrKRN3LeO6qSN/GxohhuRv8qH9bNQ4B8W7e/vFL+0XTnmHPB4/kegunZGA4Org==", + "version": "6.19.8", + "resolved": "https://registry.npmjs.org/undici-types/-/undici-types-6.19.8.tgz", + "integrity": "sha512-ve2KP6f/JnbPBFyobGHuerC9g1FYGn/F8n1LWTwNxCEzd6IfqTwUQcNXgEtmmQ6DlRrC1hrSrBnCZPokRrDHjw==", "license": "MIT" }, "node_modules/unpipe": { diff --git a/src/events/chat/SendMessage.ts b/src/events/chat/SendMessage.ts index dc0bf4f..106777b 100644 --- a/src/events/chat/SendMessage.ts +++ b/src/events/chat/SendMessage.ts @@ -2,6 +2,7 @@ import { Server } from 'socket.io' import { TSocket } from '../../utilities/Types' import CharacterRepository from '../../repositories/CharacterRepository' import ZoneRepository from '../../repositories/ZoneRepository' +import { isCommand } from '../../utilities/Chat' type TypePayload = { message: string @@ -9,28 +10,19 @@ type TypePayload = { export default function (socket: TSocket, io: Server) { socket.on('chat:send_message', async (data: TypePayload, callback: (response: boolean) => void) => { - console.log(`---User ${socket.character?.id} has sent a message via chat.`) - - if (!data.message) { - console.log(`---Message not provided.`) - return - } - try { + if (!data.message) return + if (isCommand(data.message)) return + const character = await CharacterRepository.getByUserAndId(socket.user?.id as number, socket.character?.id as number) - if (!character) { - console.log(`---Character not found.`) - return - } + if (!character) return const zone = await ZoneRepository.getById(character.zoneId) - if (!zone) { - console.log(`---Zone not found.`) - return - } + if (!zone) return + - // send over zone and characters to socket callback(true) + io.to(zone.id.toString()).emit('chat:message', { character: character, message: data.message diff --git a/src/events/chat/gm/AlertCommand.ts b/src/events/chat/gm/AlertCommand.ts new file mode 100644 index 0000000..70c648e --- /dev/null +++ b/src/events/chat/gm/AlertCommand.ts @@ -0,0 +1,33 @@ +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 (socket: TSocket, io: Server) { + 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.character?.id as number) + if (!character) return + + // When 1 arg is provided, only send message. 2 includes a title + if (args.length === 1) { + return io.emit('notification', { message: args[0] }) + } + + io.emit('notification', { title: args[0], message: args.slice(1).join(' ') }) + callback(true) + } catch (error: any) { + console.log(`---Error sending message: ${error.message}`) + } + }) +} diff --git a/src/utilities/Chat.ts b/src/utilities/Chat.ts new file mode 100644 index 0000000..07b0feb --- /dev/null +++ b/src/utilities/Chat.ts @@ -0,0 +1,12 @@ +export function isCommand(message: string, command?: string) { + if (command) { + return message.startsWith(`:${command} `) + } + return message.startsWith(':') +} + +export function getArgs(command: string, message: string): string[] | undefined +{ + if (!isCommand(message, command)) return + return message.split(`:${command} `)[1].split(' ') +} \ No newline at end of file