private void OnInviteFriends(object sender, RoutedEventArgs e)
 {
     if (conv == null)
     {
         var friends = playerDCListView.SelectedItems.Cast<PlayerDC>();
         var c = new ConversationDC()
         {
             Host = mainWindow.Player,
             Participants = friends
         };
         mainWindow.Server.CreateConversation(c);
     }
     else
     {
         //conversation already existes, add friends to conv...
     }
 }
 public static ConversationDC ConversationToConversationDC(Conversation conv)
 {
     ConversationDC convDC = null;
     using (var db = new BomberDbContext())
     {
         var q = (from co in db.Conversations
                   where co.ConversationId == conv.ConversationId
                   select co).ToList();
         if (q.Count > 0)
         {
             var c = q.ElementAt(0);
             convDC = new ConversationDC()
             {
                 ConversationId = c.ConversationId,
                 ConversationLog = c.ConversationLog,
                 Host = PlayerToPlayerDC(c.Host),
                 Participants = PlayersToPlayerDCs(c.Players)
             };
         }
     }
     return convDC;
 }
 public void SendConversationMessage(ConversationDC msgContainer)
 {
     if (msgContainer.NewMessageType == 1)
     {
         if (Conversations.Keys.Contains(msgContainer.ConversationId))
         {
             var cm = Conversations[msgContainer.ConversationId];
             cm.TriggerAudioMessageEvent(
                 msgContainer.NewAudioMessageBuffer,
                 msgContainer.NewAudioMessageBytesRecorded,
                 msgContainer.MessageSenderPlayerId);
         }
         return;
     }
     //using (var db = new BomberDbContext())
     //{
     //    var q = (from c in db.Conversations
     //             where c.ConversationId == msgContainer.ConversationId
     //             select c).ToList();
     //    if (q.Count > 0)
     //    {
     //        var conv = q.ElementAt(0);
     //        conv.ConversationLog += msgContainer.NewMessage + " \n";
     //        db.SaveChanges();
     //    }
     //}
     if (Conversations.Keys.Contains(msgContainer.ConversationId))
     {
         var cm = Conversations[msgContainer.ConversationId];
         cm.TriggerMessageEvent(msgContainer.NewMessage);
     }
 }
 private void OnPartyMsgAreaKeyDown(object sender, KeyEventArgs e)
 {
     if (e.Key == Key.Return && Keyboard.Modifiers == ModifierKeys.Shift)
     {
         _convMsgArea.Text += " \n";
         _convMsgArea.CaretIndex = _convMsgArea.Text.Length;
     }
     else if (e.Key == Key.Return)
     {
         DateTime utcTime = DateTime.SpecifyKind(DateTime.Now, DateTimeKind.Utc);
         string msg = utcTime.ToString();
         msg += String.Format(" [{0}] : ", mainWindow.Player.Pseudonym);
         msg += _convMsgArea.Text;
         var msgContainer = new ConversationDC()
         {
             ConversationId = Party.ConversationId,
             NewMessage = msg,
             NewMessageType = 0
         };
         mainWindow.Server.SendConversationMessage(msgContainer);
         _convMsgArea.Text = "";
         _convLogScroller.ScrollToEnd();
     }
 }
 public void KillPlayer(ConversationDC conv, PlayerDC player)
 {
     Game.GameContentManager.KillPlayer(player.Login);
 }
 public void UpdatePlayerPosition(ConversationDC conv, PlayerDC player)
 {
     Game.GameContentManager.UpdatePlayerPosition(player.Login, player.PlayerPositionX, player.PlayerPositionY);
 }
 public void RemovePlayerFromParty(PlayerDC player, ConversationDC conv)
 {
     if (player.PlayerId == Player.PlayerId)
     {
         DropConversation(conv);
         return;
     }
     var selectedConv = conversationDCListView.SelectedItem as ConversationDC;
     var q = (from c in conversations where c.ConversationId == conv.ConversationId select c).ToList();
     if (q.Count > 0)
     {
         var c = q.ElementAt(0);
         var q1 = (from p in c.Participants where p.PlayerId == player.PlayerId select p).ToList();
         if (q1.Count > 0)
         {
             c.Participants = c.Participants.ToList();
             ((List<PlayerDC>)c.Participants).Remove(q1.ElementAt(0));
             c.ParticipantsNames = " " + c.Host.Pseudonym + " (Host) \n ";
             foreach (var p in c.Participants)
             {
                 c.ParticipantsNames += p.Pseudonym + ", ";
             }
             c.ParticipantsNames = c.ParticipantsNames.Remove(c.ParticipantsNames.Length - 2);
             partyPage.LoadConversation();
             ConversationsListViewSource.View.Refresh();
         }
     }
 }
 public void ReceiveConversationMessage(ConversationDC c)
 {
     if (c.NewMessageType == 1 && partyPage != null)
     {
         if (LoudSpeakerEnabled && c.MessageSenderPlayerId != Player.PlayerId)
             partyPage.waveProvider.AddSamples(c.NewAudioMessageBuffer, 0, c.NewAudioMessageBytesRecorded);
         return;
     }
     var q = (from co in conversations
              where co.ConversationId == c.ConversationId
              select co).ToList();
     if (q.Count > 0)
     {
         var conv = q.ElementAt(0);
         conv.ConversationLog += c.NewMessage + " \n";
         partyPage.LoadConversation();
     }
 }
 private void KickFromConvEventHandler(PlayerDC player, ConversationDC conv)
 {
     //TODO
 }
 private void JoinConvEventHandler(PlayerDC player, ConversationDC conv)
 {
     //TODO
 }
 private void LeaveConvEventHandler(PlayerDC player, ConversationDC conv)
 {
     Callback.RemovePlayerFromParty(player, conv);
 }
 private void ConvMessageEventHandler(ConversationDC conv)
 {
     var c = new ConversationDC()
     {
         ConversationId = conv.ConversationId,
         NewMessage = conv.NewMessage
     };
     Callback.ReceiveConversationMessage(c);
 }
 public void SwitchMainConversation(ConversationDC conversationDC)
 {
     if (Conversations.Keys.Contains(conversationDC.ConversationId))
     {
         var cm = Conversations[conversationDC.ConversationId];
         PlayerDC p = null;
         if (cm.Conv.Host.PlayerId == Player.PlayerId)
             p = cm.Conv.Host;
         else
         {
             var q = (from m in cm.Conv.Participants where m.PlayerId == Player.PlayerId select m).ToList();
             if (q.Count > 0)
                 p = q.ElementAt(0);
         }
         if (p != null)
         {
             var lastMainRoomId = p.MainRoomId;
             if (Conversations.Keys.Contains(lastMainRoomId))
             {
                 var cm1 = Conversations[lastMainRoomId];
                 cm1.AudioMessageEvent -= AudioMessageEventHandler;
             }
             cm.AudioMessageEvent += AudioMessageEventHandler;
             foreach (var cm2 in Conversations)
             {
                 PlayerDC thisPlayerPossibleOccurence = null;
                 if (cm2.Value.Conv.Host.PlayerId == Player.PlayerId)
                     thisPlayerPossibleOccurence = cm2.Value.Conv.Host;
                 else
                 {
                     var q1 = (from member in cm2.Value.Conv.Participants where member.PlayerId == Player.PlayerId select member).ToList();
                     if (q1.Count > 0)
                         thisPlayerPossibleOccurence = q1.ElementAt(0);
                 }
                 if (thisPlayerPossibleOccurence != null)
                     thisPlayerPossibleOccurence.MainRoomId = conversationDC.ConversationId;
             }
         }
     }
 }
 public void LeaveConversation(ConversationDC Party)
 {
     using (var db = new BomberDbContext())
     {
         var q = (from c in db.Conversations
                  where c.ConversationId == Party.ConversationId
                  select c).ToList();
         var q1 = (from p in db.Players
                   where p.PlayerId == Player.PlayerId
                   select p).ToList();
         if (q.Count > 0 && q1.Count > 0)
         {
             var conv = q.ElementAt(0);
             var thisPlayer = q1.ElementAt(0);
             conv.Players.Remove(thisPlayer);
             var remainingPlayers = conv.Players.Count;
             if (remainingPlayers == 0)
             {
                 db.Conversations.DeleteObject(conv);
                 if (Conversations.Keys.Contains(Party.ConversationId))
                 {
                     var cm = Conversations[Party.ConversationId];
                     cm.TriggerEndOfConvEvent();
                     Conversations.Remove(Party.ConversationId);
                 }
             }
             else if (Conversations.Keys.Contains(Party.ConversationId))
             {
                 var cm = Conversations[Party.ConversationId];
                 var q2 = (from p in cm.Conv.Participants
                           where p.PlayerId == Player.PlayerId
                           select p).ToList();
                 if (q2.Count > 0)
                 {
                     var p = q2.ElementAt(0);
                     cm.Conv.Participants = cm.Conv.Participants.ToList();
                     ((List<PlayerDC>)cm.Conv.Participants).Remove(p);
                     cm.TriggerLeaveEvent(new PlayerDC() { PlayerId = Player.PlayerId });
                     cm.EndOfConvEvent -= EndOfConvEventHandler;
                     cm.JoinEvent -= JoinConvEventHandler;
                     cm.KickEvent -= KickFromConvEventHandler;
                     cm.LeaveEvent -= LeaveConvEventHandler;
                     cm.MessageEvent -= ConvMessageEventHandler;
                 }
             }
             db.SaveChanges();
         }
     }
 }
 public void DeleteConversation(ConversationDC Party)
 {
     using (var db = new BomberDbContext())
     {
         var q = (from c in db.Conversations
                  where c.ConversationId == Party.ConversationId
                  select c).ToList();
         if (q.Count > 0)
         {
             var convToDrop = q.ElementAt(0);
             convToDrop.Players.Clear();
             db.Conversations.DeleteObject(convToDrop);
             db.SaveChanges();
             if (Conversations.Keys.Contains(Party.ConversationId))
             {
                 var cm = Conversations[Party.ConversationId];
                 cm.TriggerEndOfConvEvent();
                 Conversations.Remove(Party.ConversationId);
             }
         }
     }
 }
 public ConversationManager(ConversationDC conv)
 {
     Conv = conv;
 }
 public void InitializeConversation(ConversationDC conversationDC)
 {
     conversationDC.ParticipantsNames = " " + conversationDC.Host.Pseudonym + " (Host) \n ";
     foreach (var p in conversationDC.Participants)
     {
         conversationDC.ParticipantsNames += p.Pseudonym + ", ";
     }
     conversationDC.ParticipantsNames = conversationDC.ParticipantsNames.Remove(conversationDC.ParticipantsNames.Length - 2);
     conversationDC.NewMessage = "";
     var q = (from co in conversations
              where co.ConversationId == conversationDC.ConversationId
              select co).ToList();
     if (q.Count != 0)
     {
         foreach (var co in q)
         {
             ((List<ConversationDC>)conversations).Remove(co);
         }
     }
     ((List<ConversationDC>)conversations).Add(conversationDC);
     ConversationsListViewSource.Source = conversations;
     ConversationsListViewSource.View.Refresh();
 }
 private void EndOfConvEventHandler(ConversationDC conv)
 {
     Callback.DropConversation(conv);
 }
 public void DropConversation(ConversationDC conv)
 {
     var selectedConv = conversationDCListView.SelectedItem as ConversationDC;
     var q = (from c in conversations where c.ConversationId == conv.ConversationId select c).ToList();
     if (q.Count > 0)
     {
         var c = q.ElementAt(0);
         ((List<ConversationDC>)conversations).Remove(c);
         ConversationsListViewSource.Source = conversations;
         ConversationsListViewSource.View.Refresh();
         if (selectedConv != null && selectedConv != c)
         {
             conversationDCListView.SelectedItem = selectedConv;
             partyPage.LoadConversation(selectedConv);
         }
         if (_contentFrame.Content == partyPage && partyPage.Party == c)
             _contentFrame.Content = null;
     }
 }
 private void AudioMessageEventHandler(ConversationDC conv)
 {
     Callback.ReceiveConversationMessage(conv);
 }
 public void OpenGameWindow(ConversationDC conv)
 {
     List<string> allPlayers = new List<string>();
     allPlayers.Add(conv.Host.Login);
     var logins = (from p in conv.Participants select p.Login).ToList();
     allPlayers = allPlayers.Concat<string>(logins).ToList();
     GameLaunching(allPlayers, conv.ConversationId);
 }
 public void GameStartEventHandler(ConversationDC conv)
 {
     Callback.OpenGameWindow(conv);
 }
 public void SpawnBomb(ConversationDC conv, int BombPositionX, int BombPositionY)
 {
     Game.GameContentManager.SpawnBomb(BombPositionX, BombPositionY);
 }
 public void PlayerPositionReplicationEventHandler(ConversationDC conv, PlayerDC player)
 {
     Callback.UpdatePlayerPosition(conv, player);
 }
 public void LoadConversation(ConversationDC conv = null)
 {
     if (conv != null)
         Party = conv;
     PartyListViewSource.Source = new List<ConversationDC>() { Party };
     PartyListViewSource.View.Refresh();
     if (Party.Host.PlayerId == mainWindow.Player.PlayerId)
     {
         _delConvBtn.Visibility = System.Windows.Visibility.Visible;
         _inviteBtn.Visibility = System.Windows.Visibility.Visible;
         _kickBtn.Visibility = System.Windows.Visibility.Visible;
         _leaveBtn.Visibility = System.Windows.Visibility.Hidden;
         _playBtn.IsEnabled = true;
     }
     else
     {
         _delConvBtn.Visibility = System.Windows.Visibility.Hidden;
         _inviteBtn.Visibility = System.Windows.Visibility.Hidden;
         _kickBtn.Visibility = System.Windows.Visibility.Hidden;
         _leaveBtn.Visibility = System.Windows.Visibility.Visible;
         _playBtn.IsEnabled = false;
     }
     if (mainWindow.Player.MainRoomId == Party.ConversationId)
         _mainRoomBtn.IsEnabled = false;
     else
         _mainRoomBtn.IsEnabled = true;
 }
 public void BombSpawnReplicationEventHandler(ConversationDC conv, int BombPositionX, int BombPositionY)
 {
     Callback.SpawnBomb(conv, BombPositionX, BombPositionY);
 }
 public void PlayerDeathReplicationEventHandler(ConversationDC conv, PlayerDC player)
 {
     Callback.KillPlayer(conv, player);
 }
 public void CreateConversation(ConversationDC newConv)
 {
     using (var db = new BomberDbContext())
     {
         var q = (from p in db.Players
                  where p.PlayerId == newConv.Host.PlayerId
                  select p).ToList();
         var convMembersIds = from i in newConv.Participants
                              select i.PlayerId;
         var convMembers = (from p in db.Players
                            where convMembersIds.Contains(p.PlayerId)
                            select p);
         if (q.Count > 0)
         {
             var host = q.ElementAt(0);
             Conversation c = new Conversation()
             {
                 Host = host,
                 ConversationLog = ""
             };
             // discussion : add the host to c.Players or not? let's say no, for now...
             foreach (var p in convMembers)
             {
                 if (!c.Players.Contains(p))
                     c.Players.Add(p);
             }
             db.Conversations.AddObject(c);
             db.SaveChanges();
             var convDC = EntityContractTranslator.ConversationToConversationDC(c);
             var cm = new ConversationManager(convDC);
             Conversations.Add(convDC.ConversationId, cm);
             if (Sessions.Keys.Contains(host.PlayerId))
             {
                 var s = Sessions[host.PlayerId];
                 cm.EndOfConvEvent += s.EndOfConvEventHandler;
                 cm.JoinEvent += s.JoinConvEventHandler;
                 cm.KickEvent += s.KickFromConvEventHandler;
                 cm.LeaveEvent += s.LeaveConvEventHandler;
                 cm.MessageEvent += s.ConvMessageEventHandler;
                 s.Callback.InitializeConversation(convDC);
             }
             foreach (var p in convMembers)
             {
                 if (Sessions.Keys.Contains(p.PlayerId))
                 {
                     var s = Sessions[p.PlayerId];
                     cm.EndOfConvEvent += s.EndOfConvEventHandler;
                     cm.JoinEvent += s.JoinConvEventHandler;
                     cm.KickEvent += s.KickFromConvEventHandler;
                     cm.LeaveEvent += s.LeaveConvEventHandler;
                     cm.MessageEvent += s.ConvMessageEventHandler;
                     s.Callback.InitializeConversation(convDC);
                 }
             }
         }
     }
 }