//Place holder file for server operations

var util = require("util"),
    io = require("socket.io"),
    PlayerData = require("./playerdata").PlayerData;

var socket,
    players;

function init() {
    players = [];
    
    //Socket configuration
    socket = io.listen(8000);
    socket.configure(function() {
        socket.set("transports", ["websocket"]);
        socket.set("log level", 2);
    });
    
    setEventHandlers();
};

//Socket connection events
var setEventHandlers = function() {
    socket.sockets.on("connection", onSocketConnection);
};

function onSocketConnection(client) {
    util.log("New player has connected: "+client.id);
    client.on("disconnect", onClientDisconnect);
    client.on("new player", onNewPlayer);
    client.on("move player", onMovePlayer);
};

function onClientDisconnect() {
    util.log("Player has disconnected: "+this.id);
}

function onNewPlayer(data){
    //create player server data
    var newPlayer = new PlayerData(data.x, data.y);
    newPlayer.id = this.id;
    
    //send player to other clients
    this.broadcast.emit("new player", {id: newPlayer.id, x: newPlayer.getX(), y: newPlayer.getY()}); //emits player data to all clients
    
    //get existing players for the new client
    var i, existingPlayer;
    for (i = 0; i < players.length; i++) {
        existingPlayer = players[i];
        this.emit("new player", {id: existingPlayer.id, x: existingPlayer.getX(), y: existingPlayer.getY()});  //emits player data to this client
    }
    players.push(newPlayer);
};

function onMovePlayer(data){
    
};

init();
@@ -277,46 +277,8 @@
 0);%0A
-    socket.configure(function() %7B%0A    
     
@@ -318,20 +318,16 @@
 ket%22%5D);%0A
-    
     sock
@@ -354,16 +354,8 @@
 2);%0A
-    %7D);%0A
     
@@ -1528,8 +1528,9 @@
 %0Ainit();
+%0A

configure depreciated