This commit is contained in:
JustArchi
2017-07-23 02:11:16 +02:00
parent 65c2b81d0a
commit 2db65d324a
4 changed files with 262 additions and 23 deletions

View File

@@ -461,6 +461,16 @@ namespace ArchiSteamFarm {
return GetSteamUserPermission(steamID) >= BotConfig.EPermission.Master;
}
internal bool IsPriorityIdling(uint appID) {
if (appID == 0) {
ArchiLogger.LogNullError(nameof(appID));
return false;
}
bool result = BotDatabase.IsPriorityIdling(appID);
return result;
}
internal async Task LootIfNeeded() {
if (!IsConnectedAndLoggedOn || !BotConfig.SendOnFarmingFinished) {
return;
@@ -627,6 +637,8 @@ namespace ArchiSteamFarm {
return await ResponseFarm(steamID).ConfigureAwait(false);
case "!HELP":
return ResponseHelp(steamID);
case "!IQ":
return ResponseIdleQueue(steamID);
case "!LOOT":
return await ResponseLoot(steamID).ConfigureAwait(false);
case "!LOOT^":
@@ -698,6 +710,20 @@ namespace ArchiSteamFarm {
}
return args.Length == 3 ? ResponseInput(steamID, args[1], args[2]) : ResponseUnknown(steamID);
case "!IQ":
return await ResponseIdleQueue(steamID, args[1]).ConfigureAwait(false);
case "!IQADD":
if (args.Length > 2) {
return await ResponseIdleQueueAdd(steamID, args[1], args.GetArgsAsString(2)).ConfigureAwait(false);
}
return ResponseIdleQueueAdd(steamID, args[1]);
case "!IQRM":
if (args.Length > 2) {
return await ResponseIdleQueueRemove(steamID, args[1], args.GetArgsAsString(2)).ConfigureAwait(false);
}
return ResponseIdleQueueRemove(steamID, args[1]);
case "!LOOT":
return await ResponseLoot(steamID, args[1]).ConfigureAwait(false);
case "!LOOT^":
@@ -1051,8 +1077,7 @@ namespace ArchiSteamFarm {
SetUserInput(ASF.EUserInputType.DeviceID, deviceID);
}
BotDatabase.MobileAuthenticator.CorrectDeviceID(DeviceID);
BotDatabase.Save();
BotDatabase.CorrectMobileAuthenticatorDeviceID(DeviceID);
}
ArchiLogger.LogGenericInfo(Strings.BotAuthenticatorImportFinished);
@@ -2405,6 +2430,167 @@ namespace ArchiSteamFarm {
return null;
}
private static async Task<string> ResponseIdleQueue(ulong steamID, string botNames) {
if ((steamID == 0) || string.IsNullOrEmpty(botNames)) {
ASF.ArchiLogger.LogNullError(nameof(steamID) + " || " + nameof(botNames));
return null;
}
HashSet<Bot> bots = GetBots(botNames);
if ((bots == null) || (bots.Count == 0)) {
return IsOwner(steamID) ? FormatStaticResponse(string.Format(Strings.BotNotFound, botNames)) : null;
}
IEnumerable<Task<string>> tasks = bots.Select(bot => Task.Run(() => bot.ResponseIdleQueue(steamID)));
ICollection<string> results;
switch (Program.GlobalConfig.OptimizationMode) {
case GlobalConfig.EOptimizationMode.MinMemoryUsage:
results = new List<string>(bots.Count);
foreach (Task<string> task in tasks) {
results.Add(await task.ConfigureAwait(false));
}
break;
default:
results = await Task.WhenAll(tasks).ConfigureAwait(false);
break;
}
List<string> responses = new List<string>(results.Where(result => !string.IsNullOrEmpty(result)));
return responses.Count > 0 ? string.Join("", responses) : null;
}
private string ResponseIdleQueue(ulong steamID) {
if (steamID == 0) {
ArchiLogger.LogNullError(nameof(steamID));
return null;
}
string result = IsMaster(steamID) ? FormatBotResponse(string.Join(", ", BotDatabase.GetIdlingPriorityAppIDs())) : null;
return result;
}
private string ResponseIdleQueueAdd(ulong steamID, string targetsText) {
if ((steamID == 0) || string.IsNullOrEmpty(targetsText)) {
ArchiLogger.LogNullError(nameof(steamID) + " || " + nameof(targetsText));
return null;
}
if (!IsMaster(steamID)) {
return null;
}
string[] targets = targetsText.Split(new[] { ',' }, StringSplitOptions.RemoveEmptyEntries);
HashSet<uint> appIDs = new HashSet<uint>();
foreach (string target in targets) {
if (!uint.TryParse(target, out uint appID) || (appID == 0)) {
return FormatBotResponse(string.Format(Strings.ErrorParsingObject, nameof(appID)));
}
appIDs.Add(appID);
}
if (appIDs.Count == 0) {
return FormatBotResponse(string.Format(Strings.ErrorIsEmpty, nameof(appIDs)));
}
BotDatabase.AddIdlingPriorityAppIDs(appIDs);
return FormatBotResponse(Strings.Done);
}
private static async Task<string> ResponseIdleQueueAdd(ulong steamID, string botNames, string targetsText) {
if ((steamID == 0) || string.IsNullOrEmpty(botNames) || string.IsNullOrEmpty(targetsText)) {
ASF.ArchiLogger.LogNullError(nameof(steamID) + " || " + nameof(botNames) + " || " + nameof(targetsText));
return null;
}
HashSet<Bot> bots = GetBots(botNames);
if ((bots == null) || (bots.Count == 0)) {
return IsOwner(steamID) ? FormatStaticResponse(string.Format(Strings.BotNotFound, botNames)) : null;
}
IEnumerable<Task<string>> tasks = bots.Select(bot => Task.Run(() => bot.ResponseIdleQueueAdd(steamID, targetsText)));
ICollection<string> results;
switch (Program.GlobalConfig.OptimizationMode) {
case GlobalConfig.EOptimizationMode.MinMemoryUsage:
results = new List<string>(bots.Count);
foreach (Task<string> task in tasks) {
results.Add(await task.ConfigureAwait(false));
}
break;
default:
results = await Task.WhenAll(tasks).ConfigureAwait(false);
break;
}
List<string> responses = new List<string>(results.Where(result => !string.IsNullOrEmpty(result)));
return responses.Count > 0 ? string.Join("", responses) : null;
}
private static async Task<string> ResponseIdleQueueRemove(ulong steamID, string botNames, string targetsText) {
if ((steamID == 0) || string.IsNullOrEmpty(botNames) || string.IsNullOrEmpty(targetsText)) {
ASF.ArchiLogger.LogNullError(nameof(steamID) + " || " + nameof(botNames) + " || " + nameof(targetsText));
return null;
}
HashSet<Bot> bots = GetBots(botNames);
if ((bots == null) || (bots.Count == 0)) {
return IsOwner(steamID) ? FormatStaticResponse(string.Format(Strings.BotNotFound, botNames)) : null;
}
IEnumerable<Task<string>> tasks = bots.Select(bot => Task.Run(() => bot.ResponseIdleQueueRemove(steamID, targetsText)));
ICollection<string> results;
switch (Program.GlobalConfig.OptimizationMode) {
case GlobalConfig.EOptimizationMode.MinMemoryUsage:
results = new List<string>(bots.Count);
foreach (Task<string> task in tasks) {
results.Add(await task.ConfigureAwait(false));
}
break;
default:
results = await Task.WhenAll(tasks).ConfigureAwait(false);
break;
}
List<string> responses = new List<string>(results.Where(result => !string.IsNullOrEmpty(result)));
return responses.Count > 0 ? string.Join("", responses) : null;
}
private string ResponseIdleQueueRemove(ulong steamID, string targetsText) {
if ((steamID == 0) || string.IsNullOrEmpty(targetsText)) {
ArchiLogger.LogNullError(nameof(steamID) + " || " + nameof(targetsText));
return null;
}
if (!IsMaster(steamID)) {
return null;
}
string[] targets = targetsText.Split(new[] { ',' }, StringSplitOptions.RemoveEmptyEntries);
HashSet<uint> appIDs = new HashSet<uint>();
foreach (string target in targets) {
if (!uint.TryParse(target, out uint appID) || (appID == 0)) {
return FormatBotResponse(string.Format(Strings.ErrorParsingObject, nameof(appID)));
}
appIDs.Add(appID);
}
if (appIDs.Count == 0) {
return FormatBotResponse(string.Format(Strings.ErrorIsEmpty, nameof(appIDs)));
}
BotDatabase.RemoveIdlingPriorityAppIDs(appIDs);
return FormatBotResponse(Strings.Done);
}
private string ResponseInput(ulong steamID, string propertyName, string inputValue) {
if ((steamID == 0) || string.IsNullOrEmpty(propertyName) || string.IsNullOrEmpty(inputValue)) {
ASF.ArchiLogger.LogNullError(nameof(steamID) + " || " + nameof(propertyName) + " || " + nameof(inputValue));

View File

@@ -35,6 +35,9 @@ namespace ArchiSteamFarm {
private readonly object FileLock = new object();
[JsonProperty(Required = Required.DisallowNull)]
private readonly ConcurrentHashSet<uint> IdlingPriorityAppIDs = new ConcurrentHashSet<uint>();
internal string LoginKey {
get => _LoginKey;
@@ -94,15 +97,49 @@ namespace ArchiSteamFarm {
}
}
internal IEnumerable<ulong> GetBlacklistedFromTradesSteamIDs() => BlacklistedFromTradesSteamIDs;
internal bool IsBlacklistedFromTrades(ulong steamID) {
if (steamID != 0) {
return BlacklistedFromTradesSteamIDs.Contains(steamID);
internal void AddIdlingPriorityAppIDs(HashSet<uint> appIDs) {
if ((appIDs == null) || (appIDs.Count == 0)) {
ASF.ArchiLogger.LogNullError(nameof(appIDs));
return;
}
ASF.ArchiLogger.LogNullError(nameof(steamID));
return false;
if (IdlingPriorityAppIDs.AddRange(appIDs)) {
Save();
}
}
internal void CorrectMobileAuthenticatorDeviceID(string deviceID) {
if (string.IsNullOrEmpty(deviceID) || (MobileAuthenticator == null)) {
ASF.ArchiLogger.LogNullError(nameof(deviceID) + " || " + nameof(MobileAuthenticator));
return;
}
if (MobileAuthenticator.CorrectDeviceID(deviceID)) {
Save();
}
}
internal IEnumerable<ulong> GetBlacklistedFromTradesSteamIDs() => BlacklistedFromTradesSteamIDs;
internal IEnumerable<uint> GetIdlingPriorityAppIDs() => IdlingPriorityAppIDs;
internal bool IsBlacklistedFromTrades(ulong steamID) {
if (steamID == 0) {
ASF.ArchiLogger.LogNullError(nameof(steamID));
return false;
}
bool result = BlacklistedFromTradesSteamIDs.Contains(steamID);
return result;
}
internal bool IsPriorityIdling(uint appID) {
if (appID == 0) {
ASF.ArchiLogger.LogNullError(nameof(appID));
return false;
}
bool result = IdlingPriorityAppIDs.Contains(appID);
return result;
}
internal static BotDatabase Load(string filePath) {
@@ -144,7 +181,18 @@ namespace ArchiSteamFarm {
}
}
internal void Save() {
internal void RemoveIdlingPriorityAppIDs(HashSet<uint> appIDs) {
if ((appIDs == null) || (appIDs.Count == 0)) {
ASF.ArchiLogger.LogNullError(nameof(appIDs));
return;
}
if (IdlingPriorityAppIDs.RemoveRange(appIDs)) {
Save();
}
}
private void Save() {
string json = JsonConvert.SerializeObject(this);
if (string.IsNullOrEmpty(json)) {
ASF.ArchiLogger.LogNullError(nameof(json));

View File

@@ -817,37 +817,37 @@ namespace ArchiSteamFarm {
}
private void SortGamesToFarm() {
IOrderedEnumerable<Game> gamesToFarm;
IOrderedEnumerable<Game> gamesToFarm = GamesToFarm.OrderBy(game => Bot.IsPriorityIdling(game.AppID) ? 1 : 0);
switch (Bot.BotConfig.FarmingOrder) {
case BotConfig.EFarmingOrder.Unordered:
return;
break;
case BotConfig.EFarmingOrder.AppIDsAscending:
gamesToFarm = GamesToFarm.OrderBy(game => game.AppID);
gamesToFarm = gamesToFarm.ThenBy(game => game.AppID);
break;
case BotConfig.EFarmingOrder.AppIDsDescending:
gamesToFarm = GamesToFarm.OrderByDescending(game => game.AppID);
gamesToFarm = gamesToFarm.ThenByDescending(game => game.AppID);
break;
case BotConfig.EFarmingOrder.CardDropsAscending:
gamesToFarm = GamesToFarm.OrderBy(game => game.CardsRemaining);
gamesToFarm = gamesToFarm.ThenBy(game => game.CardsRemaining);
break;
case BotConfig.EFarmingOrder.CardDropsDescending:
gamesToFarm = GamesToFarm.OrderByDescending(game => game.CardsRemaining);
gamesToFarm = gamesToFarm.ThenByDescending(game => game.CardsRemaining);
break;
case BotConfig.EFarmingOrder.HoursAscending:
gamesToFarm = GamesToFarm.OrderBy(game => game.HoursPlayed);
gamesToFarm = gamesToFarm.ThenBy(game => game.HoursPlayed);
break;
case BotConfig.EFarmingOrder.HoursDescending:
gamesToFarm = GamesToFarm.OrderByDescending(game => game.HoursPlayed);
gamesToFarm = gamesToFarm.ThenByDescending(game => game.HoursPlayed);
break;
case BotConfig.EFarmingOrder.NamesAscending:
gamesToFarm = GamesToFarm.OrderBy(game => game.GameName);
gamesToFarm = gamesToFarm.ThenBy(game => game.GameName);
break;
case BotConfig.EFarmingOrder.NamesDescending:
gamesToFarm = GamesToFarm.OrderByDescending(game => game.GameName);
gamesToFarm = gamesToFarm.ThenByDescending(game => game.GameName);
break;
case BotConfig.EFarmingOrder.Random:
gamesToFarm = GamesToFarm.OrderBy(game => Utilities.RandomNext());
gamesToFarm = gamesToFarm.ThenBy(game => Utilities.RandomNext());
break;
default:
Bot.ArchiLogger.LogGenericError(string.Format(Strings.ErrorIsInvalid, nameof(Bot.BotConfig.FarmingOrder)));

View File

@@ -70,13 +70,18 @@ namespace ArchiSteamFarm {
public void Dispose() => ConfirmationsSemaphore.Dispose();
internal void CorrectDeviceID(string deviceID) {
internal bool CorrectDeviceID(string deviceID) {
if (string.IsNullOrEmpty(deviceID)) {
Bot.ArchiLogger.LogNullError(nameof(deviceID));
return;
return false;
}
if (!string.IsNullOrEmpty(DeviceID) && DeviceID.Equals(deviceID)) {
return false;
}
DeviceID = deviceID;
return true;
}
internal async Task<string> GenerateToken() {