/// <summary> /// Sends trade offer to user /// </summary> /// <param name="offer">Offer to send</param> /// <returns>Returns empty if failed, else offer id</returns> private string RequestTradeOffer(TradeOffer offer, Config.TradeObject trade, string message) { string offerId = string.Empty; string exceptionMsg = string.Empty; for (int i = 0; i < 5; i++) { try { if (offer.SendWithToken(out offerId, trade.RU_Token, message)) { mLogOffer.Write(Log.LogLevel.Debug, $"Trade offer sent to user {offer.PartnerSteamId} with id {offerId}"); break; } } catch (WebException ex) { var respStream = ex?.Response?.GetResponseStream(); if (respStream != null) { var read = new StreamReader(respStream).ReadToEnd(); exceptionMsg = $"Webexeption: {read}"; } } catch (Exception ex) { exceptionMsg = $"Exception: {ex.Message}"; } mLogOffer.Write(Log.LogLevel.Warn, $"Unable to send trade offer to user {trade.SteamId}. Trying again in 3 seconds."); Thread.Sleep(3000); } if (string.IsNullOrWhiteSpace(offerId)) { mLogOffer.Write(Log.LogLevel.Error, $"Failed to send trade offer to user {trade.SteamId}. Error: {exceptionMsg}"); } return(offerId); }
private void CreateTradeOffer(Bot bot, GameServerRequest gsr) { float tradeValue = -1; string message = gsr.Arguments; string[] assetIDs = null; string[] myAssetIDs = null; string[] steamIDitems = message.Split('/'); SteamID steamid = GetSteamIDFromString(steamIDitems[0]); if (!steamid.IsValid) { return; } TradeOffer to = bot.TradeOfferManager.NewOffer(steamid); GameServer gameServer = bot.Manager.GetServerByID(gsr.ServerID); if (steamIDitems[1].Length > 1 && steamIDitems[1] != "NULL") { assetIDs = steamIDitems[1].Split(','); List <long> contextId = new List <long>(); contextId.Add(2); bot.OtherGenericInventory.load((int)Games.CSGO, contextId, steamid); foreach (GenericInventory.Item item in bot.OtherGenericInventory.items.Values) { if (Array.IndexOf(assetIDs, item.assetid.ToString()) > -1) { GenericInventory.ItemDescription description = (ItemDescription)bot.OtherGenericInventory.getDescription(item.assetid); to.Items.AddTheirItem(item.appid, item.contextid, (long)item.assetid); } } bot.OtherGenericInventory.load((int)Games.TF2, contextId, steamid); foreach (GenericInventory.Item item in bot.OtherGenericInventory.items.Values) { if (Array.IndexOf(assetIDs, item.assetid.ToString()) > -1) { GenericInventory.ItemDescription description = (ItemDescription)bot.OtherGenericInventory.getDescription(item.assetid); to.Items.AddTheirItem(item.appid, item.contextid, (long)item.assetid); } } bot.OtherGenericInventory.load((int)Games.Dota2, contextId, steamid); foreach (GenericInventory.Item item in bot.OtherGenericInventory.items.Values) { if (Array.IndexOf(assetIDs, item.assetid.ToString()) > -1) { GenericInventory.ItemDescription description = (ItemDescription)bot.OtherGenericInventory.getDescription(item.assetid); to.Items.AddTheirItem(item.appid, item.contextid, (long)item.assetid); } } } if (steamIDitems[2].Length > 1 && steamIDitems[2] != "NULL") { myAssetIDs = steamIDitems[2].Split(','); List <long> contextId = new List <long>(); contextId.Add(2); bot.MyGenericInventory.load((int)Games.CSGO, contextId, bot.getSteamID()); foreach (GenericInventory.Item item in bot.MyGenericInventory.items.Values) { if (Array.IndexOf(myAssetIDs, item.assetid.ToString()) > -1) { ItemDescription description = (ItemDescription)bot.MyGenericInventory.getDescription(item.assetid); to.Items.AddMyItem(item.appid, item.contextid, (long)item.assetid); } } bot.MyGenericInventory.load((int)Games.TF2, contextId, bot.getSteamID()); foreach (GenericInventory.Item item in bot.MyGenericInventory.items.Values) { if (Array.IndexOf(myAssetIDs, item.assetid.ToString()) > -1) { ItemDescription description = (ItemDescription)bot.MyGenericInventory.getDescription(item.assetid); to.Items.AddMyItem(item.appid, item.contextid, (long)item.assetid); } } bot.MyGenericInventory.load((int)Games.Dota2, contextId, bot.getSteamID()); foreach (GenericInventory.Item item in bot.MyGenericInventory.items.Values) { if (Array.IndexOf(myAssetIDs, item.assetid.ToString()) > -1) { ItemDescription description = (ItemDescription)bot.MyGenericInventory.getDescription(item.assetid); to.Items.AddMyItem(item.appid, item.contextid, (long)item.assetid); } } } if (steamIDitems[3] != "NULL") { tradeValue = float.Parse(steamIDitems[3], CultureInfo.InvariantCulture); } string offerId = ""; string token = bot.GetToken(steamid); if (bot.Friends.Contains(steamid.ConvertToUInt64())) { to.Send(out offerId, String.Format("\"{0}\" the {1}@{2}", gameServer.Name, DateTime.Now.ToString("dd/MM/yyyy"), DateTime.Now.ToString("HH:mm"))); } else if (token != null) { to.SendWithToken(out offerId, token, String.Format("\"{0}\" the {1}@{2}", gameServer.Name, DateTime.Now.ToString("dd/MM/yyyy"), DateTime.Now.ToString("HH:mm"))); } else { bot.Manager.Send(gsr.ServerID, gsr.ModuleID, NetworkCode.ASteambotCode.TradeToken, steamid.ConvertToUInt64().ToString() + "/" + "trade_token_not_found"); } if (offerId != "") { bot.Manager.Send(gsr.ServerID, gsr.ModuleID, NetworkCode.ASteambotCode.CreateTradeOffer, steamid.ConvertToUInt64() + "/" + offerId); bot.TradeoffersGS.Add(offerId, gsr.ServerID + "|" + gsr.ModuleID + "|" + tradeValue); bot.TradeOfferValue.Add(offerId, tradeValue); bot.AcceptMobileTradeConfirmation(offerId); bot.UpdateTradeOfferInDatabase(to, tradeValue); } else { if (token != null) { bot.Manager.Send(gsr.ServerID, gsr.ModuleID, NetworkCode.ASteambotCode.TradeToken, steamid.ConvertToUInt64().ToString() + "/" + "trade_token_invalid"); } bot.Manager.Send(gsr.ServerID, gsr.ModuleID, NetworkCode.ASteambotCode.CreateTradeOffer, steamid.ConvertToUInt64() + "/" + "-1"); } }