Skip to content

Commit

Permalink
Implement global status message
Browse files Browse the repository at this point in the history
  • Loading branch information
vicr123 committed May 11, 2024
1 parent 8d03085 commit 228560a
Show file tree
Hide file tree
Showing 4 changed files with 44 additions and 1 deletion.
20 changes: 20 additions & 0 deletions DiscordMonies/Game/Embeds.cs
Original file line number Diff line number Diff line change
Expand Up @@ -327,6 +327,26 @@ public static Embed Status(GameState gameState)
.Build().Get();
}

public static Embed GameStatus(GameState gameState)
{
var builder = new EmbedBuilder()
.WithTitle("Game Status");

builder = gameState.Players.Aggregate(builder, (current, player) =>
current.AddField(player.DisplayName, $"""
Balance: {player.Money.MoneyString()}
Colour: {player.Color.Name}
Position: {(player.JailTurns == -1 ? player.CurrentSpace.NameWithPositionString : "In Jail")}
{(gameState.CurrentPlayer == player ? "**It's your turn!**" : "")}
""")
.Get());

builder = builder.AddField("Available Houses", $"{gameState.Board.HousesRemaining}", true).Get()
.AddField("Available Hotels", $"{gameState.Board.HotelsRemaining}", true).Get();

return builder.Build().Get();
}

public static Embed Space(Space space)
{
return new EmbedBuilder()
Expand Down
19 changes: 18 additions & 1 deletion DiscordMonies/Game/GameControllerService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,14 @@

namespace DiscordMonies.Game;

public class GameControllerService(GameState state, IFeedbackService service, IInteractionContext context, BoardRenderer renderer, GameService gameService, IServiceProvider serviceProvider)
public class GameControllerService(
GameState state,
IFeedbackService service,
IInteractionContext context,
BoardRenderer renderer,
GameService gameService,
IServiceProvider serviceProvider,
IDiscordRestChannelAPI discordRestChannelApi)
{
/// <summary>
/// Advances the turn to the next player and performs necessary actions.
Expand Down Expand Up @@ -59,6 +66,8 @@ private async Task ProcessCurrentlyUnresolvedMatter()
var type = resolver.GetType();
var resolveMethod = type.GetMethod("Resolve")!;
await (Task) resolveMethod.Invoke(resolver, [state.CurrentlyResolvingMatter])!;

await UpdateStatusMessage();
}

public async Task RollDie()
Expand Down Expand Up @@ -188,6 +197,8 @@ public async Task<bool> TryPay(GamePlayer? from, GamePlayer? to, int value, bool
await ProcessCurrentlyUnresolvedMatter();
}

await UpdateStatusMessage();

return true;
}

Expand Down Expand Up @@ -1377,6 +1388,12 @@ private async Task RecalculateAutoSell()
});
}
}

public async Task UpdateStatusMessage()
{
await discordRestChannelApi.EditMessageAsync(state.ThreadId, state.StatusMessage!.Value,
embeds: new([Embeds.GameStatus(state)]));
}
}

public abstract record RecoupFundsOperation;
Expand Down
2 changes: 2 additions & 0 deletions DiscordMonies/Game/GameState.cs
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,8 @@ public GamePlayer GetById(Snowflake userId)
public IList<TradeTable> TradeTables { get; } = [];

public Snowflake? LastRollMessage { get; set; }

public Snowflake? StatusMessage { get; set; }
}

public enum GameAction
Expand Down
4 changes: 4 additions & 0 deletions DiscordMonies/Lobby/LobbyCommands.cs
Original file line number Diff line number Diff line change
Expand Up @@ -160,6 +160,10 @@ public async Task<IResult> CloseLobby(string state)

var gameState = await BuildNewGameState(lobby.Players, thread, lobby);
gameService.OpenGame(gameState);

var gameStateMessage = await feedbackService.SendEmbedAsync(gameState.ThreadId, Embeds.GameStatus(gameState));
await channelApi.PinMessageAsync(gameState.ThreadId, gameStateMessage.Entity.ID);
gameState.StatusMessage = gameStateMessage.Entity.ID;

// Kick off the game
await using var scope = serviceProvider.CreateAsyncScope();
Expand Down

0 comments on commit 228560a

Please sign in to comment.