examples/game/lib/client.js
function readData(raw){

            var dataObj = micro.toJSON(new Buffer(raw));
            var type = dataObj._type;

            delete dataObj._type;

            switch (type){
                case "Ping":
                    //Grab latency (if exists) and bounce back the exact same data packet
                    latency = dataObj.latency || latency;
                    socket.send(raw);
                    break;

                case "GameData" :
                    //Initialize the game data and update the each of the zone's player data according to latency
                    gameData.set(dataObj);
                    currentZone = gameData.currentZone.update(latency);
                    break;

                case "Zone":
                    if (!gameData.get("isRunning")) return;

                    //Use dummy Zone Model to first update the data values according to latency
                    dataObj = new Zone(dataObj).update(latency).toJSON();
                    //Set easing to true so we don't simply override each existing player's x,y coordinates
                    currentZone.set(dataObj, {easing:true, silent:true});
                    break;

                case "Player":
                    if (!gameData.get("isRunning")) return;

                    //Use dummy Player Model to update the data values according to latency
                    dataObj = new Player(dataObj).update(latency).toJSON();

                    //Set easing to true so we don't simply override this player's x,y coordinates (if they exist)
                    currentZone.players.set(dataObj, {easing:true, remove:false, silent:true});
                    break;

                case "PlayerUpdate":
                    if (!gameData.get("isRunning")) return;

                    //A player update contains minimal player information, so we don't need to worry about easing
                    currentZone.players.set(dataObj, {add:false, remove:false, silent:true});
                    break;

                default:
                    console.warn("Unknown schema type received: "+type);
                    break;
            }
        }
function readData(raw){

            var dataObj = micro.toJSON(new Buffer(raw));
            var type = dataObj._type;

            delete dataObj._type;

            switch (type){
                case "Ping":
                    //Grab latency (if exists) and bounce back the exact same data packet
                    latency = dataObj.latency || latency;
                    socket.send(raw);
                    break;

                case "GameData" :
                    //Initialize the game data and update the each of the zone's player data according to latency
                    gameData.set(dataObj);
                    currentZone = gameData.currentZone.update(latency);
                    break;

                case "Zone":
                    if (!gameData.get("isRunning")) return;

                    //Use dummy Zone Model to first update the data values according to latency
                    dataObj = new Zone(dataObj).update(latency).toJSON();
                    //Set easing to true so we don't simply override each existing player's x,y coordinates
                    currentZone.set(dataObj, {easing:true, silent:true});
                    break;

                case "Player":
                    if (!gameData.get("isRunning")) return;

                    //Use dummy Player Model to update the data values according to latency
                    dataObj = new Player(dataObj).update(latency).toJSON();

                    //Set easing to true so we don't simply override this player's x,y coordinates (if they exist)
                    currentZone.players.set(dataObj, {easing:true, remove:false, silent:true});
                    break;

                case "PlayerUpdate":
                    if (!gameData.get("isRunning")) return;

                    //A player update contains minimal player information, so we don't need to worry about easing
                    currentZone.players.set(dataObj, {add:false, remove:false, silent:true});
                    break;

                default:
                    console.warn("Unknown schema type received: "+type);
                    break;
            }
        }
function readData ( raw ) { var dataObj = micro . toJSON ( new Buffer ( raw ) ) ; var type = dataObj . _type ; delete dataObj . _type ; switch ( type ) { case "Ping" : //Grab latency (if exists) and bounce back the exact same data packet latency = dataObj . latency || latency ; socket . send ( raw ) ; break ; case "GameData" : //Initialize the game data and update the each of the zone's player data according to latency gameData . set ( dataObj ) ; currentZone = gameData . currentZone . update ( latency ) ; break ; case "Zone" : if ( ! gameData . get ( "isRunning" ) ) return ; //Use dummy Zone Model to first update the data values according to latency dataObj = new Zone ( dataObj ) . update ( latency ) . toJSON ( ) ; //Set easing to true so we don't simply override each existing player's x,y coordinates currentZone . set ( dataObj , { easing : true , silent : true } ) ; break ; case "Player" : if ( ! gameData . get ( "isRunning" ) ) return ; //Use dummy Player Model to update the data values according to latency dataObj = new Player ( dataObj ) . update ( latency ) . toJSON ( ) ; //Set easing to true so we don't simply override this player's x,y coordinates (if they exist) currentZone . players . set ( dataObj , { easing : true , remove : false , silent : true } ) ; break ; case "PlayerUpdate" : if ( ! gameData . get ( "isRunning" ) ) return ; //A player update contains minimal player information, so we don't need to worry about easing currentZone . players . set ( dataObj , { add : false , remove : false , silent : true } ) ; break ; default : console . warn ( "Unknown schema type received: " + type ) ; break ; } }
Handle data received from server
Handle data received from server