moved bots to own library, added microsoft logging and updated main program
This commit is contained in:
parent
5d7aa8a6d9
commit
167b94e4de
14
Bobo.System.Maze.Bot/Bobo.System.Maze.Bot.csproj
Normal file
14
Bobo.System.Maze.Bot/Bobo.System.Maze.Bot.csproj
Normal file
@ -0,0 +1,14 @@
|
||||
<Project Sdk="Microsoft.NET.Sdk">
|
||||
|
||||
<PropertyGroup>
|
||||
<TargetFramework>net6.0</TargetFramework>
|
||||
<ImplicitUsings>enable</ImplicitUsings>
|
||||
<Nullable>enable</Nullable>
|
||||
</PropertyGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<PackageReference Include="HightechICT.Amazeing.Client.Rest" Version="1.0.2" />
|
||||
<PackageReference Include="Microsoft.Extensions.Logging" Version="7.0.0" />
|
||||
</ItemGroup>
|
||||
|
||||
</Project>
|
||||
@ -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<MazeTile> 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;
|
||||
@ -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}'");
|
||||
84
Bobo.System.Maze.Bot/SimpleBot.cs
Normal file
84
Bobo.System.Maze.Bot/SimpleBot.cs
Normal file
@ -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<int> 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<Direction> directions = new Stack<Direction>();
|
||||
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;
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -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<int> Run(string mazeName, AmazeingClient mazeClient)
|
||||
public static async Task<int> 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;
|
||||
@ -1,4 +1,4 @@
|
||||
<Project Sdk="Microsoft.NET.Sdk">
|
||||
<Project Sdk="Microsoft.NET.Sdk">
|
||||
|
||||
<PropertyGroup>
|
||||
<OutputType>Exe</OutputType>
|
||||
@ -8,7 +8,13 @@
|
||||
</PropertyGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<PackageReference Include="HightechICT.Amazeing.Client.Rest" Version="1.0.2" />
|
||||
<PackageReference Include="Serilog" Version="3.0.1" />
|
||||
<PackageReference Include="Serilog.Extensions.Logging" Version="7.0.0" />
|
||||
<PackageReference Include="Serilog.Sinks.Console" Version="4.1.0" />
|
||||
</ItemGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<ProjectReference Include="..\Bobo.System.Maze.Bot\Bobo.System.Maze.Bot.csproj" />
|
||||
</ItemGroup>
|
||||
|
||||
</Project>
|
||||
|
||||
@ -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<bool> RegisterPlayer(string username, bool reset = false)
|
||||
|
||||
@ -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
|
||||
|
||||
Loading…
Reference in New Issue
Block a user