diff --git a/Application/Common/IRepository.cs b/Application/Common/IRepository.cs index 091565e..a476330 100644 --- a/Application/Common/IRepository.cs +++ b/Application/Common/IRepository.cs @@ -68,7 +68,7 @@ private static Monopoly ToApplication(this Domain.Monopoly domainMonopoly) }; if (gamestage == GameStage.Preparing) { - return new Monopoly(domainMonopoly.Id, players, map, domainMonopoly.HostId, null!, null!, gamestage); + return new Monopoly(domainMonopoly.Id, [..players], map, domainMonopoly.HostId, null!, null!, gamestage); } var currentPlayer = domainMonopoly.Players.First(player => player.Id == domainMonopoly.CurrentPlayerState.PlayerId); var auction = domainMonopoly.CurrentPlayerState.Auction; diff --git a/Monopoly.Web/Pages/Ready/Components/RoleChoicePanel.razor.cs b/Monopoly.Web/Pages/Ready/Components/RoleChoicePanel.razor.cs index 2c25969..090b79e 100644 --- a/Monopoly.Web/Pages/Ready/Components/RoleChoicePanel.razor.cs +++ b/Monopoly.Web/Pages/Ready/Components/RoleChoicePanel.razor.cs @@ -7,13 +7,13 @@ public partial class RoleChoicePanel { [CascadingParameter] public ReadyPage Parent { get; set; } = default!; private Player? CurrentPlayer => Parent.CurrentPlayer; - private void ChangeRole(RoleEnum role) + private async Task ChangeRole(RoleEnum role) { if (CurrentPlayer is null) { return; } - CurrentPlayer.Role = role; + await Parent.Connection.PlayerSelectRole(role.ToString()); Parent.Update(); } } diff --git a/Monopoly.Web/Pages/Ready/ReadyPage.razor.cs b/Monopoly.Web/Pages/Ready/ReadyPage.razor.cs index 8aa5748..02abba1 100644 --- a/Monopoly.Web/Pages/Ready/ReadyPage.razor.cs +++ b/Monopoly.Web/Pages/Ready/ReadyPage.razor.cs @@ -11,13 +11,17 @@ public partial class ReadyPage [Parameter] public string UserId { get; set; } = string.Empty; [CascadingParameter] internal TypedHubConnection Connection { get; set; } = default!; public Player? CurrentPlayer => Players.FirstOrDefault(x => x.Id == UserId); + protected override async Task OnInitializedAsync() { Connection.GetReadyInfoEventHandler += OnGetReadyInfoEvent; Connection.PlayerSelectLocationEventHandler += OnPlayerSelectLocationEvent; + Connection.PlayerSelectRoleEventHandler += OnPlayerSelectRoleEvent; await Connection.GetReadyInfo(); } + public void Update() => StateHasChanged(); + private void OnGetReadyInfoEvent(GetReadyInfoEventArgs e) { Players = e.Players.Select(x => new Player @@ -26,12 +30,12 @@ private void OnGetReadyInfoEvent(GetReadyInfoEventArgs e) Name = x.Name, IsReady = x.IsReady, IsHost = e.HostId == x.Id, - Color = (ColorEnum)Enum.Parse(typeof(ColorEnum), x.Color.ToString()), - Role = (RoleEnum)Enum.Parse(typeof(RoleEnum), x.Color.ToString()) + Color = Enum.Parse(x.Color.ToString()), + Role = Enum.Parse(x.Role.ToString()) }).ToList(); Update(); } - + private void OnPlayerSelectLocationEvent(PlayerSelectLocationEventArgs e) { var player = Players.FirstOrDefault(x => x.Id == e.PlayerId); @@ -39,7 +43,20 @@ private void OnPlayerSelectLocationEvent(PlayerSelectLocationEventArgs e) { return; } + player.Color = (ColorEnum)e.LocationId; Update(); } + + private void OnPlayerSelectRoleEvent(PlayerSelectRoleEventArgs e) + { + var player = Players.FirstOrDefault(x => x.Id == e.PlayerId); + if (player is null) + { + return; + } + + player.Role = Enum.Parse(e.RoleId); + Update(); + } } \ No newline at end of file diff --git a/Monopoly.Web/Pages/TypedHubConnection.cs b/Monopoly.Web/Pages/TypedHubConnection.cs index b588e7d..1340429 100644 --- a/Monopoly.Web/Pages/TypedHubConnection.cs +++ b/Monopoly.Web/Pages/TypedHubConnection.cs @@ -10,4 +10,5 @@ public interface IMonopolyRequests { public Task GetReadyInfo(); public Task PlayerSelectLocation(int locationId); + public Task PlayerSelectRole(string roleId); } diff --git a/Server/Hubs/MonopolyHub.cs b/Server/Hubs/MonopolyHub.cs index ae04bae..4c81b65 100644 --- a/Server/Hubs/MonopolyHub.cs +++ b/Server/Hubs/MonopolyHub.cs @@ -152,13 +152,15 @@ await Clients.Caller.GetReadyInfoEvent(new GetReadyInfoEventArgs { roleEnum = GetReadyInfoEventArgs.RoleEnum.None; } + + var colorEnum = (GetReadyInfoEventArgs.ColorEnum?)x.LocationId ?? GetReadyInfoEventArgs.ColorEnum.None; return new GetReadyInfoEventArgs.Player { Id = x.PlayerId, Name = x.PlayerId, IsReady = x.IsReady, Role = roleEnum, - Color = (GetReadyInfoEventArgs.ColorEnum?)x.LocationId ?? GetReadyInfoEventArgs.ColorEnum.None + Color = colorEnum }; }).ToList(), HostId = presenter.Value.Info.HostId, diff --git a/Server/Repositories/InMemoryRepository.cs b/Server/Repositories/InMemoryRepository.cs index 9918467..76ff5aa 100644 --- a/Server/Repositories/InMemoryRepository.cs +++ b/Server/Repositories/InMemoryRepository.cs @@ -5,32 +5,39 @@ namespace Server.Repositories; public class InMemoryRepository : ICommandRepository, IQueryRepository { - private readonly Dictionary Games = new(); + private readonly Dictionary games = new(); public Monopoly FindGameById(string id) { - Games.TryGetValue(id, out Monopoly? game); + games.TryGetValue(id, out var game); if (game == null) { throw new GameNotFoundException(id); } + return game; } public string[] GetRooms() { - return Games.Keys.ToArray(); + return games.Keys.ToArray(); } public bool IsExist(string id) { - return Games.ContainsKey(id); + return games.ContainsKey(id); } public string Save(Monopoly monopoly) { - var game = monopoly with { Id = (Games.Count + 1).ToString() }; - Games[game.Id] = game; + var id = GetGameId(monopoly.Id); + var game = monopoly with { Id = id }; + games[game.Id] = game; return game.Id; } + + private string GetGameId(string gameId) + { + return string.IsNullOrWhiteSpace(gameId) ? (games.Count + 1).ToString() : gameId; + } } \ No newline at end of file diff --git a/Test/ServerTests/Utils.cs b/Test/ServerTests/Utils.cs index 77f705b..c6fb6c6 100644 --- a/Test/ServerTests/Utils.cs +++ b/Test/ServerTests/Utils.cs @@ -86,7 +86,7 @@ public MonopolyBuilder WithLandHouse(string id, int house) private Application.DataModels.Monopoly Build() { return new Application.DataModels.Monopoly(Id: GameId, - Players: Players.ToArray(), + Players: [..Players], Map: Map, HostId: HostId, GameStage: GameStage,