123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135 |
- #!/usr/bin/env node
- var fs = require('fs'),
- http = require('http'),
- websocket = require('websocket');
- config = require('./config');
- game = require('./game');
- try {
- game.init();
- } catch (error) {
- console.error("Game cannot be loaded.", error);
- process.exit(1);
- }
- var webserver = http.createServer(function(request, response) {});
- webserver.on('error', function(error) {
- console.error("Unknown webserver error", error);
- });
- // create the server
- var websocketserver = new websocket.server({
- httpServer: webserver
- });
- var socketConnections = [];
- // WebSocket server
- websocketserver.on('request', function(request) {
- var connection = request.accept(null, request.origin);
- var key = request.httpRequest.headers["sec-websocket-key"];
- game.addConnection(key, connection);
- connection.on('message', function(message) {
- var key = request.httpRequest.headers["sec-websocket-key"];
- if (message.type === 'utf8') {
- try {
- var receivedData = JSON.parse(message.utf8Data);
- switch(receivedData.action) {
- case "join":
- var newPlayer = game.addPlayer(receivedData.data, key);
- this.send(JSON.stringify({action: "connect", playerid: newPlayer.id, update: game.getUpdate()}));
- break;
- case "move":
- game.movePlayer(receivedData.playerid, receivedData.data);
- break;
- case "tick":
- this.send(JSON.stringify({action: "tickresponse", data: game.getUpdate()}));
- break;
- case "sync":
- game.updatePlayer(receivedData.playerid, receivedData.data);
- break;
- case "chat":
- game.handleChat(receivedData);
- break;
- default:
- console.warn("Unhandled event:", receivedData.action, receivedData.data);
- game.sendToAll(JSON.stringify(receivedData));
- break;
- }
-
- } catch (error) {
- console.error("Text Error", error);
- }
- } else {
- //receiving data from socket
- }
- });
- connection.on('close', function(connection) {
- //TODO: close connection on server
- var key = request.httpRequest.headers["sec-websocket-key"];
- game.removePlayer(key);
- //game.takeOverPlayer(key);
- game.removeConnection(key);
- });
- });
- var buffersEqual = function(buffer1, buffer2) {
- return 0 == Buffer.compare(buffer1, buffer2);
- }
- webserver.listen(config.websocketport, config.websocketlisten, function() {
- console.log("Websocket server listening on " + config.websocketlisten + ":" + config.websocketport);
- });
- process.stdin.resume();
- process.stdin.setEncoding('utf8');
- process.stdin.on('data', function (data) {
- var input = data.trim().split(" ");
- switch(input.shift()) {
- case "exit":
- case "quit":
- case "stop":
- //TODO: shutdown server
- process.exit(0);
- break;
- case "mobiles":
- var mobileData = game.getUpdate();
- console.log(prettyPrintObject(mobileData));
- break;
- case "restart":
- game.shutdown();
- game.init();
- break;
- }
-
- });
- var prettyPrintObject = function(displayObject, tabs) {
- var tabChar = " ";
- if(tabs == null) {
- tabs = "";
- }
- if(Array.isArray(displayObject)) {
- var displayString = "[";
- for(var index in displayObject) {
- var subObj = displayObject[index];
- displayString += "\n" + tabs + tabChar + index + ": " + prettyPrintObject(subObj, tabs + tabChar);
- }
- return displayString + "\n" + tabs + "]";
- }
- if(typeof displayObject == 'object') {
- var displayString = "{";
- for(var index in displayObject) {
- var subObj = displayObject[index];
- displayString += "\n" + tabs + tabChar + index + ": " + prettyPrintObject(subObj, tabs + tabChar);
- }
- return displayString + "\n" + tabs + "}";
- }
-
- return "'" + displayObject + "'";
- }
|