Trading: Lots of cleanup and improvements

This commit is contained in:
JustArchi
2016-01-14 01:30:12 +01:00
parent 047af8912f
commit 3df34ece61
10 changed files with 262 additions and 94 deletions

View File

@@ -97,8 +97,12 @@
<Compile Include="Logging.cs" />
<Compile Include="Program.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
<Compile Include="SteamInventoryItem.cs" />
<Compile Include="SteamItem.cs" />
<Compile Include="SteamTradeItem.cs" />
<Compile Include="SteamTradeItemList.cs" />
<Compile Include="SteamTradeOffer.cs" />
<Compile Include="SteamTradeOfferRequest.cs" />
<Compile Include="Trading.cs" />
<Compile Include="Utilities.cs" />
<Compile Include="WCF.cs" />

View File

@@ -375,50 +375,59 @@ namespace ArchiSteamFarm {
internal async Task<List<SteamInventoryItem>> GetInventory() {
List<SteamInventoryItem> result = new List<SteamInventoryItem>();
try {
JObject jobj = await WebBrowser.UrlGetToJObject("http://steamcommunity.com/my/inventory/json/753/6", Cookie).ConfigureAwait(false);
IList<JToken> results = jobj.SelectTokens("$.rgInventory.*").ToList();
foreach (JToken res in results) {
result.Add(JsonConvert.DeserializeObject<SteamInventoryItem>(res.ToString()));
JObject jObject = await WebBrowser.UrlGetToJObject("https://steamcommunity.com/my/inventory/json/753/6", Cookie).ConfigureAwait(false);
IEnumerable<JToken> jTokens = jObject.SelectTokens("$.rgInventory.*");
foreach (JToken jToken in jTokens) {
result.Add(JsonConvert.DeserializeObject<SteamInventoryItem>(jToken.ToString()));
}
} catch (Exception) {
//just return empty list on error
} catch (Exception e) {
Logging.LogGenericException(Bot.BotName, e);
}
return result;
}
internal async Task<bool> SendTradeOffer(List<SteamInventoryItem> itemsSend, string masterid,string token=null) {
internal async Task<bool> SendTradeOffer(List<SteamInventoryItem> items, ulong partnerID, string token = null) {
if (items == null || partnerID == 0) {
return false;
}
string sessionID;
if (!Cookie.TryGetValue("sessionid", out sessionID)) {
return false;
}
SteamTradeItemList items = new SteamTradeItemList();
foreach (var item in itemsSend) {
items.assets.Add(new SteamTradeItem(753, 6, Int32.Parse(item.amount), item.id));
SteamTradeOfferRequest trade = new SteamTradeOfferRequest();
foreach (SteamInventoryItem item in items) {
trade.me.assets.Add(new SteamTradeItem() {
appid = 753,
contextid = 6,
amount = int.Parse(item.amount),
assetid = item.id
});
}
SteamTradeOfferRequest trade = new SteamTradeOfferRequest(true, 2, items, new SteamTradeItemList());
string referer = "https://steamcommunity.com/tradeoffer/new";
string request = referer + "/send";
string referer = String.Format("https://steamcommunity.com/tradeoffer/new/?partner={0}", ((Int32)Int64.Parse(masterid)).ToString());
if (!string.IsNullOrEmpty(token)) {
referer += String.Format("&token={0}",token);
}
Dictionary <string, string> postData = new Dictionary<string, string>() {
Dictionary<string, string> postData = new Dictionary<string, string>() {
{"sessionid", sessionID},
{"serverid","1" },
{"partner",masterid },
{"tradeoffermessage","sent by ASF" },
{"json_tradeoffer",JsonConvert.SerializeObject(trade) },
{"trade_offer_create_params",string.IsNullOrEmpty(token)?"":String.Format("{{ \"trade_offer_access_token\":\"{0}\" }}", token) }
{"serverid", "1"},
{"partner", partnerID.ToString()},
{"tradeoffermessage", "Sent by ASF"},
{"json_tradeoffer", JsonConvert.SerializeObject(trade)},
{"trade_offer_create_params", string.IsNullOrEmpty(token) ? "" : string.Format("{{ \"trade_offer_access_token\":\"{0}\" }}", token)} // TODO: This should be rewrote
};
HttpResponseMessage response = await WebBrowser.UrlPost("https://steamcommunity.com/tradeoffer/new/send", postData, Cookie, referer).ConfigureAwait(false);
HttpResponseMessage response = await WebBrowser.UrlPost(request, postData, Cookie, referer).ConfigureAwait(false);
if (response == null) {
return false;
}
return response.IsSuccessStatusCode;
return true;
}
internal async Task<HtmlDocument> GetBadgePage(int page) {

View File

@@ -72,7 +72,7 @@ namespace ArchiSteamFarm {
internal string SteamPassword { get; private set; } = "null";
internal string SteamNickname { get; private set; } = "null";
internal string SteamApiKey { get; private set; } = "null";
internal string SteamTradeToken {get; private set; } = "null";
internal string SteamTradeToken { get; private set; } = "null";
internal string SteamParentalPIN { get; private set; } = "0";
internal ulong SteamMasterID { get; private set; } = 0;
internal ulong SteamMasterClanID { get; private set; } = 0;
@@ -83,7 +83,7 @@ namespace ArchiSteamFarm {
internal bool UseAsfAsMobileAuthenticator { get; private set; } = false;
internal bool ShutdownOnFarmingFinished { get; private set; } = false;
internal bool SendOnFarmingFinished { get; private set; } = false;
internal uint SendTradePeriod {get; private set; } = 0;
internal uint SendTradePeriod { get; private set; } = 0;
internal HashSet<uint> Blacklist { get; private set; } = new HashSet<uint>();
internal bool Statistics { get; private set; } = true;
@@ -485,32 +485,35 @@ namespace ArchiSteamFarm {
}
internal static async Task<string> ResponseSendTrade(string botName) {
Bot bot;
string token=null;
if (string.IsNullOrEmpty(botName)) {
return "Error, no name specified";
return null;
}
Bot bot;
if (!Bots.TryGetValue(botName, out bot)) {
return "Couldn't find any bot named " + botName + "!";
}
if (bot.SendTradePeriod!=0) {
bot.Timer.Change(TimeSpan.FromHours(bot.SendTradePeriod),Timeout.InfiniteTimeSpan);
if (bot.SteamMasterID == 0) {
return "Trade couldn't be send because SteamMasterID is not defined!";
}
if (bot.SteamMasterID==0) {
return "No master set";
string token = null;
if (!string.IsNullOrEmpty(bot.SteamTradeToken) && !bot.SteamTradeToken.Equals("null")) {
token = bot.SteamTradeToken;
}
if ((!string.IsNullOrEmpty(bot.SteamTradeToken))&&(!bot.SteamTradeToken.Equals("null"))) {
token=bot.SteamTradeToken;
List<SteamInventoryItem> inventory = await bot.ArchiWebHandler.GetInventory().ConfigureAwait(false);
if (inventory.Count == 0) {
return "Nothing to send, inventory seems empty!";
}
List<SteamInventoryItem> inv = await bot.ArchiWebHandler.GetInventory().ConfigureAwait(false);
if (inv.Count == 0) {
return "Nothing to send";
}
if (await bot.ArchiWebHandler.SendTradeOffer(inv, bot.SteamMasterID.ToString(),token).ConfigureAwait(false)) {
if (await bot.ArchiWebHandler.SendTradeOffer(inventory, bot.SteamMasterID, token).ConfigureAwait(false)) {
await bot.AcceptAllConfirmations().ConfigureAwait(false);
return "Trade offer sent";
return "Trade offer sent successfully!";
} else {
return "Trade offer failed due to error!";
}
return "Error sending trade offer";
}
internal static string Response2FA(string botName) {
@@ -1042,7 +1045,7 @@ namespace ArchiSteamFarm {
await CardsFarmer.StartFarming().ConfigureAwait(false);
if (SendTradePeriod!=0) {
if (SendTradePeriod != 0) {
Timer = new Timer(
async e => await ResponseSendTrade(BotName).ConfigureAwait(false),
null,

View File

@@ -1,12 +1,44 @@

namespace ArchiSteamFarm
{
public class SteamInventoryItem
{
public string id { get; set; }
public string classid { get; set; }
public string instanceid { get; set; }
public string amount { get; set; }
public int pos { get; set; }
}
/*
_ _ _ ____ _ _____
/ \ _ __ ___ | |__ (_)/ ___| | |_ ___ __ _ _ __ ___ | ___|__ _ _ __ _ __ ___
/ _ \ | '__|/ __|| '_ \ | |\___ \ | __|/ _ \ / _` || '_ ` _ \ | |_ / _` || '__|| '_ ` _ \
/ ___ \ | | | (__ | | | || | ___) || |_| __/| (_| || | | | | || _|| (_| || | | | | | | |
/_/ \_\|_| \___||_| |_||_||____/ \__|\___| \__,_||_| |_| |_||_| \__,_||_| |_| |_| |_|
Copyright 2015 Łukasz "JustArchi" Domeradzki
Contact: JustArchi@JustArchi.net
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
using Newtonsoft.Json;
namespace ArchiSteamFarm {
internal class SteamInventoryItem {
[JsonProperty]
internal string id { get; set; }
[JsonProperty]
internal string classid { get; set; }
[JsonProperty]
internal string instanceid { get; set; }
[JsonProperty]
internal string amount { get; set; }
[JsonProperty]
internal int pos { get; set; }
}
}

View File

@@ -22,16 +22,34 @@
*/
using Newtonsoft.Json;
namespace ArchiSteamFarm {
internal sealed class SteamItem {
// REF: https://developer.valvesoftware.com/wiki/Steam_Web_API/IEconService#CEcon_Asset
[JsonProperty]
internal string appid { get; set; }
[JsonProperty]
internal string contextid { get; set; }
[JsonProperty]
internal string assetid { get; set; }
[JsonProperty]
internal string currencyid { get; set; }
[JsonProperty]
internal string classid { get; set; }
[JsonProperty]
internal string instanceid { get; set; }
[JsonProperty]
internal string amount { get; set; }
[JsonProperty]
internal bool missing { get; set; }
}
}

View File

@@ -1,15 +1,41 @@

/*
_ _ _ ____ _ _____
/ \ _ __ ___ | |__ (_)/ ___| | |_ ___ __ _ _ __ ___ | ___|__ _ _ __ _ __ ___
/ _ \ | '__|/ __|| '_ \ | |\___ \ | __|/ _ \ / _` || '_ ` _ \ | |_ / _` || '__|| '_ ` _ \
/ ___ \ | | | (__ | | | || | ___) || |_| __/| (_| || | | | | || _|| (_| || | | | | | | |
/_/ \_\|_| \___||_| |_||_||____/ \__|\___| \__,_||_| |_| |_||_| \__,_||_| |_| |_| |_|
Copyright 2015 Łukasz "JustArchi" Domeradzki
Contact: JustArchi@JustArchi.net
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
using Newtonsoft.Json;
namespace ArchiSteamFarm {
public class SteamTradeItem {
public int appid { get; set; }
public int contextid { get; set; }
public int amount { get; set; }
public string assetid { get; set; }
public SteamTradeItem (int aid, int cid, int am, string asset) {
appid = aid;
contextid = cid;
amount = am;
assetid = asset;
}
internal class SteamTradeItem {
[JsonProperty]
internal int appid { get; set; }
[JsonProperty]
internal int contextid { get; set; }
[JsonProperty]
internal int amount { get; set; }
[JsonProperty]
internal string assetid { get; set; }
}
}

View File

@@ -1,14 +1,39 @@
using System.Collections.Generic;
/*
_ _ _ ____ _ _____
/ \ _ __ ___ | |__ (_)/ ___| | |_ ___ __ _ _ __ ___ | ___|__ _ _ __ _ __ ___
/ _ \ | '__|/ __|| '_ \ | |\___ \ | __|/ _ \ / _` || '_ ` _ \ | |_ / _` || '__|| '_ ` _ \
/ ___ \ | | | (__ | | | || | ___) || |_| __/| (_| || | | | | || _|| (_| || | | | | | | |
/_/ \_\|_| \___||_| |_||_||____/ \__|\___| \__,_||_| |_| |_||_| \__,_||_| |_| |_| |_|
Copyright 2015 Łukasz "JustArchi" Domeradzki
Contact: JustArchi@JustArchi.net
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
using Newtonsoft.Json;
using System.Collections.Generic;
namespace ArchiSteamFarm {
public class SteamTradeItemList {
public List<SteamTradeItem> assets { get; set; }
public List<string> currency { get; set; }
public bool ready { get; set; }
public SteamTradeItemList() {
assets = new List<SteamTradeItem>();
currency = new List<string>();
ready = false;
}
internal class SteamTradeItemList {
[JsonProperty]
internal List<SteamTradeItem> assets { get; set; } = new List<SteamTradeItem>();
[JsonProperty]
internal List<string> currency { get; set; } = new List<string>();
[JsonProperty]
internal bool ready { get; set; } = false;
}
}

View File

@@ -22,6 +22,7 @@
*/
using Newtonsoft.Json;
using SteamKit2;
using System.Collections.Generic;
@@ -49,18 +50,43 @@ namespace ArchiSteamFarm {
MobileApp
}
[JsonProperty]
internal string tradeofferid { get; set; }
[JsonProperty]
internal int accountid_other { get; set; }
[JsonProperty]
internal string message { get; set; }
[JsonProperty]
internal int expiration_time { get; set; }
[JsonProperty]
internal ETradeOfferState trade_offer_state { get; set; }
internal List<SteamItem> items_to_give { get; set; }
internal List<SteamItem> items_to_receive { get; set; }
[JsonProperty]
internal List<SteamItem> items_to_give { get; set; } = new List<SteamItem>();
[JsonProperty]
internal List<SteamItem> items_to_receive { get; set; } = new List<SteamItem>();
[JsonProperty]
internal bool is_our_offer { get; set; }
[JsonProperty]
internal int time_created { get; set; }
[JsonProperty]
internal int time_updated { get; set; }
[JsonProperty]
internal bool from_real_time_trade { get; set; }
[JsonProperty]
internal int escrow_end_date { get; set; }
[JsonProperty]
internal ETradeOfferConfirmationMethod confirmation_method { get; set; }
// Extra

View File

@@ -1,15 +1,41 @@

/*
_ _ _ ____ _ _____
/ \ _ __ ___ | |__ (_)/ ___| | |_ ___ __ _ _ __ ___ | ___|__ _ _ __ _ __ ___
/ _ \ | '__|/ __|| '_ \ | |\___ \ | __|/ _ \ / _` || '_ ` _ \ | |_ / _` || '__|| '_ ` _ \
/ ___ \ | | | (__ | | | || | ___) || |_| __/| (_| || | | | | || _|| (_| || | | | | | | |
/_/ \_\|_| \___||_| |_||_||____/ \__|\___| \__,_||_| |_| |_||_| \__,_||_| |_| |_| |_|
Copyright 2015 Łukasz "JustArchi" Domeradzki
Contact: JustArchi@JustArchi.net
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
using Newtonsoft.Json;
namespace ArchiSteamFarm {
public class SteamTradeOfferRequest {
public bool newversion { get; set; }
public int version { get; set; }
public SteamTradeItemList me { get; set; }
public SteamTradeItemList them { get; set; }
public SteamTradeOfferRequest (bool nv, int v, SteamTradeItemList m, SteamTradeItemList t) {
newversion = nv;
version = v;
me = m;
them = t;
}
}
internal class SteamTradeOfferRequest {
[JsonProperty]
internal bool newversion { get; set; } = true;
[JsonProperty]
internal int version { get; set; } = 2;
[JsonProperty]
internal SteamTradeItemList me { get; set; } = new SteamTradeItemList();
[JsonProperty]
internal SteamTradeItemList them { get; set; } = new SteamTradeItemList();
}
}

View File

@@ -1,7 +1,7 @@
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<!-- This is full-fledged example config, you may be also interested in minimal.xml for bare minimum one -->
<!-- This config includes all user-switchable properties that you may want to change on per-bot basis -->
<!-- Default values used in config match default ASF values when given config property is not found -->
<!-- In other words, if given property is not defined, ASF will assume default pre-programmed value -->
@@ -104,7 +104,6 @@
<!-- Personally I suggest leaving it at "false", unless you have a reason to close the process after all bots finished farming -->
<ShutdownOnFarmingFinished type="bool" value="false"/>
<!-- if this switch set to "true",the bot would try to send trade offer with all cards to master after farming is finished -->
<!-- for sucessfull trade offer bot needs SteamMasterID to be set properly -->
<!-- if the master is not a friend of this bot, SteamTradeToken would be needed also -->