const Client = require('./index'); const fetch = require('node-fetch'); const LISTEN_ON = '5fcbf598b39160080e797ad6'; const PREFIX = '::'; const ADMIN_ID = '5fc828ea4e96e00725c17fd7'; const joined = []; const selected = []; // https://stackoverflow.com/questions/17619741/randomly-generating-unique-number-in-pairs-multiple-times const randomPairs = (players) => { const pairs = []; while(players.length) { pairs.push([ pluckRandomElement(players), pluckRandomElement(players) ]); } return pairs; }; const pluckRandomElement = (array) => { const i = randomInt(array.length); return array.splice(i, 1)[0]; }; const randomInt = (limit) => { return Math.floor(Math.random() * limit); }; const getRandomUser = (self, count=0) => { if (count > 3) return joined[0]; count++; let final; let chosen = joined[Math.floor(Math.random() * joined.length)]; final = chosen; if (chosen._id === self._id) return final = getRandomUser(self, count); if ((selected.indexOf(chosen)) !== 1) return final = getRandomUser(self, count); if (!final) return final = getRandomUser(self, count); return final || null; }; const main = async () => { const client = new Client('https://b.hippoz.xyz', { throwErrors: true }); const res = await fetch(`${client.url}/api/v1/users/token/create`, { method: 'POST', headers: { 'Accept': 'application/json', 'Content-Type': 'application/json' }, body: JSON.stringify({ username: 'D', password: 'D' }) }); const json = await res.json(); if (!res.ok || json.error) throw new Error('Failed to generate token from API endpoint'); await client.setToken(json.token); await client.gatewayConnect(); client.gateway.on('connect', () => { const category = client.gateway.subscribeToCategoryChat(LISTEN_ON); client.gateway.on('message', (e) => { if (e.author._id === client.user._id) return; if (!e.content.startsWith(PREFIX)) return; if (e.category._id !== category) return; const cmdString = e.content.substring(PREFIX.length); const cmdFull = cmdString.split(' '); const cmd = cmdFull[0] || 'NO_CMD'; console.log(cmdFull); switch (cmd) { case 'join': { const existing = joined.findIndex((o) => { return o._id === e.author._id; }); if (existing !== -1) { client.gateway.sendMessage(category, 'Already joined', { nickAuthor: { username: 'Error' }, destUser: { _id: e.author._id } }); break; } joined.push(e.author); console.log(`[*] User joined`, e.author); client.gateway.sendMessage(category, `${e.author.username} joined!`, { nickAuthor: { username: 'New join!' } }); break; } case 'roll': { if (e.author._id !== ADMIN_ID) { client.gateway.sendMessage(category, 'Access denied', { nickAuthor: { username: 'Error' }, destUser: { _id: e.author._id } }); break; } client.gateway.sendMessage(category, 'Rolling...', { nickAuthor: { username: 'Woo' } }); const pairs = randomPairs(joined); for (const pair of pairs) { const p1 = pair[0]; const p2 = pair[1]; if (!p1 || !p2) continue; for (const player of pair) { client.gateway.sendMessage(category, `${p1.username} with ${p2.username}`, { nickAuthor: { username: 'Your result' }, destUser: { _id: player._id } }); } } break; } } }); }); }; main();