<commit_before>'use strict'

const swarm = require('discovery-swarm')
const brain = require('brain')
const Node = require('./lib/node')
const Unit = require('./lib/unit')
const BrainUnit = require('./lib/brain-unit')
const Executor = require('./lib/executor')
const trained = require('./trained')

const topic = 'magi'
const names = ['melchior', 'balthasar', 'casper']

function createSwarm() {
	const sw = swarm()
	sw.listen(0)
	sw.join(topic)
	return sw
}

function allConnected(nodes) {
	return Promise.all(nodes.map(node => {
		return new Promise((resolve, reject) => {
			node.on('connected', () => resolve())
		})
	}))
}

const units = []
names.forEach((name, i) => {
	const sw = createSwarm()

	const net = new brain.NeuralNetwork()
	net.fromJSON(trained[i])

	const unit = new BrainUnit(sw, net, 'black')
	units.push(unit)

	console.log('Created unit', name + '-' + (i+1), unit.id)
})

const exec = new Executor(createSwarm(), 'selfdestruct', units.map(unit => unit.id))
console.log('Created executor', exec.id)

const hud = new Node(createSwarm())
console.log('Created HUD', hud.id)

allConnected(units.concat(exec, hud)).then(() => {
	console.log('Network is fully connected')

	const question = {
		id: '0',
		module: 'selfdestruct',
		data: { r: 0.95, g: 0.02, b: 0.43 }
	}

	return new Promise((resolve, reject) => {
		exec.on('vote', (question, poll) => {
			console.log('Got vote:', question, poll)
		})

		exec.on('result', (question, ok) => {
			resolve({question, ok})
		})

		hud.publish('question', question)
		console.log('Started poll:', question)
	})
}).then(({question, ok}) => {
	console.log('Poll result:', question, ok)
	process.exit()
}).catch(err => {
	console.error(err)
	process.exit(1)
})<commit_msg>Allow to specify question with argv<commit_after>'use strict'

const swarm = require('discovery-swarm')
const brain = require('brain')
const Node = require('./lib/node')
const Unit = require('./lib/unit')
const BrainUnit = require('./lib/brain-unit')
const Executor = require('./lib/executor')
const trained = require('./trained')

const topic = 'magi'
const names = ['melchior', 'balthasar', 'casper']

function createSwarm() {
	const sw = swarm()
	sw.listen(0)
	sw.join(topic)
	return sw
}

function allConnected(nodes) {
	return Promise.all(nodes.map(node => {
		return new Promise((resolve, reject) => {
			node.on('connected', () => resolve())
		})
	}))
}

const units = []
names.forEach((name, i) => {
	const sw = createSwarm()

	const net = new brain.NeuralNetwork()
	net.fromJSON(trained[i])

	const unit = new BrainUnit(sw, net, 'black')
	units.push(unit)

	console.log('Created unit', name + '-' + (i+1), unit.id)
})

const exec = new Executor(createSwarm(), 'selfdestruct', units.map(unit => unit.id))
console.log('Created executor', exec.id)

const hud = new Node(createSwarm())
console.log('Created HUD', hud.id)

allConnected(units.concat(exec, hud)).then(() => {
	console.log('Network is fully connected')

	const question = {
		id: '0',
		module: 'selfdestruct',
		data: { r: 0.95, g: 0.02, b: 0.43 }
	}

	var questionJson = process.argv[2]
	if (questionJson) {
		question.data = JSON.parse(questionJson)
	}

	return new Promise((resolve, reject) => {
		exec.on('vote', (question, poll) => {
			console.log('Got vote:', question, poll)
		})

		exec.on('result', (question, ok) => {
			resolve({question, ok})
		})

		hud.publish('question', question)
		console.log('Started poll:', question)
	})
}).then(({question, ok}) => {
	console.log('Poll result:', question, ok)
	process.exit()
}).catch(err => {
	console.error(err)
	process.exit(1)
})
{'fraction_non_alphanumeric': 0.19415041782729806, 'fraction_numerical': 0.007520891364902507, 'mean_word_length': 4.474085365853658, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 25, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5020016', 'n_tokens_mistral': 1326, 'n_tokens_neox': 1243, 'n_words': 294}