-1

This is my current code:

public override void OnTradeRemoveItem(Schema.Item schemaItem, Inventory.Item inventoryItem)
{
    foreach (ulong id in Trade.OtherOfferedItems)
    {
        var item = Trade.OtherInventory.GetItem(id);
            Trade.RemoveItemByDefindex(item.Defindex);
    }
}

public override void OnTradeMessage(string message)
{
    switch (message.ToLower())
    {
        case "errors":
            if (OtherSteamInventory.errors.Count > 0)
            {
                Trade.SendMessage("User Errors:");
                foreach (string error in OtherSteamInventory.errors)
                {
                    Trade.SendMessage(" * " + error);
                }
            }

            if (mySteamInventory.errors.Count > 0)
            {
                Trade.SendMessage("Bot Errors:");
                foreach (string error in mySteamInventory.errors)
                {
                    Trade.SendMessage(" * " + error);
                }
            }
            break;

        case "ticket":
            int totalScrap = 0;
            int scrapPart1 = 0;
            int scrapPart2 = 0;
            int scrapPart3 = 0;

            foreach (ulong id in Trade.OtherOfferedItems)
            {
                var items = Trade.OtherOfferedItems;
                var itemType = Trade.OtherInventory.GetItem(id);

                if (itemType.Defindex == 5000)
                {
                    scrapPart1 = items.Count;
                }
                else
                {
                    scrapPart1 = 0;
                }
            }

            foreach (ulong id2 in Trade.OtherOfferedItems)
            {
                var items2 = Trade.OtherOfferedItems;
                var itemType2 = Trade.OtherInventory.GetItem(id2);

                if (itemType2.Defindex == 5001)
                {
                    int Count = 0;
                    Count = items2.Count * 3;
                    scrapPart2 = Count / items2.Count;
                }
                else
                {
                    scrapPart2 = 0;
                }
            }

            foreach (ulong id3 in Trade.OtherOfferedItems)
            {
                Trade.SendMessage("Please pay 3.44 ref");
                var items3 = Trade.OtherOfferedItems;
                var itemType3 = Trade.OtherInventory.GetItem(id3);

                if (itemType3.Defindex == 5002)
                {
                    int Count1 = 0;
                    Count1 = items3.Count * 9;
                    System.Console.WriteLine(Count1);
                    scrapPart3 = Count1 / items3.Count;
                }
                else
                {
                    scrapPart3 = 0;
                }

                totalScrap = scrapPart1 + scrapPart2 + scrapPart3;

                System.Console.WriteLine(scrapPart1);
                System.Console.WriteLine(scrapPart2);
                System.Console.WriteLine(scrapPart3);
                System.Console.WriteLine(totalScrap);
            }

            Trade.RemoveAllItems();
            if (totalScrap == 31)
            {
                int Change = 0;
                Trade.AddItemByDefindex(725);
                Change = 31 - totalScrap;
                while (Change > 0)
                {
                    Trade.AddItemByDefindex(5000);
                }
            }
            else
            {
                Trade.SendMessage("You have only added a total of " + totalScrap + " Scrap, please put up the correct amount and type ticket again");
            }
            break;
    }
}

The amount of totalScrap is wrong because there are lots of things in the Trade.OtherOfferedItems (Which is a list). and multiplying it by how many scrap is the item's worth doesn't work because it multiplies the number of items there are in Trade.OtherOfferedItems (So if someone adds a 3 items with an item of Defindex 5000 and 1 item of item Defindex 5002 I want totalScrap to be 28)

Re Captcha
  • 3,125
  • 2
  • 22
  • 34
rakagunarto
  • 325
  • 3
  • 23
  • I meant in a list not a list – rakagunarto May 06 '14 at 12:15
  • Any chance you could try to shorten your example a bit? Refer to http://www.sscce.org/. At first glance your problem seems to be easily solvable by `LINQ`, but it seem like there's a lot going on in there, so it's hard to jump in and pinpoint a solution. – decPL May 06 '14 at 12:26

1 Answers1

0

The first problem you have is that you are looping through your items three times when you only need to do it once, this leads to more confusion and more opportunities to introduce a logic error. The logic you want/need to calculate the scrap for each item type is unclear, but here's a simplified version of what you have that should make it much easier to figure out:

case "ticket":
    int totalScrap = 0, scrapPart1 = 0, scrapPart2 = 0, scrapPart3 = 0;
    var totalItemCount = Trade.OtherOfferedItems.Count;

    foreach (ulong id in items)
    {
        var itemType = Trade.OtherInventory.GetItem(id);

        //i assume you want each scrap part to be a cumulative total,
        //so we'll add it to what we already have (+=)
        switch(itemType.Defindex)
        {
            case 5000:
                scrapPart1 += totalItemCount;
                break;
            case 5001:
                int Count = 0;
                Count = totalItemCount * 3;
                scrapPart2 += Count / totalItemCount;
                break;
            case 5002:
                int Count1 = 0;
                Count1 = totalItemCount * 9;
                System.Console.WriteLine(Count1);
                scrapPart3 += Count1 / totalItemCount;
                break;
        }
    }

    //now that we are done calculating all the parts,
    //now let's calculate the total
    totalScrap = scrapPart1 + scrapPart2 + scrapPart3;
    System.Console.WriteLine(scrapPart1);
    System.Console.WriteLine(scrapPart2);
    System.Console.WriteLine(scrapPart3);
    System.Console.WriteLine(totalScrap);
    break;
Sven Grosen
  • 5,616
  • 3
  • 30
  • 52
  • Okay, thx for simplifying my work but I need a solution to my question... The resean the total is wrong is because there can be one reclaimed and one scrap in one offering. So items.Count will always be how much there are altogether, instead of a specific type of item. WIll ths switch statement add up all the items? Like if someone puts in an item with Defindex 5000 and Defindex 5001 will that count them both? – rakagunarto May 06 '14 at 12:38
  • @xXbi0phaz3Xx You want the `items.Count` to be the count of the items of that type only? – Sven Grosen May 06 '14 at 12:40
  • @xXbi0phaz3Xx To do that, you'd need to provide a method to retrieve items based on their `Defindex`, or expose such a property on whatever class constitutes an item in `OtherOfferedItems`. – Sven Grosen May 06 '14 at 13:00