Code review

This commit is contained in:
JustArchi
2016-03-21 16:31:59 +01:00
parent 41c2bd9131
commit 7d3c05088a
3 changed files with 102 additions and 89 deletions

View File

@@ -229,7 +229,7 @@ namespace ArchiSteamFarm {
CardsFarmer = new CardsFarmer(this);
Trading = new Trading(this);
if (BotConfig.AcceptConfirmationsPeriod > 0 && AcceptConfirmationsTimer == null) {
if (BotConfig.AcceptConfirmationsPeriod > 0) {
AcceptConfirmationsTimer = new Timer(
async e => await AcceptConfirmations().ConfigureAwait(false),
null,
@@ -238,7 +238,7 @@ namespace ArchiSteamFarm {
);
}
if (BotConfig.SendTradePeriod > 0 && SendItemsTimer == null) {
if (BotConfig.SendTradePeriod > 0) {
SendItemsTimer = new Timer(
async e => await ResponseSendTrade(BotConfig.SteamMasterID).ConfigureAwait(false),
null,
@@ -516,9 +516,13 @@ namespace ArchiSteamFarm {
}
if (CardsFarmer.CurrentGamesFarming.Count > 0) {
return "Bot " + BotName + " is currently farming appIDs: " + string.Join(", ", CardsFarmer.CurrentGamesFarming) + " and has a total of " + CardsFarmer.GamesToFarm.Count + " games left to farm.";
return "Bot " + BotName + " is farming appIDs: " + string.Join(", ", CardsFarmer.CurrentGamesFarming) + " and has a total of " + CardsFarmer.GamesToFarm.Count + " games left to farm.";
} else if (SteamClient.IsConnected) {
return "Bot " + BotName + " is not farming anything.";
} else if (KeepRunning) {
return "Bot " + BotName + " is not connected.";
} else {
return "Bot " + BotName + " is currently not farming anything.";
return "Bot " + BotName + " is not running.";
}
}
@@ -546,9 +550,7 @@ namespace ArchiSteamFarm {
StringBuilder result = new StringBuilder(Environment.NewLine);
int totalBotsCount = Bots.Count;
int runningBotsCount = 0;
byte runningBotsCount = 0;
foreach (Bot bot in Bots.Values) {
result.Append(bot.ResponseStatus(steamID) + Environment.NewLine);
if (bot.KeepRunning) {
@@ -556,7 +558,7 @@ namespace ArchiSteamFarm {
}
}
result.Append("There are " + totalBotsCount + " bots initialized and " + runningBotsCount + " of them are currently running.");
result.Append("There are " + runningBotsCount + "/" + Bots.Count + "bots running.");
return result.ToString();
}

View File

@@ -39,12 +39,10 @@ namespace ArchiSteamFarm {
private readonly ManualResetEvent FarmResetEvent = new ManualResetEvent(false);
private readonly SemaphoreSlim Semaphore = new SemaphoreSlim(1);
private readonly Bot Bot;
private readonly Timer Timer;
private bool ManualMode = false;
private bool NowFarming = false;
private bool ManualMode, NowFarming;
internal CardsFarmer(Bot bot) {
if (bot == null) {
@@ -53,7 +51,7 @@ namespace ArchiSteamFarm {
Bot = bot;
if (Program.GlobalConfig.IdleFarmingPeriod > 0 && Timer == null) {
if (Program.GlobalConfig.IdleFarmingPeriod > 0) {
Timer = new Timer(
async e => await CheckGamesForFarming().ConfigureAwait(false),
null,
@@ -63,29 +61,6 @@ namespace ArchiSteamFarm {
}
}
internal static HashSet<uint> GetGamesToFarmSolo(ConcurrentDictionary<uint, float> gamesToFarm) {
if (gamesToFarm == null) {
return null;
}
HashSet<uint> result = new HashSet<uint>();
foreach (KeyValuePair<uint, float> keyValue in gamesToFarm) {
if (keyValue.Value >= 2) {
result.Add(keyValue.Key);
}
}
return result;
}
internal static uint GetAnyGameToFarm(ConcurrentDictionary<uint, float> gamesToFarm) {
if (gamesToFarm == null) {
return 0;
}
return gamesToFarm.Keys.FirstOrDefault();
}
internal async Task<bool> SwitchToManualMode(bool manualMode) {
if (ManualMode == manualMode) {
return false;
@@ -104,62 +79,18 @@ namespace ArchiSteamFarm {
return true;
}
internal bool FarmMultiple(ConcurrentDictionary<uint, float> appIDs) {
if (appIDs.Count == 0) {
return true;
}
float maxHour = 0;
foreach (float hour in appIDs.Values) {
if (hour > maxHour) {
maxHour = hour;
}
}
CurrentGamesFarming.Clear();
CurrentGamesFarming.TrimExcess();
foreach (uint appID in appIDs.Keys) {
CurrentGamesFarming.Add(appID);
}
Logging.LogGenericInfo("Now farming: " + string.Join(", ", appIDs.Keys), Bot.BotName);
if (Farm(maxHour, appIDs.Keys)) {
CurrentGamesFarming.Clear();
return true;
} else {
CurrentGamesFarming.Clear();
return false;
}
}
internal async Task<bool> FarmSolo(uint appID) {
if (appID == 0) {
return true;
}
CurrentGamesFarming.Clear();
CurrentGamesFarming.TrimExcess();
CurrentGamesFarming.Add(appID);
Logging.LogGenericInfo("Now farming: " + appID, Bot.BotName);
if (await Farm(appID).ConfigureAwait(false)) {
float hours;
GamesToFarm.TryRemove(appID, out hours);
return true;
} else {
CurrentGamesFarming.Clear();
return false;
}
}
internal async Task RestartFarming() {
await StopFarming().ConfigureAwait(false);
await StartFarming().ConfigureAwait(false);
}
internal async Task StartFarming() {
if (NowFarming) {
return;
}
await Semaphore.WaitAsync().ConfigureAwait(false);
if (NowFarming) {
Semaphore.Release();
return;
}
if (ManualMode) {
Semaphore.Release(); // We have nothing to do, don't forget to release semaphore
return;
@@ -234,6 +165,10 @@ namespace ArchiSteamFarm {
}
internal async Task StopFarming() {
if (!NowFarming) {
return;
}
await Semaphore.WaitAsync().ConfigureAwait(false);
if (!NowFarming) {
@@ -252,6 +187,34 @@ namespace ArchiSteamFarm {
Semaphore.Release();
}
internal async Task RestartFarming() {
await StopFarming().ConfigureAwait(false);
await StartFarming().ConfigureAwait(false);
}
private static HashSet<uint> GetGamesToFarmSolo(ConcurrentDictionary<uint, float> gamesToFarm) {
if (gamesToFarm == null) {
return null;
}
HashSet<uint> result = new HashSet<uint>();
foreach (KeyValuePair<uint, float> keyValue in gamesToFarm) {
if (keyValue.Value >= 2) {
result.Add(keyValue.Key);
}
}
return result;
}
private static uint GetAnyGameToFarm(ConcurrentDictionary<uint, float> gamesToFarm) {
if (gamesToFarm == null) {
return 0;
}
return gamesToFarm.Keys.FirstOrDefault();
}
private async Task<bool> IsAnythingToFarm() {
if (NowFarming) {
return true;
@@ -418,6 +381,54 @@ namespace ArchiSteamFarm {
return !htmlNode.InnerText.Contains("No card drops");
}
private bool FarmMultiple(ConcurrentDictionary<uint, float> appIDs) {
if (appIDs.Count == 0) {
return true;
}
float maxHour = 0;
foreach (float hour in appIDs.Values) {
if (hour > maxHour) {
maxHour = hour;
}
}
CurrentGamesFarming.Clear();
CurrentGamesFarming.TrimExcess();
foreach (uint appID in appIDs.Keys) {
CurrentGamesFarming.Add(appID);
}
Logging.LogGenericInfo("Now farming: " + string.Join(", ", appIDs.Keys), Bot.BotName);
if (Farm(maxHour, appIDs.Keys)) {
CurrentGamesFarming.Clear();
return true;
} else {
CurrentGamesFarming.Clear();
return false;
}
}
private async Task<bool> FarmSolo(uint appID) {
if (appID == 0) {
return true;
}
CurrentGamesFarming.Clear();
CurrentGamesFarming.TrimExcess();
CurrentGamesFarming.Add(appID);
Logging.LogGenericInfo("Now farming: " + appID, Bot.BotName);
if (await Farm(appID).ConfigureAwait(false)) {
float hours;
GamesToFarm.TryRemove(appID, out hours);
return true;
} else {
CurrentGamesFarming.Clear();
return false;
}
}
private async Task<bool> Farm(uint appID) {
Bot.ArchiHandler.PlayGames(appID);

View File

@@ -149,7 +149,7 @@ namespace ArchiSteamFarm {
Logging.LogGenericInfo("Local version: " + Version + " | Remote version: " + newVersion);
if (Version.CompareTo(newVersion) >= 0) { // If local version is the same or newer than remote version
if (GlobalConfig.AutoUpdates && AutoUpdatesTimer == null) {
if (AutoUpdatesTimer == null && GlobalConfig.AutoUpdates) {
Logging.LogGenericInfo("ASF will automatically check for new versions every 24 hours");
AutoUpdatesTimer = new Timer(
async e => await CheckForUpdate().ConfigureAwait(false),