Advertisement
Guest User

Untitled

a guest
Sep 29th, 2015
236
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 11.08 KB | None | 0 0
  1. using SteamKit2;
  2. using SteamTrade;
  3. using SteamTrade.TradeOffer;
  4. using System;
  5. using System.Collections.Generic;
  6. using TradeAsset = SteamTrade.TradeOffer.TradeOffer.TradeStatusUser.TradeAsset;
  7.  
  8. using System.IO;
  9. using System.Collections;
  10. using System.Text;
  11. using System.Net;
  12. using System.Web.Script.Serialization;
  13.  
  14. using Newtonsoft.Json;
  15. using Newtonsoft.Json.Linq;
  16.  
  17. namespace SteamBot
  18. {
  19. public class DepositTradeOfferUserHandler : UserHandler
  20. {
  21. public DepositTradeOfferUserHandler(Bot bot, SteamID sid) : base(bot, sid) { }
  22.  
  23. public override void OnBotCommand(string command)
  24. {
  25. if (command.Equals ("withdraw")) {
  26. //Get current pot and all items in inventory
  27. string withdrawUrl = "http://localhost/php/bot-withdraw.php";
  28. var withdrawRequest = (HttpWebRequest)WebRequest.Create (withdrawUrl);
  29. var withdrawResponse = (HttpWebResponse)withdrawRequest.GetResponse ();
  30. string withdrawString = new StreamReader (withdrawResponse.GetResponseStream()).ReadToEnd();
  31.  
  32. WithdrawResponse botInventory = JsonConvert.DeserializeObject<WithdrawResponse> (withdrawString);
  33.  
  34. var data = botInventory.data;
  35.  
  36. var rgInventory = data.rgInventory;
  37. var currentPot = data.currentPot;
  38.  
  39. var withdrawTradeOffer = Bot.NewTradeOffer (new SteamID(EDIT:HIDDEN));
  40.  
  41. foreach (var inventoryItemKeyVal in rgInventory) {
  42. var invItem = inventoryItemKeyVal.Value;
  43. long classId = invItem.classid, instanceId = invItem.instanceid;
  44.  
  45. bool withdrawThisItem = true;
  46. //Check to see if this item is in the current pot
  47. foreach (var potItem in currentPot) {
  48. long classIdPot = potItem.classid, instanceIdPot = potItem.instanceid;
  49.  
  50. if (classId == classIdPot && instanceId == instanceIdPot) {
  51. withdrawThisItem = false;
  52. }
  53. }
  54.  
  55. if (withdrawThisItem) {
  56. var assetId = invItem.id;
  57. withdrawTradeOffer.Items.AddMyItem (730, 2, assetId, 1);
  58. }
  59. }
  60.  
  61. if (withdrawTradeOffer.Items.GetMyItems ().Count != 0) {
  62. string withdrawOfferId;
  63. withdrawTradeOffer.Send (out withdrawOfferId, "Here are the withdraw items requested.");
  64. Log.Success ("Withdraw trade offer sent. Offer ID: " + withdrawOfferId);
  65. } else {
  66. Log.Error ("There are no profit items to withdraw at this time.");
  67. }
  68. }
  69. }
  70.  
  71. public class WithdrawResponse {
  72. public int success;
  73. public string errMsg;
  74. public WithdrawData data;
  75. }
  76.  
  77. public class WithdrawData {
  78. public IDictionary<string, inventoryItem> rgInventory;
  79. public List<inventoryItem> currentPot;
  80. }
  81.  
  82. public override void OnNewTradeOffer(TradeOffer offer)
  83. {
  84. //Get password from file on desktop EDIT BY ME: wont get a password, too much errors
  85. string pass = "Pironi";
  86.  
  87. //Get items in the trade, and ID of user sending trade
  88. var theirItems = offer.Items.GetTheirItems ();
  89. var myItems = offer.Items.GetMyItems ();
  90. var userID = offer.PartnerSteamId;
  91.  
  92. bool shouldDecline = false;
  93.  
  94. //Check if they are trying to get items from the bot
  95. if (myItems.Count > 0 || theirItems.Count == 0) {
  96. shouldDecline = true;
  97. Log.Error ("Offer declined because the offer wasn't a gift; the user wanted items instead of giving.");
  98. }
  99.  
  100. //Check to make sure all items are for CS: GO.
  101. foreach (TradeAsset item in theirItems) {
  102. if (item.AppId != 730) {
  103. shouldDecline = true;
  104. Log.Error ("Offer declined because one or more items was not for CS: GO.");
  105. }
  106. }
  107.  
  108. //Check if there are more than 10 items in the trade
  109. if (theirItems.Count > 10) {
  110. shouldDecline = true;
  111. Log.Error ("Offer declined because there were more than 10 items in the deposit.");
  112. }
  113.  
  114. if (shouldDecline) {
  115. if (offer.Decline ()) {
  116. Log.Error ("Offer declined.");
  117. }
  118. return;
  119. }
  120.  
  121. Log.Success ("Offer approved, accepting.");
  122.  
  123. //Send items to server and check if all items add up to more than $1.
  124. //If they do, accept the trade. If they don't, decline the trade.
  125. string postData = "password=" + pass;
  126. postData += "&owner=" + userID;
  127.  
  128. string theirItemsJSON = JsonConvert.SerializeObject (theirItems);
  129.  
  130. postData += "&items=" + theirItemsJSON;
  131.  
  132. string url = "http://localhost/php/check-items.php";
  133. var request = (HttpWebRequest)WebRequest.Create (url);
  134.  
  135. var data = Encoding.ASCII.GetBytes(postData);
  136.  
  137. request.Method = "POST";
  138. request.ContentType = "application/x-www-form-urlencoded";
  139. request.ContentLength = data.Length;
  140.  
  141. using (var stream = request.GetRequestStream()) {
  142. stream.Write(data, 0, data.Length);
  143. }
  144.  
  145. var response = (HttpWebResponse)request.GetResponse();
  146.  
  147. var responseString = new StreamReader(response.GetResponseStream()).ReadToEnd();
  148.  
  149. //Log.Success ("Response received from server: \n" + responseString);
  150.  
  151. JSONClass responseJsonObj = JsonConvert.DeserializeObject<JSONClass> (responseString);
  152.  
  153. if (responseJsonObj.success == 1) {
  154. //Get data array from json
  155. var jsonData = responseJsonObj.data;
  156.  
  157. if (jsonData.minDeposit == 1) {
  158. if (offer.Accept ()) {
  159. Log.Success ("Offer accepted from " + userID);
  160.  
  161. //Put items into the pot
  162. string urlPutItemsIn = "http://localhost/php/deposit.php";
  163. var requestUrlPutItemsIn = (HttpWebRequest)WebRequest.Create (urlPutItemsIn);
  164.  
  165. string postDataPutItemsIn = "password=" + pass;
  166. postDataPutItemsIn += "&owner=" + userID;
  167. postDataPutItemsIn += "&items=" + jsonData.allItems;
  168. //Log.Success (jsonData.allItems);
  169.  
  170. var dataPutItemsIn = Encoding.ASCII.GetBytes (postDataPutItemsIn);
  171.  
  172. requestUrlPutItemsIn.Method = "POST";
  173. requestUrlPutItemsIn.ContentType = "application/x-www-form-urlencoded";
  174. requestUrlPutItemsIn.ContentLength = dataPutItemsIn.Length;
  175.  
  176. using (var stream = requestUrlPutItemsIn.GetRequestStream ()) {
  177. stream.Write (dataPutItemsIn, 0, dataPutItemsIn.Length);
  178. }
  179.  
  180. var responsePutItemsIn = (HttpWebResponse)requestUrlPutItemsIn.GetResponse ();
  181. string responsePutItemsInString = new StreamReader (responsePutItemsIn.GetResponseStream ()).ReadToEnd ();
  182. JSONClass responseJsonObjPutItemsIn = JsonConvert.DeserializeObject<JSONClass> (responsePutItemsInString);
  183. jsonData = responseJsonObjPutItemsIn.data;
  184. }
  185.  
  186. //Check if the pot is over
  187. if (jsonData.potOver == 1) {
  188. //Get items to give and keep, and the winner and their trade token
  189. var itemsToGive = jsonData.tradeItems;
  190. var itemsToKeep = jsonData.profitItems;
  191.  
  192. string winnerSteamIDString = jsonData.winnerSteamID;
  193. SteamID winnerSteamID = new SteamID (winnerSteamIDString);
  194.  
  195. string winnerTradeToken = jsonData.winnerTradeToken;
  196.  
  197. Log.Success ("Winner steam id: " + winnerSteamIDString + ", token: " + winnerTradeToken);
  198.  
  199. //Get bot's inventory json
  200. string botInvUrl = "http://steamcommunity.com/profiles/EDIT:HIDDEN/inventory/json/730/2";
  201. var botInvRequest = (HttpWebRequest)WebRequest.Create (botInvUrl);
  202. var botInvResponse = (HttpWebResponse)botInvRequest.GetResponse ();
  203. string botInvString = new StreamReader (botInvResponse.GetResponseStream()).ReadToEnd();
  204.  
  205. BotInventory botInventory = JsonConvert.DeserializeObject<BotInventory> (botInvString);
  206. if (botInventory.success != true) {
  207. Log.Error ("An error occured while fetching the bot's inventory.");
  208. return;
  209. }
  210. var rgInventory = botInventory.rgInventory;
  211.  
  212. //Create trade offer for the winner
  213. var winnerTradeOffer = Bot.NewTradeOffer (winnerSteamID);
  214.  
  215. //Loop through all winner's items and add them to trade
  216. List<long> alreadyAddedToWinnerTrade = new List<long> ();
  217. foreach (CSGOItemFromWeb item in itemsToGive) {
  218. long classId = item.classId, instanceId = item.instanceId;
  219.  
  220. //Loop through all inventory items and find the asset id for the item
  221. long assetId = 0;
  222. foreach (var inventoryItem in rgInventory) {
  223. var value = inventoryItem.Value;
  224. long tAssetId = value.id, tClassId = value.classid, tInstanceId = value.instanceid;
  225.  
  226. if (tClassId == classId && tInstanceId == instanceId) {
  227. //Check if this assetId has already been added to the trade
  228. if (alreadyAddedToWinnerTrade.Contains (tAssetId)) {
  229. continue;
  230. //This is for when there are 2 of the same weapon, but they have different assetIds
  231. }
  232. assetId = tAssetId;
  233. break;
  234. }
  235. }
  236.  
  237. //Log.Success ("Adding item to winner trade offer. Asset ID: " + assetId);
  238.  
  239. winnerTradeOffer.Items.AddMyItem (730, 2, assetId, 1);
  240. alreadyAddedToWinnerTrade.Add (assetId);
  241. }
  242.  
  243. //Send trade offer to winner
  244. if (itemsToGive.Count > 0) {
  245. string winnerTradeOfferId, winnerMessage = "Congratulations, you have won on ...! Here are your items.";
  246. winnerTradeOffer.SendWithToken (out winnerTradeOfferId, winnerTradeToken, winnerMessage);
  247. Log.Success ("Offer sent to winner.");
  248. } else {
  249. Log.Info ("No items to give... strange");
  250. }
  251. }
  252. } else {
  253. if (offer.Decline ()) {
  254. Log.Error ("Minimum deposit not reached, offer declined.");
  255. }
  256. }
  257. } else {
  258. Log.Error ("Server deposit request failed, declining trade. Error message:\n" + responseJsonObj.errMsg);
  259. offer.Decline ();
  260. }
  261. }
  262.  
  263. public override void OnMessage(string message, EChatEntryType type)
  264. {
  265. SendChatMessage (Bot.ChatResponse);
  266. }
  267.  
  268. public override bool OnGroupAdd() { return false; }
  269.  
  270. public override bool OnFriendAdd() { return IsAdmin; }
  271.  
  272. public override void OnFriendRemove() { }
  273.  
  274. public override void OnLoginCompleted() { }
  275.  
  276. public override bool OnTradeRequest() { return false; }
  277.  
  278. public override void OnTradeError(string error) { }
  279.  
  280. public override void OnTradeTimeout() { }
  281.  
  282. public override void OnTradeSuccess() { }
  283.  
  284. public override void OnTradeInit() { }
  285.  
  286. public override void OnTradeAddItem(Schema.Item schemaItem, Inventory.Item inventoryItem) { }
  287.  
  288. public override void OnTradeRemoveItem(Schema.Item schemaItem, Inventory.Item inventoryItem) { }
  289.  
  290. public override void OnTradeMessage(string message) { }
  291.  
  292. public override void OnTradeReady(bool ready) { }
  293.  
  294. public override void OnTradeAccept() { }
  295. }
  296.  
  297. public class CSGOItem {
  298. public long appId;
  299. public long contextId;
  300. public long assetId;
  301. }
  302.  
  303. public class JSONClass {
  304. public int success;
  305. public string errMsg; //Error message
  306. public Data data;
  307. }
  308.  
  309. public class Data {
  310. public int minDeposit;
  311. public int potOver;
  312. public string allItems;
  313. public string winnerSteamID;
  314. public string winnerTradeToken;
  315. public List<CSGOItemFromWeb> tradeItems;
  316. public List<CSGOItemFromWeb> profitItems;
  317. }
  318.  
  319. public class CSGOItemFromWeb {
  320. public long classId;
  321. public long instanceId;
  322. }
  323.  
  324. //Classes for json bot inventory
  325. public class BotInventory {
  326. public bool success;
  327. public IDictionary<string, inventoryItem> rgInventory;
  328. }
  329.  
  330. public class inventoryItem {
  331. public long id;
  332. public long classid;
  333. public long instanceid;
  334. }
  335. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement