Skip to content

Commit

Permalink
bot updates to show statuses
Browse files Browse the repository at this point in the history
  • Loading branch information
christiannagel committed Mar 27, 2024
1 parent 3e1b434 commit 56822e3
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 11 deletions.
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
namespace CodeBreaker.Bot.Api;

public record StatusResponse(
public record StatusInfo(
int GameNumber,
string Message
);
8 changes: 8 additions & 0 deletions src/CodeBreaker.Bot/CodeBreakerGameRunner.cs
Original file line number Diff line number Diff line change
Expand Up @@ -52,19 +52,27 @@ static List<int> AddColorsToList(List<int> list1, List<int> list2)
return list4;
}

/// <summary>
/// Starts a new game asynchronously.
/// </summary>
/// <param name="cancellationToken">The cancellation token.</param>
/// <returns>A task representing the asynchronous operation.</returns>
public async Task StartGameAsync(CancellationToken cancellationToken = default)
{
// Generate a unique key for each color
static int NextKey(ref int key)
{
int next = key;
key <<= 1;
return next;
}

// Initialize the possible values for the game
_possibleValues = InitializePossibleValues();
_moves.Clear();
_moveNumber = 0;

// Start a new game and retrieve the game ID and color names
(_gameId, _, _, IDictionary<string, string[]> fieldValues) = await _gamesClient.StartGameAsync(GameType.Game6x4, "Bot", cancellationToken);
int key = 1;
_colorNames = fieldValues["colors"]
Expand Down
13 changes: 9 additions & 4 deletions src/CodeBreaker.Bot/CodeBreakerTimer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -65,9 +65,9 @@ public void Stop()
_timer?.Dispose();
}

public StatusResponse Status()
public StatusInfo GetStatus()
{
return new StatusResponse(_loop + 1, _statusMessage);
return new StatusInfo(_loop + 1, _statusMessage);
}

public static void Stop(Guid id)
Expand All @@ -84,14 +84,19 @@ public static void Stop(Guid id)
throw new BotNotFoundException();
}

public static StatusResponse Status(Guid id)
public static StatusInfo GetStatus(Guid id)
{
if (id == default)
throw new ArgumentException("Invalid argument value {id}", nameof(id));

if (_bots.TryGetValue(id, out CodeBreakerTimer? timer))
return timer?.Status() ?? throw new UnknownStatusException("id found, but unknown status");
return timer?.GetStatus() ?? throw new UnknownStatusException("id found, but unknown status");

throw new BotNotFoundException();
}

public static IEnumerable<StatusInfo> GetAllStatuses()
{
return _bots.Select(b => b.Value.GetStatus()).ToArray();
}
}
22 changes: 16 additions & 6 deletions src/CodeBreaker.Bot/Endpoints/BotEndpoints.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ public static void MapBotEndpoints(this IEndpointRouteBuilder routes)
var group = routes.MapGroup("/bot")
.WithTags("Bot API");

group.MapPost("/bots", Results<BadRequest, Accepted<Guid>> (
group.MapPost("/session", Results<BadRequest, Accepted<Guid>> (
CodeBreakerTimer timer,
int count = 3,
int delay = 10,
Expand Down Expand Up @@ -38,13 +38,23 @@ public static void MapBotEndpoints(this IEndpointRouteBuilder routes)
return x;
});

group.MapGet("/bots/{id}", Results<Ok<StatusResponse>, BadRequest<string>, NotFound>(Guid id) =>
group.MapGet("/session", () =>
{
StatusResponse result;
IEnumerable<StatusInfo> results = CodeBreakerTimer.GetAllStatuses();

return TypedResults.Ok(results);
})
.WithName("GetSessions")
.WithSummary("Gets the statuses of all sessions")
.WithOpenApi();

group.MapGet("/session/{id}", Results<Ok<StatusInfo>, BadRequest<string>, NotFound> (Guid id) =>
{
StatusInfo result;

try
{
result = CodeBreakerTimer.Status(id);
result = CodeBreakerTimer.GetStatus(id);
}
catch (ArgumentException)
{
Expand All @@ -57,15 +67,15 @@ public static void MapBotEndpoints(this IEndpointRouteBuilder routes)

return TypedResults.Ok(result);
})
.WithName("GetBot")
.WithName("GetSession")
.WithSummary("Gets the status of a bot")
.WithOpenApi(x =>
{
x.Parameters[0].Description = "The id of the bot";
return x;
});

group.MapDelete("/bots/{id}", Results<NoContent, NotFound, BadRequest<string>> (Guid id) =>
group.MapDelete("/session/{id}", Results<NoContent, NotFound, BadRequest<string>> (Guid id) =>
{
try
{
Expand Down

0 comments on commit 56822e3

Please sign in to comment.