private bool HandleTradeOfferUpdate(Offer offer) { if (knownTradeOffers.ContainsKey(offer.TradeOfferId) && knownTradeOffers[offer.TradeOfferId] == offer.TradeOfferState) { return(false); } //make sure the api loaded correctly sometimes the items are missing if (IsOfferValid(offer)) { SendOfferToHandler(offer); } else { var resp = webApi.GetTradeOffer(offer.TradeOfferId); if (IsOfferValid(resp.Offer)) { SendOfferToHandler(resp.Offer); } else { Debug.WriteLine("Offer returned from steam api is not valid : " + resp.Offer.TradeOfferId); return(false); } } return(true); }
/// <summary> /// Gets the currently active trade offers from the web api and processes them /// </summary> /// <returns></returns> public bool GetActiveTradeOffers() { var offers = webApi.GetActiveTradeOffers(false, true, false); if (offers != null && offers.TradeOffersReceived != null) { foreach (var offer in offers.TradeOffersReceived) { if (offer.TradeOfferState == TradeOfferState.TradeOfferStateActive && !tradeOfferHistory.Contains(offer.TradeOfferId)) { //make sure the api loaded correctly sometimes the items are missing if (IsOfferValid(offer)) { SendOfferToHandler(offer); } else { var resp = webApi.GetTradeOffer(offer.TradeOfferId); if (IsOfferValid(resp.Offer)) { SendOfferToHandler(resp.Offer); } else { //todo: log steam api is giving us invalid offers. Debug.WriteLine("Offer returned from steam api is not valid : " + resp.Offer.TradeOfferId); } } } } return(true); } return(false); }
public TradeOfferAcceptResponse Accept(string tradeOfferId, string partnerId = "") { var data = new NameValueCollection(); data.Add("sessionid", steamWeb.SessionId); data.Add("serverid", "1"); data.Add("tradeofferid", tradeOfferId); if (partnerId != "") { data.Add("partner", partnerId); } string url = string.Format("https://steamcommunity.com/tradeoffer/{0}/accept", tradeOfferId); string referer = string.Format("https://steamcommunity.com/tradeoffer/{0}/", tradeOfferId); string resp = steamWeb.Fetch(url, "POST", data, false, referer, true); if (!String.IsNullOrEmpty(resp)) { try { var res = JsonConvert.DeserializeObject <TradeOfferAcceptResponse>(resp); //steam can return 'null' response if (res != null) { res.Accepted = string.IsNullOrEmpty(res.TradeError); return(res); } } catch (JsonException) { return(new TradeOfferAcceptResponse { TradeError = "Error parsing server response: " + resp }); } } //if it didn't work as expected, check the state, maybe it was accepted after all var tradeOffer = webApi.GetTradeOffer(tradeOfferId); var state = TradeOfferState.TradeOfferStateUnknown; if (tradeOffer != null && tradeOffer.Offer != null) { state = tradeOffer.Offer.TradeOfferState; } if (state != TradeOfferState.TradeOfferStateAccepted) { File.AppendAllText("failedtrades", "+++++++++++++++++++++++++++++\n"); File.AppendAllText("failedtrades", JObject.FromObject(tradeOffer).ToString(Formatting.Indented)); File.AppendAllText("failedtrades", "\n----------------------------\n"); } return(new TradeOfferAcceptResponse { Accepted = state == TradeOfferState.TradeOfferStateAccepted }); }