예제 #1
0
        /// <summary>
        /// Executes a POST against the OpSkins API.
        /// </summary>
        /// <param name="url">A string containing the URL for the web request.</param>
        /// <param name="authToken">A string containing the OpSkins authentication token to use for the request.</param>
        /// <param name="postData">A NameValueCollection containing any data to be POST'd as part of the web request.</param>
        /// <returns>An ApiResponse of T containing the result returned by the web request.</returns>
        internal static ApiResponse <T> ExecutePost(string url, string authToken, NameValueCollection postData = null)
        {
            string          Response;
            ApiResponse <T> ReturnValue;

            Response    = OpSkinsWebRequest.ExecutePost(url, authToken, postData);
            ReturnValue = ProcessResponse(Response);

            return(ReturnValue);
        }
        /// <summary>
        /// Cancels the specified trade offer.
        /// </summary>
        /// <param name="tradeOfferID">An int indicating the identifier of the trade offer.</param>
        /// <returns>A bool indicating success.</returns>
        public bool CancelTradeOffer(int tradeOfferID)
        {
            bool ReturnValue = false;

            try
            {
                NameValueCollection PostData;
                ApiResponse <CancelOfferResponse> Result;

                // Build the post data
                PostData = new NameValueCollection
                {
                    { "offer_id", tradeOfferID.ToString() }
                };

                // Execute the request
                Result = OpSkinsWebRequest <CancelOfferResponse> .ExecutePost(BuildUrl("ITrade/CancelOffer/v1/"), _AuthToken, PostData);

                if (Result != null && Result.Response != null && Result.Response.TradeOffer != null)
                {
                    TradeOffer TheOffer;

                    TheOffer = Result.Response.TradeOffer;
                    if (TheOffer.ID == tradeOfferID)
                    {
                        if (TheOffer.State == TradeOfferState.Cancelled || TheOffer.State == TradeOfferState.Declined)
                        {
                            ReturnValue = true;
                        }
                    }
                    else
                    {
                        throw new InvalidDataException(string.Format("The API returned an invalid trade offer identifier ({0}) during cancellation", TheOffer.ID));
                    }
                }
                else
                {
                    if (Result.Message.Contains("cancel an inactive"))
                    {
                    }
                    else
                    {
                        throw new InvalidOperationException("No result data was received");
                    }
                }
            }
            catch (Exception ex)
            {
                throw new InvalidOperationException("Trade offer could not be cancelled", ex);
            }

            return(ReturnValue);
        }
        /// <summary>
        /// Accepts the specified trade offer.
        /// </summary>
        /// <param name="tradeOfferID">An int containing the identifier of the trade offer.</param>
        /// <returns>A bool indicating success.</returns>
        public bool AcceptTradeOffer(int tradeOfferID)
        {
            bool ReturnValue = false;

            try
            {
                string Code;
                NameValueCollection PostData;
                ApiResponse <AcceptTradeResponse> Result;

                Code = Authenticator.Compute2FA(_2FA);

                // Build the post data
                PostData = new NameValueCollection
                {
                    { "twofactor_code", Code },
                    { "offer_id", tradeOfferID.ToString() }
                };

                // Execute the request
                Result = OpSkinsWebRequest <AcceptTradeResponse> .ExecutePost(BuildUrl("ITrade/AcceptOffer/v1/"), _AuthToken, PostData);

                if (Result != null && Result.Response.TradeOffer != null)
                {
                    ReturnValue = true;
                }
                else
                {
                    throw new InvalidOperationException("No result data was received");
                }
            }
            catch (Exception ex)
            {
                throw new Exception("Trade offer could not be accepted", ex);
            }

            return(ReturnValue);
        }
        /// <summary>
        /// Sends a trade with the specified assets to the specified URL, displaying the specified message.
        /// </summary>
        /// <param name="tradeURL">A string containing the trade URL to send the trade to.</param>
        /// <param name="itemsToSend">A List of string containing the identifiers of the assets to send.</param>
        /// <param name="itemsToReceive">A List of string containing the identifiers of the assets to receive.</param>
        /// <param name="message">A string containing the message to include with the trade.</param>
        /// <param name="tradeID">A string that will be set to the identifier of the trade.</param>
        /// <param name="tradeMessage">A string that will be set to any message associated with the trade.</param>
        /// <returns>A bool indicating whether the request was successful.</returns>
        public bool SendTrade(string tradeURL, List <string> itemsToSend, List <string> itemsToReceive, string message, out string tradeID, out string tradeMessage)
        {
            bool ReturnValue = false;

            // Defaults
            tradeID      = null;
            tradeMessage = null;

            try
            {
                // Check the trade URL is valid
                if (HelperFunctions.TryParseTradeURL(tradeURL, out string UID, out string Token))
                {
                    // Check that there are items
                    if ((itemsToSend != null && itemsToSend.Count > 0) ||
                        (itemsToReceive != null && itemsToReceive.Count > 0))
                    {
                        int Retries = 0;

                        while (Retries++ < 5)
                        {
                            string Code;
                            NameValueCollection             PostData;
                            ApiResponse <SendOfferResponse> Result;

                            Code = Authenticator.Compute2FA(_2FA);

                            // Build the post data
                            PostData = new NameValueCollection
                            {
                                { "twofactor_code", Code },
                                { "uid", UID },
                                { "token", Token }
                            };
                            //PostData.Add("items", BuildItemList(items));
                            if (itemsToReceive != null && itemsToReceive.Count > 0)
                            {
                                PostData.Add("items_to_receive", BuildItemList(itemsToReceive));
                            }
                            if (itemsToSend != null && itemsToSend.Count > 0)
                            {
                                PostData.Add("items_to_send", BuildItemList(itemsToSend));
                            }
                            if (message != null)
                            {
                                PostData.Add("message", message);
                            }

                            // Execute the request
                            Result = OpSkinsWebRequest <SendOfferResponse> .ExecutePost(BuildUrl("/ITrade/SendOffer/v1/"), _AuthToken, PostData);

                            if (Result != null)
                            {
                                if (Result.Response != null && Result.Response.TradeOffer != null)
                                {
                                    TradeOffer TheOffer;

                                    TheOffer = Result.Response.TradeOffer;
                                    switch (TheOffer.State)
                                    {
                                    case TradeOfferState.Active:
                                        tradeID     = TheOffer.ID.ToString();
                                        ReturnValue = true;
                                        break;

                                    default:
                                        tradeMessage = TheOffer.StateName;
                                        break;
                                    }

                                    Retries = 10;
                                }
                                else if (Result.Message != null)
                                {
                                    if (Result.Message == "Two-factor code already used.")
                                    {
                                        tradeMessage = "The bot reported an error whilst sending the trade";
                                        throw new InvalidOperationException("The specified 2FA code has already been used");
                                    }
                                    else if (Result.Message.Contains("that do not belong to you"))
                                    {
                                        tradeMessage = "The chosen items are not valid for this trade";
                                        throw new InvalidOperationException("The specified items do not belong to the user");
                                    }
                                    else
                                    {
                                        tradeMessage = Result.Message;
                                        throw new InvalidOperationException(string.Format("The Send Trade call returned: {0}", Result.Message));
                                    }
                                }
                            }
                            else
                            {
                                throw new InvalidOperationException("No result data was received");
                            }
                        }
                    }
                    else
                    {
                        throw new ArgumentException("No items were specified for the trade", "itemsToSend");
                    }
                }
                else
                {
                    tradeMessage = "The user's trade URL appears to be invalid";
                    throw new ArgumentException("The specified trade URL is invalid", "tradeURL");
                }
            }