Example #1
0
 private BuddyList(byte cap, Character own)
 {
     Capacity      = cap;
     Owner         = new BuddyData(own.ID, own.Name);
     Buddies       = new Dictionary <int, BuddyData>(Capacity);
     BuddyRequests = new Queue <BuddyData>();
 }
Example #2
0
        public void SendBuddyList()
        {
            Packet pw = new Packet(ServerMessages.FRIEND_RESULT);

            pw.WriteByte(0x07);
            pw.WriteByte((byte)(Buddies.Values.Count + Math.Min(1, BuddyRequests.Count)));

            Buddies.Values.ForEach(bud =>
            {
                pw.WriteInt(bud.charId);
                pw.WriteString(bud.charName, 13);
                pw.WriteByte(0);
                pw.WriteInt(Owner.IsVisibleTo(bud) == true ? bud.GetChannel() : -1);
            });

            if (BuddyRequests.Count != 0)
            {
                BuddyData data = BuddyRequests.Peek();
                pw.WriteInt(data.charId);
                pw.WriteString(data.charName, 13);
                pw.WriteByte(1);
                pw.WriteInt(data.GetChannel());
            }

            Enumerable.Range(0, (Buddies.Values.Count + BuddyRequests.Count())).ForEach(e => pw.WriteInt(0));
            Owner.SendPacket(pw);
        }
Example #3
0
 public void PollRequests()
 {
     if (BuddyRequests.Count != 0)
     {
         BuddyData requestor = BuddyRequests.Peek();
         SendInviteFrom(requestor);
     }
 }
Example #4
0
        private void SendUpdate(BuddyData buddy, bool dc)
        {
            Packet pw = new Packet(ServerMessages.FRIEND_RESULT);

            pw.WriteByte(20);
            pw.WriteInt(buddy.charId);
            pw.WriteByte(0); // 0 = not in cash shop, 1 = in cash shop
            pw.WriteInt(dc == true ? -1 : buddy.GetChannel());
            Owner.SendPacket(pw);
        }
Example #5
0
        public void Add(BuddyData buddy, Boolean packet = true)
        {
            if (!HasBuddy(buddy) && !IsFull())
            {
                Buddies[buddy.charId] = buddy;

                if (packet)
                {
                    SendBuddyList();
                }
            }
        }
Example #6
0
 public void AcceptRequest()
 {
     if (BuddyRequests.Count != 0)
     {
         BuddyData requestor = BuddyRequests.Dequeue();
         Add(requestor);
         if (requestor.GetBuddyList() != null)
         {
             requestor.GetBuddyList().SendUpdate(Owner, false);
         }
         PollRequests();
     }
 }
Example #7
0
        private void SendInviteFrom(BuddyData from)
        {
            Packet pw = new Packet(ServerMessages.FRIEND_RESULT);

            pw.WriteByte(9);
            pw.WriteInt(from.charId);
            pw.WriteString(from.charName);
            pw.WriteInt(from.charId);
            pw.WriteString(from.charName, 13);
            pw.WriteByte(1);
            pw.WriteInt(from.GetChannel());
            pw.WriteByte(0);
            Owner.SendPacket(pw);
        }
Example #8
0
 public void RemoveBuddyOrRequest(Character Victim, int victimId)
 {
     if (BuddyRequests.Count != 0 && BuddyRequests.Peek().charId == victimId)
     {
         BuddyData requestor = BuddyRequests.Dequeue();
         //requestor.GetBuddyList().SendBuddyList();
         PollRequests();
         //SendBuddyList();
     }
     else
     {
         Buddies.Remove(victimId);
         SendBuddyList();
         if (Victim != null)
         {
             Victim.FriendsList.SendBuddyList(); //TODO test sending Update. Sometimes we send full buddylist when it seems like only an update is necessary
         }
     }
 }
