Add MessagingLimiterDelay

This commit is contained in:
JustArchi
2018-09-26 20:56:22 +02:00
parent 83bce52f09
commit 5e38d176cd
3 changed files with 26 additions and 0 deletions

View File

@@ -93,6 +93,7 @@ namespace ArchiSteamFarm {
private readonly SemaphoreSlim GamesRedeemerInBackgroundSemaphore = new SemaphoreSlim(1, 1);
private readonly Timer HeartBeatTimer;
private readonly SemaphoreSlim InitializationSemaphore = new SemaphoreSlim(1, 1);
private readonly SemaphoreSlim MessagingSemaphore = new SemaphoreSlim(1, 1);
private readonly SemaphoreSlim PICSSemaphore = new SemaphoreSlim(1, 1);
private readonly Statistics Statistics;
private readonly SteamClient SteamClient;
@@ -243,6 +244,7 @@ namespace ArchiSteamFarm {
CallbackSemaphore.Dispose();
GamesRedeemerInBackgroundSemaphore.Dispose();
InitializationSemaphore.Dispose();
MessagingSemaphore.Dispose();
PICSSemaphore.Dispose();
// Those are objects that might be null and the check should be in-place
@@ -922,6 +924,8 @@ namespace ArchiSteamFarm {
messagePart = Program.GlobalConfig.SteamMessagePrefix + (i > 0 ? "…" : "") + messagePart + (maxMessageLength < message.Length - i ? "…" : "");
await LimitMessagingRequestsAsync().ConfigureAwait(false);
if (!await ArchiHandler.SendMessage(steamID, messagePart).ConfigureAwait(false)) {
return false;
}
@@ -959,6 +963,8 @@ namespace ArchiSteamFarm {
messagePart = Program.GlobalConfig.SteamMessagePrefix + (i > 0 ? "…" : "") + messagePart + (maxMessageLength < message.Length - i ? "…" : "");
await LimitMessagingRequestsAsync().ConfigureAwait(false);
if (!await ArchiHandler.SendMessage(chatGroupID, chatID, messagePart).ConfigureAwait(false)) {
return false;
}
@@ -1475,6 +1481,20 @@ namespace ArchiSteamFarm {
);
}
private async Task LimitMessagingRequestsAsync() {
if (Program.GlobalConfig.MessagingLimiterDelay == 0) {
return;
}
await MessagingSemaphore.WaitAsync().ConfigureAwait(false);
Utilities.InBackground(
async () => {
await Task.Delay(Program.GlobalConfig.MessagingLimiterDelay).ConfigureAwait(false);
MessagingSemaphore.Release();
}
);
}
private async void OnConnected(SteamClient.ConnectedCallback callback) {
if (callback == null) {
ArchiLogger.LogNullError(nameof(callback));

View File

@@ -49,6 +49,7 @@ namespace ArchiSteamFarm {
private const byte DefaultLoginLimiterDelay = 10;
private const byte DefaultMaxFarmingTime = 10;
private const byte DefaultMaxTradeHoldDuration = 15;
private const ushort DefaultMessagingLimiterDelay = 1000;
private const EOptimizationMode DefaultOptimizationMode = EOptimizationMode.MaxPerformance;
private const bool DefaultStatistics = true;
private const string DefaultSteamMessagePrefix = "/me ";
@@ -117,6 +118,9 @@ namespace ArchiSteamFarm {
[JsonProperty(Required = Required.DisallowNull)]
internal readonly byte MaxTradeHoldDuration = DefaultMaxTradeHoldDuration;
[JsonProperty(Required = Required.DisallowNull)]
internal readonly ushort MessagingLimiterDelay = DefaultMessagingLimiterDelay;
[JsonProperty(Required = Required.DisallowNull)]
internal readonly EOptimizationMode OptimizationMode = DefaultOptimizationMode;
@@ -341,6 +345,7 @@ namespace ArchiSteamFarm {
public bool ShouldSerializeLoginLimiterDelay() => ShouldSerializeEverything || (LoginLimiterDelay != DefaultLoginLimiterDelay);
public bool ShouldSerializeMaxFarmingTime() => ShouldSerializeEverything || (MaxFarmingTime != DefaultMaxFarmingTime);
public bool ShouldSerializeMaxTradeHoldDuration() => ShouldSerializeEverything || (MaxTradeHoldDuration != DefaultMaxTradeHoldDuration);
public bool ShouldSerializeMessagingLimiterDelay() => ShouldSerializeEverything || (MessagingLimiterDelay != DefaultMessagingLimiterDelay);
public bool ShouldSerializeOptimizationMode() => ShouldSerializeEverything || (OptimizationMode != DefaultOptimizationMode);
public bool ShouldSerializeSSteamOwnerID() => ShouldSerializeEverything; // We never serialize helper properties
public bool ShouldSerializeStatistics() => ShouldSerializeEverything || (Statistics != DefaultStatistics);

View File

@@ -16,6 +16,7 @@
"LoginLimiterDelay": 10,
"MaxFarmingTime": 10,
"MaxTradeHoldDuration": 15,
"MessagingLimiterDelay": 1000,
"OptimizationMode": 0,
"Statistics": true,
"SteamMessagePrefix": "/me ",