private bool OnGameJoined(JObject jsonObject)
        {
            accessToken = (string)jsonObject["access_token"];
            int    mode         = (int)jsonObject["mode"];
            int    rowCellCount = (int)jsonObject["size"];
            int    numberToWin  = 3;
            JToken jNumberToWin;

            if (jsonObject.TryGetValue("number_to_win", out jNumberToWin))
            {
                numberToWin = (int)jNumberToWin;
            }
            bool   bMyFirstMove = false;
            JToken jbMyFirstMove;

            if (jsonObject.TryGetValue("your_first_turn", out jbMyFirstMove))
            {
                bMyFirstMove = (bool)jbMyFirstMove;
            }
            context.gameCtrl.Init(rowCellCount);
            context.gameCtrl.IsObserver = mode == 2;
            context.gameCtrl.SetYourMove(bMyFirstMove, false);
            context.game.SetRowCellCount((ushort)rowCellCount);
            context.game.SetNumberToWin((ushort)numberToWin);
            context.game.OnUpdateMyFirstMove();
            if (OnReceiveFirstData != null)
            {
                OnReceiveFirstData(gameToken);
            }
            TimeLimitedOperationsServiceSingleton.AddOperation(SetStateGameQuery);
            return(true);
        }
 protected override void OnReceivingError(string type, string errorMsg)
 {
     if (type == RECEIVE_FATAL_ERROR || type == outCreateGame || type == outJoinGame)
     {
         TimeLimitedOperationsServiceSingleton.RemoveOperation(SetStateGameQuery);
         context.game.OnReceivingError(RECEIVE_FATAL_ERROR, errorMsg);
     }
     else
     {
         context.game.OnReceivingError(type, errorMsg);
     }
 }
 private bool OnGameCreated(JObject jsonObject)
 {
     accessToken = (string)jsonObject["access_token"];
     gameToken   = (string)jsonObject["game_token"];
     context.gameCtrl.IsGameCreator = true;
     if (OnReceiveFirstData != null)
     {
         OnReceiveFirstData(gameToken);
     }
     bCheckGameState   = false;
     bGameBeginEnabled = false;
     TimeLimitedOperationsServiceSingleton.AddOperation(SetStateGameQuery);
     return(true);
 }
 public override void OnConnectionError(string type, string errorMsg)
 {
     if (bExiting)
     {
         int id = (context.gameCtrl.IsObserver || context.gameCtrl.IsGameFinished) ? -2 : -1;
         context.gameCtrl.FisishGame(id, string.Empty);
         TimeLimitedOperationsServiceSingleton.RemoveOperation(SetStateGameQuery);
         bExiting = false;
     }
     else
     {
         if (type == outCreateGame || type == outJoinGame)
         {
             context.game.OnConnectionError(errorMsg);
         }
         else
         {
             context.game.OnReceivingError(type, errorMsg);
         }
     }
 }
        public void SetExit(bool bAppExiting)
        {
            if (bExitSending || context.gameCtrl.IsGameFinished)
            {
                return;
            }
            bExitSending = true;
            bExiting     = true;
            Headers headers = new Headers();

            headers.Add("access_token", accessToken);
            headers.Add("game_creator", (context.gameCtrl.IsGameCreator ? 1 : 0).ToString());
            headers.Add("observer", (context.gameCtrl.IsObserver ? 1 : 0).ToString());
            JObject rss = new JObject();

            rss.Add("user_name", context.gameCtrl.UserName);
            rss.Add("exit", bAppExiting);
            if (context.gameCtrl.IsObserver)
            {
                TimeLimitedOperationsServiceSingleton.RemoveOperation(SetStateGameQuery);
            }
            SendJsonData("POST", "/exit", headers, rss.ToString());
            bExitSending = false;
        }
        public NoughtsAndCrossesForm(NoughtsAndCrossesGame noughtsAndCrossesGame)
        {
            InitializeComponent();
            _noughtsAndCrossesGame = noughtsAndCrossesGame;

            InitGameEventHandler();

            buttonApply.Click += (o, e) => {
                _noughtsAndCrossesGame.Apply();
            };

            checkMyFirstMove.CheckedChanged += (o, e) => {
                _noughtsAndCrossesGame.SetMyFirstMove(checkMyFirstMove.Checked);
            };

#if !FOR_JAVA
            buttonStartServer.Click += (o, e) => {
                _noughtsAndCrossesGame.StartServer();
            };
#endif

            buttonStopServer.Click += (o, e) => {
                _noughtsAndCrossesGame.StopServer();
            };

            buttonConnect.Click += (o, e) => {
                _noughtsAndCrossesGame.Apply();
                _noughtsAndCrossesGame.Connect(true, "");
            };

#if FOR_JAVA
            buttonCreateGame.Click += (o, e) => {
                _noughtsAndCrossesGame.Apply();
                _noughtsAndCrossesGame.Connect(true, "");
            };
            buttonJoinToGame.Click += (o, e) => {
                _noughtsAndCrossesGame.Connect(false, textBoxGameToken.Text);
            };
            comboBoxProtocols.SelectionChangeCommitted += (o, e) => {
                _noughtsAndCrossesGame.Protocol = (string)comboBoxProtocols.SelectedItem;
                if (_noughtsAndCrossesGame.Protocol == Connector.Protocols[0])
                {
                    buttonConnect.Visible    = true;
                    buttonCreateGame.Visible = false;
                    buttonJoinToGame.Visible = false;
                    textBoxGameToken.Visible = false;
                }
                else if (_noughtsAndCrossesGame.Protocol == Connector.Protocols[1])
                {
                    buttonConnect.Visible    = false;
                    buttonCreateGame.Visible = true;
                    buttonJoinToGame.Visible = true;
                    textBoxGameToken.Visible = true;
                }
            };
            comboBoxProtocols.SelectedItem = Connector.Protocols[1];
            buttonConnect.Visible          = false;
            buttonCreateGame.Visible       = true;
            buttonJoinToGame.Visible       = true;
            textBoxGameToken.Visible       = true;
#endif

            buttonDisConnect.Click += (o, e) => {
                _noughtsAndCrossesGame.DisConnect();
            };

            numericNumberToWin.Maximum = numericRowCellsCount.Value;
#if FOR_JAVA
            textBoxUserName.TextChanged += (o, e) => {
                _noughtsAndCrossesGame.SetUserName(textBoxUserName.Text);
            };
#endif

            TimeLimitedOperationsServiceSingleton.Create();
            TimeLimitedOperationsServiceSingleton.Start();
            timer.Tick += (o, e) => TimeLimitedOperationsServiceSingleton.Run();
            timer.Start();

            FormClosing += (o, e) => {
                _noughtsAndCrossesGame.Close();
            };

            FormClosed += (o, e) => {
                TimeLimitedOperationsServiceSingleton.Stop();
                TimeLimitedOperationsServiceSingleton.Delete();
            };
        }
        private bool OnGameState(JObject jsonObject)
        {
            if (!bCheckGameState)
            {
                bCheckGameState = true;
                return(true);
            }

            bool   bMyMove      = (bool)jsonObject["you_turn"];
            long   timeDuration = (long)jsonObject["game_duration"];
            JArray jField       = (JArray)jsonObject["field"];
            string sdata        = string.Empty;

            foreach (var jRow in jField)
            {
                sdata += (string)jRow;
            }

            context.game.OnDuration(timeDuration);
            JToken winner = GameStateChecker.NONE;
            bool   bSucc  = jsonObject.TryGetValue("winner", out winner);

            if (!bSucc || (int)winner == GameStateChecker.NONE)
            {
                if (context.gameCtrl.IsGameCreator && context.gameCtrl.IsMyFirstMove())
                {
                    if (!bGameBeginEnabled && !bMyMove)
                    {
                        return(true);
                    }
                    if (bMyMove)
                    {
                        bGameBeginEnabled = true;
                    }
                }
                context.gameCtrl.SetYourMove(context.gameCtrl.IsMyFirstMove(), bMyMove);
                context.gameCtrl.SetGameState(sdata);
                return(true);
            }

            bCheckGameState = false;
            JToken jwinUserName;
            string winUserName = string.Empty;

            if (jsonObject.TryGetValue("winner_name", out jwinUserName))
            {
                winUserName = (string)jwinUserName;
            }
            int id = -1;

            switch ((int)winner)
            {
            case GameStateChecker.MATCH_DRAWN: {
                id = 0;
            }
            break;

            case GameStateChecker.WIN_X: {
                id = context.gameCtrl.MyCellValue == 'X' ? 1 : -1;
            }
            break;

            case GameStateChecker.WIN_0: {
                id = context.gameCtrl.MyCellValue == '0' ? 1 : -1;
            }
            break;
            }
            TimeLimitedOperationsServiceSingleton.RemoveOperation(SetStateGameQuery);
            context.gameCtrl.SetGameState(sdata);
            context.gameCtrl.FisishGame(id, winUserName);
            return(true);
        }