Improve statistics code

This commit is contained in:
JustArchi
2018-03-24 17:14:01 +01:00
parent dec3d580a6
commit a5cf0fc694

View File

@@ -35,6 +35,8 @@ namespace ArchiSteamFarm {
private const byte MinPersonaStateTTL = 8; // Minimum amount of hours we must wait before requesting persona state update
private const string URL = "https://" + SharedInfo.StatisticsServer;
private static readonly HashSet<Steam.Asset.EType> AcceptedMatchableTypes = new HashSet<Steam.Asset.EType> { Steam.Asset.EType.Emoticon, Steam.Asset.EType.FoilTradingCard, Steam.Asset.EType.ProfileBackground, Steam.Asset.EType.TradingCard };
private readonly Bot Bot;
private readonly SemaphoreSlim RequestsSemaphore = new SemaphoreSlim(1, 1);
@@ -67,8 +69,8 @@ namespace ArchiSteamFarm {
const string request = URL + "/Api/HeartBeat";
Dictionary<string, string> data = new Dictionary<string, string>(2) {
{ "SteamID", Bot.CachedSteamID.ToString() },
{ "Guid", Program.GlobalDatabase.Guid.ToString("N") }
{ "Guid", Program.GlobalDatabase.Guid.ToString("N") },
{ "SteamID", Bot.CachedSteamID.ToString() }
};
// We don't need retry logic here
@@ -87,14 +89,6 @@ namespace ArchiSteamFarm {
return;
}
// Don't announce if we don't meet conditions
string tradeToken;
if (!Bot.HasMobileAuthenticator || !Bot.BotConfig.TradingPreferences.HasFlag(BotConfig.ETradingPreferences.SteamTradeMatcher) || (Bot.BotConfig.MatchableTypes.Count == 0) || !await Bot.ArchiWebHandler.HasValidApiKey().ConfigureAwait(false) || !await Bot.ArchiWebHandler.HasPublicInventory().ConfigureAwait(false) || string.IsNullOrEmpty(tradeToken = await Bot.ArchiWebHandler.GetTradeToken().ConfigureAwait(false))) {
LastAnnouncementCheck = DateTime.UtcNow;
ShouldSendHeartBeats = false;
return;
}
await RequestsSemaphore.WaitAsync().ConfigureAwait(false);
try {
@@ -102,7 +96,22 @@ namespace ArchiSteamFarm {
return;
}
HashSet<Steam.Asset> inventory = await Bot.ArchiWebHandler.GetMyInventory(true, wantedTypes: Bot.BotConfig.MatchableTypes).ConfigureAwait(false);
// Don't announce if we don't meet conditions
string tradeToken;
if (!await ShouldAnnounce().ConfigureAwait(false) || string.IsNullOrEmpty(tradeToken = await Bot.ArchiWebHandler.GetTradeToken().ConfigureAwait(false))) {
LastAnnouncementCheck = DateTime.UtcNow;
ShouldSendHeartBeats = false;
return;
}
HashSet<Steam.Asset.EType> acceptedMatchableTypes = new HashSet<Steam.Asset.EType>(Bot.BotConfig.MatchableTypes.Where(type => AcceptedMatchableTypes.Contains(type)));
if (acceptedMatchableTypes.Count == 0) {
LastAnnouncementCheck = DateTime.UtcNow;
ShouldSendHeartBeats = false;
return;
}
HashSet<Steam.Asset> inventory = await Bot.ArchiWebHandler.GetMyInventory(true, wantedTypes: acceptedMatchableTypes).ConfigureAwait(false);
// This is actually inventory failure, so we'll stop sending heartbeats but not record it as valid check
if (inventory == null) {
@@ -119,15 +128,15 @@ namespace ArchiSteamFarm {
const string request = URL + "/Api/Announce";
Dictionary<string, string> data = new Dictionary<string, string>(9) {
{ "SteamID", Bot.CachedSteamID.ToString() },
{ "Guid", Program.GlobalDatabase.Guid.ToString("N") },
{ "Nickname", nickname ?? "" },
{ "AvatarHash", avatarHash ?? "" },
{ "MatchableTypes", JsonConvert.SerializeObject(Bot.BotConfig.MatchableTypes) },
{ "MatchEverything", Bot.BotConfig.TradingPreferences.HasFlag(BotConfig.ETradingPreferences.MatchEverything) ? "1" : "0" },
{ "TradeToken", tradeToken },
{ "GamesCount", inventory.Select(item => item.RealAppID).Distinct().Count().ToString() },
{ "Guid", Program.GlobalDatabase.Guid.ToString("N") },
{ "ItemsCount", inventory.Count.ToString() },
{ "GamesCount", inventory.Select(item => item.RealAppID).Distinct().Count().ToString() }
{ "MatchableTypes", JsonConvert.SerializeObject(acceptedMatchableTypes) },
{ "MatchEverything", Bot.BotConfig.TradingPreferences.HasFlag(BotConfig.ETradingPreferences.MatchEverything) ? "1" : "0" },
{ "Nickname", nickname ?? "" },
{ "SteamID", Bot.CachedSteamID.ToString() },
{ "TradeToken", tradeToken }
};
// We don't need retry logic here
@@ -139,5 +148,30 @@ namespace ArchiSteamFarm {
RequestsSemaphore.Release();
}
}
private async Task<bool> ShouldAnnounce() {
// Bot must have ASF 2FA
if (!Bot.HasMobileAuthenticator) {
return false;
}
// Bot must have STM enable in TradingPreferences
if (!Bot.BotConfig.TradingPreferences.HasFlag(BotConfig.ETradingPreferences.SteamTradeMatcher)) {
return false;
}
// Bot must have at least one accepted matchable type set
if ((Bot.BotConfig.MatchableTypes.Count == 0) || Bot.BotConfig.MatchableTypes.All(type => !AcceptedMatchableTypes.Contains(type))) {
return false;
}
// Bot must have public inventory
if (!await Bot.ArchiWebHandler.HasPublicInventory().ConfigureAwait(false)) {
return false;
}
// Bot must have valid API key (e.g. not being restricted account)
return await Bot.ArchiWebHandler.HasValidApiKey().ConfigureAwait(false);
}
}
}