General improvements

This commit is contained in:
JustArchi
2016-08-01 18:17:51 +02:00
parent 6977343906
commit d85d41c215
4 changed files with 59 additions and 76 deletions

View File

@@ -223,7 +223,7 @@ namespace ArchiSteamFarm {
PlayGames(new List<uint> { gameID }, gameName); PlayGames(new List<uint> { gameID }, gameName);
} }
internal void PlayGames(ICollection<uint> gameIDs, string gameName = null) { internal void PlayGames(IEnumerable<uint> gameIDs, string gameName = null) {
if (gameIDs == null) { if (gameIDs == null) {
Logging.LogNullError(nameof(gameIDs), Bot.BotName); Logging.LogNullError(nameof(gameIDs), Bot.BotName);
return; return;

View File

@@ -79,6 +79,19 @@ namespace ArchiSteamFarm {
private bool FirstTradeSent, InvalidPassword, SkipFirstShutdown; private bool FirstTradeSent, InvalidPassword, SkipFirstShutdown;
private string AuthCode, TwoFactorCode; private string AuthCode, TwoFactorCode;
internal static string GetAPIStatus() {
var response = new {
Bots
};
try {
return JsonConvert.SerializeObject(response);
} catch (JsonException e) {
Logging.LogGenericException(e);
return null;
}
}
internal static void InitializeCMs(uint cellID, IServerListProvider serverListProvider) { internal static void InitializeCMs(uint cellID, IServerListProvider serverListProvider) {
if (serverListProvider == null) { if (serverListProvider == null) {
Logging.LogNullError(nameof(serverListProvider)); Logging.LogNullError(nameof(serverListProvider));
@@ -835,25 +848,12 @@ namespace ArchiSteamFarm {
} }
private static string ResponseAPI(ulong steamID) { private static string ResponseAPI(ulong steamID) {
if (steamID == 0) { if (steamID != 0) {
Logging.LogNullError(nameof(steamID)); return !IsOwner(steamID) ? null : GetAPIStatus();
return null;
} }
if (!IsOwner(steamID)) { Logging.LogNullError(nameof(steamID));
return null; return null;
}
var response = new {
Bots
};
try {
return JsonConvert.SerializeObject(response);
} catch (JsonException e) {
Logging.LogGenericException(e);
return null;
}
} }
private static string ResponseExit(ulong steamID) { private static string ResponseExit(ulong steamID) {

View File

@@ -81,7 +81,7 @@ namespace ArchiSteamFarm {
internal readonly ConcurrentHashSet<Game> GamesToFarm = new ConcurrentHashSet<Game>(); internal readonly ConcurrentHashSet<Game> GamesToFarm = new ConcurrentHashSet<Game>();
[JsonProperty] [JsonProperty]
internal readonly ConcurrentHashSet<uint> CurrentGamesFarming = new ConcurrentHashSet<uint>(); internal readonly ConcurrentHashSet<Game> CurrentGamesFarming = new ConcurrentHashSet<Game>();
private readonly ManualResetEventSlim FarmResetEvent = new ManualResetEventSlim(false); private readonly ManualResetEventSlim FarmResetEvent = new ManualResetEventSlim(false);
private readonly SemaphoreSlim FarmingSemaphore = new SemaphoreSlim(1); private readonly SemaphoreSlim FarmingSemaphore = new SemaphoreSlim(1);
@@ -170,12 +170,12 @@ namespace ArchiSteamFarm {
if (Bot.BotConfig.CardDropsRestricted) { // If we have restricted card drops, we use complex algorithm if (Bot.BotConfig.CardDropsRestricted) { // If we have restricted card drops, we use complex algorithm
Logging.LogGenericInfo("Chosen farming algorithm: Complex", Bot.BotName); Logging.LogGenericInfo("Chosen farming algorithm: Complex", Bot.BotName);
while (GamesToFarm.Count > 0) { while (GamesToFarm.Count > 0) {
HashSet<uint> gamesToFarmSolo = GetGamesToFarmSolo(GamesToFarm); HashSet<Game> gamesToFarmSolo = new HashSet<Game>(GamesToFarm.Where(game => game.HoursPlayed >= 2));
if (gamesToFarmSolo.Count > 0) { if (gamesToFarmSolo.Count > 0) {
while (gamesToFarmSolo.Count > 0) { while (gamesToFarmSolo.Count > 0) {
uint appID = gamesToFarmSolo.First(); Game game = gamesToFarmSolo.First();
if (await FarmSolo(appID).ConfigureAwait(false)) { if (await FarmSolo(game).ConfigureAwait(false)) {
gamesToFarmSolo.Remove(appID); gamesToFarmSolo.Remove(game);
} else { } else {
NowFarming = false; NowFarming = false;
return; return;
@@ -183,8 +183,7 @@ namespace ArchiSteamFarm {
} }
} else { } else {
if (FarmMultiple()) { if (FarmMultiple()) {
Logging.LogGenericInfo("Done farming: " + string.Join(", ", GamesToFarm.Select(g => g.AppID)), Logging.LogGenericInfo("Done farming: " + string.Join(", ", GamesToFarm.Select(g => g.AppID)), Bot.BotName);
Bot.BotName);
} else { } else {
NowFarming = false; NowFarming = false;
return; return;
@@ -194,8 +193,7 @@ namespace ArchiSteamFarm {
} else { // If we have unrestricted card drops, we use simple algorithm } else { // If we have unrestricted card drops, we use simple algorithm
Logging.LogGenericInfo("Chosen farming algorithm: Simple", Bot.BotName); Logging.LogGenericInfo("Chosen farming algorithm: Simple", Bot.BotName);
while (GamesToFarm.Count > 0) { while (GamesToFarm.Count > 0) {
uint appID = GamesToFarm.First().AppID; if (await FarmSolo(GamesToFarm.First()).ConfigureAwait(false)) {
if (await FarmSolo(appID).ConfigureAwait(false)) {
continue; continue;
} }
@@ -267,20 +265,6 @@ namespace ArchiSteamFarm {
} }
} }
private static HashSet<uint> GetGamesToFarmSolo(IEnumerable<Game> gamesToFarm) {
if (gamesToFarm == null) {
Logging.LogNullError(nameof(gamesToFarm));
return null;
}
HashSet<uint> result = new HashSet<uint>();
foreach (Game game in gamesToFarm.Where(g => g.HoursPlayed >= 2)) {
result.Add(game.AppID);
}
return result;
}
private async Task<bool> IsAnythingToFarm() { private async Task<bool> IsAnythingToFarm() {
Logging.LogGenericInfo("Checking badges...", Bot.BotName); Logging.LogGenericInfo("Checking badges...", Bot.BotName);
@@ -564,7 +548,7 @@ namespace ArchiSteamFarm {
float maxHour = 0; float maxHour = 0;
foreach (Game game in GamesToFarm) { foreach (Game game in GamesToFarm) {
CurrentGamesFarming.Add(game.AppID); CurrentGamesFarming.Add(game);
if (game.HoursPlayed > maxHour) { if (game.HoursPlayed > maxHour) {
maxHour = game.HoursPlayed; maxHour = game.HoursPlayed;
} }
@@ -586,49 +570,44 @@ namespace ArchiSteamFarm {
return result; return result;
} }
private async Task<bool> FarmSolo(uint appID) { private async Task<bool> FarmSolo(Game game) {
if (appID == 0) { if (game == null) {
Logging.LogNullError(nameof(appID), Bot.BotName); Logging.LogNullError(nameof(game), Bot.BotName);
return true; return true;
} }
CurrentGamesFarming.Add(appID); CurrentGamesFarming.Add(game);
Logging.LogGenericInfo("Now farming: " + appID, Bot.BotName); Logging.LogGenericInfo("Now farming: " + game.AppID + " (" + game.GameName + ")", Bot.BotName);
bool result = await Farm(appID).ConfigureAwait(false); bool result = await Farm(game).ConfigureAwait(false);
CurrentGamesFarming.ClearAndTrim(); CurrentGamesFarming.ClearAndTrim();
if (!result) { if (!result) {
return false; return false;
} }
Game game = GamesToFarm.FirstOrDefault(g => g.AppID == appID);
if (game == null) {
return false;
}
GamesToFarm.Remove(game); GamesToFarm.Remove(game);
TimeSpan timeSpan = TimeSpan.FromHours(game.HoursPlayed); TimeSpan timeSpan = TimeSpan.FromHours(game.HoursPlayed);
Logging.LogGenericInfo("Done farming: " + appID + " after " + timeSpan.ToString(@"hh\:mm") + " hours of playtime!", Bot.BotName); Logging.LogGenericInfo("Done farming: " + game.AppID + " (" + game.GameName + ") after " + timeSpan.ToString(@"hh\:mm") + " hours of playtime!", Bot.BotName);
return true; return true;
} }
private async Task<bool> Farm(uint appID) { private async Task<bool> Farm(Game game) {
if (appID == 0) { if (game == null) {
Logging.LogNullError(nameof(appID), Bot.BotName); Logging.LogNullError(nameof(game), Bot.BotName);
return false; return false;
} }
Bot.ArchiHandler.PlayGame(appID, Bot.BotConfig.CustomGamePlayedWhileFarming); Bot.ArchiHandler.PlayGame(game.AppID, Bot.BotConfig.CustomGamePlayedWhileFarming);
DateTime endFarmingDate = DateTime.Now.AddHours(Program.GlobalConfig.MaxFarmingTime); DateTime endFarmingDate = DateTime.Now.AddHours(Program.GlobalConfig.MaxFarmingTime);
bool success = true; bool success = true;
bool? keepFarming = await ShouldFarm(appID).ConfigureAwait(false); bool? keepFarming = await ShouldFarm(game.AppID).ConfigureAwait(false);
while (keepFarming.GetValueOrDefault(true) && (DateTime.Now < endFarmingDate)) { while (keepFarming.GetValueOrDefault(true) && (DateTime.Now < endFarmingDate)) {
Logging.LogGenericInfo("Still farming: " + appID, Bot.BotName); Logging.LogGenericInfo("Still farming: " + game.AppID + " (" + game.GameName + ")", Bot.BotName);
DateTime startFarmingPeriod = DateTime.Now; DateTime startFarmingPeriod = DateTime.Now;
if (FarmResetEvent.Wait(60 * 1000 * Program.GlobalConfig.FarmingDelay)) { if (FarmResetEvent.Wait(60 * 1000 * Program.GlobalConfig.FarmingDelay)) {
@@ -637,31 +616,30 @@ namespace ArchiSteamFarm {
} }
// Don't forget to update our GamesToFarm hours // Don't forget to update our GamesToFarm hours
Game game = GamesToFarm.First(g => g.AppID == appID);
game.HoursPlayed += (float) DateTime.Now.Subtract(startFarmingPeriod).TotalHours; game.HoursPlayed += (float) DateTime.Now.Subtract(startFarmingPeriod).TotalHours;
if (!success) { if (!success) {
break; break;
} }
keepFarming = await ShouldFarm(appID).ConfigureAwait(false); keepFarming = await ShouldFarm(game.AppID).ConfigureAwait(false);
} }
Logging.LogGenericInfo("Stopped farming: " + appID, Bot.BotName); Logging.LogGenericInfo("Stopped farming: " + game.AppID + " (" + game.GameName + ")", Bot.BotName);
return success; return success;
} }
private bool FarmHours(float maxHour, ConcurrentHashSet<uint> appIDs) { private bool FarmHours(float maxHour, ConcurrentHashSet<Game> games) {
if ((maxHour < 0) || (appIDs == null) || (appIDs.Count == 0)) { if ((maxHour < 0) || (games == null) || (games.Count == 0)) {
Logging.LogNullError(nameof(maxHour) + " || " + nameof(appIDs) + " || " + nameof(appIDs.Count), Bot.BotName); Logging.LogNullError(nameof(maxHour) + " || " + nameof(games), Bot.BotName);
return false; return false;
} }
Bot.ArchiHandler.PlayGames(appIDs, Bot.BotConfig.CustomGamePlayedWhileFarming); Bot.ArchiHandler.PlayGames(games.Select(game => game.AppID), Bot.BotConfig.CustomGamePlayedWhileFarming);
bool success = true; bool success = true;
while (maxHour < 2) { while (maxHour < 2) {
Logging.LogGenericInfo("Still farming: " + string.Join(", ", appIDs), Bot.BotName); Logging.LogGenericInfo("Still farming: " + string.Join(", ", games.Select(game => game.AppID)), Bot.BotName);
DateTime startFarmingPeriod = DateTime.Now; DateTime startFarmingPeriod = DateTime.Now;
if (FarmResetEvent.Wait(60 * 1000 * Program.GlobalConfig.FarmingDelay)) { if (FarmResetEvent.Wait(60 * 1000 * Program.GlobalConfig.FarmingDelay)) {
@@ -671,7 +649,7 @@ namespace ArchiSteamFarm {
// Don't forget to update our GamesToFarm hours // Don't forget to update our GamesToFarm hours
float timePlayed = (float) DateTime.Now.Subtract(startFarmingPeriod).TotalHours; float timePlayed = (float) DateTime.Now.Subtract(startFarmingPeriod).TotalHours;
foreach (Game game in GamesToFarm.Where(game => appIDs.Contains(game.AppID))) { foreach (Game game in games) {
game.HoursPlayed += timePlayed; game.HoursPlayed += timePlayed;
} }
@@ -682,7 +660,7 @@ namespace ArchiSteamFarm {
maxHour += timePlayed; maxHour += timePlayed;
} }
Logging.LogGenericInfo("Stopped farming: " + string.Join(", ", appIDs), Bot.BotName); Logging.LogGenericInfo("Stopped farming: " + string.Join(", ", games.Select(game => game.AppID)), Bot.BotName);
return success; return success;
} }
} }

View File

@@ -31,6 +31,9 @@ using System.ServiceModel.Description;
namespace ArchiSteamFarm { namespace ArchiSteamFarm {
[ServiceContract] [ServiceContract]
internal interface IWCF { internal interface IWCF {
[OperationContract]
string GetStatus();
[OperationContract] [OperationContract]
string HandleCommand(string input); string HandleCommand(string input);
} }
@@ -59,15 +62,15 @@ namespace ArchiSteamFarm {
return null; return null;
} }
if (Program.GlobalConfig.SteamOwnerID == 0) {
return "Refusing to handle request because SteamOwnerID is not set!";
}
Bot bot = Bot.Bots.Values.FirstOrDefault(); Bot bot = Bot.Bots.Values.FirstOrDefault();
if (bot == null) { if (bot == null) {
return "ERROR: No bots are enabled!"; return "ERROR: No bots are enabled!";
} }
if (Program.GlobalConfig.SteamOwnerID == 0) {
return "Refusing to handle request because SteamOwnerID is not set!";
}
string command = "!" + input; string command = "!" + input;
string output = bot.Response(Program.GlobalConfig.SteamOwnerID, command).Result; // TODO: This should be asynchronous string output = bot.Response(Program.GlobalConfig.SteamOwnerID, command).Result; // TODO: This should be asynchronous
@@ -75,6 +78,8 @@ namespace ArchiSteamFarm {
return output; return output;
} }
public string GetStatus() => Program.GlobalConfig.SteamOwnerID == 0 ? "{}" : Bot.GetAPIStatus();
public void Dispose() { public void Dispose() {
Client?.Close(); Client?.Close();
StopServer(); StopServer();
@@ -138,10 +143,10 @@ namespace ArchiSteamFarm {
} }
} }
internal sealed class Client : ClientBase<IWCF>, IWCF { internal sealed class Client : ClientBase<IWCF> {
internal Client(Binding binding, EndpointAddress address) : base(binding, address) { } internal Client(Binding binding, EndpointAddress address) : base(binding, address) { }
public string HandleCommand(string input) { internal string HandleCommand(string input) {
if (string.IsNullOrEmpty(input)) { if (string.IsNullOrEmpty(input)) {
Logging.LogNullError(nameof(input)); Logging.LogNullError(nameof(input));
return null; return null;