package electrosphere.server;

import electrosphere.engine.Globals;
import electrosphere.logger.LoggerInterface;

/**
 * Functions that should be fired every server frame
 */
public class MainServerFunctions {
    
    /**
     * Calls the main server routines that should fire each frame
     */
    public static void simulate(){

        //
        //Cleanup disconnected clients
        if(Globals.server != null){
            Globals.server.cleanupDeadConnections();
        }

        //
        //Synchronous player message parsing\
        Globals.profiler.beginCpuSample("Server synchronous packet parsing");
        if(Globals.server != null){
            Globals.server.synchronousPacketHandling();
        }
        if(Globals.serverSynchronizationManager != null){
            Globals.serverSynchronizationManager.processMessages();
        }
        Globals.profiler.endCpuSample();

        //
        //Micro simulation (ie simulating each scene on the server)
        Globals.profiler.beginCpuSample("Server micro simulation");
        LoggerInterface.loggerEngine.DEBUG_LOOP("Begin server micro simulation");
        if(Globals.realmManager != null){
            Globals.realmManager.simulate();
        }
        
        //
        //Macro simulation (ie simulating the larger world macro data)
        LoggerInterface.loggerEngine.DEBUG_LOOP("Server macro simulation");
        if(Globals.macroSimulation != null && Globals.macroSimulation.isReady()){
            Globals.macroSimulation.simulate();
        }
        Globals.profiler.endCpuSample();
    }
    
}
