Skip to content

Commit

Permalink
Initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
Brandon Brown committed Feb 1, 2015
0 parents commit 34b4239
Show file tree
Hide file tree
Showing 9 changed files with 237 additions and 0 deletions.
Empty file added .env
Empty file.
35 changes: 35 additions & 0 deletions .gitignore
@@ -0,0 +1,35 @@
#### joe made this: https://goel.io/joe

#####=== Node ===#####

# Logs
logs
*.log

# Runtime data
pids
*.pid
*.seed

# Directory for instrumented libs generated by jscoverage/JSCover
lib-cov

# Coverage directory used by tools like istanbul
coverage

# Grunt intermediate storage (http://gruntjs.com/creating-plugins#storing-task-files)
.grunt

# node-waf configuration
.lock-wscript

# Compiled binary addons (http://nodejs.org/api/addons.html)
build/Release

# Dependency directory
# https://www.npmjs.org/doc/misc/npm-faq.html#should-i-check-my-node_modules-folder-into-git-
node_modules

# Debug log from npm
npm-debug.log

1 change: 1 addition & 0 deletions Procfile
@@ -0,0 +1 @@
web: node index.js
64 changes: 64 additions & 0 deletions README.md
@@ -0,0 +1,64 @@
# battlesnake-node(js)

A simple [BattleSnake AI](http://battlesnake.io) written in Javascript for NodeJS (though you could probably use [iojs](https://iojs.org/)).

To get started you'll need a working NodeJS development environment, and at least read the Heroku docs on [deploying a NodeJS app](https://devcenter.heroku.com/articles/getting-started-with-nodejs).

If you haven't setup a NodeJS development environment before, read [how to get started with NodeJS](http://nodejs.org/documentation/tutorials/). You'll also need [npm](https://www.npmjs.com/) for easy JS dependency management.

This client uses [Express4](http://expressjs.com/) for easy route management, read up on the docs to learn more about reading incoming JSON params, writing responses, etc.

[![Deploy](https://www.herokucdn.com/deploy/button.png)](https://heroku.com/deploy)


### Running the AI locally

Fork and clone this repo:

```
git clone git@github.com:sendwithus/battlesnake-node.git
cd battlesnake-node
```

Install dependencies:

```
npm install -g foreman # Node implementation of Ruby Foreman
npm install
```

Run the server:

```
nf start web
```

Test the client in your browser: [http://localhost:9001](http://localhost:9001)


### Deploying to Heroku

Click the Deploy to Heroku button at the top or use the command line commands below.

Create a new NodeJS Heroku app:

```
heroku create [APP_NAME]
```

Push code to Heroku servers:
```
git push heroku master
```

Open Heroku app in browser:
```
heroku open
```

Or go directly via http://APP_NAME.herokuapp.com

View/stream server logs:
```
heroku logs --tail
```
7 changes: 7 additions & 0 deletions app.json
@@ -0,0 +1,7 @@
{
"name": "",
"description": "",
"repository": "",
"logo": "http://node-js-sample.herokuapp.com/node.svg",
"keywords": []
}
20 changes: 20 additions & 0 deletions config.json
@@ -0,0 +1,20 @@
{
"host": "localhost",
"port": 9001,
"routes": {
"state": "/",
"start": "/start",
"move": "/move",
"end": "/end"
},
"snake" : {
"name": "battlesnake-node",
"color": "#fff000",
"head_url": "http://battlesnake-node.herokuapp.com/",
"taunt": {
"state": "It's great to be alive!",
"start": "Let's do this thang!",
"end": "I've been snaked!"
}
}
}
23 changes: 23 additions & 0 deletions index.js
@@ -0,0 +1,23 @@
var config = require('./config.json');
var bodyParser = require('body-parser');
var express = require('express');
var app = express();
var routes = require('./routes');

app.set('port', (config.port || process.env.PORT));

app.use(bodyParser.json());
app.use(routes);

// Middleware to short-circuit favicon requests
app.use(function (req, res, next) {
if (req.url === '/favicon.ico') {
res.set({'Content-Type': 'image/x-icon'});
res.status(200);
res.end();
}
});

var server = app.listen(app.get('port'), function () {
console.log('Server listening at http://%s:%s', config.host, app.get('port'));
});
23 changes: 23 additions & 0 deletions package.json
@@ -0,0 +1,23 @@
{
"name": "",
"description": "",
"version": "0.0.0",
"private": true,
"author": "",
"repository": "https://github.com/sendwithus/battlesnake-node",
"licenses": [
{
"type": "MIT",
"url": "http://opensource.org/licenses/MIT"
}
],
"dependencies": {
"body-parser": "^1.11.0",
"express": "*",
"nodemon": "*"
},
"devDependencies": {},
"engines": {
"node": "0.10.x"
}
}
64 changes: 64 additions & 0 deletions routes/index.js
@@ -0,0 +1,64 @@
var config = require('../config.json');
var express = require('express');
var router = express.Router();

// Get the state of the snake
router.get(config.routes.state, function (req, res) {
// Do something here to calculate the returned state

// Response data
var data = {
id: config.snake.id,
name: config.snake.name,
color: config.snake.color,
head_url: config.snake.head_url,
taunt: config.snake.taunt.state,
state: "alive",
coords: [[0, 0], [0, 1], [0, 2], [1, 2]],
score: 4
};

res.json(data);
});

// Start
router.post(config.routes.start, function (req, res) {
// Do something here to start the game
// Hint: do something with the incoming game_id? ;)
console.log('Game ID:', req.body.game_id);

// Response data
var data = {
name: config.snake.name,
color: config.snake.color,
head_url: config.snake.head_url,
taunt: config.snake.taunt.start
};

res.json(data);
});

// Move
router.post(config.routes.move, function (req, res) {
// Do something here to generate your move

// Response data
var data = {
move: '', // one of: ["up", "down", "left", "right"]
taunt: config.snake.color
};

res.json(data);
});

// End the session
router.post(config.routes.end, function (req, res) {
// Do something here to end yoru snake's session

// We don't need a response so just send back a 200
res.status(200);
res.end();
});


module.exports = router;

0 comments on commit 34b4239

Please sign in to comment.