add chat commands main
authorxangelo <git@xangelo.ca>
Tue, 7 Jun 2022 04:09:06 +0000 (00:09 -0400)
committerxangelo <git@xangelo.ca>
Tue, 7 Jun 2022 04:09:06 +0000 (00:09 -0400)
- /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)

src/api.ts

index 86b1b93b19676f86623d9728eb09c2c81c72eb80..e1930c11bda57d50a3c4882c3cc58a00185df4d9 100644 (file)
@@ -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<string, any> = {
   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();
+      }
     }
   }