From 167b94e4de3b0d4b01d3b7ec1b641b5cf52962c9 Mon Sep 17 00:00:00 2001 From: Rob Date: Tue, 11 Jul 2023 12:40:12 +0200 Subject: [PATCH] moved bots to own library, added microsoft logging and updated main program --- .../Bobo.System.Maze.Bot.csproj | 14 ++++ .../Bot.cs | 16 ++-- .../Bot => Bobo.System.Maze.Bot}/BumpBot.cs | 6 +- .../Model/MazeTile.cs | 0 Bobo.System.Maze.Bot/SimpleBot.cs | 84 +++++++++++++++++++ .../SimplerBot.cs | 8 +- .../Bobo.System.Maze.Console.csproj | 10 ++- Bobo.Systems.Maze.Console/Program.cs | 27 ++++-- Bobo.Systems.Maze.sln | 6 ++ 9 files changed, 146 insertions(+), 25 deletions(-) create mode 100644 Bobo.System.Maze.Bot/Bobo.System.Maze.Bot.csproj rename Bobo.Systems.Maze.Console/Bot/SimpleBot.cs => Bobo.System.Maze.Bot/Bot.cs (95%) rename {Bobo.Systems.Maze.Console/Bot => Bobo.System.Maze.Bot}/BumpBot.cs (91%) rename {Bobo.Systems.Maze.Console => Bobo.System.Maze.Bot}/Model/MazeTile.cs (100%) create mode 100644 Bobo.System.Maze.Bot/SimpleBot.cs rename {Bobo.Systems.Maze.Console/Bot => Bobo.System.Maze.Bot}/SimplerBot.cs (93%) diff --git a/Bobo.System.Maze.Bot/Bobo.System.Maze.Bot.csproj b/Bobo.System.Maze.Bot/Bobo.System.Maze.Bot.csproj new file mode 100644 index 0000000..5e5c48a --- /dev/null +++ b/Bobo.System.Maze.Bot/Bobo.System.Maze.Bot.csproj @@ -0,0 +1,14 @@ + + + + net6.0 + enable + enable + + + + + + + + diff --git a/Bobo.Systems.Maze.Console/Bot/SimpleBot.cs b/Bobo.System.Maze.Bot/Bot.cs similarity index 95% rename from Bobo.Systems.Maze.Console/Bot/SimpleBot.cs rename to Bobo.System.Maze.Bot/Bot.cs index 6c6301f..7822bb7 100644 --- a/Bobo.Systems.Maze.Console/Bot/SimpleBot.cs +++ b/Bobo.System.Maze.Bot/Bot.cs @@ -1,18 +1,19 @@ -using Bobo.Systems.Maze.Console.Model; +using Bobo.System.Maze.Bot; +using Bobo.Systems.Maze.Console.Model; using HightechICT.Amazeing.Client.Rest; using System.Collections.ObjectModel; using LogConsole = System.Console; -namespace Bobo.System.Maze.Console.Bot +namespace Bobo.System.Maze.Bot { - internal class SimpleBot + public class Bot { private Collection Maze = new(); internal MazeTile CurrentTile { get; set; } = new(); - internal async Task Run(string mazeName, AmazeingClient mazeClient) + public async Task Run(string mazeName, AmazeingClient mazeClient) { if (mazeClient == null) throw new ArgumentException(nameof(mazeClient)); @@ -50,7 +51,7 @@ namespace Bobo.System.Maze.Console.Bot } int selected = random.Next(notExploredMoves.Length - 1); - + MoveAction moveAction = notExploredMoves[selected]; LogConsole.WriteLine($"Moving {moveAction.Direction}, I have {(!moveAction.HasBeenVisited ? "not" : string.Empty)} been here."); @@ -68,7 +69,7 @@ namespace Bobo.System.Maze.Console.Bot AddOrUpdateMaze(item); } - + LogConsole.WriteLine($"New tile has {(!CurrentTile.CanExitMazeHere ? "no" : string.Empty)} exit."); @@ -89,7 +90,6 @@ namespace Bobo.System.Maze.Console.Bot // fetch the tile from the collection, if there is one MazeTile? inCollection = Maze.SingleOrDefault(listTile => listTile.X == tile.X && listTile.Y == tile.Y); - if (inCollection != null) { if (CurrentTile.X == inCollection.X && CurrentTile.Y == tile.Y) @@ -146,7 +146,7 @@ namespace Bobo.System.Maze.Console.Bot public static class Converter { - public static MazeTile ToMazeTile(this PossibleActionsAndCurrentScore possibleActionsAndCurrentScore) + public static MazeTile ToMazeTile(this PossibleActionsAndCurrentScore possibleActionsAndCurrentScore) { MazeTile tile = new(); tile.PossibleMoveActions = possibleActionsAndCurrentScore.PossibleMoveActions; diff --git a/Bobo.Systems.Maze.Console/Bot/BumpBot.cs b/Bobo.System.Maze.Bot/BumpBot.cs similarity index 91% rename from Bobo.Systems.Maze.Console/Bot/BumpBot.cs rename to Bobo.System.Maze.Bot/BumpBot.cs index baecac5..41c76f3 100644 --- a/Bobo.Systems.Maze.Console/Bot/BumpBot.cs +++ b/Bobo.System.Maze.Bot/BumpBot.cs @@ -1,11 +1,11 @@ using HightechICT.Amazeing.Client.Rest; using LogConsole = System.Console; -namespace Bobo.System.Maze.Console.Bot +namespace Bobo.System.Maze.Bot { - internal class BumpBot + public class BumpBot { - internal static async Task Run(string mazeName, AmazeingClient mazeClient) + public static async Task Run(string mazeName, AmazeingClient mazeClient) { Random random = new Random(); LogConsole.WriteLine($"Entering maze '{mazeName}'"); diff --git a/Bobo.Systems.Maze.Console/Model/MazeTile.cs b/Bobo.System.Maze.Bot/Model/MazeTile.cs similarity index 100% rename from Bobo.Systems.Maze.Console/Model/MazeTile.cs rename to Bobo.System.Maze.Bot/Model/MazeTile.cs diff --git a/Bobo.System.Maze.Bot/SimpleBot.cs b/Bobo.System.Maze.Bot/SimpleBot.cs new file mode 100644 index 0000000..1d1fa24 --- /dev/null +++ b/Bobo.System.Maze.Bot/SimpleBot.cs @@ -0,0 +1,84 @@ +using HightechICT.Amazeing.Client.Rest; +using Microsoft.Extensions.Logging; +using LogConsole = System.Console; + + +namespace Bobo.System.Maze.Bot +{ + public class SimpleBot + { + public static async Task Run(MazeInfo maze, AmazeingClient mazeClient, ILogger logger) + { + if (mazeClient == null) + throw new ArgumentException(nameof(mazeClient)); + + if (string.IsNullOrWhiteSpace(maze.Name)) + throw new ArgumentException(nameof(maze.Name)); + + Random random = new Random(); + logger.Log(LogLevel.Information, "Entering maze '{MazeName}'", maze.Name); + PossibleActionsAndCurrentScore result = await mazeClient.EnterMaze(maze.Name); + Stack directions = new Stack(); + int collected = 0; + + do + { + if (result.PossibleMoveActions.All(m => m.HasBeenVisited)) + { + logger.Log(LogLevel.Debug, "All adjacent tiles have been visited! Going back."); + Direction direction = directions.Pop(); + + switch (direction) + { + case Direction.Left: + direction = Direction.Right; + break; + case Direction.Right: + direction = Direction.Left; + break; + case Direction.Up: + direction = Direction.Down; + break; + case Direction.Down: + direction = Direction.Up; + break; + + } + + logger.Log(LogLevel.Debug, "Moving {Direction}, I have been here.", direction); + + result = await mazeClient.Move(direction); + } + else + { + MoveAction[] notVisited = result.PossibleMoveActions.Where(m => !m.HasBeenVisited).ToArray(); + logger.Log(LogLevel.Debug, $"{string.Join(", ", notVisited.Select(m => m.Direction.ToString()))} have not been visited, selecting next move."); + int selected = random.Next(notVisited.Length - 1); + MoveAction moveAction = notVisited[selected]; + + logger.Log(LogLevel.Debug, $"Moving {moveAction.Direction}, I have {(!moveAction.HasBeenVisited ? "not" : string.Empty)} been here."); + + result = await mazeClient.Move(moveAction.Direction); + + directions.Push(moveAction.Direction); + } + + if (result.CanCollectScoreHere && result.CurrentScoreInHand > 0) + { + collected += result.CurrentScoreInHand; + await mazeClient.CollectScore(); + } + + logger.Log(LogLevel.Debug, $"New tile has {(!result.CanExitMazeHere ? "no" : string.Empty)} exit."); + + } while (!result.CanExitMazeHere); + + await mazeClient.ExitMaze(); + + logger.Log(LogLevel.Information, "Collected {Collected}!", collected); + logger.Log(LogLevel.Information, "Exited maze {MazeName}!", maze.Name); + + return collected; + } + } +} diff --git a/Bobo.Systems.Maze.Console/Bot/SimplerBot.cs b/Bobo.System.Maze.Bot/SimplerBot.cs similarity index 93% rename from Bobo.Systems.Maze.Console/Bot/SimplerBot.cs rename to Bobo.System.Maze.Bot/SimplerBot.cs index 6cc44c5..ea81848 100644 --- a/Bobo.Systems.Maze.Console/Bot/SimplerBot.cs +++ b/Bobo.System.Maze.Bot/SimplerBot.cs @@ -2,11 +2,11 @@ using LogConsole = System.Console; -namespace Bobo.System.Maze.Console.Bot +namespace Bobo.System.Maze.Bot { - internal class SimplerBot + public class SimplerBot { - internal static async Task Run(string mazeName, AmazeingClient mazeClient) + public static async Task Run(string mazeName, AmazeingClient mazeClient) { if (mazeClient == null) throw new ArgumentException(nameof(mazeClient)); @@ -29,7 +29,7 @@ namespace Bobo.System.Maze.Console.Bot //LogConsole.WriteLine("All adjacent tiles have been visited! Going back."); Direction direction = directions.Pop(); - switch(direction) + switch (direction) { case Direction.Left: direction = Direction.Right; diff --git a/Bobo.Systems.Maze.Console/Bobo.System.Maze.Console.csproj b/Bobo.Systems.Maze.Console/Bobo.System.Maze.Console.csproj index 8e89be5..11752b9 100644 --- a/Bobo.Systems.Maze.Console/Bobo.System.Maze.Console.csproj +++ b/Bobo.Systems.Maze.Console/Bobo.System.Maze.Console.csproj @@ -1,4 +1,4 @@ - + Exe @@ -8,7 +8,13 @@ - + + + + + + + diff --git a/Bobo.Systems.Maze.Console/Program.cs b/Bobo.Systems.Maze.Console/Program.cs index 2af2aed..ef36620 100644 --- a/Bobo.Systems.Maze.Console/Program.cs +++ b/Bobo.Systems.Maze.Console/Program.cs @@ -1,6 +1,9 @@ // See https://aka.ms/new-console-template for more information -using Bobo.System.Maze.Console.Bot; +using Bobo.System.Maze.Bot; using HightechICT.Amazeing.Client.Rest; +using Microsoft.Extensions.Logging; +using Serilog; +using Serilog.Core; using System.Net; string apiAuthorization = "HTI Thanks You [OZL]"; @@ -9,7 +12,15 @@ string baseUrl = @"https://maze.hightechict.nl/"; string username = "Hi Hightech!"; string mazeName = "Example Maze"; -Console.WriteLine("Starting bot..."); +Logger serilogger = new LoggerConfiguration() + .WriteTo.Console().CreateLogger(); + +var loggerFactory = new LoggerFactory() + .AddSerilog(serilogger); + +Microsoft.Extensions.Logging.ILogger logger = loggerFactory.CreateLogger("Logger"); + +logger.Log(LogLevel.Information, "Starting bot..."); HttpClient httpClient = new (); httpClient.DefaultRequestHeaders.Add("Authorization", apiAuthorization); @@ -22,18 +33,18 @@ bool registered = await RegisterPlayer(username, true); if (!registered) { - Console.WriteLine($"Player '{username}' failed to register"); + logger.Log(LogLevel.Information, $"Player '{username}' failed to register", username); return; } -Console.WriteLine($"Player '{username}' registered"); +logger.Log(LogLevel.Information, $"Player '{username}' registered", username); + +logger.Log(LogLevel.Information, $"Starting SimplerBot"); -Console.WriteLine($"Starting SimplerBot"); foreach (MazeInfo maze in mazes) { - int collected = await SimplerBot.Run(maze.Name, mazeClient); - Console.WriteLine($"Collected {collected}/{maze.PotentialReward}"); - Console.WriteLine(); + int collected = await SimpleBot.Run(maze, mazeClient, logger); + logger.Log(LogLevel.Information, $"Collected {collected}/{maze.PotentialReward}", collected, maze.PotentialReward); } async Task RegisterPlayer(string username, bool reset = false) diff --git a/Bobo.Systems.Maze.sln b/Bobo.Systems.Maze.sln index 0c223de..98a4501 100644 --- a/Bobo.Systems.Maze.sln +++ b/Bobo.Systems.Maze.sln @@ -5,6 +5,8 @@ VisualStudioVersion = 17.4.33103.184 MinimumVisualStudioVersion = 10.0.40219.1 Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Bobo.System.Maze.Console", "Bobo.Systems.Maze.Console\Bobo.System.Maze.Console.csproj", "{8E397C0A-34CF-4A55-94B5-889CBE8BDD1F}" EndProject +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Bobo.System.Maze.Bot", "Bobo.System.Maze.Bot\Bobo.System.Maze.Bot.csproj", "{754FC8DB-342D-4A5E-8193-E665D6F0DE02}" +EndProject Global GlobalSection(SolutionConfigurationPlatforms) = preSolution Debug|Any CPU = Debug|Any CPU @@ -15,6 +17,10 @@ Global {8E397C0A-34CF-4A55-94B5-889CBE8BDD1F}.Debug|Any CPU.Build.0 = Debug|Any CPU {8E397C0A-34CF-4A55-94B5-889CBE8BDD1F}.Release|Any CPU.ActiveCfg = Release|Any CPU {8E397C0A-34CF-4A55-94B5-889CBE8BDD1F}.Release|Any CPU.Build.0 = Release|Any CPU + {754FC8DB-342D-4A5E-8193-E665D6F0DE02}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {754FC8DB-342D-4A5E-8193-E665D6F0DE02}.Debug|Any CPU.Build.0 = Debug|Any CPU + {754FC8DB-342D-4A5E-8193-E665D6F0DE02}.Release|Any CPU.ActiveCfg = Release|Any CPU + {754FC8DB-342D-4A5E-8193-E665D6F0DE02}.Release|Any CPU.Build.0 = Release|Any CPU EndGlobalSection GlobalSection(SolutionProperties) = preSolution HideSolutionNode = FALSE