public MultiplayerController(BotManager player, OnReciveCallBack onRcCallBack, OnDisconnect onDCCallBack,
                              OnGameIsReady onGameIsReady, OnGameFinished onGameFinished,
                              OnEnemyDCAccepted onEnemyDCAccepted, OnReconnectComplete onReconnectComplete,
                              OnReconnectFailed onReconnectFailed, OnWeakConectionDetected onWeakConnection,
                              bool isForExistingGame, OnRuleReady onRuleReady)
 {
     this.player           = player;
     io                    = new SocketIOComponent(Constants.SOCKET_URL);
     reconnectMode         = false;
     eventHandler          = new EventHandler();
     lastDataTime          = 0f;
     this.onDCCallBack     = onDCCallBack;
     this.onWeakConnection = onWeakConnection;
     if (!isForExistingGame)
     {
         eventHandler.initializeForFirstTime(this, io, onRcCallBack, onDCCallBack, onGameIsReady,
                                             onGameFinished, onEnemyDCAccepted, onReconnectComplete,
                                             onReconnectFailed);
         //Thread acceptGameThread = new Thread(new ThreadStart(acceptGame));
         //acceptGameThread.Start();
     }
     else
     {
         (new Thread(() =>
         {
             requestGameRule(onRcCallBack, onDCCallBack, onGameIsReady,
                             onGameFinished, onEnemyDCAccepted, onReconnectComplete, onReconnectFailed, onRuleReady);
         })).Start();
     }
 }
 public void onLoadForExistingGameComplete(MultiplayerController parent, OnReciveCallBack onRcCallBack, OnDisconnect onDCCallBack,
                                           OnGameIsReady onGameIsReady, OnGameFinished onGameFinished, OnEnemyDCAccepted onEnemyDCAccepted,
                                           OnReconnectComplete onReconnectComplete, OnReconnectFailed onReconnectFailed,
                                           OnRuleReady onRuleReady, OnWeakConectionDetected onWeakConnection, bool isExistingGame)
 {
     this.onDCCallBack     = onDCCallBack;
     this.onWeakConnection = onWeakConnection;
     (new Thread(() =>
     {
         requestGameRule(onRcCallBack, onDCCallBack, onGameIsReady,
                         onGameFinished, onEnemyDCAccepted, onReconnectComplete, onReconnectFailed, onRuleReady);
     })).Start();
 }
 public void initializeForFirstTime(MultiplayerController parent, SocketIOComponent io, OnReciveCallBack onRcCallBack, OnDisconnect onDCCallBack, OnGameIsReady onGameIsReady, OnGameFinished onGameFinished, OnEnemyDCAccepted onEnemyDCAccepted, OnReconnectComplete onReconnectComplete, OnReconnectFailed onReconnectFailed)
 {
     this.parent              = parent;
     this.io                  = io;
     this.onRcCallBack        = onRcCallBack;
     this.onDCCallBack        = onDCCallBack;
     this.onGameIsReady       = onGameIsReady;
     this.onGameFinished      = onGameFinished;
     this.onEnemyDCAccepted   = onEnemyDCAccepted;
     this.onReconnectComplete = onReconnectComplete;
     this.onReconnectFailed   = onReconnectFailed;
     io.On(Constants.serverMessage.events.START_GAME, startGame);
     io.On(Constants.serverMessage.events.END_GAME, endGame);
     io.On(Constants.serverMessage.events.ENEMY_DC_ACCEPTED, enemyDCAccepted);
     io.On(Constants.serverMessage.events.ON_GAME_STATUS_READY, gameStatusRecieve);
     io.On(Constants.serverMessage.events.ON_RECONNECT_FAILED, onRecFailed);
     io.On(Constants.serverMessage.events.NEW_TOKENS, onNewTokenRecieved);
     io.On(Constants.serverMessage.events.SERVER_ORDER, onServerOrder);
     io.On(Constants.serverMessage.events.CORRECTION, onCorrection);
     io.On(Constants.serverMessage.events.PING_SUC, onPingSuc);
     //io.On(Strings.serverMessage.events.ENABLE_USER, onEnableUser);
 }
        private void requestGameRule(OnReciveCallBack onRcCallBack, OnDisconnect onDCCallBack, OnGameIsReady onGameIsReady,
                                     OnGameFinished onGameFinished, OnEnemyDCAccepted onEnemyDCAccepted,
                                     OnReconnectComplete onReconnectComplete, OnReconnectFailed onReconnectFailed, OnRuleReady onRuleReady)
        {
            io.Connect();
            while (!io.IsConnected)
            {
                io.On(Constants.serverMessage.events.CONNECT, (SocketIOEvent obj) =>
                {
                }
                      );
                io.On(Constants.serverMessage.events.DISCONNECT, onDisconnect);
                Thread.Sleep(100);
            }
            eventHandler.initializeForFirstTime(this, io, onRcCallBack, onDCCallBack, onGameIsReady,
                                                onGameFinished, onEnemyDCAccepted, onReconnectComplete, onReconnectFailed);
            bool ruleIsReady = false;

            io.On(Constants.serverMessage.events.GAME_RULE_READY, (obj) =>
            {
                ruleIsReady       = true;
                GameInfo info     = new GameInfo();
                string gameToken  = player.user.getGameToken();
                string userToken  = player.user.getUserToken();
                string enemyToken = player.user.getOpponentToken();
                string enemyName  = player.user.getEnemyName();
                info.fillWithCommonData(obj, userToken, gameToken, enemyToken, enemyName);
                onRuleReady(info);
                player.user.setGameMode(Constants.gameMode.MULTI_PLAYER, info);
                Thread t = new Thread(new ThreadStart(retryConnection));
                t.Start();
            }
                  );
            while (!ruleIsReady)
            {
                io.Emit(Constants.serverMessage.events.REQUEST_GAME_RULE);
                Thread.Sleep(100);
            }
        }