Пример #1
0
 public void Broadcast(BroadcastPacket packet)
 {
     try
     {
         SpreadBroadcastpacket(packet);
     }
     catch (Exception ex)
     {
         Logger.Error(ex);
     }
 }
Пример #2
0
        private void SpreadBroadcastpacket(BroadcastPacket sentPacket)
        {
            if (Sessions == null || string.IsNullOrEmpty(sentPacket?.Packet))
            {
                return;
            }

            switch (sentPacket.Receiver)
            {
                case ReceiverType.All: // send packet to everyone
                    if (sentPacket.Packet.StartsWith("out"))
                    {
                        foreach (ClientSession session in Sessions)
                        {
                            if (!session.HasSelectedCharacter)
                            {
                                continue;
                            }

                            if (sentPacket.Sender != null)
                            {
                                if (!sentPacket.Sender.Character.IsBlockedByCharacter(session.Character.CharacterId))
                                {
                                    session.SendPacket(sentPacket.Packet);
                                }
                            }
                            else
                            {
                                session.SendPacket(sentPacket.Packet);
                            }
                        }
                    }
                    else
                    {
                        Parallel.ForEach(Sessions, session =>
                        {
                            if (!session.HasSelectedCharacter)
                            {
                                return;
                            }

                            if (sentPacket.Sender != null)
                            {
                                if (!sentPacket.Sender.Character.IsBlockedByCharacter(session.Character.CharacterId))
                                {
                                    session.SendPacket(sentPacket.Packet);
                                }
                            }
                            else
                            {
                                session.SendPacket(sentPacket.Packet);
                            }
                        });
                    }

                    break;
                case ReceiverType.AllExceptMeAct4:
                    if (sentPacket.Sender == null)
                    {
                        return;
                    }

                    foreach (ClientSession session in Sessions.Where(s =>
                        s.SessionId != sentPacket.Sender.SessionId &&
                        s.Character.Faction == sentPacket.Sender.Character.Faction && s.HasSelectedCharacter))
                    {
                        session.SendPacket(sentPacket.Packet);
                    }

                    break;
                case ReceiverType.AllExceptMe: // send to everyone except the sender
                    if (sentPacket.Packet.StartsWith("out"))
                    {
                        foreach (ClientSession session in Sessions.Where(
                            s => s.SessionId != sentPacket.Sender.SessionId))
                        {
                            if (!session.HasSelectedCharacter)
                            {
                                continue;
                            }

                            if (sentPacket.Sender != null)
                            {
                                if (!sentPacket.Sender.Character.IsBlockedByCharacter(session.Character.CharacterId))
                                {
                                    session.SendPacket(sentPacket.Packet);
                                }
                            }
                            else
                            {
                                session.SendPacket(sentPacket.Packet);
                            }
                        }
                    }
                    else
                    {
                        Parallel.ForEach(Sessions.Where(s => s.SessionId != sentPacket.Sender.SessionId), session =>
                        {
                            if (!session.HasSelectedCharacter)
                            {
                                return;
                            }

                            if (sentPacket.Sender != null)
                            {
                                if (!sentPacket.Sender.Character.IsBlockedByCharacter(session.Character.CharacterId))
                                {
                                    session.SendPacket(sentPacket.Packet);
                                }
                            }
                            else
                            {
                                session.SendPacket(sentPacket.Packet);
                            }
                        });
                    }

                    break;
                case ReceiverType.AllExceptGroup:
                    foreach (ClientSession session in Sessions.Where(s =>
                        s.SessionId != sentPacket.Sender.SessionId &&
                        (s.Character?.Group == null ||
                            s.Character?.Group?.GroupId != sentPacket.Sender?.Character?.Group?.GroupId)))
                    {
                        if (!session.HasSelectedCharacter)
                        {
                            continue;
                        }

                        if (sentPacket.Sender != null)
                        {
                            if (!sentPacket.Sender.Character.IsBlockedByCharacter(session.Character.CharacterId))
                            {
                                session.SendPacket(sentPacket.Packet);
                            }
                        }
                        else
                        {
                            session.SendPacket(sentPacket.Packet);
                        }
                    }

                    break;
                case ReceiverType.AllInRange: // send to everyone which is in a range of 50x50
                    if (sentPacket.XCoordinate != 0 && sentPacket.YCoordinate != 0)
                    {
                        Parallel.ForEach(
                            Sessions.Where(s => s.Character.IsInRange(sentPacket.XCoordinate, sentPacket.YCoordinate)),
                            session =>
                            {
                                if (!session.HasSelectedCharacter)
                                {
                                    return;
                                }

                                if (sentPacket.Sender != null)
                                {
                                    if (!sentPacket.Sender.Character.IsBlockedByCharacter(session.Character.CharacterId)
                                    )
                                    {
                                        session.SendPacket(sentPacket.Packet);
                                    }
                                }
                                else
                                {
                                    session.SendPacket(sentPacket.Packet);
                                }
                            });
                    }

                    break;

                case ReceiverType.OnlySomeone:
                    if (sentPacket.SomeonesCharacterId > 0 || !string.IsNullOrEmpty(sentPacket.SomeonesCharacterName))
                    {
                        ClientSession targetSession = Sessions.SingleOrDefault(s =>
                            s.Character.CharacterId == sentPacket.SomeonesCharacterId ||
                            s.Character.Name == sentPacket.SomeonesCharacterName);
                        if (targetSession != null && targetSession.HasSelectedCharacter)
                        {
                            if (sentPacket.Sender != null)
                            {
                                if (!sentPacket.Sender.Character.IsBlockedByCharacter(targetSession.Character
                                    .CharacterId))
                                {
                                    targetSession.SendPacket(sentPacket.Packet);
                                }
                                else
                                {
                                    sentPacket.Sender.SendPacket(
                                        UserInterfaceHelper.Instance.GenerateInfo(
                                            Language.Instance.GetMessageFromKey("BLACKLIST_BLOCKED")));
                                }
                            }
                            else
                            {
                                targetSession.SendPacket(sentPacket.Packet);
                            }
                        }
                    }

                    break;

                case ReceiverType.AllNoEmoBlocked:
                    Parallel.ForEach(Sessions.Where(s => !s.Character.EmoticonsBlocked), session =>
                    {
                        if (!session.HasSelectedCharacter)
                        {
                            return;
                        }

                        if (!sentPacket.Sender.Character.IsBlockedByCharacter(session.Character.CharacterId))
                        {
                            session.SendPacket(sentPacket.Packet);
                        }
                    });
                    break;

                case ReceiverType.AllNoHeroBlocked:
                    Parallel.ForEach(Sessions.Where(s => !s.Character.HeroChatBlocked), session =>
                    {
                        if (!session.HasSelectedCharacter)
                        {
                            return;
                        }

                        if (!sentPacket.Sender.Character.IsBlockedByCharacter(session.Character.CharacterId))
                        {
                            session.SendPacket(sentPacket.Packet);
                        }
                    });
                    break;

                case ReceiverType.Group:
                    Parallel.ForEach(
                        Sessions.Where(s =>
                            s.Character?.Group != null && sentPacket.Sender?.Character?.Group != null &&
                            s.Character.Group.GroupId == sentPacket.Sender.Character.Group.GroupId),
                        session => { session.SendPacket(sentPacket.Packet); });
                    break;

                case ReceiverType.Unknown:
                    break;
            }
        }
        private void SpreadBroadcastpacket(BroadcastPacket sentPacket)
        {
            if (Sessions != null && !string.IsNullOrEmpty(sentPacket?.Packet))
            {
                switch (sentPacket.Receiver)
                {
                case ReceiverType.All:     // send packet to everyone
                    if (sentPacket.Packet.StartsWith("out", StringComparison.CurrentCulture))
                    {
                        foreach (ClientSession session in Sessions)
                        {
                            if (session.HasSelectedCharacter)
                            {
                                if (sentPacket.Sender != null)
                                {
                                    if (!sentPacket.Sender.Character.IsBlockedByCharacter(session.Character.CharacterId))
                                    {
                                        session.SendPacket(sentPacket.Packet);
                                    }
                                }
                                else
                                {
                                    session.SendPacket(sentPacket.Packet);
                                }
                            }
                        }
                    }
                    else
                    {
                        Parallel.ForEach(Sessions, session =>
                        {
                            if (session?.HasSelectedCharacter == true)
                            {
                                if (sentPacket.Sender != null)
                                {
                                    if (!sentPacket.Sender.Character.IsBlockedByCharacter(session.Character.CharacterId))
                                    {
                                        session.SendPacket(sentPacket.Packet);
                                    }
                                }
                                else
                                {
                                    session.SendPacket(sentPacket.Packet);
                                }
                            }
                        });
                    }
                    break;

                case ReceiverType.AllExceptMe:     // send to everyone except the sender
                    if (sentPacket.Packet.StartsWith("out", StringComparison.CurrentCulture))
                    {
                        foreach (ClientSession session in Sessions.Where(s => s?.SessionId != sentPacket.Sender?.SessionId))
                        {
                            if (session.HasSelectedCharacter)
                            {
                                if (sentPacket.Sender != null)
                                {
                                    if (!sentPacket.Sender.Character.IsBlockedByCharacter(session.Character.CharacterId))
                                    {
                                        session.SendPacket(sentPacket.Packet);
                                    }
                                }
                                else
                                {
                                    session.SendPacket(sentPacket.Packet);
                                }
                            }
                        }
                    }
                    else
                    {
                        Parallel.ForEach(Sessions.Where(s => s?.SessionId != sentPacket.Sender?.SessionId), session =>
                        {
                            if (session?.HasSelectedCharacter == true)
                            {
                                if (sentPacket.Sender != null)
                                {
                                    if (!sentPacket.Sender.Character.IsBlockedByCharacter(session.Character.CharacterId))
                                    {
                                        session.SendPacket(sentPacket.Packet);
                                    }
                                }
                                else
                                {
                                    session.SendPacket(sentPacket.Packet);
                                }
                            }
                        });
                    }
                    break;

                case ReceiverType.AllExceptGroup:
                    if (sentPacket.Packet.StartsWith("out", StringComparison.CurrentCulture))
                    {
                        foreach (ClientSession session in Sessions.Where(s => s.SessionId != sentPacket.Sender.SessionId))
                        {
                            if (session.HasSelectedCharacter)
                            {
                                if (sentPacket.Sender != null)
                                {
                                    if (!sentPacket.Sender.Character.IsBlockedByCharacter(session.Character.CharacterId))
                                    {
                                        session.SendPacket(sentPacket.Packet);
                                    }
                                }
                                else
                                {
                                    session.SendPacket(sentPacket.Packet);
                                }
                            }
                        }
                    }
                    else
                    {
                        foreach (ClientSession session in Sessions.Where(s => s.SessionId != sentPacket.Sender.SessionId && (s.Character?.Group == null || (s.Character?.Group?.GroupId != sentPacket.Sender?.Character?.Group?.GroupId))))
                        {
                            if (session.HasSelectedCharacter && !sentPacket.Sender.Character.IsBlockedByCharacter(session.Character.CharacterId))
                            {
                                session.SendPacket(sentPacket.Packet);
                            }
                        }
                    }
                    break;

                case ReceiverType.AllExceptMeAct4:     // send to everyone except the sender(Act4)
                    Parallel.ForEach(Sessions.Where(s => s.SessionId != sentPacket.Sender.SessionId), session =>
                    {
                        if (session?.HasSelectedCharacter == true)
                        {
                            if (sentPacket.Sender != null)
                            {
                                if (!sentPacket.Sender.Character.IsBlockedByCharacter(session.Character.CharacterId))
                                {
                                    if (session.Character.Faction == sentPacket.Sender.Character.Faction)
                                    {
                                        session.SendPacket(sentPacket.Packet);
                                    }
                                    // ReSharper disable once RedundantIfElseBlock
                                    else
                                    {
                                        // TODO: Scrambled Packet for Act4
                                    }
                                }
                            }
                            else
                            {
                                session.SendPacket(sentPacket.Packet);
                            }
                        }
                    });
                    break;

                case ReceiverType.AllInRange:     // send to everyone which is in a range of 50x50
                    if (sentPacket.XCoordinate != 0 && sentPacket.YCoordinate != 0)
                    {
                        Parallel.ForEach(Sessions.Where(s => s?.Character.IsInRange(sentPacket.XCoordinate, sentPacket.YCoordinate) == true), session =>
                        {
                            if (session?.HasSelectedCharacter == true)
                            {
                                if (sentPacket.Sender != null)
                                {
                                    if (!sentPacket.Sender.Character.IsBlockedByCharacter(session.Character.CharacterId))
                                    {
                                        session.SendPacket(sentPacket.Packet);
                                    }
                                }
                                else
                                {
                                    session.SendPacket(sentPacket.Packet);
                                }
                            }
                        });
                    }
                    break;

                case ReceiverType.OnlySomeone:
                    if (sentPacket.SomeonesCharacterId > 0 || !string.IsNullOrEmpty(sentPacket.SomeonesCharacterName))
                    {
                        ClientSession targetSession = Sessions.SingleOrDefault(s => s.Character.CharacterId == sentPacket.SomeonesCharacterId || s.Character.Name == sentPacket.SomeonesCharacterName);
                        if (targetSession?.HasSelectedCharacter == true)
                        {
                            if (sentPacket.Sender != null)
                            {
                                if (!sentPacket.Sender.Character.IsBlockedByCharacter(targetSession.Character.CharacterId))
                                {
                                    targetSession.SendPacket(sentPacket.Packet);
                                }
                                else
                                {
                                    sentPacket.Sender.SendPacket(UserInterfaceHelper.GenerateInfo(Language.Instance.GetMessageFromKey("BLACKLIST_BLOCKED")));
                                }
                            }
                            else
                            {
                                targetSession.SendPacket(sentPacket.Packet);
                            }
                        }
                    }
                    break;

                case ReceiverType.AllNoEmoBlocked:
                    Parallel.ForEach(Sessions.Where(s => s?.Character.EmoticonsBlocked == false), session =>
                    {
                        if (session?.HasSelectedCharacter == true && sentPacket.Sender?.Character.IsBlockedByCharacter(session.Character.CharacterId) == false)
                        {
                            session.SendPacket(sentPacket.Packet);
                        }
                    });
                    break;

                case ReceiverType.AllNoHeroBlocked:
                    Parallel.ForEach(Sessions.Where(s => s?.Character.HeroChatBlocked == false), session =>
                    {
                        if (session?.HasSelectedCharacter == true && sentPacket.Sender?.Character.IsBlockedByCharacter(session.Character.CharacterId) == false)
                        {
                            session.SendPacket(sentPacket.Packet);
                        }
                    });
                    break;

                case ReceiverType.Group:
                    foreach (ClientSession session in Sessions.Where(s => s.Character?.Group != null && sentPacket.Sender?.Character?.Group != null && s.Character.Group.GroupId == sentPacket.Sender.Character.Group.GroupId))
                    {
                        session.SendPacket(sentPacket.Packet);
                    }
                    break;

                case ReceiverType.Unknown:
                    break;
                }
            }
        }