mirror of
https://github.com/JustArchiNET/ArchiSteamFarm.git
synced 2026-01-01 22:20:52 +00:00
Use sorted dictionary for background games redeemer
This commit is contained in:
@@ -20,7 +20,6 @@
|
||||
// limitations under the License.
|
||||
|
||||
using System;
|
||||
using System.Collections.Concurrent;
|
||||
using System.Collections.Generic;
|
||||
using System.IO;
|
||||
using System.Linq;
|
||||
@@ -30,7 +29,13 @@ using Newtonsoft.Json;
|
||||
|
||||
namespace ArchiSteamFarm {
|
||||
internal sealed class BotDatabase : IDisposable {
|
||||
internal bool HasGamesToRedeemInBackground => GamesToRedeemInBackground.Count > 0;
|
||||
internal bool HasGamesToRedeemInBackground {
|
||||
get {
|
||||
lock (GamesToRedeemInBackground) {
|
||||
return GamesToRedeemInBackground.Count > 0;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
[JsonProperty(Required = Required.DisallowNull)]
|
||||
private readonly ConcurrentHashSet<ulong> BlacklistedFromTradesSteamIDs = new ConcurrentHashSet<ulong>();
|
||||
@@ -38,7 +43,7 @@ namespace ArchiSteamFarm {
|
||||
private readonly SemaphoreSlim FileSemaphore = new SemaphoreSlim(1, 1);
|
||||
|
||||
[JsonProperty(Required = Required.DisallowNull)]
|
||||
private readonly ConcurrentDictionary<string, string> GamesToRedeemInBackground = new ConcurrentDictionary<string, string>();
|
||||
private readonly SortedDictionary<string, string> GamesToRedeemInBackground = new SortedDictionary<string, string>();
|
||||
|
||||
[JsonProperty(Required = Required.DisallowNull)]
|
||||
private readonly ConcurrentHashSet<uint> IdlingBlacklistedAppIDs = new ConcurrentHashSet<uint>();
|
||||
@@ -87,8 +92,14 @@ namespace ArchiSteamFarm {
|
||||
return;
|
||||
}
|
||||
|
||||
// We use Count() and not Any() because we must ensure full loop pass
|
||||
if (games.Count(game => GamesToRedeemInBackground.TryAdd(game.Key, game.Value)) > 0) {
|
||||
bool save;
|
||||
|
||||
lock (GamesToRedeemInBackground) {
|
||||
// We use Count() and not Any() because we must ensure full loop pass
|
||||
save = games.Count(game => GamesToRedeemInBackground.TryAdd(game.Key, game.Value)) > 0;
|
||||
}
|
||||
|
||||
if (save) {
|
||||
await Save().ConfigureAwait(false);
|
||||
}
|
||||
}
|
||||
@@ -99,7 +110,13 @@ namespace ArchiSteamFarm {
|
||||
return;
|
||||
}
|
||||
|
||||
if (GamesToRedeemInBackground.TryAdd(key, game)) {
|
||||
bool save;
|
||||
|
||||
lock (GamesToRedeemInBackground) {
|
||||
save = GamesToRedeemInBackground.TryAdd(key, game);
|
||||
}
|
||||
|
||||
if (save) {
|
||||
await Save().ConfigureAwait(false);
|
||||
}
|
||||
}
|
||||
@@ -138,7 +155,13 @@ namespace ArchiSteamFarm {
|
||||
}
|
||||
|
||||
internal IReadOnlyCollection<ulong> GetBlacklistedFromTradesSteamIDs() => BlacklistedFromTradesSteamIDs;
|
||||
internal KeyValuePair<string, string> GetGameToRedeemInBackground() => GamesToRedeemInBackground.FirstOrDefault();
|
||||
|
||||
internal KeyValuePair<string, string> GetGameToRedeemInBackground() {
|
||||
lock (GamesToRedeemInBackground) {
|
||||
return GamesToRedeemInBackground.FirstOrDefault();
|
||||
}
|
||||
}
|
||||
|
||||
internal IReadOnlyCollection<uint> GetIdlingBlacklistedAppIDs() => IdlingBlacklistedAppIDs;
|
||||
internal IReadOnlyCollection<uint> GetIdlingPriorityAppIDs() => IdlingPriorityAppIDs;
|
||||
|
||||
@@ -235,7 +258,13 @@ namespace ArchiSteamFarm {
|
||||
return;
|
||||
}
|
||||
|
||||
if (GamesToRedeemInBackground.Remove(key, out _)) {
|
||||
bool save;
|
||||
|
||||
lock (GamesToRedeemInBackground) {
|
||||
save = GamesToRedeemInBackground.Remove(key, out _);
|
||||
}
|
||||
|
||||
if (save) {
|
||||
await Save().ConfigureAwait(false);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,51 +0,0 @@
|
||||
// _ _ _ ____ _ _____
|
||||
// / \ _ __ ___ | |__ (_)/ ___| | |_ ___ __ _ _ __ ___ | ___|__ _ _ __ _ __ ___
|
||||
// / _ \ | '__|/ __|| '_ \ | |\___ \ | __|/ _ \ / _` || '_ ` _ \ | |_ / _` || '__|| '_ ` _ \
|
||||
// / ___ \ | | | (__ | | | || | ___) || |_| __/| (_| || | | | | || _|| (_| || | | | | | | |
|
||||
// /_/ \_\|_| \___||_| |_||_||____/ \__|\___| \__,_||_| |_| |_||_| \__,_||_| |_| |_| |_|
|
||||
//
|
||||
// Copyright 2015-2018 Ł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 System;
|
||||
using System.Collections;
|
||||
using System.Collections.Generic;
|
||||
using System.Threading;
|
||||
|
||||
namespace ArchiSteamFarm {
|
||||
internal sealed class ConcurrentEnumerator<T> : IEnumerator<T> {
|
||||
public T Current => Enumerator.Current;
|
||||
|
||||
private readonly IEnumerator<T> Enumerator;
|
||||
private readonly SemaphoreSlim SemaphoreSlim;
|
||||
|
||||
object IEnumerator.Current => Current;
|
||||
|
||||
internal ConcurrentEnumerator(IReadOnlyCollection<T> collection, SemaphoreSlim semaphoreSlim) {
|
||||
if ((collection == null) || (semaphoreSlim == null)) {
|
||||
throw new ArgumentNullException(nameof(collection) + " || " + nameof(semaphoreSlim));
|
||||
}
|
||||
|
||||
SemaphoreSlim = semaphoreSlim;
|
||||
SemaphoreSlim.Wait();
|
||||
|
||||
Enumerator = collection.GetEnumerator();
|
||||
}
|
||||
|
||||
public void Dispose() => SemaphoreSlim.Release();
|
||||
public bool MoveNext() => Enumerator.MoveNext();
|
||||
public void Reset() => Enumerator.Reset();
|
||||
}
|
||||
}
|
||||
@@ -95,7 +95,7 @@ namespace ArchiSteamFarm {
|
||||
}
|
||||
}
|
||||
|
||||
public IEnumerator<T> GetEnumerator() => new ConcurrentEnumerator<T>(BackingCollection, CollectionSemaphore);
|
||||
public IEnumerator<T> GetEnumerator() => new ConcurrentEnumerator(BackingCollection, CollectionSemaphore);
|
||||
|
||||
public void IntersectWith(IEnumerable<T> other) {
|
||||
CollectionSemaphore.Wait();
|
||||
@@ -213,5 +213,29 @@ namespace ArchiSteamFarm {
|
||||
CollectionSemaphore.Release();
|
||||
}
|
||||
}
|
||||
|
||||
private sealed class ConcurrentEnumerator : IEnumerator<T> {
|
||||
public T Current => Enumerator.Current;
|
||||
|
||||
private readonly IEnumerator<T> Enumerator;
|
||||
private readonly SemaphoreSlim SemaphoreSlim;
|
||||
|
||||
object IEnumerator.Current => Current;
|
||||
|
||||
internal ConcurrentEnumerator(IReadOnlyCollection<T> collection, SemaphoreSlim semaphoreSlim) {
|
||||
if ((collection == null) || (semaphoreSlim == null)) {
|
||||
throw new ArgumentNullException(nameof(collection) + " || " + nameof(semaphoreSlim));
|
||||
}
|
||||
|
||||
SemaphoreSlim = semaphoreSlim;
|
||||
semaphoreSlim.Wait();
|
||||
|
||||
Enumerator = collection.GetEnumerator();
|
||||
}
|
||||
|
||||
public void Dispose() => SemaphoreSlim.Release();
|
||||
public bool MoveNext() => Enumerator.MoveNext();
|
||||
public void Reset() => Enumerator.Reset();
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user