문제

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)

도움이 되었습니까?

해결책

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;
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top