Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
  • Loading branch information
RowanHarley authored Jun 15, 2016
1 parent a1c62cb commit c330418
Show file tree
Hide file tree
Showing 7 changed files with 117 additions and 50 deletions.
13 changes: 0 additions & 13 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,16 +5,3 @@
# Aim

The aim of this project is to make a server for Slither using Node.JS.

#Roadmap

For our roadmap, visit our [wiki page](https://github.com/RowanHarley/Slither-Server/wiki/Roadmap).
Some of the features we hope to add are:

- [ ] Bots
- [ ] Commands through Console
- [ ] Multiple Server Support (Meaning you can have players play on different, smaller servers)
- [ ] Ability to set (and save) highscores (You currently have to set them in the code)
- [ ] ...

If you have any suggestions, please feel free to comment them [here](https://github.com/RowanHarley/Slither-Server/issues/1)
4 changes: 3 additions & 1 deletion config/config.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@ module.exports = {
port: 8080,
bots: 0, // Not Implemented
maxConnections: 100,
startFood: 20000,
food: 20000,
foodColours: 8,
foodSize: [15, 47],
foodPerSpawn: 1000, // Not Implemented
gameRadius: 21600,
sectorSize: 480
Expand Down
117 changes: 88 additions & 29 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,56 +1,82 @@
var config = require("./config/config.js");
//var spawn = require("./src/spawn.js");
var WebSocketServer = require("ws").Server;
var pkg = require("./package.json");
var WebSocket = require("ws").Server;
var snake = require("./src/entities/snake");
var food = require("./src/entities/food");
var sector = require("./src/entities/sector");
var messages = require("./src/messages");
var message = require("./src/utils/message");
var math = require("./src/utils/math");


var counter = 0;
var clients = [];
var foods = [];
var sectors = []; // Development Code
var botCount = 1;



console.log("[DEBUG] You are currently running on " + pkg.version);
console.log("[SERVER] Starting Server...");
var server;
server = new WebSocketServer({port: config["port"], path: "/slither"});

console.log("[SERVER] Server Started at 127.0.0.1:" + config["port"] + "! Waiting for Connections...");
server.on('error', function() {
server = new WebSocket({port: config["port"], path: "/slither"}, function(){
console.log("[SERVER] Server Started at 127.0.0.1:" + config["port"] + "! Waiting for Connections...");
console.log("[BOTS] Bot Status:");
console.log("[BOTS] Creating " + config["bots"] + " bots!");
console.log("[BOTS] Bots successfully loaded: " + botCount + (botCount == 0 ? "\n[BOTS] Reason: Bot's aren't implemented yet. Please try again later": ""));
generateFood(config["food"]);
generateSectors();
});
/* server.on('error', function() {
console.log('[DEBUG] Error while connecting!');
});
server.on("connection", handleConnection.bind(this));
console.log("[SERVER] Server Started at 127.0.0.1:" + config["port"] + "! Waiting for Connections...");
*/
if(server.readyState === server.OPEN){
server.on("connection", handleConnection.bind(server));
}else{
console.log(server.readyState);
}
function handleConnection(conn) {
if(config['isDebug']){
console.log("[DEBUG] handleConnection() has begun");
}
if (clients.length >= config['max-connections']) {
console.log("[SERVER] Too many connections. Closing newest connections!");
conn.close();
return;
}
conn.id = ++counter;
clients[conn.id] = conn;
try {
conn.id = ++counter;
clients[conn.id] = conn;
}catch(e){
console.log("[ERROR] " + e);
}

function close(id) {
console.log("[ERROR] Error!");
console.log("[DEBUG] Connection closed.");
conn.send = function() {};
//clearInterval(conn.snake.update);
delete clients[id];
}
conn.on('message', handleMessage.bind(this, conn));
conn.on('error', close.bind(conn.id));
//conn.on('error', close.bind(conn.id));
conn.on('error', function(e){
console.log(e);
close(conn.id);
delete clients[conn.id];
});

conn.on('close', close.bind(conn.id));
send(conn.id, messages.initial);
};
}
function handleMessage(conn, data) {
var firstByte, name, radians, secondByte, skin, speed, value, x, y;
if (data.length === 0) {
console.log("[SERVER] No Data to handle!");
return;
}
if (data.length >= 227) {
console.log("[SERVER] Data length less than 227!");
conn.close();
} else if (data.length === 1) {
value = message.readInt8(0, data);
Expand All @@ -74,6 +100,7 @@ function handleMessage(conn, data) {
send(conn.id, messages.pong);
}
} else {

firstByte = message.readInt8(0, data);
secondByte = message.readInt8(1, data);
if (firstByte === 115) {
Expand All @@ -84,35 +111,64 @@ function handleMessage(conn, data) {
y: 21137.4 * 5
}, skin);
broadcast(messages.snake.build(conn.snake));
var move = messages.movement.build(conn.id, conn.snake.direction.x, conn.snake.direction.y)
var dir = messages.direction.build(conn.id, conn.snake.direction);
console.log("[DEBUG] A new snake called " + conn.snake.name + " was connected!");
spawnSnakes(conn.id);
conn.snake.update = setInterval((function() {
conn.snake.body.x += Math.round(Math.cos(conn.snake.direction.angle * 1.44 * Math.PI / 180) * 170);
conn.snake.body.y += Math.round(Math.sin(conn.snake.direction.angle * 1.44 * Math.PI / 180) * 170);
broadcast(messages.direction.build(conn.snake.id, conn.snake.direction));
broadcast(messages.movement.build(conn.snake.id, conn.snake.direction.x, conn.snake.direction.y));
broadcast(dir);
broadcast(move);
}), 230);
} else {
console.log("[ERROR] Unhandled message " + (String.fromCharCode(firstByte)));
}
send(conn.id, messages.leaderboard.build([conn], 1, [conn]));
send(conn.id, messages.highscore.build("Rowan", "Test Message"));
send(conn.id, messages.minimap.build(this.foods));
send(conn.id, messages.minimap.build(foods));
}
}

function generateFood(amount) {
var color, i, id, results, size, x, y;
i = 0;
results = [];
while (i < amount) {
x = math.randomInt(0, 65535);
y = math.randomInt(0, 65535);
id = x * config['game-radius'] * 3 + y;
color = math.randomInt(0, config['foodColors']);
size = math.randomInt(config['foodSize'][0], config['foodSize'][1]);
foods.push(new food(id, {
x: x,
y: y
}, size, color));
results.push(i++);
}
return results;
}
function generateSectors() {
var i, results, sectorsAmount;
sectorsAmount = config['gameRadius'] / config['sectorSize'];
i = 0;
results = [];
while (i < sectorsAmount) {
results.push(i++);
}
return results;
}
function spawnSnakes(id){
clients.forEach((function(_this){
return function(client){
if(client.id !== id){
send(id, messages.snake.build(client.snake));
}
};
})(this));
clients.forEach(function(newClient){
if(newClient.id !== id){
send(id, messages.snake.build(newClient.snake));
}
});
}

function send(id,data){
clients[id].send(data, {binary:true});
if(clients[id]){
clients[id].send(data, {binary:true});
}
}

function broadcast(data){
Expand All @@ -123,8 +179,8 @@ function broadcast(data){
}
} */
for(var i = 0; i < clients.length; i++){
if(clients[i] != null || clients[i] != undefined){
clients[i].send(data, {binary: true});
if(clients[i]){
clients[i].send(data, {binary:true});
}
}
}
Expand All @@ -139,4 +195,7 @@ function broadcast(data){
}) : void 0);
}
return results;
}; */
}; */
function close(){
server.close();
}
8 changes: 3 additions & 5 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "Slither-Server",
"description": "A server for slither.io",
"version": "0.0.2-b",
"version": "0.0.2-b2",
"repository": {
"type": "git",
"url": "https://github.com/RowanHarley/Slither-Server/"
Expand All @@ -10,13 +10,11 @@
"chalk": "^1.1.3",
"object-keys": "^1.0.9",
"semver": "^5.1.0",
"ws": "^0.8.1"
"ws": "*"
},
"packageDependencies": {},
"scripts": {
"start": "node ./index.js",
"start:prod": "npm run compile && node ./bin/slither --prod",
"compile": "coffee --output lib --compile src"
"start": "node ./index.js"
},
"private": true
}
2 changes: 1 addition & 1 deletion src/entities/snake.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ module.exports = Snake = (function() {
this.speed = 5.79 * 1e3;
this.head = this.body;
this.D = 5.69941607541398 / 2 / Math.PI * 16777215;
this.X = this.D;
this.X = D;
this.length = 10;
this.J = 306;
this.I = 0.7810754645511785 * 16777215;
Expand Down
21 changes: 21 additions & 0 deletions src/messages/food.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
var message = require('../utils/message');

var type = 'F'.charCodeAt(0);

exports.build = function(foods) {
var arr = new Uint8Array(3 + (6 * foods.length));
var b = 0;
b += message.writeInt8(b, arr, 0);
b += message.writeInt8(b, arr, 0);
b += message.writeInt8(b, arr, type);
var i = 0;
while (i < foods.length) {
var food = foods[i];
b += message.writeInt8(b, arr, food.color);
b += message.writeInt16(b, arr, food.position.x);
b += message.writeInt16(b, arr, food.position.y);
b += message.writeInt8(b, arr, food.size);
i++;
}
return arr;
};
2 changes: 1 addition & 1 deletion src/messages/leaderboard.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ exports.build = function(rank, players, top) {
length += player.snake.name.length;
}
arr = new Uint8Array((8 + length) + (top.length * 7));
b = 0;
var b = 0;
b += message.writeInt8(b, arr, 0);
b += message.writeInt8(b, arr, 0);
b += message.writeInt8(b, arr, type);
Expand Down

0 comments on commit c330418

Please sign in to comment.