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);
const cache: Record<string, any> = {
online_users: []
};
+
const msgBuffer: string[] = [];
createBullBoard({
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();
+ }
}
}