Example #9
0
        public void SaveBuddiesToDb()
        {
            //BUDDY LIST
            CenterServer.Instance.CharacterDatabase.RunTransaction(comm =>
            {
                comm.CommandText = "DELETE FROM buddylist WHERE charid = @charid";
                comm.Parameters.AddWithValue("@charid", Owner.charId);
                comm.ExecuteNonQuery();

                foreach (var buddyEntry in Buddies)
                {
                    comm.Parameters.Clear();
                    comm.CommandText = "INSERT INTO buddylist (charid, buddy_charid, buddy_charname) VALUES (@ownerCharId, @charId, @charname)";
                    comm.Parameters.AddWithValue("@ownerCharId", Owner.charId);
                    comm.Parameters.AddWithValue("@charId", buddyEntry.Value.charId);
                    comm.Parameters.AddWithValue("@charname", buddyEntry.Value.charName);
                    comm.ExecuteNonQuery();
                }
            }, Program.MainForm.LogAppend);

            //PENDING REQUESTS
            CenterServer.Instance.CharacterDatabase.RunTransaction(comm =>
            {
                comm.CommandText = "DELETE FROM buddylist_pending WHERE charid = @charid";
                comm.Parameters.AddWithValue("@charid", Owner.charId);
                comm.ExecuteNonQuery();

                while (BuddyRequests.Count > 0)
                {
                    BuddyData requestor = BuddyRequests.Dequeue();

                    comm.Parameters.Clear();
                    comm.CommandText = "INSERT INTO buddylist_pending (charid, inviter_charid, inviter_charname) VALUES (@charid, @inviterCharId, @inviterCharName)";
                    comm.Parameters.AddWithValue("@charid", Owner.charId);
                    comm.Parameters.AddWithValue("@inviterCharId", requestor.charId);
                    comm.Parameters.AddWithValue("@inviterCharName", requestor.charName);
                    comm.ExecuteNonQuery();
                }
            }, Program.MainForm.LogAppend);
        }
Example #10
0
        public void Request(BuddyData requestor)
        {
            if (requestor.GetBuddyList().IsFull())
            {
                log.Warn($"[{Owner.charName}] Buddylist invite failed: its full");
                requestor.GetBuddyList().SendRequestError(11);
                return;
            }

            if (Owner.GetChar().IsGM&& requestor.GetChar().IsGM == false)
            {
                log.Warn($"[{Owner.charName}] Buddylist invite failed: nonadmin to admin invite");
                requestor.GetBuddyList().SendRequestError(14);
                return;
            }

            if (HasBuddy(requestor))
            {
                log.Warn($"[{Owner.charName}] Buddylist invite failed: already as buddy");
                requestor.GetBuddyList().Add(Owner);
                SendBuddyList(); //TODO test sending Update. Sometimes we send full buddylist when it seems like only an update is necessary
                return;
            }
            if (IsFull())
            {
                log.Warn($"[{Owner.charName}] Buddylist invite failed: own is full");
                requestor.GetBuddyList().SendRequestError(12);
                return;
            }

            log.Warn($"[{Owner.charName}] invited {requestor.charName}");

            requestor.GetBuddyList().Add(Owner);
            BuddyRequests.Enqueue(requestor);
            if (BuddyRequests.Count == 1)
            {
                PollRequests();
            }
        }
Example #11
0
        public BuddyList(Packet pr)
        {
            Capacity      = pr.ReadByte();
            Owner         = new BuddyData(pr);
            Buddies       = new Dictionary <int, BuddyData>(Capacity);
            BuddyRequests = new Queue <BuddyData>();

            int count = pr.ReadByte();

            for (var i = 0; i < count; i++)
            {
                var buddy = new BuddyData(pr);
                Buddies.Add(buddy.charId, buddy);
            }

            count = pr.ReadByte();
            for (var i = 0; i < count; i++)
            {
                var buddy = new BuddyData(pr);
                BuddyRequests.Enqueue(buddy);
            }
        }
Example #12
0
 public Boolean HasBuddy(BuddyData bud)
 {
     return(HasBuddy(bud.charId));
 }
Example #13
0
 public Boolean IsVisibleTo(BuddyData other)
 {
     return(other.IsOnline() && other.GetBuddyList().HasBuddy(this));
 }