1
0
forked from noxious/server
This commit is contained in:
Dennis Postma 2024-08-20 01:50:05 +02:00
parent 706107ba4d
commit cb90e0cdf8
4 changed files with 56 additions and 19 deletions

6
package-lock.json generated
View File

@ -2335,9 +2335,9 @@
"license": "MIT" "license": "MIT"
}, },
"node_modules/undici-types": { "node_modules/undici-types": {
"version": "6.19.6", "version": "6.19.8",
"resolved": "https://registry.npmjs.org/undici-types/-/undici-types-6.19.6.tgz", "resolved": "https://registry.npmjs.org/undici-types/-/undici-types-6.19.8.tgz",
"integrity": "sha512-e/vggGopEfTKSvj4ihnOLTsqhrKRN3LeO6qSN/GxohhuRv8qH9bNQ4B8W7e/vFL+0XTnmHPB4/kegunZGA4Org==", "integrity": "sha512-ve2KP6f/JnbPBFyobGHuerC9g1FYGn/F8n1LWTwNxCEzd6IfqTwUQcNXgEtmmQ6DlRrC1hrSrBnCZPokRrDHjw==",
"license": "MIT" "license": "MIT"
}, },
"node_modules/unpipe": { "node_modules/unpipe": {

View File

@ -2,6 +2,7 @@ import { Server } from 'socket.io'
import { TSocket } from '../../utilities/Types' import { TSocket } from '../../utilities/Types'
import CharacterRepository from '../../repositories/CharacterRepository' import CharacterRepository from '../../repositories/CharacterRepository'
import ZoneRepository from '../../repositories/ZoneRepository' import ZoneRepository from '../../repositories/ZoneRepository'
import { isCommand } from '../../utilities/Chat'
type TypePayload = { type TypePayload = {
message: string message: string
@ -9,28 +10,19 @@ type TypePayload = {
export default function (socket: TSocket, io: Server) { export default function (socket: TSocket, io: Server) {
socket.on('chat:send_message', async (data: TypePayload, callback: (response: boolean) => void) => { 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 { 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) const character = await CharacterRepository.getByUserAndId(socket.user?.id as number, socket.character?.id as number)
if (!character) { if (!character) return
console.log(`---Character not found.`)
return
}
const zone = await ZoneRepository.getById(character.zoneId) const zone = await ZoneRepository.getById(character.zoneId)
if (!zone) { if (!zone) return
console.log(`---Zone not found.`)
return
}
// send over zone and characters to socket
callback(true) callback(true)
io.to(zone.id.toString()).emit('chat:message', { io.to(zone.id.toString()).emit('chat:message', {
character: character, character: character,
message: data.message message: data.message

View File

@ -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}`)
}
})
}

12
src/utilities/Chat.ts Normal file
View File

@ -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(' ')
}