Weird problem at itens removal [Help]

04/11/2011 22:27 12tails#1
yo!

hmmm i'm just learning more with my current patched source but... there is something bothering me...

i've tried many times and the problem stay the same....
when i call the void for the second or third time... the iten is not removed anymore .... oO
those are my voids for remove and check to contains an item :
Code:
        public static void RemoveItem(uint UID, ClientSocket CSocket)
        {
        Again:
            try
            {
                CSocket.Client.Inventory.Remove(UID);
            }
            catch { Console.WriteLine("Error while removing UID " + UID); goto Again; }
        }

        public static void RemoveItem(uint ID, byte Times, ClientSocket CSocket)
        {
            byte ItemsFound = 0;

            foreach (Struct.ItemInfo Item in CSocket.Client.Inventory.Values)
            {
                if (Item == null) continue;
                if (ItemsFound == Times) break;
                if (Item.ID == ID)
                {
                    RemoveItem(Item.UID, CSocket);
                    ItemsFound++;
                }
            }
        }

        public static bool ContainsItem(uint ID, byte Times, ClientSocket CSocket)
        {
            byte Count = 0;
            Struct.ItemInfo[] Inv = CSocket.Client.Inventory.Values;

                foreach (Struct.ItemInfo Item in Inv)
                    if (Item != null)
                        if (Item.ID == ID)
                            Count++;

            return (Count >= Times);
        }
//Don't say you haven't added database removal and the itenusage packet... they're at my Inventory class ^^

if somene saw something wrong inside the codes.... i would like the help ^^
04/11/2011 22:52 |NeoX#2
I dont see the problem but smething worse...

public static void RemoveItem(uint UID, ClientSocket CSocket)
{
Again:
try
{
CSocket.Client.Inventory.Remove(UID);
}
catch { Console.WriteLine("Error while removing UID " + UID); goto Again; }
}

Basicly it will loop forever if that UID isnt existing? Oh Shit!
04/11/2011 23:06 12tails#3
that's the remove void :
Code:
        public void Remove(uint key)
        {
            byte c = 0;
            foreach (Struct.ItemInfo it in Items)
                if (it != null)
                {
                    if (it.UID == key)
                    {
                        Owner.Owner.Send(ConquerPacket.ItemUsage(it.UID, 255, Struct.ItemUsage.RemoveItem));
                        Database.DeleteItem(it.UID);
                        Items[c] = null;
                        CountDown();
                        break;
                    }
                    c++;
                }
        }
so it will loop if there is a problem here ^^
04/12/2011 10:35 |NeoX#4
Ohh im sorry :)
04/12/2011 13:13 teroareboss1#5
Items is list? ( list<object> Items = ...) ?

if is list , try:

public void Remove(uint key)
{

for(byte x =0; x < Items.Count; x++)
{
if(Items[x] != null)
if(Items[x].UID == key)

{
Owner.Owner.Send(ConquerPacket.ItemUsage(Items[x].UID, 255, Struct.ItemUsage.RemoveItem));
Database.DeleteItem(Items[x].UID);
Items.Remove(Items[x]); //Items[x] = null;
CountDown();
break;

}



}
}
04/12/2011 18:38 { Angelius }#6
Quote:
public bool RemoveItems(uint ID, byte Times, ClientSocket CSocket)
{
var Items = from I in this.Inventory.Values where I.ID == ID select I;
if (Items.Count() == Times || Items.Count() >= Times)
{
int Count = 0;
foreach (Struct.ItemInfo Item in Items)
{
this.Inventory.Remove(Item.UID);
this.Send(ConquerPacket.ItemUsage(Item.UID, 255, Struct.ItemUsage.RemoveItem));
Database.DeleteItem(Item.UID);
if (Count == Times) return true;
Count++;
}
}
return false;
}
Quote:
if (Client.RemoveItems(ID, Times, Client))
{
do something
}
else {Message"You dont have the whatever you are looking for "}
thats gonna do all 3 voids work thought

i dident test that but i believe that should do the job ,
good luck ,
04/12/2011 19:13 pro4never#7
if (Items.Count() == Times || Items.Count() >= Times)

can just be

if (Items.Count() >= Times)

You're already checking == in that section.


But yes, Linq is a fantastic tool for all sorts of applications including this one.

Looks like a very nice solution to this.
04/12/2011 21:06 12tails#8
hmm... i see

tkx guys ^^