Created
November 16, 2015 12:32
-
-
Save ruippeixotog/e2cc02fed47b7a5caa22 to your computer and use it in GitHub Desktop.
botwars - JavaScript client
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
{ | |
"name": "botarena-bot-js", | |
"version": "1.0.0", | |
"description": "", | |
"main": "app.js", | |
"scripts": { | |
"test": "echo \"Error: no test specified\" && exit 1" | |
}, | |
"author": "Rui Gonçalves", | |
"dependencies": { | |
"request": "^2.64.0", | |
"ws": "^0.8.0" | |
} | |
} |
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
var request = require("request"); | |
var WebSocket = require('ws'); | |
module.exports = function(host, gameHref, player, getMove) { | |
var playerName = 'Player ' + player; | |
var gamesUri = 'http://' + host + '/api/' + gameHref + '/games'; | |
var registerUri = function(gameId) { | |
return 'http://' + host + '/api/' + gameHref + '/games/' + gameId + '/register?player=' + player; | |
}; | |
var streamUri = function(gameId, playerToken) { | |
return 'ws://' + host + '/api/' + gameHref + '/games/' + gameId + '/stream?playerToken=' + playerToken; | |
}; | |
function enterGame(gameId, next) { | |
request.post(registerUri(gameId), function(err, res, body) { | |
if(err) { console.log('Could not register in the game'); return; } | |
var playerToken = JSON.parse(body).playerToken; | |
var ws = new WebSocket(streamUri(gameId, playerToken)); | |
ws.on('open', function() { | |
console.log('[' + playerName + '] Connected to game ' + gameId + ' (with token ' + playerToken + ')'); | |
}); | |
ws.on('message', function(data) { | |
var evt = JSON.parse(data); | |
switch(evt.eventType) { | |
case 'requestMove': | |
ws.send(JSON.stringify(getMove(evt.state))); | |
break; | |
default: | |
// 'move' and 'state' events can be handled here | |
break; | |
} | |
}); | |
ws.on('close', function() { | |
console.log('[' + playerName + '] Finished'); | |
next(); | |
}); | |
}); | |
} | |
function findGame() { | |
request.get(gamesUri, function(err, res, body) { | |
if(err) { console.log('Could not retrieve the list of current games'); return; } | |
var games = JSON.parse(body); | |
for(var i = 0; i < games.length; i++) { | |
if(games[i].status == 'not_started') { | |
enterGame(games[i].gameId, findGame); | |
return; | |
} | |
} | |
setTimeout(findGame, 2000); | |
}); | |
} | |
findGame(); | |
}; |
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
var play = require("./play"); | |
var host = 'localhost:3000'; | |
var gameType = 'sueca'; | |
function randElem(arr) { | |
return arr[Math.floor(Math.random() * arr.length)]; | |
} | |
var getDumbMove = function(state) { | |
var trickSuitCards = state.hand.filter(function(c) { return c.suit == state.trickSuit; }); | |
if(trickSuitCards.length > 0) { | |
return randElem(trickSuitCards); | |
} | |
return randElem(state.hand); | |
}; | |
play(host, gameType, 1, getDumbMove); | |
play(host, gameType, 2, getDumbMove); | |
play(host, gameType, 3, getDumbMove); | |
play(host, gameType, 4, getDumbMove); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment