From 59218684b2512c51a2840324a08fd3cbe44e08da Mon Sep 17 00:00:00 2001 From: JustArchi Date: Fri, 21 Sep 2018 17:16:43 +0200 Subject: [PATCH] Add BGR action Existing one soon to be deprecated --- .../IPC/Controllers/Api/ASFController.cs | 10 +-- .../IPC/Controllers/Api/BotController.cs | 74 +++++++++++++++++-- .../IPC/Controllers/Api/CommandController.cs | 2 +- .../GamesToRedeemInBackgroundController.cs | 6 +- .../IPC/Controllers/Api/NLogController.cs | 2 +- .../Controllers/Api/StructureController.cs | 2 +- .../IPC/Controllers/Api/TypeController.cs | 2 +- 7 files changed, 79 insertions(+), 19 deletions(-) diff --git a/ArchiSteamFarm/IPC/Controllers/Api/ASFController.cs b/ArchiSteamFarm/IPC/Controllers/Api/ASFController.cs index 1f1f0b5ff..547e230ca 100644 --- a/ArchiSteamFarm/IPC/Controllers/Api/ASFController.cs +++ b/ArchiSteamFarm/IPC/Controllers/Api/ASFController.cs @@ -33,7 +33,7 @@ namespace ArchiSteamFarm.IPC.Controllers.Api { [Route("Api/ASF")] public sealed class ASFController : ControllerBase { [HttpGet] - public ActionResult> Get() { + public ActionResult> ASFGet() { uint memoryUsage = (uint) GC.GetTotalMemory(false) / 1024; DateTime processStartTime; @@ -47,7 +47,7 @@ namespace ArchiSteamFarm.IPC.Controllers.Api { } [HttpPost] - public async Task> Post([FromBody] ASFRequest request) { + public async Task> ASFPost([FromBody] ASFRequest request) { if (request == null) { ASF.ArchiLogger.LogNullError(nameof(request)); return BadRequest(new GenericResponse(false, string.Format(Strings.ErrorIsEmpty, nameof(request)))); @@ -73,19 +73,19 @@ namespace ArchiSteamFarm.IPC.Controllers.Api { } [HttpPost("Exit")] - public ActionResult PostExit() { + public ActionResult ExitPost() { (bool success, string output) = Actions.Exit(); return Ok(new GenericResponse(success, output)); } [HttpPost("Restart")] - public ActionResult PostRestart() { + public ActionResult RestartPost() { (bool success, string output) = Actions.Restart(); return Ok(new GenericResponse(success, output)); } [HttpPost("Update")] - public async Task>> PostUpdate() { + public async Task>> UpdatePost() { (bool success, Version version) = await Actions.Update().ConfigureAwait(false); return Ok(new GenericResponse(success, version)); } diff --git a/ArchiSteamFarm/IPC/Controllers/Api/BotController.cs b/ArchiSteamFarm/IPC/Controllers/Api/BotController.cs index 746f52fa5..10d1090cb 100644 --- a/ArchiSteamFarm/IPC/Controllers/Api/BotController.cs +++ b/ArchiSteamFarm/IPC/Controllers/Api/BotController.cs @@ -21,6 +21,7 @@ using System; using System.Collections.Generic; +using System.Collections.Specialized; using System.IO; using System.Linq; using System.Threading.Tasks; @@ -34,7 +35,7 @@ namespace ArchiSteamFarm.IPC.Controllers.Api { [Route("Api/Bot")] public sealed class BotController : ControllerBase { [HttpDelete("{botNames:required}")] - public async Task> Delete(string botNames) { + public async Task> BotDelete(string botNames) { if (string.IsNullOrEmpty(botNames)) { ASF.ArchiLogger.LogNullError(nameof(botNames)); return BadRequest(new GenericResponse(false, string.Format(Strings.ErrorIsEmpty, nameof(botNames)))); @@ -50,7 +51,7 @@ namespace ArchiSteamFarm.IPC.Controllers.Api { } [HttpGet("{botNames:required}")] - public ActionResult>> Get(string botNames) { + public ActionResult>> BotGet(string botNames) { if (string.IsNullOrEmpty(botNames)) { ASF.ArchiLogger.LogNullError(nameof(botNames)); return BadRequest(new GenericResponse>(false, string.Format(Strings.ErrorIsEmpty, nameof(botNames)))); @@ -65,7 +66,7 @@ namespace ArchiSteamFarm.IPC.Controllers.Api { } [HttpPost("{botName:required}")] - public async Task> Post(string botName, [FromBody] BotRequest request) { + public async Task> BotPost(string botName, [FromBody] BotRequest request) { if (string.IsNullOrEmpty(botName) || (request == null)) { ASF.ArchiLogger.LogNullError(nameof(botName) + " || " + nameof(request)); return BadRequest(new GenericResponse(false, string.Format(Strings.ErrorIsEmpty, nameof(botName) + " || " + nameof(request)))); @@ -98,8 +99,67 @@ namespace ArchiSteamFarm.IPC.Controllers.Api { return Ok(new GenericResponse(result)); } + [HttpDelete("{botNames:required}/GamesToRedeemInBackground")] + public async Task> GamesToRedeemInBackgroundDelete(string botNames) { + if (string.IsNullOrEmpty(botNames)) { + ASF.ArchiLogger.LogNullError(nameof(botNames)); + return BadRequest(new GenericResponse(false, string.Format(Strings.ErrorIsEmpty, nameof(botNames)))); + } + + HashSet bots = Bot.GetBots(botNames); + if ((bots == null) || (bots.Count == 0)) { + return BadRequest(new GenericResponse(false, string.Format(Strings.BotNotFound, botNames))); + } + + IList results = await Utilities.InParallel(bots.Select(bot => Task.Run(bot.DeleteRedeemedKeysFiles))).ConfigureAwait(false); + return Ok(results.All(result => result) ? new GenericResponse(true) : new GenericResponse(false, Strings.WarningFailed)); + } + + [HttpGet("{botNames:required}/GamesToRedeemInBackground")] + public async Task>>> GamesToRedeemInBackgroundGet(string botNames) { + if (string.IsNullOrEmpty(botNames)) { + ASF.ArchiLogger.LogNullError(nameof(botNames)); + return BadRequest(new GenericResponse>(false, string.Format(Strings.ErrorIsEmpty, nameof(botNames)))); + } + + HashSet bots = Bot.GetBots(botNames); + if ((bots == null) || (bots.Count == 0)) { + return BadRequest(new GenericResponse>(false, string.Format(Strings.BotNotFound, botNames))); + } + + IList<(Dictionary UnusedKeys, Dictionary UsedKeys)> results = await Utilities.InParallel(bots.Select(bot => bot.GetUsedAndUnusedKeys())).ConfigureAwait(false); + + Dictionary result = new Dictionary(); + + foreach (Bot bot in bots) { + (Dictionary unusedKeys, Dictionary usedKeys) = results[result.Count]; + result[bot.BotName] = new GamesToRedeemInBackgroundResponse(unusedKeys, usedKeys); + } + + return Ok(new GenericResponse>(result)); + } + + [HttpPost("{botName:required}/GamesToRedeemInBackground")] + public async Task>> GamesToRedeemInBackgroundPost(string botName, [FromBody] GamesToRedeemInBackgroundRequest request) { + if (string.IsNullOrEmpty(botName) || (request == null)) { + ASF.ArchiLogger.LogNullError(nameof(botName) + " || " + nameof(request)); + return BadRequest(new GenericResponse(false, string.Format(Strings.ErrorIsEmpty, nameof(botName) + " || " + nameof(request)))); + } + + if (request.GamesToRedeemInBackground.Count == 0) { + return BadRequest(new GenericResponse(false, string.Format(Strings.ErrorIsEmpty, nameof(request.GamesToRedeemInBackground)))); + } + + if (!Bot.Bots.TryGetValue(botName, out Bot bot)) { + return BadRequest(new GenericResponse(false, string.Format(Strings.BotNotFound, botName))); + } + + bool result = await bot.ValidateAndAddGamesToRedeemInBackground(request.GamesToRedeemInBackground).ConfigureAwait(false); + return Ok(new GenericResponse(result, request.GamesToRedeemInBackground)); + } + [HttpPost("{botNames:required}/Pause")] - public async Task> PostPause(string botNames, [FromBody] BotPauseRequest request) { + public async Task> PausePost(string botNames, [FromBody] BotPauseRequest request) { if (string.IsNullOrEmpty(botNames) || (request == null)) { ASF.ArchiLogger.LogNullError(nameof(botNames) + " || " + nameof(request)); return BadRequest(new GenericResponse(false, string.Format(Strings.ErrorIsEmpty, nameof(botNames) + " || " + nameof(request)))); @@ -115,7 +175,7 @@ namespace ArchiSteamFarm.IPC.Controllers.Api { } [HttpPost("{botNames:required}/Resume")] - public async Task> PostResume(string botNames) { + public async Task> ResumePost(string botNames) { if (string.IsNullOrEmpty(botNames)) { ASF.ArchiLogger.LogNullError(nameof(botNames)); return BadRequest(new GenericResponse(false, string.Format(Strings.ErrorIsEmpty, nameof(botNames)))); @@ -131,7 +191,7 @@ namespace ArchiSteamFarm.IPC.Controllers.Api { } [HttpPost("{botNames:required}/Start")] - public async Task> PostStart(string botNames) { + public async Task> StartPost(string botNames) { if (string.IsNullOrEmpty(botNames)) { ASF.ArchiLogger.LogNullError(nameof(botNames)); return BadRequest(new GenericResponse(false, string.Format(Strings.ErrorIsEmpty, nameof(botNames)))); @@ -147,7 +207,7 @@ namespace ArchiSteamFarm.IPC.Controllers.Api { } [HttpPost("{botNames:required}/Stop")] - public async Task> PostStop(string botNames) { + public async Task> StopPost(string botNames) { if (string.IsNullOrEmpty(botNames)) { ASF.ArchiLogger.LogNullError(nameof(botNames)); return BadRequest(new GenericResponse(false, string.Format(Strings.ErrorIsEmpty, nameof(botNames)))); diff --git a/ArchiSteamFarm/IPC/Controllers/Api/CommandController.cs b/ArchiSteamFarm/IPC/Controllers/Api/CommandController.cs index 5fc5bfcd9..e1eb3756e 100644 --- a/ArchiSteamFarm/IPC/Controllers/Api/CommandController.cs +++ b/ArchiSteamFarm/IPC/Controllers/Api/CommandController.cs @@ -31,7 +31,7 @@ namespace ArchiSteamFarm.IPC.Controllers.Api { [Route("Api/Command")] public sealed class CommandController : ControllerBase { [HttpPost("{command:required}")] - public async Task>> Post(string command) { + public async Task>> CommandPost(string command) { if (string.IsNullOrEmpty(command)) { ASF.ArchiLogger.LogNullError(nameof(command)); return BadRequest(new GenericResponse(false, string.Format(Strings.ErrorIsEmpty, nameof(command)))); diff --git a/ArchiSteamFarm/IPC/Controllers/Api/GamesToRedeemInBackgroundController.cs b/ArchiSteamFarm/IPC/Controllers/Api/GamesToRedeemInBackgroundController.cs index f08ef4211..68facffb0 100644 --- a/ArchiSteamFarm/IPC/Controllers/Api/GamesToRedeemInBackgroundController.cs +++ b/ArchiSteamFarm/IPC/Controllers/Api/GamesToRedeemInBackgroundController.cs @@ -33,7 +33,7 @@ namespace ArchiSteamFarm.IPC.Controllers.Api { [Route("Api/GamesToRedeemInBackground")] public sealed class GamesToRedeemInBackgroundController : ControllerBase { [HttpDelete("{botNames:required}")] - public async Task> Delete(string botNames) { + public async Task> GamesToRedeemInBackgroundDelete(string botNames) { if (string.IsNullOrEmpty(botNames)) { ASF.ArchiLogger.LogNullError(nameof(botNames)); return BadRequest(new GenericResponse(false, string.Format(Strings.ErrorIsEmpty, nameof(botNames)))); @@ -49,7 +49,7 @@ namespace ArchiSteamFarm.IPC.Controllers.Api { } [HttpGet("{botNames:required}")] - public async Task>>> Get(string botNames) { + public async Task>>> GamesToRedeemInBackgroundGet(string botNames) { if (string.IsNullOrEmpty(botNames)) { ASF.ArchiLogger.LogNullError(nameof(botNames)); return BadRequest(new GenericResponse>(false, string.Format(Strings.ErrorIsEmpty, nameof(botNames)))); @@ -73,7 +73,7 @@ namespace ArchiSteamFarm.IPC.Controllers.Api { } [HttpPost("{botName:required}")] - public async Task>> Post(string botName, [FromBody] GamesToRedeemInBackgroundRequest request) { + public async Task>> GamesToRedeemInBackgroundPost(string botName, [FromBody] GamesToRedeemInBackgroundRequest request) { if (string.IsNullOrEmpty(botName) || (request == null)) { ASF.ArchiLogger.LogNullError(nameof(botName) + " || " + nameof(request)); return BadRequest(new GenericResponse(false, string.Format(Strings.ErrorIsEmpty, nameof(botName) + " || " + nameof(request)))); diff --git a/ArchiSteamFarm/IPC/Controllers/Api/NLogController.cs b/ArchiSteamFarm/IPC/Controllers/Api/NLogController.cs index 52177695e..b45482375 100644 --- a/ArchiSteamFarm/IPC/Controllers/Api/NLogController.cs +++ b/ArchiSteamFarm/IPC/Controllers/Api/NLogController.cs @@ -39,7 +39,7 @@ namespace ArchiSteamFarm.IPC.Controllers.Api { private static readonly ConcurrentDictionary ActiveLogWebSockets = new ConcurrentDictionary(); [HttpGet] - public async Task Get() { + public async Task NLogGet() { if (!HttpContext.WebSockets.IsWebSocketRequest) { return BadRequest(new GenericResponse(false, string.Format(Strings.WarningFailedWithError, nameof(HttpContext.WebSockets.IsWebSocketRequest) + ": " + HttpContext.WebSockets.IsWebSocketRequest))); } diff --git a/ArchiSteamFarm/IPC/Controllers/Api/StructureController.cs b/ArchiSteamFarm/IPC/Controllers/Api/StructureController.cs index 874cc6b51..dda5a76ec 100644 --- a/ArchiSteamFarm/IPC/Controllers/Api/StructureController.cs +++ b/ArchiSteamFarm/IPC/Controllers/Api/StructureController.cs @@ -29,7 +29,7 @@ namespace ArchiSteamFarm.IPC.Controllers.Api { [Route("Api/Structure")] public sealed class StructureController : ControllerBase { [HttpGet("{structure:required}")] - public ActionResult> Get(string structure) { + public ActionResult> StructureGet(string structure) { if (string.IsNullOrEmpty(structure)) { ASF.ArchiLogger.LogNullError(nameof(structure)); return BadRequest(new GenericResponse(false, string.Format(Strings.ErrorIsEmpty, nameof(structure)))); diff --git a/ArchiSteamFarm/IPC/Controllers/Api/TypeController.cs b/ArchiSteamFarm/IPC/Controllers/Api/TypeController.cs index 47ae8f8f4..9817636dc 100644 --- a/ArchiSteamFarm/IPC/Controllers/Api/TypeController.cs +++ b/ArchiSteamFarm/IPC/Controllers/Api/TypeController.cs @@ -33,7 +33,7 @@ namespace ArchiSteamFarm.IPC.Controllers.Api { [Route("Api/Type")] public sealed class TypeController : ControllerBase { [HttpGet("{type:required}")] - public ActionResult> Get(string type) { + public ActionResult> TypeGet(string type) { if (string.IsNullOrEmpty(type)) { ASF.ArchiLogger.LogNullError(nameof(type)); return BadRequest(new GenericResponse(false, string.Format(Strings.ErrorIsEmpty, nameof(type))));