Advertisement
lowheartrate

DepositTradeOfferUserHandler.cs

Oct 27th, 2015
302
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 17.42 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.Threading;
  9. using System.IO;
  10. using System.Collections;
  11. using System.Text;
  12. using System.Net;
  13. using System.Web.Script.Serialization;
  14. using System.Timers;
  15.  
  16. using Newtonsoft.Json;
  17. using Newtonsoft.Json.Linq;
  18.  
  19. namespace SteamBot
  20. {
  21.     public class DepositTradeOfferUserHandler : UserHandler
  22.     {
  23.         public DepositTradeOfferUserHandler(Bot bot, SteamID sid) : base(bot, sid) { }
  24.  
  25.         System.Timers.Timer timer;
  26.  
  27.         public override void OnBotCommand(string command)
  28.         {
  29.             /* if (command.Equals ("withdraw")) {
  30.                 //Get current pot and all items in inventory
  31.                 string withdrawUrl = "http://csgowinbig.com/php/bot-withdraw.php";
  32.                 var withdrawRequest = (HttpWebRequest)WebRequest.Create (withdrawUrl);
  33.                 var withdrawResponse = (HttpWebResponse)withdrawRequest.GetResponse ();
  34.                 string withdrawString = new StreamReader (withdrawResponse.GetResponseStream()).ReadToEnd();
  35.  
  36.                 WithdrawResponse botInventory = JsonConvert.DeserializeObject<WithdrawResponse> (withdrawString);
  37.  
  38.                 var data = botInventory.data;
  39.  
  40.                 var rgInventory = data.rgInventory;
  41.                 var currentPot = data.currentPot;
  42.  
  43.                 var withdrawTradeOffer = Bot.NewTradeOffer (new SteamID(76561198020620333));
  44.  
  45.                 foreach (var inventoryItemKeyVal in rgInventory) {
  46.                     var invItem = inventoryItemKeyVal.Value;
  47.                     long classId = invItem.classid, instanceId = invItem.instanceid;
  48.  
  49.                     bool withdrawThisItem = true;
  50.                     //Check to see if this item is in the current pot
  51.                     foreach (var potItem in currentPot) {
  52.                         long classIdPot = potItem.classid, instanceIdPot = potItem.instanceid;
  53.  
  54.                         if (classId == classIdPot && instanceId == instanceIdPot) {
  55.                             withdrawThisItem = false;
  56.                         }
  57.                     }
  58.  
  59.                     if (withdrawThisItem) {
  60.                         var assetId = invItem.id;
  61.                         withdrawTradeOffer.Items.AddMyItem (730, 2, assetId, 1);
  62.                     }
  63.                 }
  64.  
  65.                 if (withdrawTradeOffer.Items.GetMyItems ().Count != 0) {
  66.                     string withdrawOfferId;
  67.                     withdrawTradeOffer.Send (out withdrawOfferId, "Here are the withdraw items requested.");
  68.                     Log.Success ("Withdraw trade offer sent. Offer ID: " + withdrawOfferId);
  69.                 } else {
  70.                     Log.Error ("There are no profit items to withdraw at this time.");
  71.                 }
  72.             } */
  73.         }
  74.  
  75.         public class WithdrawResponse {
  76.             public int success;
  77.             public string errMsg;
  78.             public WithdrawData data;
  79.         }
  80.  
  81.         public class WithdrawData {
  82.             public IDictionary<string, inventoryItem> rgInventory;
  83.             public List<inventoryItem> currentPot;
  84.         }
  85.  
  86.         public override void OnNewTradeOffer(TradeOffer offer)
  87.         {
  88.             //Get password from file on desktop
  89.             string pass = "Joseph";
  90.  
  91.             //Get items in the trade, and ID of user sending trade
  92.             var theirItems = offer.Items.GetTheirItems ();
  93.             var myItems = offer.Items.GetMyItems ();
  94.             var userID = offer.PartnerSteamId;
  95.  
  96.             bool shouldDecline = false;
  97.  
  98.             //Check if they are trying to get items from the bot
  99.             if (myItems.Count > 0 || theirItems.Count == 0) {
  100.                 shouldDecline = true;
  101.                 Log.Error ("Offer declined because the offer wasn't a gift; the user wanted items instead of giving.");
  102.             }
  103.  
  104.             //Check to make sure all items are for CS: GO.
  105.             foreach (TradeAsset item in theirItems) {
  106.                 if (item.AppId != 730) {
  107.                     shouldDecline = true;
  108.                     Log.Error ("Offer declined because one or more items was not for CS: GO.");
  109.                 }
  110.             }
  111.  
  112.             //Check if there are more than 10 items in the trade
  113.             if (theirItems.Count > 10) {
  114.                 shouldDecline = true;
  115.                 Log.Error ("Offer declined because there were more than 10 items in the deposit.");
  116.             }
  117.  
  118.             if (shouldDecline) {
  119.                 while (true) {
  120.                     try {
  121.                         if (offer.Decline ()) {
  122.                             Log.Error ("Offer declined.");
  123.  
  124.                             break;
  125.                         }
  126.                     } catch (WebException e) {
  127.                         Thread.Sleep (1000);
  128.  
  129.                         continue;
  130.                     }
  131.                 }
  132.  
  133.                 return;
  134.             }
  135.  
  136.             Log.Success ("Offer approved, accepting.");
  137.  
  138.             //Send items to server and check if all items add up to more than $1.
  139.             //If they do, accept the trade. If they don't, decline the trade.
  140.             string postData = "password=" + pass;
  141.             postData += "&owner=" + userID;
  142.  
  143.             string theirItemsJSON = JsonConvert.SerializeObject (theirItems);
  144.  
  145.             postData += "&items=" + theirItemsJSON;
  146.  
  147.             string url = "http://csgoheart.com/php/check-items.php";
  148.             var request = (HttpWebRequest)WebRequest.Create (url);
  149.  
  150.             var data = Encoding.ASCII.GetBytes(postData);
  151.  
  152.             request.Method = "POST";
  153.             request.ContentType = "application/x-www-form-urlencoded";
  154.             request.ContentLength = data.Length;
  155.  
  156.             using (var stream = request.GetRequestStream()) {
  157.                 stream.Write(data, 0, data.Length);
  158.             }
  159.  
  160.             var response = (HttpWebResponse)request.GetResponse();
  161.  
  162.             var responseString = new StreamReader(response.GetResponseStream()).ReadToEnd();
  163.  
  164.             //Uncomment this line to view the response from check-items.php
  165.             //Log.Success ("Response received from check-items.php: \n" + responseString);
  166.  
  167.             JSONClass responseJsonObj = JsonConvert.DeserializeObject<JSONClass> (responseString);
  168.  
  169.             if (responseJsonObj.success == 1) {
  170.                 //Get data array from json
  171.                 var jsonData = responseJsonObj.data;
  172.  
  173.                 if (jsonData.minDeposit == 1) {
  174.                     while (true) {
  175.                         try {
  176.                             if (offer.Accept ()) {
  177.                                 Log.Success ("Offer accepted from " + userID);
  178.  
  179.                                 //Put items into the pot
  180.                                 string urlPutItemsIn = "http://csgoheart.com/php/deposit.php";
  181.                                 var requestUrlPutItemsIn = (HttpWebRequest)WebRequest.Create (urlPutItemsIn);
  182.  
  183.                                 string postDataPutItemsIn = "password=" + pass;
  184.                                 postDataPutItemsIn += "&owner=" + userID;
  185.                                 postDataPutItemsIn += "&items=" + jsonData.allItems;
  186.                                 //Log.Success (jsonData.allItems);
  187.  
  188.                                 var dataPutItemsIn = Encoding.ASCII.GetBytes (postDataPutItemsIn);
  189.  
  190.                                 requestUrlPutItemsIn.Method = "POST";
  191.                                 requestUrlPutItemsIn.ContentType = "application/x-www-form-urlencoded";
  192.                                 requestUrlPutItemsIn.ContentLength = dataPutItemsIn.Length;
  193.  
  194.                                 using (var stream = requestUrlPutItemsIn.GetRequestStream ()) {
  195.                                     stream.Write (dataPutItemsIn, 0, dataPutItemsIn.Length);
  196.                                 }
  197.  
  198.                                 var responsePutItemsIn = (HttpWebResponse)requestUrlPutItemsIn.GetResponse ();
  199.                                 string responsePutItemsInString = new StreamReader (responsePutItemsIn.GetResponseStream ()).ReadToEnd ();
  200.  
  201.                                 //Uncomment this line to view the response from deposit.php
  202.                                 //Log.Success ("Response received from deposit.php: " + responsePutItemsInString);
  203.  
  204.                                 JSONClass responseJsonObjPutItemsIn = JsonConvert.DeserializeObject<JSONClass> (responsePutItemsInString);
  205.                                 jsonData = responseJsonObjPutItemsIn.data;
  206.  
  207.                                 break;
  208.                             }
  209.                         } catch (WebException e) {
  210.                             Thread.Sleep (1000);
  211.  
  212.                             continue;
  213.                         }
  214.                     }
  215.  
  216.  
  217.                     //Check if it should start the timer
  218.                     if (jsonData.startTimer == 1) {
  219.                         //Check if the timer is already running.
  220.                         if (!timerRunning) {
  221.                             timer = new System.Timers.Timer ();
  222.                             timer.Elapsed += new ElapsedEventHandler (timerEvent);
  223.                             timer.Interval = 1000;
  224.                             timer.Start ();
  225.  
  226.                             timerRunning = true;
  227.                         }
  228.                     }
  229.  
  230.                     //Check if the pot is over
  231.                     if (jsonData.potOver == 1) {
  232.                         //End the timer
  233.                         timerTime = 0;
  234.                         timer.Stop();
  235.  
  236.                         //Get items to give and keep, and the winner and their trade token
  237.                         var itemsToGive = jsonData.tradeItems;
  238.                         var itemsToKeep = jsonData.profitItems;
  239.  
  240.                         string winnerSteamIDString = jsonData.winnerSteamId;
  241.                         SteamID winnerSteamID = new SteamID (winnerSteamIDString);
  242.  
  243.                         string winnerTradeToken = jsonData.winnerTradeToken;
  244.  
  245.                         Log.Success ("Winner steam id: " + winnerSteamIDString + ", token: " + winnerTradeToken);
  246.  
  247.                         //Get bot's inventory json
  248.                         string botInvUrl = "http://steamcommunity.com/profiles/76561198149496042/inventory/json/730/2";
  249.                         var botInvRequest = (HttpWebRequest)WebRequest.Create (botInvUrl);
  250.                         var botInvResponse = (HttpWebResponse)botInvRequest.GetResponse ();
  251.                         string botInvString = new StreamReader (botInvResponse.GetResponseStream()).ReadToEnd();
  252.  
  253.                         BotInventory botInventory = JsonConvert.DeserializeObject<BotInventory> (botInvString);
  254.                         if (botInventory.success != true) {
  255.                             Log.Error ("An error occured while fetching the bot's inventory.");
  256.                             return;
  257.                         }
  258.                         var rgInventory = botInventory.rgInventory;
  259.  
  260.                         //Create trade offer for the winner
  261.                         var winnerTradeOffer = Bot.NewTradeOffer (winnerSteamID);
  262.  
  263.                         //Loop through all winner's items and add them to trade
  264.                         List<long> alreadyAddedToWinnerTrade = new List<long> ();
  265.                         foreach (CSGOItemFromWeb item in itemsToGive) {
  266.                             long classId = item.classId, instanceId = item.instanceId;
  267.  
  268.                             //Loop through all inventory items and find the asset id for the item
  269.                             long assetId = 0;
  270.                             foreach (var inventoryItem in rgInventory) {
  271.                                 var value = inventoryItem.Value;
  272.                                 long tAssetId = value.id, tClassId = value.classid, tInstanceId = value.instanceid;
  273.  
  274.                                 if (tClassId == classId && tInstanceId == instanceId) {
  275.                                     //Check if this assetId has already been added to the trade
  276.                                     if (alreadyAddedToWinnerTrade.Contains (tAssetId)) {
  277.                                         continue;
  278.                                         //This is for when there are 2 of the same weapon, but they have different assetIds
  279.                                     }
  280.                                     assetId = tAssetId;
  281.                                     break;
  282.                                 }
  283.                             }
  284.  
  285.                             //Log.Success ("Adding item to winner trade offer. Asset ID: " + assetId);
  286.  
  287.                             winnerTradeOffer.Items.AddMyItem (730, 2, assetId, 1);
  288.                             alreadyAddedToWinnerTrade.Add (assetId);
  289.                         }
  290.  
  291.                         //Send trade offer to winner
  292.                         if (itemsToGive.Count > 0) {
  293.                             string winnerTradeOfferId, winnerMessage = "Congratulations, you have won on CSGO Win Big! Here are your items.";
  294.  
  295.                             while (true) {
  296.                                 try {
  297.                                     winnerTradeOffer.SendWithToken (out winnerTradeOfferId, winnerTradeToken, winnerMessage);
  298.  
  299.                                     break;
  300.                                 } catch (WebException e) {
  301.                                     Thread.Sleep (1000);
  302.  
  303.                                     continue;
  304.                                 }
  305.                             }
  306.  
  307.                             Log.Success ("Offer sent to winner.");
  308.                         } else {
  309.                             Log.Info ("No items to give... strange");
  310.                         }
  311.                     }
  312.                 } else {
  313.                     while (true) {
  314.                         try {
  315.                             if (offer.Decline ()) {
  316.                                 Log.Error ("Minimum deposit not reached, offer declined.");
  317.  
  318.                                 break;
  319.                             }
  320.                         } catch (WebException e) {
  321.                             Thread.Sleep (1000);
  322.  
  323.                             continue;
  324.                         }
  325.                     }
  326.                 }
  327.             } else {
  328.                 while (true) {
  329.                     try {
  330.                         if (offer.Decline ()) {
  331.                             Log.Error ("Server deposit request failed, declining trade. Error message:\n" + responseJsonObj.errMsg);
  332.  
  333.                             break;
  334.                         }
  335.                     } catch (WebException e) {
  336.                         Thread.Sleep (1000);
  337.  
  338.                         continue;
  339.                     }
  340.                 }
  341.             }
  342.         }
  343.  
  344.         //Timer stuff
  345.         bool timerRunning = false;
  346.         int timerTime = 0;
  347.         private void timerEvent(object CancellationTokenSource, ElapsedEventArgs e) {
  348.             Log.Success ("The timer is running, time: " + timerTime);
  349.             timerTime++;
  350.  
  351.             //Check if the timer is at 2 minutes
  352.             if (timerTime >= 120) {
  353.                 //If the timer is done, stop the timer and call to server to end the round/pick a winner
  354.                 timer.Stop();
  355.                 timerTime = 0;
  356.                 timerRunning = false;
  357.  
  358.                 //Get password from file on desktop
  359.                 string pass = File.ReadAllText(@"C:\Users\Jordan Turley\Desktop\password.txt");
  360.  
  361.                 string urlTimerEnd = "http://csgowinbig.com/php/timer-end.php";
  362.                 var requestUrlTimerEnd = (HttpWebRequest)WebRequest.Create (urlTimerEnd);
  363.  
  364.                 string postDataTimerEnd = "password=" + pass;
  365.  
  366.                 var dataTimerEnd = Encoding.ASCII.GetBytes (postDataTimerEnd);
  367.  
  368.                 requestUrlTimerEnd.Method = "POST";
  369.                 requestUrlTimerEnd.ContentType = "application/x-www-form-urlencoded";
  370.                 requestUrlTimerEnd.ContentLength = dataTimerEnd.Length;
  371.  
  372.                 using (var stream = requestUrlTimerEnd.GetRequestStream ()) {
  373.                     stream.Write (dataTimerEnd, 0, dataTimerEnd.Length);
  374.                 }
  375.  
  376.                 var responseTimerEnd = (HttpWebResponse)requestUrlTimerEnd.GetResponse ();
  377.                 string responseTimerEndString = new StreamReader (responseTimerEnd.GetResponseStream ()).ReadToEnd ();
  378.  
  379.                 //Uncomment this line to print out the response from timer-end.php
  380.                 //Log.Success ("Response received from timer-end.php: " + responseTimerEndString);
  381.  
  382.                 JSONClass timerEndJsonObj = JsonConvert.DeserializeObject<JSONClass> (responseTimerEndString);
  383.  
  384.                 if (timerEndJsonObj.success != 1) {
  385.                     Log.Error ("Server request failed. Error message:\n" + timerEndJsonObj.errMsg);
  386.  
  387.                     return;
  388.                 }
  389.  
  390.                 Data timerEndData = timerEndJsonObj.data;
  391.  
  392.                 //Get items to give and keep, and the winner and their trade token
  393.                 var itemsToGive = timerEndData.tradeItems;
  394.                 var itemsToKeep = timerEndData.profitItems;
  395.  
  396.                 string winnerSteamIDString = timerEndData.winnerSteamId;
  397.                 SteamID winnerSteamID = new SteamID (winnerSteamIDString);
  398.  
  399.                 //Print something for the SteamID
  400.                 Log.Success ("STEAM ID 64: " + winnerSteamID.ToString() + " | RESPONSE FROM SERVER: " + winnerSteamIDString);
  401.  
  402.                 string winnerTradeToken = timerEndData.winnerTradeToken;
  403.  
  404.                 Log.Success ("Winner steam id: " + winnerSteamIDString + ", token: " + winnerTradeToken);
  405.  
  406.                 //Get bot's inventory json
  407.                 string botInvUrl = "http://steamcommunity.com/profiles/76561198238743988/inventory/json/730/2";
  408.                 var botInvRequest = (HttpWebRequest)WebRequest.Create (botInvUrl);
  409.                 var botInvResponse = (HttpWebResponse)botInvRequest.GetResponse ();
  410.                 string botInvString = new StreamReader (botInvResponse.GetResponseStream()).ReadToEnd();
  411.  
  412.                 BotInventory botInventory = JsonConvert.DeserializeObject<BotInventory> (botInvString);
  413.                 if (botInventory.success != true) {
  414.                     Log.Error ("An error occured while fetching the bot's inventory.");
  415.                     return;
  416.                 }
  417.                 var rgInventory = botInventory.rgInventory;
  418.  
  419.                 //Create trade offer for the winner
  420.                 var winnerTradeOffer = Bot.NewTradeOffer (winnerSteamID);
  421.  
  422.                 //Loop through all winner's items and add them to trade
  423.                 List<long> alreadyAddedToWinnerTrade = new List<long> ();
  424.                 foreach (CSGOItemFromWeb item in itemsToGive) {
  425.                     long classId = item.classId, instanceId = item.instanceId;
  426.  
  427.                     //Loop through all inventory items and find the asset id for the item
  428.                     long assetId = 0;
  429.                     foreach (var inventoryItem in rgInventory) {
  430.                         var value = inventoryItem.Value;
  431.                         long tAssetId = value.id, tClassId = value.classid, tInstanceId = value.instanceid;
  432.  
  433.                         if (tClassId == classId && tInstanceId == instanceId) {
  434.                             //Check if this assetId has already been added to the trade
  435.                             if (alreadyAddedToWinnerTrade.Contains (tAssetId)) {
  436.                                 continue;
  437.                                 //This is for when there are 2 of the same weapon, but they have different assetIds
  438.                             }
  439.                             assetId = tAssetId;
  440.                             break;
  441.                         }
  442.                     }
  443.  
  444.                     //Log.Success ("Adding item to winner trade offer. Asset ID: " + assetId);
  445.  
  446.                     winnerTradeOffer.Items.AddMyItem (730, 2, assetId, 1);
  447.                     alreadyAddedToWinnerTrade.Add (assetId);
  448.                 }
  449.  
  450.                 //Send trade offer to winner
  451.                 if (itemsToGive.Count > 0) {
  452.                     string winnerTradeOfferId, winnerMessage = "Congratulations, you have won on CSGO Win Big! Here are your items.";
  453.  
  454.                     while (true) {
  455.                         try {
  456.                             winnerTradeOffer.SendWithToken (out winnerTradeOfferId, winnerTradeToken, winnerMessage);
  457.  
  458.                             break;
  459.                         } catch (WebException e1) {
  460.                             Thread.Sleep (1000);
  461.  
  462.                             continue;
  463.                         }
  464.                     }
  465.  
  466.                     Log.Success ("Offer sent to winner.");
  467.                 } else {
  468.                     Log.Info ("No items to give... strange");
  469.                 }
  470.             }
  471.         }
  472.  
  473.         public override void OnMessage(string message, EChatEntryType type)
  474.         {
  475.             SendChatMessage (Bot.ChatResponse);
  476.         }
  477.  
  478.         public override bool OnGroupAdd() { return false; }
  479.  
  480.         public override bool OnFriendAdd() { return IsAdmin; }
  481.  
  482.         public override void OnFriendRemove() { }
  483.  
  484.         public override void OnLoginCompleted() { }
  485.  
  486.         public override bool OnTradeRequest() { return false; }
  487.  
  488.         public override void OnTradeError(string error) { }
  489.  
  490.         public override void OnTradeTimeout() { }
  491.  
  492.         public override void OnTradeSuccess() { }
  493.  
  494.         public override void OnTradeInit() { }
  495.  
  496.         public override void OnTradeAddItem(Schema.Item schemaItem, Inventory.Item inventoryItem) { }
  497.  
  498.         public override void OnTradeRemoveItem(Schema.Item schemaItem, Inventory.Item inventoryItem) { }
  499.  
  500.         public override void OnTradeMessage(string message) { }
  501.  
  502.         public override void OnTradeReady(bool ready) { }
  503.  
  504.         public override void OnTradeAccept() { }
  505.     }
  506.  
  507.     public class CSGOItem {
  508.         public long appId;
  509.         public long contextId;
  510.         public long assetId;
  511.     }
  512.  
  513.     public class JSONClass {
  514.         public int success;
  515.         public string errMsg; //Error message
  516.         public Data data;
  517.     }
  518.  
  519.     public class Data {
  520.         public int minDeposit;
  521.         public int potOver;
  522.         public int startTimer;
  523.         public string allItems;
  524.         public string winnerSteamId;
  525.         public string winnerTradeToken;
  526.         public List<CSGOItemFromWeb> tradeItems;
  527.         public List<CSGOItemFromWeb> profitItems;
  528.     }
  529.  
  530.     public class CSGOItemFromWeb {
  531.         public long classId;
  532.         public long instanceId;
  533.     }
  534.  
  535.     //Classes for json bot inventory
  536.     public class BotInventory {
  537.         public bool success;
  538.         public IDictionary<string, inventoryItem> rgInventory;
  539.     }
  540.  
  541.     public class inventoryItem {
  542.         public long id;
  543.         public long classid;
  544.         public long instanceid;
  545.     }
  546. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement