Created
August 8, 2016 00:22
-
-
Save CreaturePhil/35a2783a418204511f811f3fc3351393 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
'use strict'; | |
let db = require('machdb')('mongo url here'); | |
const faker = require('faker'); | |
const userUtils = require('./dev-tools/users-utils.js'); | |
const Connection = userUtils.Connection; | |
const User = userUtils.User; | |
const userTypes = [1, 2, 4]; | |
const minute = 1000 * 60; | |
const hour = minute * 60; | |
function randRange(min, max) { | |
return Math.floor(Math.random() * (max - min + 1)) + min; | |
} | |
let Bots = { | |
bots: [], | |
isChatting: false | |
}; | |
function createBot() { | |
const connection = new Connection(faker.internet.ip()); | |
const user = new User(connection); | |
const name = faker.internet.userName(); | |
const userType = userTypes[randRange(0, userTypes.length)]; | |
user.handleRename(name, toId(name), false, userType); | |
user.tryJoinRoom('lobby', connection); | |
const offset = minute * randRange(1, 30); | |
setTimeout(() => { | |
let bots = Bots.bots; | |
const length = bots.length; | |
for (let i = 0; i < length; i++) { | |
if (bots[i].userid === user.userid) { | |
bots.splice(i, 1); | |
break; | |
} | |
} | |
user.disconnectAll(); | |
}, hour * randRange(1, 1 * 6) + offset); | |
return user; | |
} | |
function spawn(amount) { | |
for (let i = 0; i < amount; i++) { | |
Bots.bots.push(createBot()); | |
} | |
} | |
Bots.spawn = spawn; | |
function uniq(list) { | |
return Array.from(new Set(list)); | |
} | |
function getName(line) { | |
return line.split(':')[0]; | |
} | |
function chat(messages) { | |
//if (!messages) messages = db('messages').get(0); | |
//console.log(messages.length); | |
messages = messages | |
.filter(line => line !== '') | |
.filter(line => line.indexOf(':') >= 0); | |
//console.log(messages); | |
const names = uniq(messages | |
.map(getName) | |
.filter(name => name !== '' && name.length < 19)); | |
const bots = Bots.bots; | |
let namesToBots = {}; | |
if (bots.length < names.length) return false; | |
//console.log(messages, 'PAST'); | |
for (let i = 0; i < names.length; i++) { | |
let bot = (bots[randRange(0, bots.length)] || {}).userid; | |
if (!bot) return false; | |
while (namesToBots[bot]) { | |
bot = (bots[randRange(0, bots.length)] || {}).userid; | |
if (!bot) return false; | |
} | |
namesToBots[names[i]] = bot; | |
} | |
//console.log(namesToBots); | |
function botChat(message) { | |
if (!message) return; | |
const name = getName(message); | |
const botName = namesToBots[name]; | |
const bot = Users.get(botName) || { | |
chat: function() {}, | |
connections: [{ip: faker.internet.ip()}] | |
}; | |
const msg = message.split(':')[1]; | |
bot.chat(msg, Rooms.get('lobby'), bot.connections[0]); | |
} | |
return new Promise((resolve, reject) => { | |
Bots.isChatting = true; | |
botChat(messages[0]); | |
messages.shift(); | |
chatDelay(); | |
function chatDelay() { | |
setTimeout(() => { | |
botChat(messages[0]); | |
messages.shift(); | |
if (messages.length) { | |
Bots.isChatting = true; | |
chatDelay(); | |
} else { | |
Bots.isChatting = false; | |
resolve(); | |
} | |
}, randRange(1000, 10000)); | |
} | |
}); | |
} | |
Bots.chat = chat; | |
function startChatSession(numSess, specificChatNum) { | |
if (Bots.isChatting) return false; | |
if (!numSess) { | |
return chat(db('messages').get(specificChatNum)); | |
} | |
function delaySess() { | |
numSess--; | |
const randMsg = randRange(0, db('messages').get('count')); | |
const then = chat(db('messages').get(randMsg)).then; | |
if (then) { | |
return then(() => { | |
if (numSess) delaySess(); | |
}); | |
} else { | |
return false; | |
} | |
} | |
return delaySess(); | |
} | |
Bots.startChatSession = startChatSession; | |
module.exports = Bots; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment