public MainWindow()
        {
            _humanPlayer = new HumanPlayer();
            _computerPlayer = new ComputerPlayer();
            _humanGrid = new HumanGridVM(_humanPlayer, _computerPlayer);
            _computerGrid = new ComputerGridVM(_humanPlayer, _computerPlayer);

            InitializeComponent();
            _mainWindow = this;
            humanGrid.DataContext = _humanGrid;
            computerGrid.DataContext = _computerGrid;
            UpdateTb(0,19);
        }
        void System.Windows.Markup.IComponentConnector.Connect(int connectionId, object target)
        {
            switch (connectionId)
            {
            case 1:
                this.MainWindow1 = ((Battleship.MainWindow)(target));
                return;

            case 2:
                this.MainGrid = ((System.Windows.Controls.Grid)(target));
                return;

            case 3:
                this.Start_Game_Button = ((System.Windows.Controls.Button)(target));

            #line 12 "..\..\MainWindow.xaml"
                this.Start_Game_Button.Click += new System.Windows.RoutedEventHandler(this.Start_Game_Button_Click);

            #line default
            #line hidden
                return;

            case 4:
                this.Main_Menu_Label = ((System.Windows.Controls.Label)(target));
                return;

            case 5:
                this.Enemy_Shipyard = ((System.Windows.Shapes.Rectangle)(target));
                return;

            case 6:
                this.Player_Shipyard = ((System.Windows.Shapes.Rectangle)(target));
                return;

            case 7:
                this.Ship_Descriptions_Label = ((System.Windows.Controls.Label)(target));
                return;

            case 8:
                this.Fire_Missile = ((System.Windows.Controls.Button)(target));

            #line 22 "..\..\MainWindow.xaml"
                this.Fire_Missile.Click += new System.Windows.RoutedEventHandler(this.Fire_Missile_Click);

            #line default
            #line hidden
                return;

            case 9:
                this.Retreat = ((System.Windows.Controls.Button)(target));

            #line 23 "..\..\MainWindow.xaml"
                this.Retreat.Click += new System.Windows.RoutedEventHandler(this.Retreat_Click);

            #line default
            #line hidden
                return;

            case 10:
                this.Hit_Ratio = ((System.Windows.Controls.Label)(target));
                return;

            case 11:
                this.Miss_Ratio = ((System.Windows.Controls.Label)(target));
                return;

            case 12:
                this.Player_One_Label = ((System.Windows.Controls.Label)(target));
                return;

            case 13:
                this.Player_Two_Label = ((System.Windows.Controls.Label)(target));
                return;

            case 14:
                this.Hit_Ratio_Enemy = ((System.Windows.Controls.Label)(target));
                return;

            case 15:
                this.Miss_Ratio_Enemy = ((System.Windows.Controls.Label)(target));
                return;

            case 16:
                this.One_TextBox = ((System.Windows.Controls.TextBox)(target));
                return;

            case 17:
                this.Two_TextBox = ((System.Windows.Controls.TextBox)(target));
                return;

            case 18:
                this.Three_TextBox = ((System.Windows.Controls.TextBox)(target));
                return;

            case 19:
                this.Four_TextBox = ((System.Windows.Controls.TextBox)(target));
                return;

            case 20:
                this.Five_TextBox = ((System.Windows.Controls.TextBox)(target));
                return;

            case 21:
                this.Six_TextBox = ((System.Windows.Controls.TextBox)(target));
                return;

            case 22:
                this.Seven_TextBox = ((System.Windows.Controls.TextBox)(target));
                return;

            case 23:
                this.Eight_TextBox = ((System.Windows.Controls.TextBox)(target));
                return;

            case 24:
                this.Nine_TextBox = ((System.Windows.Controls.TextBox)(target));
                return;

            case 25:
                this.Ten_TextBox = ((System.Windows.Controls.TextBox)(target));
                return;

            case 26:
                this.Player_Grid = ((System.Windows.Controls.Grid)(target));
                return;

            case 27:
                this.Player_A_TextBox = ((System.Windows.Controls.TextBox)(target));
                return;

            case 28:
                this.Player_B_TextBox = ((System.Windows.Controls.TextBox)(target));
                return;

            case 29:
                this.Player_C_TextBox = ((System.Windows.Controls.TextBox)(target));
                return;

            case 30:
                this.Player_D_TextBox = ((System.Windows.Controls.TextBox)(target));
                return;

            case 31:
                this.Player_E_TextBox = ((System.Windows.Controls.TextBox)(target));
                return;

            case 32:
                this.Player_F_TextBox = ((System.Windows.Controls.TextBox)(target));
                return;

            case 33:
                this.Player_G_TextBox = ((System.Windows.Controls.TextBox)(target));
                return;

            case 34:
                this.Player_H_TextBox = ((System.Windows.Controls.TextBox)(target));
                return;

            case 35:
                this.Player_I_TextBox = ((System.Windows.Controls.TextBox)(target));
                return;

            case 36:
                this.Player_J_TextBox = ((System.Windows.Controls.TextBox)(target));
                return;

            case 37:
                this.Enemy_Grid = ((System.Windows.Controls.Grid)(target));

            #line 79 "..\..\MainWindow.xaml"
                this.Enemy_Grid.MouseDown += new System.Windows.Input.MouseButtonEventHandler(this.Enemy_Grid_MouseDown);

            #line default
            #line hidden
                return;

            case 38:
                this.Enemy_Canvas = ((System.Windows.Controls.Canvas)(target));
                return;

            case 39:
                this.Enemy_A_TextBox = ((System.Windows.Controls.TextBox)(target));
                return;

            case 40:
                this.Enemy_B_TextBox = ((System.Windows.Controls.TextBox)(target));
                return;

            case 41:
                this.Enemy_C_TextBox = ((System.Windows.Controls.TextBox)(target));
                return;

            case 42:
                this.Enemy_D_TextBox = ((System.Windows.Controls.TextBox)(target));
                return;

            case 43:
                this.Enemy_E_TextBox = ((System.Windows.Controls.TextBox)(target));
                return;

            case 44:
                this.Enemy_F_TextBox = ((System.Windows.Controls.TextBox)(target));
                return;

            case 45:
                this.Enemy_G_TextBox = ((System.Windows.Controls.TextBox)(target));
                return;

            case 46:
                this.Enemy_H_TextBox = ((System.Windows.Controls.TextBox)(target));
                return;

            case 47:
                this.Enemy_I_TextBox = ((System.Windows.Controls.TextBox)(target));
                return;

            case 48:
                this.Enemy_J_TextBox = ((System.Windows.Controls.TextBox)(target));
                return;

            case 49:
                this.PlaceShips = ((System.Windows.Controls.Button)(target));

            #line 118 "..\..\MainWindow.xaml"
                this.PlaceShips.Click += new System.Windows.RoutedEventHandler(this.PlaceShips_Click);

            #line default
            #line hidden
                return;

            case 50:
                this.Enemy_Shipyard_Text_Block = ((System.Windows.Controls.TextBox)(target));
                return;

            case 51:
                this.Player_Shipyard_Text_Block = ((System.Windows.Controls.TextBox)(target));
                return;

            case 52:
                this.Player_Canvas = ((System.Windows.Controls.Canvas)(target));
                return;

            case 53:
                this.Confirm_Ships_Button = ((System.Windows.Controls.Button)(target));

            #line 122 "..\..\MainWindow.xaml"
                this.Confirm_Ships_Button.Click += new System.Windows.RoutedEventHandler(this.Confirm_Ships_Button_Click);

            #line default
            #line hidden
                return;

            case 54:
                this.Restart_Game_Button = ((System.Windows.Controls.Button)(target));

            #line 123 "..\..\MainWindow.xaml"
                this.Restart_Game_Button.Click += new System.Windows.RoutedEventHandler(this.Start_Game_Button_Click);

            #line default
            #line hidden
                return;

            case 55:
                this.Exit_Game_Button = ((System.Windows.Controls.Button)(target));

            #line 124 "..\..\MainWindow.xaml"
                this.Exit_Game_Button.Click += new System.Windows.RoutedEventHandler(this.Exit_Game_Button_Click);

            #line default
            #line hidden
                return;

            case 56:
                this.Enemy_Destroyer = ((System.Windows.Shapes.Ellipse)(target));
                return;

            case 57:
                this.Enemy_Sub = ((System.Windows.Shapes.Ellipse)(target));
                return;

            case 58:
                this.Enemy_Battleship = ((System.Windows.Shapes.Ellipse)(target));
                return;

            case 59:
                this.Enemy_Carrier = ((System.Windows.Shapes.Ellipse)(target));
                return;

            case 60:
                this.Enemy_PatrolBoat = ((System.Windows.Shapes.Ellipse)(target));
                return;

            case 61:
                this.Player_Destroyer = ((System.Windows.Shapes.Ellipse)(target));
                return;

            case 62:
                this.Player_Sub = ((System.Windows.Shapes.Ellipse)(target));
                return;

            case 63:
                this.Player_Battleship = ((System.Windows.Shapes.Ellipse)(target));
                return;

            case 64:
                this.Player_Carrier = ((System.Windows.Shapes.Ellipse)(target));
                return;

            case 65:
                this.Player_PatrolBoat = ((System.Windows.Shapes.Ellipse)(target));
                return;
            }
            this._contentLoaded = true;
        }
