/// <summary>
        /// Конструктор окна с игрой
        /// </summary>
        /// <param name="client"></param>
        public GameWindow(SBClientModel client)
        {
            _initLength   = 4;
            _initCount    = 0;
            _initVertical = false;
            _state        = BattleState.Init;
            _client       = client.Client;
            this.Client   = client;

            InitializeComponent();

            myField.Colors       = CellValues.BattleFieldColors;
            myField.BorderColors = BorderValues.BattleFieldBorderColors;

            enemyField.Colors       = CellValues.BattleFieldColors;
            enemyField.BorderColors = BorderValues.BattleFieldBorderColors;

            #region мышь указатели

            enemyField.OnBattleFieldCellMouseEnter += (sender, ea) => {
                if (_state == BattleState.Turn)
                {
                    if (ea.Cell.Value == CellValues.None)
                    {
                        ea.Cell.BorderValue = BorderValues.TargetSelection;
                    }
                }
            };
            enemyField.OnBattleFieldCellMouseLeave += (sender, ea) => {
                ea.Cell.BorderValue = BorderValues.None;
            };
            enemyField.OnBattleFieldCellMouseUp += (sender, ea) => {
                if (_state == BattleState.Turn)
                {
                    if (ea.Cell.Value == CellValues.None)
                    {
                        _state = BattleState.Wait;
                        _cell  = ea.Cell;
                        _client.SendFire(ea.Cell.X, ea.Cell.Y);
                    }
                }
            };

            myField.OnBattleFieldCellMouseEnter += (sender, ea) => {
                if (_state == BattleState.Init && _initLength > 0)
                {
                    var ex = _initVertical ? ea.Cell.X : ea.Cell.X + _initLength - 1;
                    var ey = _initVertical ? ea.Cell.Y + _initLength - 1 : ea.Cell.Y;

                    if (ex < 10 && ey < 10)
                    {
                        if (this.IsRectEmpty(myField, ea.Cell.X - 1, ea.Cell.Y - 1, ex + 1, ey + 1))
                        {
                            for (int x = ea.Cell.X; x <= ex; x++)
                            {
                                for (int y = ea.Cell.Y; y <= ey; y++)
                                {
                                    myField[x, y].BorderValue = BorderValues.PlaceSelection;
                                    _cells.Add(myField[x, y]);
                                }
                            }
                        }
                    }
                }
            };
            myField.OnBattleFieldCellMouseLeave += (sender, ea) => {
                if (_state == BattleState.Init && _cells.Count > 0)
                {
                    _cells.ForEach(cell => cell.BorderValue = BorderValues.None);
                    _cells.Clear();
                }
            };
            myField.OnBattleFieldCellMouseUp += (sender, ea) => {
                if (ea.Button == MouseButton.Left)
                {
                    if (_state == BattleState.Init && _initLength > 0)
                    {
                        if (_cells.Count > 0)
                        {
                            _cells.ForEach(cell => {
                                cell.BorderValue = BorderValues.None;
                                cell.Value       = CellValues.Own;
                            });
                            _cells.Clear();

                            _client.SendPlace(ea.Cell.X, ea.Cell.Y, _initLength, _initVertical);

                            _initCount++;
                            if (_initLength + _initCount == 5)
                            {
                                _initLength--;
                                _initCount = 0;

                                if (_initLength == 0)
                                {
                                    _state = BattleState.Wait;
                                }
                            }
                        }
                    }
                }
                else if (tt.IsEnabled)
                {
                    _initVertical = !_initVertical;
                }
            };

            #endregion

            _client.OnOpponentFire += (x, y, dead) => {
                if (_state == BattleState.Wait)
                {
                    this.Invoke(() => {
                        if (myField[x, y].Value == CellValues.Own)
                        {
                            myField[x, y].Value = CellValues.Dead;

                            if (dead)
                            {
                                SetCompletlyDead(myField, x, y);
                            }
                        }
                        else
                        {
                            myField[x, y].Value = CellValues.FiredEmpty;
                        }
                    });
                }
            };

            _client.OnFireResult += (miss, dead) => {
                if (_state == BattleState.Wait)
                {
                    if (_cell != null)
                    {
                        this.Invoke(() => {
                            if (miss)
                            {
                                _cell.Value             = CellValues.FiredEmpty;
                                SoundPlayer simpleSound = new SoundPlayer(@"C:\Users\Мазимез\Desktop\sbattle\sbattle\SBattle\Voice\Voice_MissClick.wav");
                                simpleSound.Play();
                            }
                            else
                            {
                                SoundPlayer simpleSound = new SoundPlayer(@"C:\Users\Мазимез\Desktop\sbattle\sbattle\SBattle\Voice\VoiceSuc.wav");
                                simpleSound.Play();
                                _cell.Value = CellValues.Dead;

                                if (dead)
                                {
                                    SetCompletlyDead(enemyField, _cell.X, _cell.Y);
                                }
                            }
                        });
                        _cell = null;
                    }
                    else
                    {
                        MessageBox.Show("Неожиданный результат");
                    }
                }
            };
            #region Меню кто одержал победу исправить путь аудио
            _client.OnGameFinished += winner => {
                if (_state == BattleState.Wait)
                {
                    _state = BattleState.Finished;
                    this.Invoke(() => {
                        SoundPlayer simpleSound = new SoundPlayer(@"C:\Users\Мазимез\Desktop\sbattle\sbattle\SBattle\Voice\VoiceWin.wav");
                        simpleSound.Play();
                        MessageBox.Show(this, winner + " одержал победу!");
                        //создание объекта
                        // MediaPlayer player = new MediaPlayer();  //загрузка звука из ресурсов
                        //загрузка выбранного файла
                        // player.Open(new Uri(@"pack://*****:*****@"pack://application:,,,/pic/orig.jpg",
                        // player.Play();
                        //SoundPlayer simpleSound = new SoundPlayer(@"C:\Users\Мазимез\Desktop\sbattle\sbattle\SBattle\Voice\VoiceWin.wav");
                        // simpleSound.Play();
                        this.DialogResult = true;
                        this.Close();
                    });
                }
            };
            #endregion

            _client.OnTurn += () => {
                if (_state == BattleState.Wait)
                {
                    _state = BattleState.Turn;
                }
            };

            BattleState st = BattleState.Finished;
            #region Шляпа если сервер потерпит крах
            _client.OnConnectionLost += () => {
                if (_state != BattleState.Finished)
                {
                    this.Invoke(() => chat.AppendLine("Переподключение..."));
                    st     = _state;
                    _state = BattleState.Wait;

                    if (!_client.Reconnect())
                    {
                        this.Invoke(() => chat.AppendLine("Ошибка"));
                    }
                    else
                    {
                        this.Invoke(() => chat.AppendLine("Соединение установлено. Восстанавливается сеанс..."));
                    }
                }
            };

            _client.OnConnectedSuccessful += () => {
                if (_state != BattleState.Finished)
                {
                    //this.Invoke(() => chat.AppendLine("Сессия восстановлена."));
                    _state = st;
                }
            };

            _client.OnSessionRestoreFailed += () => {
                if (_state != BattleState.Finished)
                {
                    this.Invoke(() => {
                        MessageBox.Show(this, "Ошибка восстановления сессии.");
                        this.DialogResult = false;
                        this.Close();
                    });
                }
            };
        }
        /// <summary>
        /// Конструктор окна с игрой
        /// </summary>
        /// <param name="client"></param>
        public GameWindow(SBClientModel client)
        {
            _initLength   = 4;
            _initCount    = 0;
            _initVertical = false;
            _state        = BattleState.Init;
            _client       = client.Client;
            this.Client   = client;

            InitializeComponent();

            myField.Colors       = CellValues.BattleFieldColors;
            myField.BorderColors = BorderValues.BattleFieldBorderColors;

            enemyField.Colors       = CellValues.BattleFieldColors;
            enemyField.BorderColors = BorderValues.BattleFieldBorderColors;

            #region мышь указатели

            enemyField.OnBattleFieldCellMouseEnter += (sender, ea) => {
                if (_state == BattleState.Turn)
                {
                    if (ea.Cell.Value == CellValues.None)
                    {
                        ea.Cell.BorderValue = BorderValues.TargetSelection;
                    }
                }
            };
            enemyField.OnBattleFieldCellMouseLeave += (sender, ea) => {
                ea.Cell.BorderValue = BorderValues.None;
            };
            enemyField.OnBattleFieldCellMouseUp += (sender, ea) => {
                if (_state == BattleState.Turn)
                {
                    if (ea.Cell.Value == CellValues.None)
                    {
                        _state = BattleState.Wait;
                        _cell  = ea.Cell;
                        _client.SendFire(ea.Cell.X, ea.Cell.Y);
                    }
                }
            };

            myField.OnBattleFieldCellMouseEnter += (sender, ea) => {
                if (_state == BattleState.Init && _initLength > 0)
                {
                    var ex = _initVertical ? ea.Cell.X : ea.Cell.X + _initLength - 1;
                    var ey = _initVertical ? ea.Cell.Y + _initLength - 1 : ea.Cell.Y;

                    if (ex < 10 && ey < 10)
                    {
                        if (this.IsRectEmpty(myField, ea.Cell.X - 1, ea.Cell.Y - 1, ex + 1, ey + 1))
                        {
                            for (int x = ea.Cell.X; x <= ex; x++)
                            {
                                for (int y = ea.Cell.Y; y <= ey; y++)
                                {
                                    myField[x, y].BorderValue = BorderValues.PlaceSelection;
                                    _cells.Add(myField[x, y]);
                                }
                            }
                        }
                    }
                }
            };
            myField.OnBattleFieldCellMouseLeave += (sender, ea) => {
                if (_state == BattleState.Init && _cells.Count > 0)
                {
                    _cells.ForEach(cell => cell.BorderValue = BorderValues.None);
                    _cells.Clear();
                }
            };
            myField.OnBattleFieldCellMouseUp += (sender, ea) => {
                if (ea.Button == MouseButton.Left)
                {
                    if (_state == BattleState.Init && _initLength > 0)
                    {
                        if (_cells.Count > 0)
                        {
                            _cells.ForEach(cell => {
                                cell.BorderValue = BorderValues.None;
                                cell.Value       = CellValues.Own;
                            });
                            _cells.Clear();

                            _client.SendPlace(ea.Cell.X, ea.Cell.Y, _initLength, _initVertical);

                            _initCount++;
                            if (_initLength + _initCount == 5)
                            {
                                _initLength--;
                                _initCount = 0;

                                if (_initLength == 0)
                                {
                                    _state = BattleState.Wait;
                                }
                            }
                        }
                    }
                }
                else if (tt.IsEnabled)
                {
                    _initVertical = !_initVertical;
                }
            };

            #endregion

            _client.OnOpponentFire += (x, y, dead) => {
                if (_state == BattleState.Wait)
                {
                    this.Invoke(() => {
                        if (myField[x, y].Value == CellValues.Own)
                        {
                            myField[x, y].Value = CellValues.Dead;

                            if (dead)
                            {
                                SetCompletlyDead(myField, x, y);
                            }
                        }
                        else
                        {
                            myField[x, y].Value = CellValues.FiredEmpty;
                        }
                    });
                }
            };

            _client.OnFireResult += (miss, dead) => {
                if (_state == BattleState.Wait)
                {
                    if (_cell != null)
                    {
                        this.Invoke(() => {
                            if (miss)
                            {
                                _cell.Value = CellValues.FiredEmpty;
                            }
                            else
                            {
                                _cell.Value = CellValues.Dead;
                                if (dead)
                                {
                                    SetCompletlyDead(enemyField, _cell.X, _cell.Y);
                                }
                            }
                        });
                        _cell = null;
                    }
                    else
                    {
                        MessageBox.Show("Неожиданный результат");
                    }
                }
            };

            _client.OnGameFinished += winner => {
                if (_state == BattleState.Wait)
                {
                    _state = BattleState.Finished;
                    this.Invoke(() => {
                        MessageBox.Show(this, winner + " одержал победу!");
                        this.DialogResult = true;
                        this.Close();
                    });
                }
            };

            _client.OnTurn += () => {
                if (_state == BattleState.Wait)
                {
                    _state = BattleState.Turn;
                }
            };

            BattleState st = BattleState.Finished;

            _client.OnConnectionLost += () => {
                if (_state != BattleState.Finished)
                {
                    this.Invoke(() => chat.AppendLine("Переподключение..."));
                    st     = _state;
                    _state = BattleState.Wait;

                    if (!_client.Reconnect())
                    {
                        this.Invoke(() => chat.AppendLine("Ошибка"));
                    }
                    else
                    {
                        this.Invoke(() => chat.AppendLine("Соединение установлено. Восстанавливается сеанс..."));
                    }
                }
            };

            _client.OnConnectedSuccessful += () => {
                if (_state != BattleState.Finished)
                {
                    //this.Invoke(() => chat.AppendLine("Сессия восстановлена."));
                    _state = st;
                }
            };

            _client.OnSessionRestoreFailed += () => {
                if (_state != BattleState.Finished)
                {
                    this.Invoke(() => {
                        MessageBox.Show(this, "Ошибка восстановления сессии.");
                        this.DialogResult = false;
                        this.Close();
                    });
                }
            };
        }