Ejemplo n.º 1
0
        void waitTimer_Tick(object sender, EventArgs e)
        {
            //долбим сервер в ожидании противника
            string response = Connection.setUserWait(userInfo[0]);

            if (response.CompareTo("OK") != 0 && response.CompareTo("AGAIN") != 0 && response.CompareTo("ERROR") != 0)
            {
                rivalInfo = new string[2];
                dynamic jsonObj = JObject.Parse(response);
                if (jsonObj.firstPlayer == null)
                {
                    rivalInfo[0] = jsonObj.secondPlayer;
                    rivalInfo[1] = Initialisator.getCountOfWinsIntoJArray(online_users, (string)jsonObj.secondPlayer);
                    stepPriority = true;
                }
                else
                {
                    rivalInfo[0] = jsonObj.firstPlayer;
                    rivalInfo[1] = rivalCountOfWins.Text += Initialisator.getCountOfWinsIntoJArray(online_users, (string)jsonObj.firstPlayer);
                    stepPriority = false;
                }
                this.Controls.Remove(waitPanel);
                Initialisator.rivalPanelInit(rivalPanel, rivalField, rivalLogin, rivalCountOfWins);
                Initialisator.priorityLabelInit(priority, stepPriority);
                this.Controls.Add(rivalPanel);
                timerLabel.Visible     = true;
                rivalField.Paint      += field_Paint;
                rivalField.MouseClick += rivalField_MouseClick;
                rivalField.Enabled     = stepPriority;
                gameId             = jsonObj.id;
                gameTimer          = new Timer();
                gameTimer.Interval = 1000;
                gameTimer.Tick    += gameTimer_Tick;
                gameTimer.Start();
                waitTimer.Stop();
            }
        }
Ejemplo n.º 2
0
 void gameTimer_Tick(object sender, EventArgs e)
 {
     if (timer > 0)
     {
         if (!stepPriority)                                                 //если мы походили
         {
             if (myStep != null)                                            //если мы только что походили
             {
                 int status = Connection.getStepStatus(stepNumber, gameId); //узнаем статус нашего попадания
                 if (status != 0)
                 {
                     rivalFieldPaper[myStep[0], myStep[1]] = status;
                     if (status == 2)                //если мы не попали
                     {
                         rivalField.Enabled = false; //даём походить противнику
                     }
                     int countOfKilledShips = Connection.getCountOfKilledShips(userInfo[0], gameId);
                     if (countOfKilledShips == countOfCubs) // если мы выйграли
                     {
                         Connection.setUserWin(userInfo[0]);
                         MessageBox.Show("Поздравляем! Вы выйграли!", "Победа", MessageBoxButtons.OK, MessageBoxIcon.Asterisk);
                         this.Controls.Remove(rivalPanel);
                         roolsPanel.Visible = true;
                         fieldPaper         = new int[10, 10];
                         field.Refresh();
                         userInfo[1]          = (int.Parse(userInfo[1]) + 1).ToString();
                         infoCountOfWins.Text = "Побед: " + userInfo[1];
                         gameTimer.Stop();
                         timerLabel.Visible = false;
                         priority.Visible   = false;
                     }
                     else
                     {
                         stepPriority = true; //ходим снова
                         Initialisator.priorityLabelInit(priority, stepPriority);
                         stepNumber++;
                         timer  = 60;
                         myStep = null;
                         rivalField.Refresh();
                     }
                 }
             }
             else // если была не наша очередь и мы ждем хода противника
             {
                 int[] rivalStep = Connection.getLastStep(stepNumber, gameId);
                 if (rivalStep != null)
                 {
                     if (fieldPaper[rivalStep[0], rivalStep[1]] == 1)     //если у нас тут корабль
                     {
                         Connection.setStepStatus(stepNumber, gameId, 3); //сообщить противнику, что он попал
                         fieldPaper[rivalStep[0], rivalStep[1]] = 3;
                     }
                     else
                     {
                         Connection.setStepStatus(stepNumber, gameId, 2);//сообщить противнику, что он промазал
                         fieldPaper[rivalStep[0], rivalStep[1]] = 2;
                         rivalField.Enabled = true;
                         stepPriority       = true;
                         Initialisator.priorityLabelInit(priority, stepPriority);
                     }
                     int countOfKilledShips = Connection.getCountOfKilledShips(rivalInfo[0], gameId);
                     if (countOfKilledShips == countOfCubs) // если противник выйграл
                     {
                         Connection.setUserLouse(userInfo[0]);
                         MessageBox.Show("Увы! Вы проиграли!", "Поражение", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                         this.Controls.Remove(rivalPanel);
                         roolsPanel.Visible = true;
                         fieldPaper         = new int[10, 10];
                         field.Refresh();
                         userInfo[1]          = (int.Parse(userInfo[1]) - 1).ToString();
                         infoCountOfWins.Text = "Побед: " + userInfo[1];
                         gameTimer.Stop();
                         timerLabel.Visible = false;
                         priority.Visible   = false;
                     }
                     else
                     {
                         stepNumber++;
                         timer = 60;
                         field.Refresh();
                     }
                 }
             }
         }
     }
     else
     {
         if (stepPriority)//если мы не походили
         {
             Connection.setUserLouse(userInfo[0]);
             MessageBox.Show("Нужно следить за временем!", "Техническое поражение", MessageBoxButtons.OK, MessageBoxIcon.Information);
         }
         else // если не походил противник, тут 2 случая: или противник просто замешкался, либо он отключился....
         {
             string   timediff  = Connection.getLastActivity(rivalInfo[0], DateTime.Now.ToString("HH:mm:ss"));
             string[] timearray = timediff.Split(':');
             if (int.Parse(timearray[0]) > 0 || int.Parse(timearray[1]) > 0 || int.Parse(timearray[2]) > 30)
             {
                 Connection.setUserLouse(userInfo[0]);
             }
             //если юзер вылетел, то поражение сам себе он уже не поставит,... делаем это за него
             Connection.setUserWin(userInfo[0]);
             MessageBox.Show("Вы выйграли!", "Техническая победа", MessageBoxButtons.OK, MessageBoxIcon.Information);
         }
         this.Controls.Remove(rivalPanel);
         roolsPanel.Visible = true;
         fieldPaper         = new int[10, 10];
         field.Refresh();
         userInfo[1]          = (int.Parse(userInfo[1]) - 1).ToString();
         infoCountOfWins.Text = "Побед: " + userInfo[1];
         gameTimer.Stop();
         timerLabel.Visible = false;
         priority.Visible   = false;
     }
     timer--;
     timerLabel.Text = "00:" + timer;
 }