Created
September 10, 2015 01:10
-
-
Save chrislloyd/d194bb4cd3fefe0762ae to your computer and use it in GitHub Desktop.
This file contains 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
#!/bin/bash | |
i="0" | |
while [ $i -gt -1 ] | |
do | |
i=$[$i+1] | |
echo $i | |
( | |
node bot.js ws://localhost:5000 | |
) & | |
sleep 1 | |
done | |
wait |
This file contains 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
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