Example #3
0
        public void OnMessageReceived(Message message)
        {
            Application.Current.Dispatcher.Invoke(DispatcherPriority.Background, new Action(() =>
            {
                List <byte> content = new List <byte>(message.GetContent());

                switch (message.GetId())
                {
                case Message.ID.PLAYERDATA:
                    {
                        bool isHost     = (content[0] == 1);
                        bool isReady    = (content[1] == 1);
                        string username = Encoding.UTF8.GetString(content.GetRange(2, content.Count - 2).ToArray());

                        PlayerListItem playerListItem = new PlayerListItem();
                        playerListItem.Username       = username;
                        playerListItem.IsHost         = isHost;
                        playerListItem.IsReady        = isReady;
                        playerListItem.Foreground     = Brushes.White;

                        if (con_Players.Children.Count == 0)
                        {
                            playerListItem.Margin = new Thickness(5, 5, 5, 5);
                        }
                        else
                        {
                            playerListItem.Margin = new Thickness(5, 0, 5, 5);
                        }

                        playerListItem.InnerMargin    = new Thickness(5, 5, 5, 5);
                        playerListItem.Background     = new SolidColorBrush((Color)ColorConverter.ConvertFromString("#FF2D2D30"));
                        playerListItem.ButtonTemplate = (ControlTemplate)FindResource("ButtonControlTemplate");
                        playerListItem.ButtonStyle    = (Style)FindResource("ButtonStyle");

                        PlayerListItem existingControl = null;
                        foreach (PlayerListItem control in con_Players.Children)
                        {
                            if (control.Username == username)
                            {
                                existingControl = control;
                                break;
                            }
                        }

                        if (existingControl != null)
                        {
                            existingControl = playerListItem;
                        }
                        else
                        {
                            con_Players.Children.Add(playerListItem);
                            txb_Chat.Text += $"Player {username} joined the session!" + Environment.NewLine;
                            txb_Chat.ScrollToEnd();
                        }

                        break;
                    }

                case Message.ID.READY:
                    {
                        if (message.GetState() == Message.State.OK)
                        {
                            string username = Encoding.UTF8.GetString(content.ToArray());

                            if (username == UserLogin.Username)
                            {
                                this.isReady            = true;
                                btn_ReadyToggle.Content = "Unready";
                            }

                            SetReadyForUser(username, true);
                        }
                        else if (message.GetState() == Message.State.ERROR)
                        {
                            MessageBox.Show(Encoding.UTF8.GetString(content.ToArray()));
                        }
                        break;
                    }

                case Message.ID.UNREADY:
                    {
                        if (message.GetState() == Message.State.OK)
                        {
                            string username = Encoding.UTF8.GetString(content.ToArray());

                            if (username == UserLogin.Username)
                            {
                                this.isReady            = false;
                                btn_ReadyToggle.Content = "Ready";
                            }

                            SetReadyForUser(username, false);
                        }
                        else if (message.GetState() == Message.State.ERROR)
                        {
                            MessageBox.Show(Encoding.UTF8.GetString(content.ToArray()));
                        }
                        break;
                    }

                case Message.ID.CHAT_MESSAGE:
                    {
                        if (message.GetState() == Message.State.OK)
                        {
                            int messageLength  = content[0];
                            string chatMessage = Encoding.UTF8.GetString(content.GetRange(1, messageLength).ToArray());
                            string playerName  = Encoding.UTF8.GetString(content.GetRange(messageLength + 1, content.Count - (messageLength + 1)).ToArray());

                            txb_Chat.Text += $"{playerName}: {chatMessage}" + Environment.NewLine;
                            txb_Chat.ScrollToEnd();
                        }
                        else if (message.GetState() == Message.State.ERROR)
                        {
                            MessageBox.Show(Encoding.UTF8.GetString(content.ToArray()));
                        }
                        break;
                    }

                case Message.ID.REMOVE_SESSION:
                    {
                        if (message.GetState() == Message.State.OK)
                        {
                            MessageBox.Show("Host left session!");
                            GameBrowser gameBrowser = new GameBrowser(this.battleshipClient);
                            gameBrowser.Show();
                            this.Close();
                        }
                        break;
                    }

                case Message.ID.REMOVE_PLAYER:
                    {
                        if (message.GetState() == Message.State.OK)
                        {
                            string username = Encoding.UTF8.GetString(content.ToArray());

                            PlayerListItem existingControl = null;
                            foreach (PlayerListItem control in con_Players.Children)
                            {
                                if (control.Username == username)
                                {
                                    existingControl = control;
                                    break;
                                }
                            }

                            if (existingControl != null)
                            {
                                con_Players.Children.Remove(existingControl);
                                txb_Chat.Text += $"Player {username} left the session!" + Environment.NewLine;
                                txb_Chat.ScrollToEnd();
                            }
                        }
                        break;
                    }

                case Message.ID.START_GAME:
                    {
                        if (message.GetState() == Message.State.OK)
                        {
                            MainWindow gameWindow = new MainWindow(this.battleshipClient, this.sessionName, this.sessionId, this.isHost);
                            gameWindow.Show();
                            this.Close();
                        }
                        else
                        {
                            MessageBox.Show(Encoding.UTF8.GetString(content.ToArray()));
                        }
                        break;
                    }

                default:
                    {
                        break;
                    }
                }
            }));
        }