Skip to content

Commit 5a180ac

Browse files
committed
Add blacklist parameter
1 parent fadce68 commit 5a180ac

File tree

3 files changed

+44
-16
lines changed

3 files changed

+44
-16
lines changed

AutoClaimStickers/AutoClaimStickers.cs

Lines changed: 35 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
using System;
22
using System.Collections.Generic;
3+
using System.Collections.Immutable;
34
using System.Composition;
5+
using System.Linq;
46
using System.Threading;
57
using System.Threading.Tasks;
68
using ArchiSteamFarm.Core;
@@ -14,42 +16,55 @@
1416
namespace AutoClaimStickers;
1517

1618
[Export(typeof(IPlugin))]
17-
internal sealed class AutoClaimStickers : IPlugin, IASF {
19+
internal sealed class AutoClaimStickers : IPlugin, IASF, IDisposable {
1820
public string Name => nameof(AutoClaimStickers);
1921
public Version Version => typeof(AutoClaimStickers).Assembly.GetName().Version ?? throw new InvalidOperationException(nameof(Version));
2022
private static Uri SteamApiURL => new("https://api.steampowered.com");
2123
private static Uri RefererURL => new(ArchiWebHandler.SteamStoreURL, "/category/casual");
2224
private ushort Interval = 360; // 6 * 60
23-
private static readonly Timer AutoClaimTimer = new(OnAutoClaimTimer);
25+
private ImmutableHashSet<string> Blacklist = [];
26+
private Timer? AutoClaimTimer;
2427
private static readonly SemaphoreSlim AutoClaimSemaphore = new(1, 1);
2528
private static readonly SemaphoreSlim BotSemaphore = new(3, 3);
26-
public Task OnLoaded() => Task.CompletedTask;
2729

30+
public Task OnLoaded() {
31+
AutoClaimTimer = new(OnAutoClaimTimer);
32+
return Task.CompletedTask;
33+
}
2834
public Task OnASFInit(IReadOnlyDictionary<string, JToken>? additionalConfigProperties = null) {
2935
if (additionalConfigProperties == null) {
3036
return Task.CompletedTask;
3137
}
38+
if (AutoClaimTimer == null) {
39+
throw new InvalidOperationException(nameof(AutoClaimTimer));
40+
}
3241
foreach ((string configProperty, JToken configValue) in additionalConfigProperties) {
3342
switch (configProperty) {
3443
case $"{nameof(AutoClaimStickers)}{nameof(Interval)}" when configValue.Type == JTokenType.Integer:
35-
Interval = configValue.Value<ushort>();
44+
lock (AutoClaimSemaphore) {
45+
Interval = configValue.Value<ushort>();
46+
}
47+
break;
48+
case $"{nameof(AutoClaimStickers)}{nameof(Blacklist)}" when configValue.Type == JTokenType.Array:
49+
JArray? array = configValue.Value<JArray>();
50+
if (array != null) {
51+
lock (Blacklist) {
52+
Blacklist = array.ToObject<ImmutableHashSet<string>>() ?? [];
53+
}
54+
}
3655
break;
3756
default:
3857
break;
3958
}
4059
}
41-
if (Interval != 0) {
42-
lock (AutoClaimSemaphore) {
43-
_ = AutoClaimTimer.Change(TimeSpan.FromMinutes(2), TimeSpan.FromMinutes(Interval));
44-
}
45-
} else {
46-
lock (AutoClaimSemaphore) {
47-
_ = AutoClaimTimer.Change(Timeout.InfiniteTimeSpan, Timeout.InfiniteTimeSpan);
48-
}
60+
lock (AutoClaimSemaphore) {
61+
_ = Interval != 0
62+
? AutoClaimTimer.Change(TimeSpan.FromMinutes(2), TimeSpan.FromMinutes(Interval))
63+
: AutoClaimTimer.Change(Timeout.InfiniteTimeSpan, Timeout.InfiniteTimeSpan);
4964
}
5065
return Task.CompletedTask;
5166
}
52-
private static async Task AutoClaim() {
67+
private async Task AutoClaim() {
5368
if (!await AutoClaimSemaphore.WaitAsync(0).ConfigureAwait(false)) {
5469
return;
5570
}
@@ -61,6 +76,11 @@ private static async Task AutoClaim() {
6176
List<Task> tasks = [];
6277
foreach (Bot bot in bots) {
6378
if (bot.IsConnectedAndLoggedOn) {
79+
lock (Blacklist) {
80+
if (Blacklist.Any(item => item.Equals(bot.BotName, StringComparison.OrdinalIgnoreCase))) {
81+
continue;
82+
}
83+
}
6484
tasks.Add(Task.Run(async () => {
6585
await BotSemaphore.WaitAsync().ConfigureAwait(false);
6686
try {
@@ -109,5 +129,6 @@ private static async Task<bool> CanClaimItem(Bot bot, string token) {
109129
ClaimItemData? result = response.Content;
110130
return (result?.Response?.CommunityItemId > 0, result?.Response);
111131
}
112-
private static async void OnAutoClaimTimer(object? state = null) => await AutoClaim().ConfigureAwait(false);
132+
private async void OnAutoClaimTimer(object? state = null) => await AutoClaim().ConfigureAwait(false);
133+
public void Dispose() => AutoClaimTimer?.Dispose();
113134
}

Directory.Build.props

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33

44
<PropertyGroup>
55
<PluginName>AutoClaimStickers</PluginName>
6-
<Version>1.0.0.1</Version>
6+
<Version>1.0.1.1</Version>
77
</PropertyGroup>
88

99
<PropertyGroup>

README.md

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,15 @@ Plugin config is located in ASF.json:
66
{
77
//ASF global config
88
...
9-
"AutoClaimStickersInterval": 360
9+
"AutoClaimStickersInterval": 360,
10+
"AutoClaimStickersBlacklist": [
11+
"bot1",
12+
"bot2",
13+
...
14+
]
1015
}
1116
```
1217
### `AutoClaimStickersInterval`
1318
Optional parameter of type `ushort` with default value of `360`. The plugin will check and claim stickers every `AutoClaimStickersInterval` minutes and will not check when the value is `0`.
19+
### `AutoClaimStickersBlacklist`
20+
Optional parameter of type `ImmutableHashSet<string>` with default value of `[]`. The plugin will always ignore blacklisted bots.

0 commit comments

Comments
 (0)