コード例 #1
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.SESSIONDATA:
                    {
                        string sessionId   = Encoding.UTF8.GetString(content.GetRange(0, 32).ToArray());
                        string sessionName = Encoding.UTF8.GetString(content.GetRange(32, content.Count - 33).ToArray());
                        //int playerCount = int.Parse(Encoding.UTF8.GetString(content.GetRange(content.Count - 1, 1).ToArray()));
                        int playerCount = content.Last();

                        SessionListItem sessionListItem = new SessionListItem(JoinSession);
                        sessionListItem.SessionName     = sessionName;
                        sessionListItem.SessionId       = sessionId;
                        sessionListItem.PlayerCount     = playerCount;
                        sessionListItem.Foreground      = Brushes.White;

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

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

                        SessionListItem existingControl = null;
                        foreach (SessionListItem control in con_Sessions.Children)
                        {
                            if (control.SessionId == sessionId)
                            {
                                control.PlayerCount = playerCount;
                                existingControl     = control;
                                break;
                            }
                        }

                        if (existingControl != null)
                        {
                            existingControl = sessionListItem;
                        }
                        else
                        {
                            con_Sessions.Children.Add(sessionListItem);
                        }

                        break;
                    }

                case Message.ID.ADD_SESSION:
                    {
                        if (message.GetState() == Message.State.OK)
                        {
                            ShowLobby(txb_SessionName.Text, true);
                        }
                        else if (message.GetState() == Message.State.ERROR)
                        {
                            txb_SessionName.IsEnabled = true;
                            btn_Refresh.IsEnabled     = true;
                            btn_Logout.IsEnabled      = true;
                            btn_HostSession.IsEnabled = true;
                            MessageBox.Show(Encoding.UTF8.GetString(content.ToArray()));
                        }
                        break;
                    }

                case Message.ID.JOIN_SESSION:
                    {
                        if (message.GetState() == Message.State.OK)
                        {
                            ShowLobby(Encoding.UTF8.GetString(content.ToArray()), false);
                        }
                        else if (message.GetState() == Message.State.ERROR)
                        {
                            this.joinSessionIdCache   = "";
                            txb_SessionName.IsEnabled = true;
                            btn_Refresh.IsEnabled     = true;
                            btn_Logout.IsEnabled      = true;
                            btn_HostSession.IsEnabled = true;
                            MessageBox.Show(Encoding.UTF8.GetString(content.ToArray()));
                        }
                        break;
                    }

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

                            SessionListItem existingControl = null;
                            foreach (SessionListItem control in con_Sessions.Children)
                            {
                                if (control.SessionId == sessionId)
                                {
                                    existingControl = control;
                                    break;
                                }
                            }

                            if (existingControl != null)
                            {
                                con_Sessions.Children.Remove(existingControl);
                            }
                        }
                        break;
                    }

                default:
                    {
                        break;
                    }
                }
            }));
        }
コード例 #2
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;
                    }
                }
            }));
        }