-1

这是我当前的代码:

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;
    }
}

totalScrap 的数量是错误的,因为 Trade.OtherOfferedItems 中有很多东西(这是一个列表)。并将其乘以该物品的价值是多少废料是行不通的,因为它会乘以 Trade.OtherOfferedItems 中的物品数量(因此,如果有人添加了 3 件物品,其中一件物品为 Defindex 5000 和一件物品为 Defindex 5002 我希望 totalScrap 为 28)

4

1 回答 1

0

您遇到的第一个问题是,当您只需要执行一次时,您正在循环遍历您的项目 3 次,这会导致更多的混乱和更多引入逻辑错误的机会。您想要/需要为每种项目类型计算废料的逻辑尚不清楚,但这里有一个简化版本,应该可以更容易地弄清楚:

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;
于 2014-05-06T12:30:15.333 回答