From ededac9ba711a919ff2e32a7bc46901d5fe7dbb9 Mon Sep 17 00:00:00 2001 From: aa89227 Date: Sun, 12 May 2024 23:01:17 +0800 Subject: [PATCH] =?UTF-8?q?feat(ReadyPage):=20=E5=AE=8C=E6=88=90=E9=81=B8?= =?UTF-8?q?=E6=93=87=E8=A7=92=E8=89=B2=E7=9A=84=E5=89=8D=E7=AB=AF=E4=BA=8B?= =?UTF-8?q?=E4=BB=B6=E4=B8=B2=E6=8E=A5?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Application/Common/IRepository.cs | 2 +- .../Ready/Components/RoleChoicePanel.razor.cs | 4 ++-- Monopoly.Web/Pages/Ready/ReadyPage.razor.cs | 23 ++++++++++++++++--- Monopoly.Web/Pages/TypedHubConnection.cs | 1 + Server/Hubs/MonopolyHub.cs | 4 +++- Server/Repositories/InMemoryRepository.cs | 19 ++++++++++----- Test/ServerTests/Utils.cs | 2 +- 7 files changed, 41 insertions(+), 14 deletions(-) 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,