public void SendMessage(string Text, Player Speaker = null, bool Host = false) { if (CurrentRoom.IsHost(App.CurrentUser)) { foreach (var Client in App.Server) { Client.SendAsync(Encoding.UTF8.GetBytes("ChatMessage|*|" + (Speaker?.Id.ToString() ?? "System") + "|*|" + Text)); } } else { App.Client.SendAsync(Encoding.UTF8.GetBytes("ChatMessage|*|" + (Speaker?.Id.ToString() ?? "System") + "|*|" + Text)); } bool ScrollToBottom = ChatScrollViewer.HorizontalOffset >= ChatStack.ActualHeight - ChatScrollViewer.ActualHeight; ChatterItem Item = new ChatterItem(); Item.Init(Text, Speaker, Host); Item.Opacity = 0; ChatStack.Children.Add(Item); var HeightAnimation = new DoubleAnimation() { From = ChatStack.ActualHeight, To = ChatStack.Children.OfType <FrameworkElement>().Sum(O => O.Height), Duration = TimeSpan.FromSeconds(0.3), EasingFunction = new ExponentialEase() { EasingMode = EasingMode.EaseInOut } }; HeightAnimation.Completed += delegate { Item.BeginAnimation(OpacityProperty, new DoubleAnimation() { From = 0, To = 1, Duration = TimeSpan.FromSeconds(0.2), EasingFunction = new ExponentialEase() { EasingMode = EasingMode.EaseInOut } }); if (ScrollToBottom) { ChatScrollViewer.ScrollToBottom(); } }; ChatStack.BeginAnimation(HeightProperty, HeightAnimation); }
private void ParticipateChanged(object Sender, SocketEventArgs E) { string Data = Encoding.UTF8.GetString(E.Data); string[] Fragments = Data.Split(new[] { "|*|" }, StringSplitOptions.RemoveEmptyEntries); if (Fragments[0] != "ParticipateChanged") { return; } switch (Fragments[1]) { case "Add": CurrentRoom.AddParticipate(Guid.Parse(Fragments[2]), Fragments[3], int.Parse(Fragments[4])); this.Dispatcher.Invoke(() => { ChatWindow.SendMessage("玩家角色:" + CurrentRoom.GetPlayer(Guid.Parse(Fragments[2])) + "已加入队伍:" + Fragments[3] + "的" + (int.Parse(Fragments[4]) + 1) + "号位。"); GroupStack.Children.OfType <Components.GroupItem>().First(O => O.Group.Name == Fragments[3]) .ParticipantStack.Children.OfType <ParticipantItem>() .First(O => O.GroupIndex == int.Parse(Fragments[4])) .Join(CurrentRoom.GetPlayer(Guid.Parse(Fragments[2])), false); }); break; case "Remove": CurrentRoom.RemoveParticipate(Guid.Parse(Fragments[2])); this.Dispatcher.Invoke(() => { ChatWindow.SendMessage("玩家角色:" + CurrentRoom.GetPlayer(Guid.Parse(Fragments[2])) + "已退出队伍:" + Fragments[3] + "的" + (int.Parse(Fragments[4]) + 1) + "号位。"); GroupStack.Children.OfType <Components.GroupItem>().First(O => O.Group.Name == Fragments[3]) .ParticipantStack.Children.OfType <ParticipantItem>() .First(O => O.GroupIndex == int.Parse(Fragments[4])) .Join(null, false); }); break; } if (CurrentRoom.IsHost(App.CurrentUser)) { var From = E.Socket["User"] as User; foreach (var Client in App.Server .Where(Client => !From.Equals(Client["User"]))) { Client.SendAsync(E.Data); } } }