2017-11-18 17:27:06 +01:00
// _ _ _ ____ _ _____
// / \ _ __ ___ | |__ (_)/ ___| | |_ ___ __ _ _ __ ___ | ___|__ _ _ __ _ __ ___
// / _ \ | '__|/ __|| '_ \ | |\___ \ | __|/ _ \ / _` || '_ ` _ \ | |_ / _` || '__|| '_ ` _ \
// / ___ \ | | | (__ | | | || | ___) || |_| __/| (_| || | | | | || _|| (_| || | | | | | | |
// /_/ \_\|_| \___||_| |_||_||____/ \__|\___| \__,_||_| |_| |_||_| \__,_||_| |_| |_| |_|
//
2018-07-27 04:52:14 +02:00
// Copyright 2015-2018 Łukasz "JustArchi" Domeradzki
// Contact: JustArchi@JustArchi.net
2017-11-18 17:27:06 +01:00
//
2018-07-27 04:52:14 +02:00
// 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
2017-11-18 17:27:06 +01:00
//
2018-07-27 04:52:14 +02:00
// 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.
2016-12-04 01:07:37 +01:00
using System ;
using System.Collections.Generic ;
2018-02-21 17:44:06 +01:00
using System.Linq ;
2016-12-04 02:49:56 +01:00
using System.Threading ;
2016-12-04 01:07:37 +01:00
using System.Threading.Tasks ;
2017-12-14 08:23:17 +01:00
using ArchiSteamFarm.Json ;
2017-07-10 23:04:33 +02:00
using Newtonsoft.Json ;
2016-12-04 01:07:37 +01:00
namespace ArchiSteamFarm {
2016-12-04 02:49:56 +01:00
internal sealed class Statistics : IDisposable {
2017-03-20 17:21:56 +01:00
private const byte MinAnnouncementCheckTTL = 6 ; // Minimum amount of hours we must wait before checking eligibility for Announcement, should be lower than MinPersonaStateTTL
2017-02-08 14:55:40 +01:00
private const byte MinHeartBeatTTL = 10 ; // Minimum amount of minutes we must wait before sending next HeartBeat
2017-07-10 23:04:33 +02:00
private const byte MinItemsCount = 100 ; // Minimum amount of items to be eligible for public listing
2017-03-20 17:21:56 +01:00
private const byte MinPersonaStateTTL = 8 ; // Minimum amount of hours we must wait before requesting persona state update
2017-06-26 03:36:51 +02:00
private const string URL = "https://" + SharedInfo . StatisticsServer ;
2016-12-19 04:45:30 +01:00
2018-09-08 00:46:40 +02:00
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
} ;
2018-03-24 17:14:01 +01:00
2016-12-04 02:08:45 +01:00
private readonly Bot Bot ;
2017-08-04 19:26:37 +02:00
private readonly SemaphoreSlim RequestsSemaphore = new SemaphoreSlim ( 1 , 1 ) ;
2017-07-13 06:08:52 +02:00
2017-12-17 11:31:42 +01:00
private DateTime LastAnnouncementCheck ;
private DateTime LastHeartBeat ;
private DateTime LastPersonaStateRequest ;
2016-12-04 08:20:18 +01:00
private bool ShouldSendHeartBeats ;
2016-12-04 02:08:45 +01:00
2017-04-05 17:01:18 +02:00
internal Statistics ( Bot bot ) = > Bot = bot ? ? throw new ArgumentNullException ( nameof ( bot ) ) ;
2016-12-04 02:08:45 +01:00
2017-08-04 19:26:37 +02:00
public void Dispose ( ) = > RequestsSemaphore . Dispose ( ) ;
2016-12-04 02:49:56 +01:00
2016-12-04 02:08:45 +01:00
internal async Task OnHeartBeat ( ) {
2017-03-19 21:56:36 +01:00
// Request persona update if needed
if ( ( DateTime . UtcNow > LastPersonaStateRequest . AddHours ( MinPersonaStateTTL ) ) & & ( DateTime . UtcNow > LastAnnouncementCheck . AddHours ( MinAnnouncementCheckTTL ) ) ) {
LastPersonaStateRequest = DateTime . UtcNow ;
Bot . RequestPersonaStateUpdate ( ) ;
}
2017-01-23 00:46:44 +01:00
if ( ! ShouldSendHeartBeats | | ( DateTime . UtcNow < LastHeartBeat . AddMinutes ( MinHeartBeatTTL ) ) ) {
2016-12-04 01:07:37 +01:00
return ;
}
2017-08-04 19:26:37 +02:00
await RequestsSemaphore . WaitAsync ( ) . ConfigureAwait ( false ) ;
2016-12-04 02:49:56 +01:00
try {
2017-01-23 00:46:44 +01:00
if ( ! ShouldSendHeartBeats | | ( DateTime . UtcNow < LastHeartBeat . AddMinutes ( MinHeartBeatTTL ) ) ) {
2016-12-04 02:49:56 +01:00
return ;
}
2018-02-21 17:47:17 +01:00
const string request = URL + "/Api/HeartBeat" ;
2016-12-07 14:27:59 +01:00
Dictionary < string , string > data = new Dictionary < string , string > ( 2 ) {
2018-03-24 18:06:09 +01:00
{ "SteamID" , Bot . CachedSteamID . ToString ( ) } ,
{ "Guid" , Program . GlobalDatabase . Guid . ToString ( "N" ) }
2016-12-04 02:49:56 +01:00
} ;
2016-12-04 01:07:37 +01:00
2016-12-04 02:49:56 +01:00
// We don't need retry logic here
2018-03-09 23:48:47 +01:00
if ( await Program . WebBrowser . UrlPost ( request , data , maxTries : 1 ) . ConfigureAwait ( false ) ! = null ) {
2017-01-23 00:46:44 +01:00
LastHeartBeat = DateTime . UtcNow ;
2016-12-04 02:49:56 +01:00
}
} finally {
2017-08-04 19:26:37 +02:00
RequestsSemaphore . Release ( ) ;
2016-12-04 01:07:37 +01:00
}
2016-12-04 02:08:45 +01:00
}
2016-12-04 01:07:37 +01:00
2017-11-26 19:08:48 +01:00
internal async Task OnLoggedOn ( ) = > await Bot . ArchiWebHandler . JoinGroup ( SharedInfo . ASFGroupSteamID ) . ConfigureAwait ( false ) ;
2018-02-01 00:46:18 +01:00
internal async Task OnPersonaState ( string nickname = null , string avatarHash = null ) {
2017-03-19 21:56:36 +01:00
if ( DateTime . UtcNow < LastAnnouncementCheck . AddHours ( MinAnnouncementCheckTTL ) ) {
return ;
}
2017-08-04 19:26:37 +02:00
await RequestsSemaphore . WaitAsync ( ) . ConfigureAwait ( false ) ;
2016-12-04 02:49:56 +01:00
try {
2017-03-19 21:56:36 +01:00
if ( DateTime . UtcNow < LastAnnouncementCheck . AddHours ( MinAnnouncementCheckTTL ) ) {
2017-02-08 14:35:01 +01:00
return ;
}
2018-03-24 17:14:01 +01:00
// 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 ;
}
2018-06-09 00:45:15 +02:00
HashSet < Steam . Asset . EType > acceptedMatchableTypes = Bot . BotConfig . MatchableTypes . Where ( type = > AcceptedMatchableTypes . Contains ( type ) ) . ToHashSet ( ) ;
2018-03-24 17:14:01 +01:00
if ( acceptedMatchableTypes . Count = = 0 ) {
LastAnnouncementCheck = DateTime . UtcNow ;
ShouldSendHeartBeats = false ;
return ;
}
2018-06-27 21:18:54 +02:00
HashSet < Steam . Asset > inventory = await Bot . ArchiWebHandler . GetInventory ( Bot . CachedSteamID , tradable : true , wantedTypes : acceptedMatchableTypes ) . ConfigureAwait ( false ) ;
2017-02-08 14:35:01 +01:00
2017-03-19 21:56:36 +01:00
// This is actually inventory failure, so we'll stop sending heartbeats but not record it as valid check
if ( inventory = = null ) {
2017-02-08 14:41:28 +01:00
ShouldSendHeartBeats = false ;
2016-12-04 02:49:56 +01:00
return ;
}
2017-03-30 01:26:15 +02:00
// This is actual inventory
2017-07-10 23:04:33 +02:00
if ( inventory . Count < MinItemsCount ) {
2017-03-19 21:56:36 +01:00
LastAnnouncementCheck = DateTime . UtcNow ;
ShouldSendHeartBeats = false ;
return ;
}
2016-12-17 02:27:14 +01:00
2018-02-21 17:47:17 +01:00
const string request = URL + "/Api/Announce" ;
2018-02-21 17:44:06 +01:00
Dictionary < string , string > data = new Dictionary < string , string > ( 9 ) {
2018-03-24 18:06:09 +01:00
{ "SteamID" , Bot . CachedSteamID . ToString ( ) } ,
{ "Guid" , Program . GlobalDatabase . Guid . ToString ( "N" ) } ,
{ "Nickname" , nickname ? ? "" } ,
2018-02-01 00:46:18 +01:00
{ "AvatarHash" , avatarHash ? ? "" } ,
2018-03-24 17:14:01 +01:00
{ "GamesCount" , inventory . Select ( item = > item . RealAppID ) . Distinct ( ) . Count ( ) . ToString ( ) } ,
2018-02-21 17:44:06 +01:00
{ "ItemsCount" , inventory . Count . ToString ( ) } ,
2018-03-24 17:14:01 +01:00
{ "MatchableTypes" , JsonConvert . SerializeObject ( acceptedMatchableTypes ) } ,
{ "MatchEverything" , Bot . BotConfig . TradingPreferences . HasFlag ( BotConfig . ETradingPreferences . MatchEverything ) ? "1" : "0" } ,
{ "TradeToken" , tradeToken }
2016-12-04 02:49:56 +01:00
} ;
2016-12-04 15:30:00 +01:00
// We don't need retry logic here
2018-03-09 23:48:47 +01:00
if ( await Program . WebBrowser . UrlPost ( request , data , maxTries : 1 ) . ConfigureAwait ( false ) ! = null ) {
2017-03-19 21:56:36 +01:00
LastAnnouncementCheck = DateTime . UtcNow ;
ShouldSendHeartBeats = true ;
2016-12-04 20:41:01 +01:00
}
2016-12-04 02:49:56 +01:00
} finally {
2017-08-04 19:26:37 +02:00
RequestsSemaphore . Release ( ) ;
2016-12-04 02:49:56 +01:00
}
2016-12-04 01:07:37 +01:00
}
2018-03-24 17:14:01 +01:00
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 ) ;
}
2016-12-04 01:07:37 +01:00
}
2018-09-08 00:46:40 +02:00
}