Advertisement
Guest User

Untitled

a guest
Jul 27th, 2015
236
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 38.17 KB | None | 0 0
  1. using SteamKit2;
  2. using System.Collections.Generic;
  3. using SteamTrade;
  4. using System;
  5. using System.Timers;
  6.  
  7. namespace SteamBot
  8. {
  9.     public class SimpleUserHandler : UserHandler
  10.     {
  11.         static string BotVersion = "2.5.2";
  12.         static int SellPricePerKey = 31; // price in scrap, e.g. 31 / 9 = 3.55 ref
  13.         static int BuyPricePerKey = 29; // price in scrap, e.g. 29 / 9 = 3.33 ref
  14.         static int InviteTimerInterval = 2000;
  15.  
  16.         int UserMetalAdded, UserScrapAdded, UserRecAdded, UserRefAdded, UserKeysAdded, BotKeysAdded, BotMetalAdded, BotScrapAdded, BotRecAdded, BotRefAdded, InventoryMetal, InventoryScrap, InventoryRec, InventoryRef, InventoryKeys, OverpayNumKeys, ExcessInScrap, PreviousKeys, WhileLoop, InvalidItem = 0;
  17.  
  18.         double ExcessRefined = 0.0;
  19.  
  20.         bool InGroupChat, TimerEnabled, HasRun, HasErrorRun, ChooseDonate, AskOverpay, IsOverpaying, HasCounted = false;
  21.         bool TimerDisabled = true;
  22.  
  23.         ulong uid;
  24.         SteamID currentSID;
  25.  
  26.         Timer inviteMsgTimer = new System.Timers.Timer(InviteTimerInterval);
  27.  
  28.         public SimpleUserHandler(Bot bot, SteamID sid)
  29.             : base(bot, sid)
  30.         {
  31.         }
  32.  
  33.         public override bool OnFriendAdd()
  34.         {
  35.             Bot.log.Success(Bot.SteamFriends.GetFriendPersonaName(OtherSID) + " (" + OtherSID.ToString() + ") added me!");
  36.             // Using a timer here because the message will fail to send if you do it too quickly
  37.             inviteMsgTimer.Interval = InviteTimerInterval;
  38.             inviteMsgTimer.Elapsed += (sender, e) => OnInviteTimerElapsed(sender, e, EChatEntryType.ChatMsg);
  39.             inviteMsgTimer.Enabled = true;
  40.             return true;
  41.         }
  42.  
  43.         public override void OnFriendRemove()
  44.         {
  45.             Bot.log.Success(Bot.SteamFriends.GetFriendPersonaName(OtherSID) + " (" + OtherSID.ToString() + ") removed me!");
  46.         }
  47.  
  48.         public override void OnMessage(string message, EChatEntryType type)
  49.         {
  50.             message = message.ToLower();
  51.  
  52.             //REGULAR chat commands
  53.             if (message.Contains("buying") || message.Contains("what") || message.Contains("how many") || message.Contains("how much") || message.Contains("price") || message.Contains("selling"))
  54.             {
  55.                 Bot.SteamFriends.SendChatMessage(OtherSID, type, "I buy keys for " + String.Format("{0:0.00}", (BuyPricePerKey / 9.0)) + " ref, and sell keys for " + String.Format("{0:0.00}", (SellPricePerKey / 9.0)) + " ref.");
  56.             }
  57.             else if ((message.Contains("love") || message.Contains("luv") || message.Contains("<3")) && (message.Contains("y") || message.Contains("u")))
  58.             {
  59.                 if (message.Contains("do"))
  60.                 {
  61.                     Bot.SteamFriends.SendChatMessage(OtherSID, type, "I love you lots. <3");
  62.                 }
  63.                 else
  64.                 {
  65.                     Bot.SteamFriends.SendChatMessage(OtherSID, type, "I love you too!");
  66.                 }
  67.             }
  68.             else if (message.Contains("<3"))
  69.             {
  70.                 Bot.SteamFriends.SendChatMessage(OtherSID, type, "<3");
  71.             }
  72.             else if (message.Contains("fuck") || message.Contains("suck") || message.Contains("dick") || message.Contains("cock") || message.Contains("tit") || message.Contains("boob") || message.Contains("pussy") || message.Contains("vagina") || message.Contains("cunt") || message.Contains("penis"))
  73.             {
  74.                 Bot.SteamFriends.SendChatMessage(OtherSID, type, "Sorry, but as a robot I cannot perform sexual functions.");
  75.             }
  76.             else if (message.Contains("thank"))
  77.             {
  78.                 Bot.SteamFriends.SendChatMessage(OtherSID, type, "You're welcome!");
  79.             }
  80.             else if (message == "donate")
  81.             {
  82.                 Bot.SteamFriends.SendChatMessage(OtherSID, type, "Please type that command into the TRADE WINDOW, not here! And thanks. <3");
  83.             }
  84.             else if (message == "buy")
  85.             {
  86.                 Bot.SteamFriends.SendChatMessage(OtherSID, type, "That's an old command, and is unnecessary. Just trade me to begin!");
  87.             }
  88.             else if (message == "sell")
  89.             {
  90.                 Bot.SteamFriends.SendChatMessage(OtherSID, type, "That's an old command, and is unnecessary. Just trade me to begin!");
  91.             }
  92.             else if (message.Contains("help"))
  93.             {
  94.                 Bot.SteamFriends.SendChatMessage(OtherSID, EChatEntryType.ChatMsg, "Hi. Thanks for using The CTS Community's keybanking bot! Trade me, then simply put up your keys or metal and I will add my keys or metal automatically. I also accept donations of either keys or metal. To donate, type \"donate\" in the trade window!");
  95.             }
  96.             // ADMIN commands
  97.             else if (IsAdmin)
  98.             {
  99.                 if (message.StartsWith(".join"))
  100.                 {
  101.                     // Usage: .join GroupID - e.g. ".join 103582791433582049" or ".join cts" - this will allow the bot to join a group's chatroom
  102.                     if (message.Length >= 7)
  103.                     {
  104.                         if (message.Substring(6) == "tf2")
  105.                         {
  106.                             uid = 103582791430075519;
  107.                         }
  108.                         else
  109.                         {
  110.                             ulong.TryParse(message.Substring(6), out uid);
  111.                         }
  112.                         var chatid = new SteamID(uid);
  113.                         Bot.SteamFriends.JoinChat(chatid);
  114.                         Bot.SteamFriends.SendChatMessage(OtherSID, type, "Joining chat: " + chatid.ConvertToUInt64().ToString());
  115.                         InGroupChat = true;
  116.                         Bot.SteamFriends.SetPersonaState(EPersonaState.Online);
  117.                         Bot.log.Success("Joining chat: " + chatid.ConvertToUInt64().ToString());
  118.                     }
  119.                 }
  120.                 else if (message.StartsWith(".leave"))
  121.                 {
  122.                     // Usage: .leave GroupID, same concept as joining
  123.                     if (message.Length >= 8)
  124.                     {
  125.                         if (message.Substring(7) == "tf2")
  126.                         {
  127.                             uid = 103582791430075519;
  128.                         }
  129.                         else
  130.                         {
  131.                             ulong.TryParse(message.Substring(7), out uid);
  132.                         }
  133.                         var chatid = new SteamID(uid);
  134.                         Bot.SteamFriends.LeaveChat(chatid);
  135.                         Bot.SteamFriends.SendChatMessage(OtherSID, type, "Leaving chat: " + chatid.ConvertToUInt64().ToString());
  136.                         InGroupChat = false;
  137.                         Bot.log.Success("Leaving chat: " + chatid.ConvertToUInt64().ToString());
  138.                     }
  139.                 }
  140.                 else if (message.StartsWith(".sell"))
  141.                 {
  142.                     // Usage: .sell newprice "e.g. sell 26"
  143.                     int NewSellPrice = 0;
  144.                     if (message.Length >= 6)
  145.                     {
  146.                         Bot.SteamFriends.SendChatMessage(OtherSID, type, "Current selling price: " + SellPricePerKey + " scrap.");
  147.                         int.TryParse(message.Substring(5), out NewSellPrice);
  148.                         Bot.log.Success("Admin has requested that I set the new selling price from " + SellPricePerKey + " scrap to " + NewSellPrice + " scrap.");
  149.                         SellPricePerKey = NewSellPrice;
  150.                         Bot.SteamFriends.SendChatMessage(OtherSID, type, "Setting new selling price to: " + SellPricePerKey + " scrap.");
  151.                         Bot.log.Success("Successfully set new price.");
  152.                     }
  153.                     else
  154.                     {
  155.                         Bot.SteamFriends.SendChatMessage(OtherSID, type, "I need more arguments. Current selling price: " + SellPricePerKey + " scrap.");
  156.                     }
  157.                 }
  158.                 else if (message.StartsWith(".buy"))
  159.                 {
  160.                     // Usage: .buy newprice "e.g. .buy 24"
  161.                     int NewBuyPrice = 0;
  162.                     if (message.Length >= 5)
  163.                     {
  164.                         Bot.SteamFriends.SendChatMessage(OtherSID, type, "Current buying price: " + BuyPricePerKey + " scrap.");
  165.                         int.TryParse(message.Substring(4), out NewBuyPrice);
  166.                         Bot.log.Success("Admin has requested that I set the new selling price from " + BuyPricePerKey + " scrap to " + NewBuyPrice + " scrap.");
  167.                         BuyPricePerKey = NewBuyPrice;
  168.                         Bot.SteamFriends.SendChatMessage(OtherSID, type, "Setting new buying price to: " + BuyPricePerKey + " scrap.");
  169.                         Bot.log.Success("Successfully set new price.");
  170.                     }
  171.                     else
  172.                     {
  173.                         Bot.SteamFriends.SendChatMessage(OtherSID, type, "I need more arguments. Current buying price: " + BuyPricePerKey + " scrap.");
  174.                     }
  175.                 }
  176.                 else if (message.StartsWith(".gmessage"))
  177.                 {
  178.                     // usage: say ".gmessage Hello!" to the bot will send "Hello!" into group chat
  179.                     if (message.Length >= 10)
  180.                     {
  181.                         if (InGroupChat)
  182.                         {
  183.                             var chatid = new SteamID(uid);
  184.                             string gmessage = message.Substring(10);
  185.                             Bot.SteamFriends.SendChatRoomMessage(chatid, type, gmessage);
  186.                             Bot.log.Success("Said into group chat: " + gmessage);
  187.                         }
  188.                         else
  189.                         {
  190.                             Bot.log.Warn("Cannot send message because I am not in a group chatroom!");
  191.                         }
  192.                     }
  193.                 }
  194.                 else if (message == ".canceltrade")
  195.                 {
  196.                     // Cancels the trade. Occasionally the message will be sent to YOU instead of the current user. Oops.
  197.                     Trade.CancelTrade();
  198.                     Bot.SteamFriends.SendChatMessage(currentSID, EChatEntryType.ChatMsg, "My creator has forcefully cancelled the trade. Whatever you were doing, he probably wants you to stop.");
  199.                 }
  200.             }
  201.             else
  202.             {
  203.                 Bot.SteamFriends.SendChatMessage(OtherSID, type, Bot.ChatResponse);
  204.             }
  205.         }
  206.  
  207.         public override bool OnTradeRequest()
  208.         {
  209.             Bot.log.Success(Bot.SteamFriends.GetFriendPersonaName(OtherSID) + " (" + OtherSID.ToString() + ") has requested to trade with me!");
  210.             return true;
  211.         }
  212.  
  213.         public override void OnTradeError(string error)
  214.         {
  215.             Bot.SteamFriends.SendChatMessage(OtherSID,
  216.                                               EChatEntryType.ChatMsg,
  217.                                               "Error: " + error + "."
  218.                                               );
  219.             Bot.log.Warn(error);
  220.             if (!HasErrorRun)
  221.             {
  222.                 Bot.SteamFriends.SendChatMessage(OtherSID, EChatEntryType.ChatMsg, "Did something go horribly wrong? If you have found a bug or something that you think wasn't supposed to happen, please leave a message on my owner's profile!");
  223.                 HasErrorRun = true;
  224.             }
  225.             Bot.SteamFriends.SetPersonaState(EPersonaState.Online);
  226.         }
  227.  
  228.         public override void OnTradeTimeout()
  229.         {
  230.             Bot.SteamFriends.SendChatMessage(OtherSID, EChatEntryType.ChatMsg,
  231.                                               "Sorry, but you were either AFK or took too long and the trade was canceled.");
  232.             Bot.log.Info("User was kicked because he was AFK.");
  233.             Bot.SteamFriends.SetPersonaState(EPersonaState.Online);
  234.         }
  235.  
  236.         public override void OnTradeInit()
  237.         {
  238.             ReInit();
  239.             TradeCountInventory(true);
  240.             Trade.SendMessage("Welcome to ScrapBank.Me's public keybanking bot (v" + BotVersion + "). This bot was coded by http://steamcommunity.com/id/waylaidwanderer. To use this bot, just add your metal or keys, and the bot will automatically add keys or metal when you have put up enough.");
  241.             if (InventoryKeys == 0)
  242.             {
  243.                 Trade.SendMessage("I don't have any keys to sell right now! I am currently buying keys for " + String.Format("{0:0.00}", (BuyPricePerKey / 9.0)) + " ref.");
  244.             }
  245.             else if (InventoryMetal < BuyPricePerKey)
  246.             {
  247.                 Trade.SendMessage("I don't have enough metal to buy keys! I am selling keys for " + String.Format("{0:0.00}", (SellPricePerKey / 9.0)) + " ref.");
  248.             }
  249.             else
  250.             {
  251.                 Trade.SendMessage("I am currently buying keys for " + String.Format("{0:0.00}", (BuyPricePerKey / 9.0)) + " ref, and selling keys for " + String.Format("{0:0.00}", (SellPricePerKey / 9.0)) + " ref.");
  252.             }
  253.             Bot.SteamFriends.SetPersonaState(EPersonaState.Busy);
  254.         }
  255.  
  256.         public override void OnTradeAddItem(Schema.Item schemaItem, Inventory.Item inventoryItem)
  257.         {
  258.             var item = Trade.CurrentSchema.GetItem(schemaItem.Defindex);
  259.             if (!HasCounted)
  260.             {
  261.                 Trade.SendMessage("ERROR: I haven't finished counting my inventory yet! Please remove any items you added, and then re-add them or there could be errors.");
  262.             }
  263.             else if (InvalidItem >= 4)
  264.             {
  265.                 Trade.CancelTrade();
  266.                 Bot.SteamFriends.SendChatMessage(OtherSID, EChatEntryType.ChatMsg, "Please stop messing around. I am used for buying and selling keys only. I can only accept metal or keys as payment.");
  267.                 Bot.log.Warn("Booted user for messing around.");
  268.                 Bot.SteamFriends.SetPersonaState(EPersonaState.Online);
  269.             }
  270.             else if (item.Defindex == 5000)
  271.             {
  272.                 // Put up scrap metal
  273.                 UserMetalAdded++;
  274.                 UserScrapAdded++;
  275.                 Bot.log.Success("User added: " + item.ItemName);
  276.             }
  277.             else if (item.Defindex == 5001)
  278.             {
  279.                 // Put up reclaimed metal
  280.                 UserMetalAdded += 3;
  281.                 UserRecAdded++;
  282.                 Bot.log.Success("User added: " + item.ItemName);
  283.             }
  284.             else if (item.Defindex == 5002)
  285.             {
  286.                 // Put up refined metal
  287.                 UserMetalAdded += 9;
  288.                 UserRefAdded++;
  289.                 Bot.log.Success("User added: " + item.ItemName);
  290.             }
  291.             else if (schemaItem.ItemName == "Mann Co. Supply Crate Key" || schemaItem.ItemName == "#TF_Tool_DecoderRing")
  292.             {
  293.                 // Put up keys
  294.                 UserKeysAdded++;
  295.                 Bot.log.Success("User added: " + item.ItemName);
  296.                 // USER IS SELLING KEYS
  297.                 if (!ChooseDonate)
  298.                 {
  299.                     // BOT ADDS METAL
  300.                     int KeysToScrap = UserKeysAdded * BuyPricePerKey;
  301.                     if (InventoryMetal < KeysToScrap)
  302.                     {
  303.                         Trade.SendMessage("I only have " + InventoryMetal + " scrap. You need to remove some keys.");
  304.                         Bot.log.Warn("I don't have enough metal for the user.");
  305.                     }
  306.                     else
  307.                     {
  308.                         Trade.SendMessage("You have given me " + UserKeysAdded + " key(s). I will give you " + KeysToScrap + " scrap.");
  309.                         Bot.log.Success("User gave me " + UserKeysAdded + " key(s). I will now give him " + KeysToScrap + " scrap.");
  310.                         // Put up required metal
  311.                         bool DoneAddingMetal = false;
  312.                         while (!DoneAddingMetal)
  313.                         {
  314.                             if (InventoryRef > 0 && BotMetalAdded + 9 <= KeysToScrap)
  315.                             {
  316.                                 Trade.AddItemByDefindex(5002);
  317.                                 Bot.log.Warn("I added Refined Metal.");
  318.                                 BotMetalAdded += 9;
  319.                                 BotRefAdded++;
  320.                                 InventoryRef--;
  321.                             }
  322.                             else if (InventoryRec > 0 && BotMetalAdded + 3 <= KeysToScrap)
  323.                             {
  324.                                 Trade.AddItemByDefindex(5001);
  325.                                 Bot.log.Warn("I added Reclaimed Metal.");
  326.                                 BotMetalAdded += 3;
  327.                                 BotRecAdded++;
  328.                                 InventoryRec--;
  329.                             }
  330.                             else if (InventoryScrap > 0 && BotMetalAdded + 1 <= KeysToScrap)
  331.                             {
  332.                                 Trade.AddItemByDefindex(5000);
  333.                                 Bot.log.Warn("I added Scrap Metal.");
  334.                                 BotMetalAdded++;
  335.                                 BotScrapAdded++;
  336.                                 InventoryScrap--;
  337.                             }
  338.                             else if (InventoryScrap == 0 && BotMetalAdded + 2 == KeysToScrap)
  339.                             {
  340.                                 Trade.SendMessage("Sorry, but I don't have enough scrap metal to give you! Please remove some keys or add two keys.");
  341.                                 Bot.log.Warn("Couldn't add enough metal for the user!");
  342.                                 DoneAddingMetal = true;
  343.                             }
  344.                             else if (InventoryScrap == 0 && BotMetalAdded + 1 == KeysToScrap)
  345.                             {
  346.                                 Trade.SendMessage("Sorry, but I don't have enough scrap metal to give you! Please remove some keys or add a key.");
  347.                                 Bot.log.Warn("Couldn't add enough metal for the user!");
  348.                                 DoneAddingMetal = true;
  349.                             }
  350.                             else if (BotMetalAdded == KeysToScrap)
  351.                             {
  352.                                 Trade.SendMessage("Added enough metal. " + BotRefAdded + " ref, " + BotRecAdded + " rec, " + BotScrapAdded + " scrap.");
  353.                                 Bot.log.Success("Gave user enough metal!");
  354.                                 DoneAddingMetal = true;
  355.                             }
  356.                         }
  357.                     }
  358.                 }
  359.             }
  360.             else
  361.             {
  362.                 // Put up other items
  363.                 Trade.SendMessage("Sorry, I don't accept " + item.ItemName + "! I only accept metal/keys. Please remove it from the trade to continue.");
  364.                 Bot.log.Warn("User added:  " + item.ItemName);
  365.                 InvalidItem++;
  366.             }
  367.             // USER IS BUYING KEYS
  368.             if (!ChooseDonate)
  369.             {
  370.                 if (UserMetalAdded % SellPricePerKey >= 0 && UserMetalAdded > 0)
  371.                 {
  372.                     // Count refined and convert to keys -- X scrap per key
  373.                     int NumKeys = UserMetalAdded / SellPricePerKey;
  374.                     if (NumKeys > 0 && NumKeys != PreviousKeys)
  375.                     {
  376.                         Trade.SendMessage("You put up enough metal for " + NumKeys + " key(s). Adding your keys now...");
  377.                         Bot.log.Success("User put up enough metal for " + NumKeys + " key(s).");
  378.                         if (NumKeys > InventoryKeys)
  379.                         {
  380.                             double excess = ((NumKeys - BotKeysAdded) * SellPricePerKey) / 9.0;
  381.                             string refined = string.Format("{0:N2}", excess);
  382.                             Trade.SendMessage("I only have " + InventoryKeys + " in my backpack. :(");
  383.                             Bot.log.Warn("User wanted to buy " + NumKeys + " key(s), but I only have " + InventoryKeys + " key(s).");
  384.                             Trade.SendMessage("Please remove " + refined + " ref.");
  385.                             NumKeys = InventoryKeys;
  386.                         }
  387.                         // Add the keys to the trade window
  388.                         for (int count = BotKeysAdded; count < NumKeys; count++)
  389.                         {
  390.                             if (AddKey())
  391.                             {
  392.                                 Bot.log.Warn("I am adding Mann Co. Supply Crate Key.");
  393.                                 BotKeysAdded++;
  394.                             }
  395.                         }
  396.                         Trade.SendMessage("I have added " + BotKeysAdded + " key(s) for you.");
  397.                         Bot.log.Success("I have added " + BotKeysAdded + " key(s) for the user.");
  398.                         PreviousKeys = NumKeys;
  399.                     }
  400.                 }
  401.             }
  402.         }
  403.  
  404.         public override void OnTradeRemoveItem(Schema.Item schemaItem, Inventory.Item inventoryItem)
  405.         {
  406.             var item = Trade.CurrentSchema.GetItem(schemaItem.Defindex);
  407.             if (item.Defindex == 5000)
  408.             {
  409.                 // Removed scrap metal
  410.                 UserMetalAdded--;
  411.                 UserScrapAdded--;
  412.                 Bot.log.Success("User removed: " + item.ItemName);
  413.             }
  414.             else if (item.Defindex == 5001)
  415.             {
  416.                 // Removed reclaimed metal
  417.                 UserMetalAdded -= 3;
  418.                 UserRecAdded--;
  419.                 Bot.log.Success("User removed: " + item.ItemName);
  420.             }
  421.             else if (item.Defindex == 5002)
  422.             {
  423.                 // Removed refined metal
  424.                 UserMetalAdded -= 9;
  425.                 UserRefAdded--;
  426.                 Bot.log.Success("User removed: " + item.ItemName);
  427.             }
  428.             else if (schemaItem.ItemName == "Mann Co. Supply Crate Key" || schemaItem.ItemName == "#TF_Tool_DecoderRing")
  429.             {
  430.                 // Removed keys
  431.                 UserKeysAdded--;
  432.                 Bot.log.Success("User removed: " + item.ItemName);
  433.             }
  434.             else
  435.             {
  436.                 // Removed other items
  437.                 Bot.log.Warn("User removed: " + item.ItemName);
  438.             }
  439.             // User removes key from trade
  440.             if (UserKeysAdded < (float)BotMetalAdded / BuyPricePerKey)
  441.             {
  442.                 int KeysToScrap = UserKeysAdded * BuyPricePerKey;
  443.                 bool DoneAddingMetal = false;
  444.                 while (!DoneAddingMetal)
  445.                 {
  446.                     WhileLoop++;
  447.                     if (BotRefAdded > 0 && BotMetalAdded - 9 >= KeysToScrap)
  448.                     {
  449.                         Trade.RemoveItemByDefindex(5002);
  450.                         Bot.log.Warn("I removed Refined Metal.");
  451.                         BotMetalAdded -= 9;
  452.                         BotRefAdded--;
  453.                         InventoryRef++;
  454.                     }
  455.                     else if (BotRecAdded > 0 && BotMetalAdded - 3 >= KeysToScrap)
  456.                     {
  457.                         Trade.RemoveItemByDefindex(5001);
  458.                         Bot.log.Warn("I removed Reclaimed Metal.");
  459.                         BotMetalAdded -= 3;
  460.                         BotRecAdded--;
  461.                         InventoryRec++;
  462.                     }
  463.                     else if (BotScrapAdded > 0 && BotMetalAdded - 1 >= KeysToScrap)
  464.                     {
  465.                         Trade.RemoveItemByDefindex(5000);
  466.                         Bot.log.Warn("I removed Scrap Metal.");
  467.                         BotMetalAdded--;
  468.                         BotScrapAdded--;
  469.                         InventoryScrap++;
  470.                     }
  471.                     else if (BotMetalAdded == KeysToScrap)
  472.                     {
  473.                         DoneAddingMetal = true;
  474.                     }
  475.                     else if (WhileLoop > 50)
  476.                     {
  477.                         Trade.SendMessage("Error: I could not remove the proper amounts of metal from the trade. I might be out of scrap metal - try adding more keys if possible, or remove a few keys.");
  478.                         WhileLoop = 0;
  479.                         DoneAddingMetal = true;
  480.                         break;
  481.                     }
  482.                 }
  483.             }
  484.             // User removes metal from trade
  485.             while ((float)UserMetalAdded / SellPricePerKey < BotKeysAdded)
  486.             {
  487.                 if (RemoveKey())
  488.                 {
  489.                     Bot.log.Warn("I removed Mann Co. Supply Crate Key.");
  490.                     BotKeysAdded--;
  491.                     InventoryKeys++;
  492.                     PreviousKeys = BotKeysAdded;
  493.                     IsOverpaying = false;
  494.                 }
  495.             }
  496.         }
  497.  
  498.         public override void OnTradeMessage(string message)
  499.         {
  500.             Bot.log.Info("[TRADE MESSAGE] " + message);
  501.             message = message.ToLower();
  502.  
  503.             if (message == "donate")
  504.             {
  505.                 ChooseDonate = true;
  506.                 Trade.SendMessage("Oh, you want to donate metal or keys? Thank you so much! Please put up your items and simply click \"Ready to Trade\" when done! If you want to buy or sell keys again you need to start a new trade with me.");
  507.                 Bot.log.Success("User wants to donate!");
  508.             }
  509.             else if (message == "continue")
  510.             {
  511.                 if (AskOverpay)
  512.                 {
  513.                     IsOverpaying = true;
  514.                     Trade.SendMessage("You have chosen to continue overpaying. Click \"Ready to Trade\" again to complete the trade.");
  515.                     Bot.log.Warn("User has chosen to continue overpaying!");
  516.                 }
  517.                 else
  518.                 {
  519.                     Trade.SendMessage("You cannot use this command right now!");
  520.                     Bot.log.Warn("User typed \"continue\" for no reason.");
  521.                 }
  522.             }
  523.  
  524.         }
  525.  
  526.         public override void OnTradeReady(bool ready)
  527.         {
  528.             if (!ready)
  529.             {
  530.                 Trade.SetReady(false);
  531.             }
  532.             else
  533.             {
  534.                 Bot.log.Success("User is ready to trade!");
  535.                 if (Validate())
  536.                 {
  537.                     Trade.SetReady(true);
  538.                 }
  539.                 else
  540.                 {
  541.                     if (AskOverpay && OverpayNumKeys != 0 && !ChooseDonate)
  542.                     {
  543.                         double AdditionalRefined = (SellPricePerKey / 9.0) - ExcessRefined;
  544.                         string addRef = string.Format("{0:N2}", AdditionalRefined);
  545.                         string refined = string.Format("{0:N2}", ExcessRefined);
  546.                         Trade.SendMessage("WARNING: You will be overpaying. If you'd like to continue, type \"continue\", otherwise remove " + refined + " ref, or add " + addRef + " ref. You cannot complete the trade unless you do so.");
  547.                         Bot.log.Warn("User has added an excess of " + refined + " ref. He can add " + addRef + " ref for another key. Asking user if they want to continue.");
  548.                     }
  549.                     else
  550.                     {
  551.                         ResetTrade(false);
  552.                     }
  553.                 }
  554.             }
  555.         }
  556.  
  557.         public override void OnTradeAccept()
  558.         {
  559.             if (Validate() || IsAdmin)
  560.             {
  561.                 bool success = Trade.AcceptTrade();
  562.                 if (success)
  563.                 {
  564.                     Log.Success("Trade was successful!");
  565.                     Bot.SteamFriends.SendChatMessage(OtherSID, EChatEntryType.ChatMsg, "Thanks for a successful trade!");
  566.                     Bot.SteamFriends.SendChatMessage(OtherSID, EChatEntryType.ChatMsg, "This bot was coded by http://steamcommunity.com/id/waylaidwanderer . Bugs or problems? Please report them to the owner of the bot instead.");
  567.                     Bot.SteamFriends.SetPersonaState(EPersonaState.Online);
  568.                 }
  569.                 else
  570.                 {
  571.                     Log.Warn("Trade might have failed.");
  572.                     Bot.SteamFriends.SetPersonaState(EPersonaState.Online);
  573.                 }
  574.             }
  575.             OnTradeClose();
  576.         }
  577.  
  578.         public override void OnTradeClose()
  579.         {
  580.             Bot.SteamFriends.SetPersonaState(EPersonaState.Online);
  581.             base.OnTradeClose();
  582.         }
  583.  
  584.         public bool Validate()
  585.         {
  586.             int ScrapCount = 0;
  587.             int KeyCount = 0;
  588.  
  589.             List<string> errors = new List<string>();
  590.  
  591.             foreach (ulong id in Trade.OtherOfferedItems)
  592.             {
  593.                 var item = Trade.OtherInventory.GetItem(id);
  594.                 var schemaItem = Trade.CurrentSchema.GetItem(item.Defindex);
  595.                 if (item.Defindex == 5000)
  596.                 {
  597.                     ScrapCount++;
  598.                 }
  599.                 else if (item.Defindex == 5001)
  600.                 {
  601.                     ScrapCount += 3;
  602.                 }
  603.                 else if (item.Defindex == 5002)
  604.                 {
  605.                     ScrapCount += 9;
  606.                 }
  607.                 else if (schemaItem.ItemName == "Mann Co. Supply Crate Key" || schemaItem.ItemName == "#TF_Tool_DecoderRing")
  608.                 {
  609.                     KeyCount++;
  610.                 }
  611.                 else
  612.                 {
  613.                     errors.Add("I can't accept " + schemaItem.ItemName + "!");
  614.                 }
  615.             }
  616.  
  617.             if (ChooseDonate)
  618.             {
  619.                 foreach (ulong id in Trade.OtherOfferedItems)
  620.                 {
  621.                     var item = Trade.OtherInventory.GetItem(id);
  622.                     var schemaItem = Trade.CurrentSchema.GetItem(item.Defindex);
  623.                     if (schemaItem.ItemName != "Mann Co. Supply Crate Key" && schemaItem.ItemName != "#TF_Tool_DecoderRing" && item.Defindex != 5000 && item.Defindex != 5001 && item.Defindex != 5002)
  624.                     {
  625.                         errors.Add("I'm sorry, but I cannot accept " + schemaItem.ItemName + "!");
  626.                     }
  627.                 }
  628.  
  629.                 if (BotMetalAdded > 0 || BotKeysAdded > 0)
  630.                 {
  631.                     errors.Add("You can't do that :( I still have items put up!");
  632.                 }
  633.             }
  634.             else if (UserKeysAdded > 0)
  635.             {
  636.                 Bot.log.Warn("User has " + KeyCount + " key(s) put up. Verifying if " + (float)BotMetalAdded / BuyPricePerKey + " == " + KeyCount + ".");
  637.                 if (KeyCount != (float)BotMetalAdded / BuyPricePerKey)
  638.                 {
  639.                     errors.Add("Something went wrong. Either you do not have the correct amount of keys or I don't have the correct amount of metal.");
  640.                 }
  641.             }
  642.             else if (ScrapCount % SellPricePerKey != 0 && !IsOverpaying)
  643.             {
  644.                 // Count refined and convert to keys -- X scrap per key
  645.                 OverpayNumKeys = ScrapCount / SellPricePerKey;
  646.                 ExcessInScrap = ScrapCount - (OverpayNumKeys * SellPricePerKey);
  647.                 ExcessRefined = (ExcessInScrap / 9.0);
  648.                 string refined = string.Format("{0:N2}", ExcessRefined);
  649.                 Trade.SendMessage("You put up enough metal for " + OverpayNumKeys + " key(s), with " + refined + " ref extra.");
  650.                 Bot.log.Success("User put up enough metal for " + OverpayNumKeys + " key(s), with " + refined + " ref extra.");
  651.                 if (OverpayNumKeys == 0)
  652.                 {
  653.                     double AdditionalRefined = (SellPricePerKey / 9.0) - ExcessRefined;
  654.                     string addRef = string.Format("{0:N2}", AdditionalRefined);
  655.                     errors.Add("ERROR: You need to add " + addRef + " ref for a key.");
  656.                     Bot.log.Warn("User doesn't have enough metal added, and needs add " + addRef + " ref for a key.");
  657.                 }
  658.                 else if (OverpayNumKeys >= 1)
  659.                 {
  660.                     errors.Add("You have put up more metal than what I'm asking.");
  661.                     AskOverpay = true;
  662.                 }
  663.             }
  664.             else if (ScrapCount > 0 && !IsOverpaying)
  665.             {
  666.                 if (ScrapCount < BotKeysAdded * SellPricePerKey || (ScrapCount > BotKeysAdded * SellPricePerKey))
  667.                 {
  668.                     errors.Add("You must put up exactly " + String.Format("{0:0.00}", (SellPricePerKey / 9.0)) + " ref per key.");
  669.                 }
  670.             }
  671.  
  672.             // send the errors
  673.             if (errors.Count != 0)
  674.                 Trade.SendMessage("There were errors in your trade: ");
  675.  
  676.             foreach (string error in errors)
  677.             {
  678.                 Trade.SendMessage(error);
  679.             }
  680.  
  681.             return errors.Count == 0;
  682.         }
  683.  
  684.         public void TradeCountInventory(bool message)
  685.         {
  686.             // Let's count our inventory
  687.             Inventory.Item[] inventory = Trade.MyInventory.Items;
  688.             InventoryMetal = 0;
  689.             InventoryKeys = 0;
  690.             InventoryRef = 0;
  691.             InventoryRec = 0;
  692.             InventoryScrap = 0;
  693.             foreach (Inventory.Item item in inventory)
  694.             {
  695.                 var schemaItem = Trade.CurrentSchema.GetItem(item.Defindex);
  696.                 if (item.Defindex == 5000)
  697.                 {
  698.                     InventoryMetal++;
  699.                     InventoryScrap++;
  700.                 }
  701.                 else if (item.Defindex == 5001)
  702.                 {
  703.                     InventoryMetal += 3;
  704.                     InventoryRec++;
  705.                 }
  706.                 else if (item.Defindex == 5002)
  707.                 {
  708.                     InventoryMetal += 9;
  709.                     InventoryRef++;
  710.                 }
  711.                 else if (schemaItem.ItemName == "Mann Co. Supply Crate Key" || schemaItem.ItemName == "#TF_Tool_DecoderRing")
  712.                 {
  713.                     InventoryKeys++;
  714.                 }
  715.             }
  716.             if (message)
  717.             {
  718.                 double MetalToRef = (InventoryMetal / 9.0) - 0.01;
  719.                 string refined = string.Format("{0:N2}", MetalToRef);
  720.                 Trade.SendMessage("Current stock: I have " + refined + " ref (" + InventoryRef + " ref, " + InventoryRec + " rec, and " + InventoryScrap + " scrap) and " + InventoryKeys + " key(s) in my backpack.");
  721.                 Bot.log.Success("Current stock: I have " + refined + " ref (" + InventoryRef + " ref, " + InventoryRec + " rec, and " + InventoryScrap + " scrap) and " + InventoryKeys + " key(s) in my backpack.");
  722.             }
  723.             HasCounted = true;
  724.         }
  725.  
  726.         public void ReInit()
  727.         {
  728.             UserMetalAdded = 0;
  729.             UserRefAdded = 0;
  730.             UserRecAdded = 0;
  731.             UserScrapAdded = 0;
  732.             UserKeysAdded = 0;
  733.             BotKeysAdded = 0;
  734.             BotMetalAdded = 0;
  735.             BotRefAdded = 0;
  736.             BotRecAdded = 0;
  737.             BotScrapAdded = 0;
  738.             OverpayNumKeys = 0;
  739.             PreviousKeys = 0;
  740.             ExcessInScrap = 0;
  741.             ExcessRefined = 0.0;
  742.             WhileLoop = 0;
  743.             InvalidItem = 0;
  744.             HasErrorRun = false;
  745.             ChooseDonate = false;
  746.             AskOverpay = false;
  747.             IsOverpaying = false;
  748.             HasCounted = false;
  749.             currentSID = OtherSID;
  750.         }
  751.  
  752.         public bool AddKey()
  753.         {
  754.             bool added = false;
  755.             while (!added)
  756.             {
  757.                 foreach (var item in Trade.MyInventory.Items)
  758.                 {
  759.                     var schemaItem = Trade.CurrentSchema.GetItem(item.Defindex);
  760.                     if (schemaItem.ItemName == "Mann Co. Supply Crate Key" || schemaItem.ItemName == "#TF_Tool_DecoderRing")
  761.                     {
  762.                         added = Trade.AddItem(item.Id);
  763.                         if (added) break;
  764.                     }
  765.                 }
  766.                 break;
  767.             }
  768.             return added;
  769.         }
  770.  
  771.         public bool RemoveKey()
  772.         {
  773.             bool removed = false;
  774.             while (!removed)
  775.             {
  776.                 foreach (var item in Trade.MyInventory.Items)
  777.                 {
  778.                     var schemaItem = Trade.CurrentSchema.GetItem(item.Defindex);
  779.                     if (schemaItem.ItemName == "Mann Co. Supply Crate Key" || schemaItem.ItemName == "#TF_Tool_DecoderRing")
  780.                     {
  781.                         removed = Trade.RemoveItem(item.Id);
  782.                         if (removed) break;
  783.                     }
  784.                 }
  785.                 break;
  786.             }
  787.             return removed;
  788.         }
  789.  
  790.         private void OnInviteTimerElapsed(object source, ElapsedEventArgs e, EChatEntryType type)
  791.         {
  792.             Bot.SteamFriends.SendChatMessage(OtherSID, EChatEntryType.ChatMsg, "Hi. You have added ScrapBank.Me's public keybanking bot! Just trade me, and add your keys or metal to begin! This bot also accept donations of either keys or metal. To donate, type \"donate\" in the trade window!");
  793.             Bot.log.Success("Sent welcome message.");
  794.             inviteMsgTimer.Enabled = false;
  795.             inviteMsgTimer.Stop();
  796.         }
  797.  
  798.         public void ResetTrade(bool message)
  799.         {
  800.             foreach (var item in Trade.MyInventory.Items)
  801.             {
  802.                 Trade.RemoveItem(item.Id);
  803.             }
  804.             BotKeysAdded = 0;
  805.             BotMetalAdded = 0;
  806.             BotRefAdded = 0;
  807.             BotRecAdded = 0;
  808.             BotScrapAdded = 0;
  809.             ChooseDonate = false;
  810.             TradeCountInventory(message);
  811.             Trade.SendMessage("Something went wrong! Scroll up to read the errors.");
  812.             Bot.log.Warn("Something went wrong! I am resetting the trade.");
  813.             Trade.SendMessage("I have reset the trade. Please try again. (If you chose to donate, you will need to type \"donate\" again)");
  814.             Bot.log.Success("Reset trade.");
  815.         }
  816.     }
  817. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement