Advertisement
Guest User

flasdjk;

a guest
Jul 24th, 2014
240
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 45.54 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 KeyUserHandler : UserHandler
  10. {
  11. static string BotVersion = "2.4.0";
  12.  
  13. //PRICE SETTINGS
  14. //PRICE IN SCRAPS
  15. //7.66
  16. static int SellPricePerKey = 69;// price in scrap, e.g. 31 / 9 = 3.55 ref
  17. //7.55
  18. static int BuyPricePerKey = 68; // price in scrap, e.g. 29 / 9 = 3.33 ref
  19.  
  20. //SHOW PRICES SETTINGS
  21. static string show_sell_price = "7.66";
  22. static string show_buy_price = "7.44";
  23.  
  24. static int TimerInterval = 170000;
  25. static int InviteTimerInterval = 2000;
  26.  
  27. int UserMetalAdded,UserScrapAdded,UserRecAdded,UserRefAdded,UserKeysAdded,BotKeysAdded,BotMetalAdded,BotScrapAdded,BotRecAdded,BotRefAdded,InventoryMetal,InventoryScrap,InventoryRec,InventoryRef,InventoryKeys,OverpayNumKeys,ExcessInScrap,PreviousKeys,WhileLoop,InvalidItem = 0;
  28.  
  29. double ExcessRefined = 0.0;
  30.  
  31. bool InGroupChat,TimerEnabled,HasRun,HasErrorRun,ChooseDonate,AskOverpay,IsOverpaying,HasCounted = false;
  32. bool TimerDisabled = true;
  33.  
  34. ulong uid;
  35. SteamID currentSID;
  36.  
  37. Timer adTimer = new System.Timers.Timer(TimerInterval);
  38. Timer inviteMsgTimer = new System.Timers.Timer(InviteTimerInterval);
  39.  
  40. public KeyUserHandler(Bot bot, SteamID sid) : base(bot, sid)
  41. {
  42.  
  43. }
  44.  
  45. public override void OnLoginCompleted()
  46. {
  47. }
  48.  
  49. public override bool OnFriendAdd()
  50. {
  51. Bot.log.Success(Bot.SteamFriends.GetFriendPersonaName(OtherSID) + " (" + OtherSID.ToString() + ") added me!");
  52. // Using a timer here because the message will fail to send if you do it too quickly
  53. inviteMsgTimer.Interval = InviteTimerInterval;
  54. inviteMsgTimer.Elapsed += (sender, e) => OnInviteTimerElapsed(sender, e, EChatEntryType.ChatMsg);
  55. inviteMsgTimer.Enabled = true;
  56. return true;
  57. }
  58.  
  59. public override void OnFriendRemove()
  60. {
  61. Bot.log.Success(Bot.SteamFriends.GetFriendPersonaName(OtherSID) + " (" + OtherSID.ToString() + ") removed me!");
  62. }
  63.  
  64. public override void OnMessage(string message, EChatEntryType type)
  65. {
  66. Bot.log.Warn("Message in the chat: "+message);
  67. message = message.ToLower();
  68.  
  69. if(message.Contains("hi") || message.Contains("hello") || message.Contains("sup") || message.Contains("hey")){
  70. Bot.SteamFriends.SendChatMessage(OtherSID, type, "Hello, I'm a key bot! Add me to trade so we can get started!");
  71. }
  72.  
  73. //REGULAR chat commands
  74. if (message.Contains("buying") || message.Contains("what") || message.Contains("how many") || message.Contains("how much") || message.Contains("price") || message.Contains("selling"))
  75. {
  76. Bot.SteamFriends.SendChatMessage(OtherSID, type, "I am currently buying keys at 7.44 Ref and selling at 7.66 Ref.");
  77. }
  78. else if ((message.Contains("love") || message.Contains("luv") || message.Contains("<3")) && (message.Contains("y") || message.Contains("u")))
  79. {
  80. if (message.Contains("do"))
  81. {
  82. Bot.SteamFriends.SendChatMessage(OtherSID, type, "I'd love you if I could, but robots are uncapable of love.");
  83. }
  84. else
  85. {
  86. Bot.SteamFriends.SendChatMessage(OtherSID, type, "I'd love you if I could, but robots are uncapable of love.");
  87. }
  88. }
  89. else if (message.Contains("stock"))
  90. {
  91. Bot.SteamFriends.SendChatMessage(OtherSID, type, "Current stock is displayed in trade");
  92. }
  93. 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"))
  94. {
  95. Bot.SteamFriends.SendChatMessage(OtherSID, type, "Sorry, but as a robot I cannot perform sexual functions.");
  96. }
  97. else if (message.Contains("thank"))
  98. {
  99. Bot.SteamFriends.SendChatMessage(OtherSID, type, "You're welcome!");
  100. }
  101. else if (message == "donate")
  102. {
  103. Bot.SteamFriends.SendChatMessage(OtherSID, type, "Please type that command into the TRADE WINDOW, not here! Thanks.");
  104. }
  105. else if (message == "owner")
  106. {
  107. Bot.SteamFriends.SendChatMessage(OtherSID, type, "Here is a link to my owners profile http://steamcommunity.com/id/BlaqkAudio1234567890/");
  108. }
  109. else if (message == "who")
  110. {
  111. Bot.SteamFriends.SendChatMessage(OtherSID, type, "Here is a link to my owners profile http://steamcommunity.com/id/BlaqkAudio1234567890/");
  112. }
  113. else if (message.Contains("help"))
  114. {
  115. Bot.SteamFriends.SendChatMessage(OtherSID, EChatEntryType.ChatMsg, "Hi. Thanks for using key 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!");
  116. }
  117. // ADMIN commands
  118. else if (IsAdmin)
  119. {
  120. if (message.StartsWith(".join"))
  121. {
  122. // Usage: .join GroupID - e.g. ".join 103582791433582049" or ".join cts" - this will allow the bot to join a group's chatroom
  123. if (message.Length >= 7)
  124. {
  125. if (message.Substring(6) == "cts")
  126. {
  127. uid = 103582791433582049;
  128. }
  129. else if (message.Substring(6) == "tf2")
  130. {
  131. uid = 103582791430075519;
  132. }
  133. else
  134. {
  135. ulong.TryParse(message.Substring(6), out uid);
  136. }
  137. var chatid = new SteamID(uid);
  138. Bot.SteamFriends.JoinChat(chatid);
  139. Bot.SteamFriends.SendChatMessage(OtherSID, type, "Joining chat: " + chatid.ConvertToUInt64().ToString());
  140. InGroupChat = true;
  141. Bot.SteamFriends.SetPersonaState(EPersonaState.Online);
  142. Bot.log.Success("Joining chat: " + chatid.ConvertToUInt64().ToString());
  143. }
  144. }
  145. else if (message.StartsWith(".leave"))
  146. {
  147. // Usage: .leave GroupID, same concept as joining
  148. if (message.Length >= 8)
  149. {
  150. if (message.Substring(7) == "cts")
  151. {
  152. uid = 103582791433582049;
  153. }
  154. else if (message.Substring(7) == "tf2")
  155. {
  156. uid = 103582791430075519;
  157. }
  158. else
  159. {
  160. ulong.TryParse(message.Substring(7), out uid);
  161. }
  162. var chatid = new SteamID(uid);
  163. Bot.SteamFriends.LeaveChat(chatid);
  164. Bot.SteamFriends.SendChatMessage(OtherSID, type, "Leaving chat: " + chatid.ConvertToUInt64().ToString());
  165. InGroupChat = false;
  166. Bot.log.Success("Leaving chat: " + chatid.ConvertToUInt64().ToString());
  167. }
  168. }
  169. else if (message.StartsWith(".sell"))
  170. {
  171. // Usage: .sell newprice "e.g. sell 26"
  172. int NewSellPrice = 0;
  173. if (message.Length >= 6)
  174. {
  175. Bot.SteamFriends.SendChatMessage(OtherSID, type, "Current selling price: " + SellPricePerKey + " scrap.");
  176. int.TryParse(message.Substring(5), out NewSellPrice);
  177. Bot.log.Success("Admin has requested that I set the new selling price from " + SellPricePerKey + " scrap to " + NewSellPrice + " scrap.");
  178. SellPricePerKey = NewSellPrice;
  179. Bot.SteamFriends.SendChatMessage(OtherSID, type, "Setting new selling price to: " + SellPricePerKey + " scrap.");
  180. Bot.log.Success("Successfully set new price.");
  181. }
  182. else
  183. {
  184. Bot.SteamFriends.SendChatMessage(OtherSID, type, "I need more arguments. Current selling price: " + SellPricePerKey + " scrap.");
  185. }
  186. }
  187. else if (message.StartsWith(".buy"))
  188. {
  189. // Usage: .buy newprice "e.g. .buy 24"
  190. int NewBuyPrice = 0;
  191. if (message.Length >= 5)
  192. {
  193. Bot.SteamFriends.SendChatMessage(OtherSID, type, "Current buying price: " + BuyPricePerKey + " scrap.");
  194. int.TryParse(message.Substring(4), out NewBuyPrice);
  195. Bot.log.Success("Admin has requested that I set the new selling price from " + BuyPricePerKey + " scrap to " + NewBuyPrice + " scrap.");
  196. BuyPricePerKey = NewBuyPrice;
  197. Bot.SteamFriends.SendChatMessage(OtherSID, type, "Setting new buying price to: " + BuyPricePerKey + " scrap.");
  198. Bot.log.Success("Successfully set new price.");
  199. }
  200. else
  201. {
  202. Bot.SteamFriends.SendChatMessage(OtherSID, type, "I need more arguments. Current buying price: " + BuyPricePerKey + " scrap.");
  203. }
  204. }
  205. else if (message.StartsWith(".gmessage"))
  206. {
  207. // usage: say ".gmessage Hello!" to the bot will send "Hello!" into group chat
  208. if (message.Length >= 10)
  209. {
  210. if (InGroupChat)
  211. {
  212. var chatid = new SteamID(uid);
  213. string gmessage = message.Substring(10);
  214. Bot.SteamFriends.SendChatRoomMessage(chatid, type, gmessage);
  215. Bot.log.Success("Said into group chat: " + gmessage);
  216. }
  217. else
  218. {
  219. Bot.log.Warn("Cannot send message because I am not in a group chatroom!");
  220. }
  221. }
  222. }
  223. else if (message == ".advertise")
  224. {
  225. // This will allow the bot to advertise into the last group it joined
  226. if (InGroupChat)
  227. {
  228. if (TimerDisabled == true)
  229. {
  230. TimerEnabled = true;
  231. Bot.SteamFriends.SendChatMessage(OtherSID, type, "Beginning advertisements.");
  232. Bot.log.Success("Beginning advertisements.");
  233. if (!HasRun)
  234. {
  235. adTimer.Interval = TimerInterval;
  236. adTimer.Elapsed += (sender, e) => OnTimerElapsed(sender, e, type);
  237. HasRun = true;
  238. }
  239. }
  240. else if (TimerDisabled == false)
  241. {
  242. TimerEnabled = false;
  243. Bot.SteamFriends.SendChatMessage(OtherSID, type, "Stopping advertisements.");
  244. Bot.log.Warn("Stopping advertisements.");
  245. HasRun = false;
  246. }
  247. Advertise(type);
  248. }
  249. }
  250. else if (message == ".canceltrade")
  251. {
  252. // Cancels the trade. Occasionally the message will be sent to YOU instead of the current user. Oops.
  253. Trade.CancelTrade();
  254. Bot.SteamFriends.SendChatMessage(currentSID, EChatEntryType.ChatMsg, "My creator has forcefully cancelled the trade. Whatever you were doing, he probably wants you to stop.");
  255. }
  256. else if (message == ".removeall")
  257. {
  258. // Commenting this out because RemoveAllFriends is a custom function I wrote.
  259. //Bot.SteamFriends.RemoveAllFriends();
  260. //Bot.log.Warn("Removed all friends from my friends list.");
  261. }
  262. }
  263. else
  264. {
  265. Bot.SteamFriends.SendChatMessage(OtherSID, type, Bot.ChatResponse);
  266. }
  267. }
  268.  
  269.  
  270.  
  271. public override bool OnTradeRequest()
  272. {
  273. Bot.log.Success(Bot.SteamFriends.GetFriendPersonaName(OtherSID) + " (" + OtherSID.ToString() + ") has requested to trade with me!");
  274. return true;
  275. }
  276.  
  277. public override void OnTradeError(string error)
  278. {
  279. Bot.SteamFriends.SendChatMessage(OtherSID,
  280. EChatEntryType.ChatMsg,
  281. "Error: " + error + "."
  282. );
  283. Bot.log.Warn(error);
  284. if (!HasErrorRun)
  285. {
  286. 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 profile! ");
  287. HasErrorRun = true;
  288. }
  289. Bot.SteamFriends.SetPersonaState(EPersonaState.Online);
  290. }
  291.  
  292. public override void OnTradeTimeout()
  293. {
  294. Bot.SteamFriends.SendChatMessage(OtherSID, EChatEntryType.ChatMsg,
  295. "Sorry, but you were either AFK or took too long and the trade was canceled.");
  296. Bot.log.Info("User was kicked because he was AFK.");
  297. Bot.SteamFriends.SetPersonaState(EPersonaState.Online);
  298. }
  299.  
  300. public override void OnTradeInit()
  301. {
  302. ReInit();
  303. TradeCountInventory(true);
  304.  
  305.  
  306. if (InventoryKeys == 0)
  307. {
  308. Trade.SendMessage("I don't have any keys to sell right now! I am currently buying keys for " + show_buy_price+ " ref.");
  309. }
  310. else if (InventoryMetal < BuyPricePerKey)
  311. {
  312. Trade.SendMessage("I don't have enough metal to buy keys! I am selling keys for " + show_sell_price + " ref.");
  313. }
  314. else
  315. {
  316. Trade.SendMessage("I am currently buying keys for " + show_buy_price + " ref, and selling keys for " + show_sell_price + " ref.");
  317. }
  318. Bot.SteamFriends.SetPersonaState(EPersonaState.Busy);
  319. }
  320.  
  321. public override void OnTradeAddItem(Schema.Item schemaItem, Inventory.Item inventoryItem)
  322. {
  323. var item = Trade.CurrentSchema.GetItem(schemaItem.Defindex);
  324.  
  325. if (!HasCounted)
  326. {
  327. 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.");
  328. }
  329. else if (InvalidItem >= 4)
  330. {
  331. Trade.CancelTrade();
  332. 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.");
  333. Bot.log.Warn("Booted user for messing around.");
  334. Bot.SteamFriends.SetPersonaState(EPersonaState.Online);
  335. }
  336. else if (item.Defindex == 5000)
  337. {
  338. // Put up scrap metal
  339. UserMetalAdded++;
  340. UserScrapAdded++;
  341. Bot.log.Success("User added: " + item.ItemName);
  342. }
  343. else if (item.Defindex == 5001)
  344. {
  345. // Put up reclaimed metal
  346. UserMetalAdded += 3;
  347. UserRecAdded++;
  348. Bot.log.Success("User added: " + item.ItemName);
  349. }
  350. else if (item.Defindex == 5002)
  351. {
  352. // Put up refined metal
  353. UserMetalAdded += 9;
  354. UserRefAdded++;
  355. Bot.log.Success("User added: " + item.ItemName);
  356. }
  357. else if (item.Defindex == 5021)
  358. {
  359. // Put up keys
  360. UserKeysAdded++;
  361. Bot.log.Success("User added: " + item.ItemName);
  362. // USER IS SELLING KEYS
  363. if (!ChooseDonate)
  364. {
  365. // BOT ADDS METAL
  366. int KeysToScrap = UserKeysAdded * BuyPricePerKey;
  367. if (InventoryMetal < KeysToScrap)
  368. {
  369. Trade.SendMessage("I only have " + InventoryMetal + " scrap. You need to remove some keys.");
  370. Bot.log.Warn("I don't have enough metal for the user.");
  371. }
  372. else
  373. {
  374. Trade.SendMessage("You have given me " + UserKeysAdded + " key(s). I will give you " + KeysToScrap + " scrap.");
  375. Bot.log.Success("User gave me " + UserKeysAdded + " key(s). I will now give him " + KeysToScrap + " scrap.");
  376. // Put up required metal
  377. bool DoneAddingMetal = false;
  378. while (!DoneAddingMetal)
  379. {
  380. if (InventoryRef > 0 && BotMetalAdded + 9 <= KeysToScrap)
  381. {
  382. Trade.AddItemByDefindex(5002);
  383. Bot.log.Warn("I added Refined Metal.");
  384. BotMetalAdded += 9;
  385. BotRefAdded++;
  386. InventoryRef--;
  387. }
  388. else if (InventoryRec > 0 && BotMetalAdded + 3 <= KeysToScrap)
  389. {
  390. Trade.AddItemByDefindex(5001);
  391. Bot.log.Warn("I added Reclaimed Metal.");
  392. BotMetalAdded += 3;
  393. BotRecAdded++;
  394. InventoryRec--;
  395. }
  396. else if (InventoryScrap > 0 && BotMetalAdded + 1 <= KeysToScrap)
  397. {
  398. Trade.AddItemByDefindex(5000);
  399. Bot.log.Warn("I added Scrap Metal.");
  400. BotMetalAdded++;
  401. BotScrapAdded++;
  402. InventoryScrap--;
  403. }
  404. else if (InventoryScrap == 0 && BotMetalAdded + 2 == KeysToScrap)
  405. {
  406. Trade.SendMessage("Sorry, but I don't have enough scrap metal to give you! Please remove some keys or add two keys.");
  407. Bot.log.Warn("Couldn't add enough metal for the user!");
  408. DoneAddingMetal = true;
  409. }
  410. else if (InventoryScrap == 0 && BotMetalAdded + 1 == KeysToScrap)
  411. {
  412. Trade.SendMessage("Sorry, but I don't have enough scrap metal to give you! Please remove some keys or add a key.");
  413. Bot.log.Warn("Couldn't add enough metal for the user!");
  414. DoneAddingMetal = true;
  415. }
  416. else if (BotMetalAdded == KeysToScrap)
  417. {
  418. Trade.SendMessage("Added enough metal. " + BotRefAdded + " ref, " + BotRecAdded + " rec, " + BotScrapAdded + " scrap.");
  419. Bot.log.Success("Gave user enough metal!");
  420. DoneAddingMetal = true;
  421. }
  422. }
  423. }
  424. }
  425. }
  426. else if (item.Defindex == 5049 || item.Defindex == 5067 || item.Defindex == 5072 || item.Defindex == 5073 || item.Defindex == 5079 || item.Defindex == 5081 || item.Defindex == 5628 || item.Defindex == 5631 || item.Defindex == 5632)
  427. {
  428. Trade.SendMessage("I'm sorry but I can't accept " + item.ItemName + "! This key used to be a special key such as a winter key that has turned back into a normal one. Unfortunately the ID is different and I am not coded to handle them at the moment. Please remove it and exchange it for a normal key!");
  429. Bot.log.Warn("User added a special key, but I cannot accept it.");
  430. }
  431. else
  432. {
  433. // Put up other items
  434. Trade.SendMessage("Sorry, I don't accept " + item.ItemName + "! I only accept metal/keys. Please remove it from the trade to continue.");
  435. Bot.log.Warn("User added: " + item.ItemName);
  436. InvalidItem++;
  437. }
  438. // USER IS BUYING KEYS
  439. if (!ChooseDonate)
  440. {
  441. if (UserMetalAdded % SellPricePerKey >= 0 && UserMetalAdded > 0)
  442. {
  443. // Count refined and convert to keys -- X scrap per key
  444. int NumKeys = UserMetalAdded / SellPricePerKey;
  445. if (NumKeys > 0 && NumKeys != PreviousKeys)
  446. {
  447. Trade.SendMessage("You put up enough metal for " + NumKeys + " key(s). Adding your keys now.");
  448. Bot.log.Success("User put up enough metal for " + NumKeys + " key(s).");
  449. if (NumKeys > InventoryKeys)
  450. {
  451. double excess = ((NumKeys - BotKeysAdded) * SellPricePerKey) / 9.0;
  452. string refined = string.Format("{0:N2}", excess);
  453. Trade.SendMessage("I only have " + InventoryKeys + " in my backpack. :(");
  454. Bot.log.Warn("User wanted to buy " + NumKeys + " key(s), but I only have " + InventoryKeys + " key(s).");
  455. Trade.SendMessage("Please remove " + refined + " ref.");
  456. NumKeys = InventoryKeys;
  457. }
  458. // Add the keys to the trade window
  459. for (int count = BotKeysAdded; count < NumKeys; count++)
  460. {
  461. Trade.AddItemByDefindex(5021);
  462. Bot.log.Warn("I am adding Mann Co. Supply Crate Key.");
  463. BotKeysAdded++;
  464.  
  465. //BUG FIX KEYS 1/2
  466. //InventoryKey--;
  467. }
  468. Trade.SendMessage("I have added " + BotKeysAdded + " key(s) for you.");
  469. Bot.log.Success("I have added " + BotKeysAdded + " key(s) for the user.");
  470. PreviousKeys = NumKeys;
  471. }
  472. }
  473. }
  474. }
  475.  
  476. public override void OnTradeRemoveItem(Schema.Item schemaItem, Inventory.Item inventoryItem)
  477. {
  478. var item = Trade.CurrentSchema.GetItem(schemaItem.Defindex);
  479. if (item.Defindex == 5000)
  480. {
  481. // Removed scrap metal
  482. UserMetalAdded--;
  483. UserScrapAdded--;
  484. Bot.log.Success("User removed: " + item.ItemName);
  485. }
  486. else if (item.Defindex == 5001)
  487. {
  488. // Removed reclaimed metal
  489. UserMetalAdded -= 3;
  490. UserRecAdded--;
  491. Bot.log.Success("User removed: " + item.ItemName);
  492. }
  493. else if (item.Defindex == 5002)
  494. {
  495. // Removed refined metal
  496. UserMetalAdded -= 9;
  497. UserRefAdded--;
  498. Bot.log.Success("User removed: " + item.ItemName);
  499. }
  500. else if (item.Defindex == 5021)
  501. {
  502. // Removed keys
  503. UserKeysAdded--;
  504. Bot.log.Success("User removed: " + item.ItemName);
  505. }
  506. else if (item.Defindex == 5049 || item.Defindex == 5067 || item.Defindex == 5072 || item.Defindex == 5073 || item.Defindex == 5079 || item.Defindex == 5081 || item.Defindex == 5628 || item.Defindex == 5631 || item.Defindex == 5632)
  507. {
  508. Bot.log.Warn("User removed special key.");
  509. }
  510. else
  511. {
  512. // Removed other items
  513. Bot.log.Warn("User removed: " + item.ItemName);
  514. }
  515. // User removes key from trade
  516. if (UserKeysAdded < (float) BotMetalAdded / BuyPricePerKey)
  517. {
  518. int KeysToScrap = UserKeysAdded * BuyPricePerKey;
  519. bool DoneAddingMetal = false;
  520. while (!DoneAddingMetal)
  521. {
  522. WhileLoop++;
  523. if (BotRefAdded > 0 && BotMetalAdded - 9 >= KeysToScrap)
  524. {
  525. Trade.RemoveItemByDefindex(5002);
  526. Bot.log.Warn("I removed Refined Metal.");
  527. BotMetalAdded -= 9;
  528. BotRefAdded--;
  529. InventoryRef++;
  530. }
  531. else if (BotRecAdded > 0 && BotMetalAdded - 3 >= KeysToScrap)
  532. {
  533. Trade.RemoveItemByDefindex(5001);
  534. Bot.log.Warn("I removed Reclaimed Metal.");
  535. BotMetalAdded -= 3;
  536. BotRecAdded--;
  537. InventoryRec++;
  538. }
  539. else if (BotScrapAdded > 0 && BotMetalAdded - 1 >= KeysToScrap)
  540. {
  541. Trade.RemoveItemByDefindex(5000);
  542. Bot.log.Warn("I removed Scrap Metal.");
  543. BotMetalAdded--;
  544. BotScrapAdded--;
  545. InventoryScrap++;
  546. }
  547. else if (BotMetalAdded == KeysToScrap)
  548. {
  549. DoneAddingMetal = true;
  550. }
  551. else if (WhileLoop > 50)
  552. {
  553. 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.");
  554. WhileLoop = 0;
  555. DoneAddingMetal = true;
  556. }
  557. }
  558. }
  559. // User removes metal from trade
  560. while ((float) UserMetalAdded / SellPricePerKey < BotKeysAdded)
  561. {
  562. Trade.RemoveItemByDefindex(5021);
  563. Bot.log.Warn("I removed Mann Co. Supply Crate Key.");
  564. BotKeysAdded--;
  565. InventoryKeys++;
  566. PreviousKeys = BotKeysAdded;
  567. IsOverpaying = false;
  568. }
  569. }
  570.  
  571. public override void OnTradeMessage(string message)
  572. {
  573. Bot.log.Info("[TRADE MESSAGE] " + message);
  574. message = message.ToLower();
  575.  
  576. if (message == "buy")
  577. {
  578. Trade.SendMessage("This is an old command and is unnecessary. Simply add your metal and I will add keys for you when you've put up enough. I am currently selling keys for " + String.Format("{0:0.00}", (SellPricePerKey / 9.0)) + " ref each.");
  579. Bot.log.Warn("User tried to use an old command.");
  580. }
  581.  
  582. if (message == "sell")
  583. {
  584. Trade.SendMessage("This is an old command and is unnecessary. Simply add your keys and I will automatically add the metal for you. I am currently buying keys for " + String.Format("{0:0.00}", (BuyPricePerKey / 9.0)) + " ref each.");
  585. Bot.log.Warn("User tried to use an old command.");
  586. }
  587.  
  588. if (message == "donate")
  589. {
  590. ChooseDonate = true;
  591. 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.");
  592. Bot.log.Success("User wants to donate!");
  593. }
  594.  
  595. if (message == "ready")
  596. {
  597. Trade.SendMessage("That is an old command and is unnecessary. I will automatically add keys and metal when you've put up enough.");
  598. Bot.log.Warn("User tried to use an old command.");
  599. }
  600.  
  601. if (message == "continue")
  602. {
  603. if (AskOverpay)
  604. {
  605. IsOverpaying = true;
  606. Trade.SendMessage("You have chosen to continue overpaying. Click \"Ready to Trade\" again to complete the trade.");
  607. Bot.log.Warn("User has chosen to continue overpaying!");
  608. }
  609. else
  610. {
  611. Trade.SendMessage("You cannot use this command right now!");
  612. Bot.log.Warn("User typed \"continue\" for no reason.");
  613. }
  614. }
  615.  
  616. }
  617.  
  618. public override void OnTradeReady(bool ready)
  619. {
  620. if (!ready)
  621. {
  622. Trade.SetReady(false);
  623. }
  624. else
  625. {
  626. Bot.log.Success("User is ready to trade!");
  627. if (Validate())
  628. {
  629. Trade.SetReady(true);
  630. }
  631. else
  632. {
  633. if (AskOverpay && OverpayNumKeys != 0 && !ChooseDonate)
  634. {
  635. double AdditionalRefined = (SellPricePerKey / 9.0) - ExcessRefined;
  636. string addRef = string.Format("{0:N2}", AdditionalRefined);
  637. string refined = string.Format("{0:N2}", ExcessRefined);
  638. 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.");
  639. 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.");
  640. }
  641. else
  642. {
  643. ResetTrade(false);
  644. }
  645. }
  646. }
  647. }
  648.  
  649. public override void OnTradeAccept()
  650. {
  651. if (Validate() || IsAdmin)
  652. {
  653. bool success = Trade.AcceptTrade();
  654. if (success)
  655. {
  656. Log.Success("Trade was successful!");
  657.  
  658. Bot.SteamFriends.SendChatMessage(OtherSID, EChatEntryType.ChatMsg, "Trade was successful! ");
  659. Bot.SteamFriends.SendChatMessage(OtherSID, EChatEntryType.ChatMsg, "your the best!");
  660. Bot.SteamFriends.SendChatMessage(OtherSID, EChatEntryType.ChatMsg, "If you had a issue or concern please leave a comment on my profile!");
  661. Bot.SteamFriends.SetPersonaState(EPersonaState.Online);
  662. }
  663. else
  664. {
  665. Log.Warn("Trade might have failed.");
  666. Bot.SteamFriends.SetPersonaState(EPersonaState.Online);
  667. }
  668. }
  669. OnTradeClose();
  670. }
  671.  
  672. public override void OnTradeClose()
  673. {
  674. bool didSomething = false;
  675. if (InventoryScrap < 9)
  676. {
  677. ulong[] smelt = new ulong[1];
  678. Bot.SetGamePlaying(440);
  679. Bot.GetInventory();
  680. Inventory myInventory = Bot.MyInventory;
  681. foreach (Inventory.Item item in myInventory.Items)
  682. {
  683. if (item.Defindex == 5001)
  684. {
  685. smelt[0] = item.Id;
  686. SteamBot.TF2GC.Crafting.CraftItems(Bot, smelt);
  687. didSomething = true;
  688. break;
  689. }
  690. }
  691. if (!didSomething)
  692. {
  693. foreach (Inventory.Item item in Trade.MyInventory.Items)
  694. {
  695. if (item.Defindex == 5002)
  696. {
  697. smelt[0] = item.Id;
  698. SteamBot.TF2GC.Crafting.CraftItems(Bot, smelt);
  699. didSomething = true;
  700. break;
  701. }
  702. }
  703. }
  704. }
  705. Bot.SetGamePlaying(0);
  706. Bot.SteamFriends.SetPersonaState(EPersonaState.Online);
  707. base.OnTradeClose();
  708. }
  709.  
  710. public bool Validate()
  711. {
  712. int ScrapCount = 0;
  713. int KeyCount = 0;
  714.  
  715. List<string> errors = new List<string>();
  716.  
  717. foreach (ulong id in Trade.OtherOfferedItems)
  718. {
  719. var item = Trade.OtherInventory.GetItem(id);
  720. if (item.Defindex == 5000)
  721. {
  722. ScrapCount++;
  723. }
  724. else if (item.Defindex == 5001)
  725. {
  726. ScrapCount += 3;
  727. }
  728. else if (item.Defindex == 5002)
  729. {
  730. ScrapCount += 9;
  731. }
  732. else if (item.Defindex == 5021)
  733. {
  734. KeyCount++;
  735. }
  736. else if (item.Defindex == 5049 || item.Defindex == 5067 || item.Defindex == 5072 || item.Defindex == 5073 || item.Defindex == 5079 || item.Defindex == 5081 || item.Defindex == 5628 || item.Defindex == 5631 || item.Defindex == 5632)
  737. {
  738. var schemaItem = Trade.CurrentSchema.GetItem(item.Defindex);
  739. errors.Add("I'm sorry, but I cannot accept " + schemaItem.ItemName + " because it is not in my code to process keys that used to be special keys like scorched keys or winter keys. Please exchange your key for a normal one and try again!");
  740. }
  741. else
  742. {
  743. var schemaItem = Trade.CurrentSchema.GetItem(item.Defindex);
  744. errors.Add("I can't accept " + schemaItem.ItemName + "!");
  745. }
  746. }
  747.  
  748. if (ChooseDonate)
  749. {
  750. foreach (ulong id in Trade.OtherOfferedItems)
  751. {
  752. var item = Trade.OtherInventory.GetItem(id);
  753. if (item.Defindex == 5021 || item.Defindex == 5049 || item.Defindex == 5067 || item.Defindex == 5072 || item.Defindex == 5073 || item.Defindex == 5079 || item.Defindex == 5081 || item.Defindex == 5628 || item.Defindex == 5631 || item.Defindex == 5632 || item.Defindex == 5000 || item.Defindex == 5001 || item.Defindex == 5002) { }
  754. else
  755. {
  756. var schemaItem = Trade.CurrentSchema.GetItem(item.Defindex);
  757. errors.Add("I'm sorry, but I cannot accept " + schemaItem.ItemName + "!");
  758. }
  759. }
  760.  
  761. if (BotMetalAdded > 0 || BotKeysAdded > 0)
  762. {
  763. errors.Add("You can't do that :( I still have items put up!");
  764. }
  765. }
  766. else if (UserKeysAdded > 0)
  767. {
  768. Bot.log.Warn("User has " + KeyCount + " key(s) put up. Verifying if " + (float) BotMetalAdded / BuyPricePerKey + " == " + KeyCount + ".");
  769. if (KeyCount != (float)BotMetalAdded / BuyPricePerKey)
  770. {
  771. 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.");
  772. }
  773. }
  774. else if (UserMetalAdded % SellPricePerKey != 0 && !IsOverpaying)
  775. {
  776. // Count refined and convert to keys -- X scrap per key
  777. OverpayNumKeys = UserMetalAdded / SellPricePerKey;
  778. ExcessInScrap = UserMetalAdded - (OverpayNumKeys * SellPricePerKey);
  779. ExcessRefined = (ExcessInScrap / 9.0);
  780. string refined = string.Format("{0:N2}", ExcessRefined);
  781. Trade.SendMessage("You put up enough metal for " + OverpayNumKeys + " key(s), with " + refined + " ref extra.");
  782. Bot.log.Success("User put up enough metal for " + OverpayNumKeys + " key(s), with " + refined + " ref extra.");
  783. if (OverpayNumKeys == 0)
  784. {
  785. double AdditionalRefined = (SellPricePerKey / 9.0) - ExcessRefined;
  786. string addRef = string.Format("{0:N2}", AdditionalRefined);
  787. errors.Add("ERROR: You need to add " + addRef + " ref for a key.");
  788. Bot.log.Warn("User doesn't have enough metal added, and needs add " + addRef + " ref for a key.");
  789. }
  790. else if (OverpayNumKeys >= 1)
  791. {
  792. errors.Add("You have put up more metal than what I'm asking.");
  793. AskOverpay = true;
  794. }
  795. }
  796. else if (UserMetalAdded > 0 && !IsOverpaying)
  797. {
  798. if (ScrapCount < BotKeysAdded * SellPricePerKey || (ScrapCount > BotKeysAdded * SellPricePerKey))
  799. {
  800. errors.Add("You must put up exactly " + String.Format("{0:0.00}", (SellPricePerKey / 9.0)) + " ref per key.");
  801. }
  802. }
  803.  
  804. // send the errors
  805. if (errors.Count != 0)
  806. Trade.SendMessage("There were errors in your trade: ");
  807.  
  808. foreach (string error in errors)
  809. {
  810. Trade.SendMessage(error);
  811. }
  812.  
  813. return errors.Count == 0;
  814. }
  815.  
  816.  
  817. bool hasDuplicateID(List<ulong> list)
  818. {
  819. var hashset = new HashSet<ulong>();
  820. foreach (var id in list)
  821. {
  822. if (!hashset.Add(id))
  823. {
  824. return true;
  825. }
  826. }
  827. return false;
  828. }
  829.  
  830.  
  831. public void TradeCountInventory(bool message)
  832. {
  833. // Let's count our inventory
  834.  
  835. Inventory.Item[] inventory = Trade.MyInventory.Items;
  836. InventoryMetal = 0;
  837. InventoryKeys = 0;
  838. InventoryRef = 0;
  839. InventoryRec = 0;
  840. InventoryScrap = 0;
  841. Bot.log.Warn("Items on BP: " +inventory.Length);
  842. foreach (Inventory.Item item in inventory)
  843. {
  844. if (item.Defindex == 5000)
  845. {
  846. InventoryMetal++;
  847. InventoryScrap++;
  848. }
  849. else if (item.Defindex == 5001)
  850. {
  851. InventoryMetal += 3;
  852. InventoryRec++;
  853. }
  854. else if (item.Defindex == 5002)
  855. {
  856. InventoryMetal += 9;
  857. InventoryRef++;
  858. }
  859. else if (item.Defindex == 5021)
  860. {
  861. InventoryKeys++;
  862. }
  863. }
  864. if (message)
  865. {
  866. double MetalToRef = (InventoryMetal / 9.0) - 0.01;
  867. string refined = string.Format("{0:N2}", MetalToRef);
  868. Trade.SendMessage("Current stock: ");
  869. Trade.SendMessage(InventoryKeys + " keys");
  870. Trade.SendMessage(InventoryRef + " ref");
  871. Trade.SendMessage(InventoryRec + " rec");
  872. Trade.SendMessage(InventoryScrap + " scrap");
  873. //Trade.SendMessage("Current stock: I have " + refined + " ref (" + InventoryRef + " ref, " + InventoryRec + " rec, and " + InventoryScrap + " scrap) and " + InventoryKeys + " key(s) in my backpack.");
  874. Bot.log.Success("Current stock: I have " + refined + " ref (" + InventoryRef + " ref, " + InventoryRec + " rec, and " + InventoryScrap + " scrap) and " + InventoryKeys + " key(s) in my backpack.");
  875. }
  876. HasCounted = true;
  877. }
  878.  
  879. public void ReInit()
  880. {
  881. UserMetalAdded = 0;
  882. UserRefAdded = 0;
  883. UserRecAdded = 0;
  884. UserScrapAdded = 0;
  885. UserKeysAdded = 0;
  886. BotKeysAdded = 0;
  887. BotMetalAdded = 0;
  888. BotRefAdded = 0;
  889. BotRecAdded = 0;
  890. BotScrapAdded = 0;
  891. OverpayNumKeys = 0;
  892. PreviousKeys = 0;
  893. ExcessInScrap = 0;
  894. ExcessRefined = 0.0;
  895. WhileLoop = 0;
  896. InvalidItem = 0;
  897. HasErrorRun = false;
  898. ChooseDonate = false;
  899. AskOverpay = false;
  900. IsOverpaying = false;
  901. HasCounted = false;
  902. currentSID = OtherSID;
  903. Bot.log.Info("All reseted");
  904. }
  905.  
  906. public void Advertise(EChatEntryType type)
  907. {
  908. if (TimerEnabled == true)
  909. {
  910. adTimer.Enabled = true;
  911. TimerDisabled = false;
  912.  
  913. }
  914. if (TimerEnabled == false)
  915. {
  916. adTimer.Enabled = false;
  917. adTimer.Stop();
  918. TimerDisabled = true;
  919. }
  920. }
  921.  
  922. private void OnTimerElapsed(object source, ElapsedEventArgs e, EChatEntryType type)
  923. {
  924. var chatid = new SteamID(uid);
  925. string adMessage;
  926. if (InventoryMetal > BuyPricePerKey && InventoryKeys > 0)
  927. {
  928. adMessage = "I am a keybanking trade bot! I am buying keys for " + show_buy_price + " ref and selling keys for " + show_sell_price + " ref. Send me a trade!";
  929. }
  930. else if (InventoryMetal < BuyPricePerKey)
  931. {
  932. adMessage = "I am a keybanking trade bot! I am only selling keys for " + show_sell_price + " ref at the moment. Send me a trade!";
  933. }
  934. else if (InventoryKeys == 0)
  935. {
  936. adMessage = "I am a keybanking trade bot! I am only buying keys for " + show_buy_price + " ref at the moment. Send me a trade!";
  937. }
  938. else
  939. {
  940. adMessage = "I am a keybanking trade bot! I am buying keys for " + show_buy_price + " ref and selling keys for " + show_sell_price + " ref. Send me a trade!";
  941. }
  942. Bot.SteamFriends.SendChatRoomMessage(chatid, type, adMessage);
  943. Bot.log.Success("Advertised into group chat: " + adMessage);
  944. }
  945.  
  946. private void OnInviteTimerElapsed(object source, ElapsedEventArgs e, EChatEntryType type)
  947. {
  948. Bot.SteamFriends.SendChatMessage(OtherSID, EChatEntryType.ChatMsg, "Hi. You have added Keybanking bot! Just trade me, and add your keys or metal to begin! ");
  949. Bot.log.Success("Sent welcome message.");
  950. inviteMsgTimer.Enabled = false;
  951. inviteMsgTimer.Stop();
  952. }
  953.  
  954. public void ResetTrade(bool message)
  955. {
  956.  
  957. for (int count = 0; count < BotKeysAdded; count++)
  958. {
  959. Trade.RemoveAllItemsByDefindex(5021);
  960. }
  961. for (int count = 0; count < BotScrapAdded; count++)
  962. {
  963. Trade.RemoveItemByDefindex(5000);
  964. }
  965. for (int count = 0; count < BotRecAdded; count++)
  966. {
  967. Trade.RemoveItemByDefindex(5001);
  968. }
  969. for (int count = 0; count < BotRefAdded; count++)
  970. {
  971. Trade.RemoveItemByDefindex(5002);
  972. }
  973. BotKeysAdded = 0;
  974. BotMetalAdded = 0;
  975. BotRefAdded = 0;
  976. BotRecAdded = 0;
  977. BotScrapAdded = 0;
  978. ChooseDonate = false;
  979. TradeCountInventory(message);
  980. Trade.SendMessage("Something went wrong! Scroll up to read the errors.");
  981. Bot.log.Warn("Something went wrong! I am resetting the trade.");
  982. Trade.SendMessage("I have reset the trade. Please try again. (If you chose to donate, you will need to type \"donate\" again)");
  983. Bot.log.Success("Reset trade.");
  984. }
  985. }
  986. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement