From: xangelo Date: Tue, 7 Jun 2022 04:09:06 +0000 (-0400) Subject: add chat commands X-Git-Url: https://git.xangelo.ca/?a=commitdiff_plain;h=8c99b5c157505e719319ca2722a511571a04427c;p=browser-rts.git add chat commands - /online lists all online players - /give [username] [amount] [resource] will modify the CITY object incrementing [resource] by [amount] for [username]. (admin) - /assume [username] returns a link to login as the user (admin) --- diff --git a/src/api.ts b/src/api.ts index 86b1b93..e1930c1 100644 --- a/src/api.ts +++ b/src/api.ts @@ -20,6 +20,7 @@ import {renderMailroom, renderMessage} from './render/mail'; import {topbar} from './render/topbar'; import {renderPublicChatMessage} from './render/chat-message'; import validator from 'validator'; +import {Socket} from 'socket.io'; const server = new HttpServer(config.API_PORT); @@ -30,6 +31,7 @@ const mailRepo = new MailRepository(); const cache: Record = { online_users: [] }; + const msgBuffer: string[] = []; createBullBoard({ @@ -383,11 +385,66 @@ server.post<{body: {message: string}}, void>('/chat', async req => { const now = Date.now(); if(!_.isEmpty(req.body.message)) { - const msg = renderPublicChatMessage(acct.username, req.body.message); - server.ws.emit('/chat-message', msg); - msgBuffer.unshift(msg); - while(msgBuffer.length > 30) { - msgBuffer.pop(); + // is this a command message?! + console.log(req.body.message); + if(req.body.message[0] === '/') { + console.log('Received command message from:', acct.username); + // this is a command message, don't record it in chat + // history, and only send the response to the single person who sent + let socket = server.getSocketFromAuthenticatedUser(req.authInfo); + if(!socket) { + return; + } + if(req.body.message === '/online') { + socket.emit('/chat-message', renderPublicChatMessage( + 'Server', + `Online Users: ${cache.online_users.join(', ')}` + )); + } + else if(req.body.message.indexOf('/give') === 0 && acct.username === 'xangelo') { + // expects message in format /give [username] [amount] [resource] + const [command, username, amount, resource] = req.body.message.split(' '); + const user = await accountRepo.FindOne({username: username}); + const city = await cityRepo.getUsersCity(user.id); + + console.log('giving!', req.body.message.split(' ')); + + await cityRepo.save({ + id: city.id, + [resource]: city[resource] + parseInt(amount) + }); + + socket.emit('/chat-message', renderPublicChatMessage( + 'Server', + `Gave ${user.username} ${amount} ${resource} ${city[resource]}=>${city[resource] + parseInt(amount)}` + )); + } + else if(req.body.message.indexOf('/assume') === 0 && acct.username === 'xangelo') { + // expects /assume [username] + const [command, username] = req.body.message.split(' '); + const user = await accountRepo.FindOne({username: username}); + const session = await accountRepo.session.FindOne({account_id: user.id}); + + socket.emit('/chat-message', renderPublicChatMessage( + 'Server', + `Login Link: /game.html?token=${session.id}&id=${session.account_id}` + )); + } + else { + console.log(req.body.message.indexOf('/give'), acct.username); + socket.emit('/chat-message', renderPublicChatMessage( + 'Server', + `The command ${req.body.message} is not valid` + )); + } + } + else { + const msg = renderPublicChatMessage(acct.username, req.body.message); + server.ws.emit('/chat-message', msg); + msgBuffer.unshift(msg); + while(msgBuffer.length > 30) { + msgBuffer.pop(); + } } }