Skip to content

Instantly share code, notes, and snippets.

@chrislloyd
Created September 10, 2015 01:10
Show Gist options
  • Save chrislloyd/d194bb4cd3fefe0762ae to your computer and use it in GitHub Desktop.
Save chrislloyd/d194bb4cd3fefe0762ae to your computer and use it in GitHub Desktop.
#!/bin/bash
i="0"
while [ $i -gt -1 ]
do
i=$[$i+1]
echo $i
(
node bot.js ws://localhost:5000
) &
sleep 1
done
wait
import WebSocket from 'ws'
function rand(lim) {
return Math.floor(Math.random() * lim)
}
function sample(arr) {
return arr[rand(arr.length)]
}
function think(state) {
const dirs = ['up', 'down', 'left', 'right']
return ["MOVE", sample(dirs)]
}
console.log('Starting')
const ws = new WebSocket(process.argv[2])
ws.on('open', function() {
console.log('Connected')
})
ws.on('end', function() {
console.log('Disconnected')
})
ws.on('message', function(data) {
const msg = JSON.parse(data)
const state = msg.state
const cmd = think(state)
ws.send(JSON.stringify(cmd))
})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment