Code review

This commit is contained in:
JustArchi
2018-02-07 09:13:58 +01:00
parent 19730f565b
commit 69e3885a96
3 changed files with 82 additions and 92 deletions

View File

@@ -265,57 +265,39 @@ namespace ArchiSteamFarm {
return false;
}
while (true) {
HashSet<MobileAuthenticator.Confirmation> confirmations = await BotDatabase.MobileAuthenticator.GetConfirmations().ConfigureAwait(false);
if ((confirmations == null) || (confirmations.Count == 0)) {
HashSet<MobileAuthenticator.Confirmation> confirmations = await BotDatabase.MobileAuthenticator.GetConfirmations(acceptedType).ConfigureAwait(false);
if ((confirmations == null) || (confirmations.Count == 0)) {
return true;
}
if ((acceptedSteamID == 0) && ((acceptedTradeIDs == null) || (acceptedTradeIDs.Count == 0))) {
return await BotDatabase.MobileAuthenticator.HandleConfirmations(confirmations, accept).ConfigureAwait(false);
}
IEnumerable<Task<Steam.ConfirmationDetails>> tasks = confirmations.Select(BotDatabase.MobileAuthenticator.GetConfirmationDetails);
ICollection<Steam.ConfirmationDetails> results;
switch (Program.GlobalConfig.OptimizationMode) {
case GlobalConfig.EOptimizationMode.MinMemoryUsage:
results = new List<Steam.ConfirmationDetails>(confirmations.Count);
foreach (Task<Steam.ConfirmationDetails> task in tasks) {
results.Add(await task.ConfigureAwait(false));
}
break;
default:
results = await Task.WhenAll(tasks).ConfigureAwait(false);
break;
}
foreach (MobileAuthenticator.Confirmation confirmation in results.Where(details => (details != null) && ((acceptedType != details.Type) || ((acceptedSteamID != 0) && (details.OtherSteamID64 != 0) && (acceptedSteamID != details.OtherSteamID64)) || ((acceptedTradeIDs != null) && (details.TradeOfferID != 0) && !acceptedTradeIDs.Contains(details.TradeOfferID)))).Select(details => details.Confirmation)) {
confirmations.Remove(confirmation);
if (confirmations.Count == 0) {
return true;
}
if (acceptedType != Steam.ConfirmationDetails.EType.Unknown) {
if (confirmations.RemoveWhere(confirmation => (confirmation.Type != acceptedType) && (confirmation.Type != Steam.ConfirmationDetails.EType.Other)) > 0) {
if (confirmations.Count == 0) {
return true;
}
}
}
if ((acceptedSteamID == 0) && ((acceptedTradeIDs == null) || (acceptedTradeIDs.Count == 0))) {
if (!await BotDatabase.MobileAuthenticator.HandleConfirmations(confirmations, accept).ConfigureAwait(false)) {
return false;
}
continue;
}
IEnumerable<Task<Steam.ConfirmationDetails>> tasks = confirmations.Select(BotDatabase.MobileAuthenticator.GetConfirmationDetails);
ICollection<Steam.ConfirmationDetails> results;
switch (Program.GlobalConfig.OptimizationMode) {
case GlobalConfig.EOptimizationMode.MinMemoryUsage:
results = new List<Steam.ConfirmationDetails>(confirmations.Count);
foreach (Task<Steam.ConfirmationDetails> task in tasks) {
results.Add(await task.ConfigureAwait(false));
}
break;
default:
results = await Task.WhenAll(tasks).ConfigureAwait(false);
break;
}
HashSet<MobileAuthenticator.Confirmation> ignoredConfirmations = new HashSet<MobileAuthenticator.Confirmation>(results.Where(details => (details != null) && (((acceptedSteamID != 0) && (details.OtherSteamID64 != 0) && (acceptedSteamID != details.OtherSteamID64)) || ((acceptedTradeIDs != null) && (details.TradeOfferID != 0) && !acceptedTradeIDs.Contains(details.TradeOfferID)))).Select(details => details.Confirmation));
if (ignoredConfirmations.Count > 0) {
confirmations.ExceptWith(ignoredConfirmations);
if (confirmations.Count == 0) {
return true;
}
}
if (!await BotDatabase.MobileAuthenticator.HandleConfirmations(confirmations, accept).ConfigureAwait(false)) {
return false;
}
}
return await BotDatabase.MobileAuthenticator.HandleConfirmations(confirmations, accept).ConfigureAwait(false);
}
internal async Task<bool> DeleteAllRelatedFiles() {

View File

@@ -23,6 +23,7 @@ using System;
using System.Collections.Generic;
using System.Diagnostics.CodeAnalysis;
using System.Linq;
using ArchiSteamFarm.Localization;
using HtmlAgilityPack;
using Newtonsoft.Json;
using SteamKit2;
@@ -180,11 +181,11 @@ namespace ArchiSteamFarm.Json {
return _TradeOfferID;
}
if ((Type != EType.Trade) || (HtmlDocument == null)) {
if ((Type != EType.Trade) || (DocumentNode == null)) {
return 0;
}
HtmlNode htmlNode = HtmlDocument.DocumentNode.SelectSingleNode("//div[@class='tradeoffer']");
HtmlNode htmlNode = DocumentNode.SelectSingleNode("//div[@class='tradeoffer']");
if (htmlNode == null) {
ASF.ArchiLogger.LogNullError(nameof(htmlNode));
return 0;
@@ -218,23 +219,55 @@ namespace ArchiSteamFarm.Json {
}
}
internal EType Type {
get {
if (_Type != EType.Unknown) {
return _Type;
}
if (DocumentNode == null) {
return EType.Unknown;
}
if (DocumentNode.SelectSingleNode("//div[@class='mobileconf_listing_prices']") != null) {
_Type = EType.Market;
return _Type;
}
if (DocumentNode.SelectSingleNode("//div[@class='mobileconf_trade_area']") != null) {
_Type = EType.Trade;
return _Type;
}
_Type = EType.Other;
ASF.ArchiLogger.LogGenericError(string.Format(Strings.WarningUnknownValuePleaseReport, nameof(_Type), _Type));
return _Type;
}
}
#pragma warning disable 649
[JsonProperty(PropertyName = "html", Required = Required.DisallowNull)]
private readonly string HTML;
#pragma warning restore 649
private HtmlDocument HtmlDocument {
private HtmlNode DocumentNode {
get {
if (_HtmlDocument != null) {
return _HtmlDocument;
if (_DocumentNode != null) {
return _DocumentNode;
}
if (string.IsNullOrEmpty(HTML)) {
return null;
}
_HtmlDocument = WebBrowser.StringToHtmlDocument(HTML);
return _HtmlDocument;
HtmlDocument htmlDocument = WebBrowser.StringToHtmlDocument(HTML);
if (htmlDocument == null) {
ASF.ArchiLogger.LogNullError(nameof(htmlDocument));
return null;
}
_DocumentNode = htmlDocument.DocumentNode;
return _DocumentNode;
}
}
@@ -244,11 +277,11 @@ namespace ArchiSteamFarm.Json {
return _OtherSteamID3;
}
if ((Type != EType.Trade) || (HtmlDocument == null)) {
if ((Type != EType.Trade) || (DocumentNode == null)) {
return 0;
}
HtmlNode htmlNode = HtmlDocument.DocumentNode.SelectSingleNode("//a/@data-miniprofile");
HtmlNode htmlNode = DocumentNode.SelectSingleNode("//a/@data-miniprofile");
if (htmlNode == null) {
ASF.ArchiLogger.LogNullError(nameof(htmlNode));
return 0;
@@ -269,33 +302,6 @@ namespace ArchiSteamFarm.Json {
}
}
private EType Type {
get {
if (_Type != EType.Unknown) {
return _Type;
}
if (HtmlDocument == null) {
return EType.Unknown;
}
HtmlNode testNode = HtmlDocument.DocumentNode.SelectSingleNode("//div[@class='mobileconf_listing_prices']");
if (testNode != null) {
_Type = EType.Market;
return _Type;
}
testNode = HtmlDocument.DocumentNode.SelectSingleNode("//div[@class='mobileconf_trade_area']");
if (testNode != null) {
_Type = EType.Trade;
return _Type;
}
_Type = EType.Other;
return _Type;
}
}
internal MobileAuthenticator.Confirmation Confirmation {
get => _Confirmation;
@@ -310,7 +316,7 @@ namespace ArchiSteamFarm.Json {
}
private MobileAuthenticator.Confirmation _Confirmation;
private HtmlDocument _HtmlDocument;
private HtmlNode _DocumentNode;
private uint _OtherSteamID3;
private ulong _OtherSteamID64;
private ulong _TradeOfferID;

View File

@@ -116,7 +116,7 @@ namespace ArchiSteamFarm {
return response?.Success == true ? response : null;
}
internal async Task<HashSet<Confirmation>> GetConfirmations() {
internal async Task<HashSet<Confirmation>> GetConfirmations(Steam.ConfirmationDetails.EType acceptedType = Steam.ConfirmationDetails.EType.Unknown) {
if (!HasCorrectDeviceID) {
Bot.ArchiLogger.LogGenericError(Strings.ErrorMobileAuthenticatorInvalidDeviceID);
return null;
@@ -187,7 +187,11 @@ namespace ArchiSteamFarm {
type = Steam.ConfirmationDetails.EType.Other;
}
result.Add(new Confirmation(id, key, type));
if ((acceptedType != Steam.ConfirmationDetails.EType.Unknown) && (type != acceptedType)) {
continue;
}
result.Add(new Confirmation(id, key));
}
return result;
@@ -373,16 +377,14 @@ namespace ArchiSteamFarm {
internal sealed class Confirmation {
internal readonly ulong ID;
internal readonly ulong Key;
internal readonly Steam.ConfirmationDetails.EType Type;
internal Confirmation(ulong id, ulong key, Steam.ConfirmationDetails.EType type) {
if ((id == 0) || (key == 0) || (type == Steam.ConfirmationDetails.EType.Unknown)) {
throw new ArgumentNullException(nameof(id) + " || " + nameof(key) + " || " + nameof(type));
internal Confirmation(ulong id, ulong key) {
if ((id == 0) || (key == 0)) {
throw new ArgumentNullException(nameof(id) + " || " + nameof(key));
}
ID = id;
Key = key;
Type = type;
}
}
}