Skip to content

Commit

Permalink
feat(ReadyPage): 完成選擇角色的前端事件串接
Browse files Browse the repository at this point in the history
  • Loading branch information
aa89227 committed May 12, 2024
1 parent b052e5a commit ededac9
Show file tree
Hide file tree
Showing 7 changed files with 41 additions and 14 deletions.
2 changes: 1 addition & 1 deletion Application/Common/IRepository.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
4 changes: 2 additions & 2 deletions Monopoly.Web/Pages/Ready/Components/RoleChoicePanel.razor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}
}
Expand Down
23 changes: 20 additions & 3 deletions Monopoly.Web/Pages/Ready/ReadyPage.razor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -26,20 +30,33 @@ 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<ColorEnum>(x.Color.ToString()),
Role = Enum.Parse<RoleEnum>(x.Role.ToString())
}).ToList();
Update();
}

private void OnPlayerSelectLocationEvent(PlayerSelectLocationEventArgs e)
{
var player = Players.FirstOrDefault(x => x.Id == e.PlayerId);
if (player is null)
{
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<RoleEnum>(e.RoleId);
Update();
}
}
1 change: 1 addition & 0 deletions Monopoly.Web/Pages/TypedHubConnection.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,5 @@ public interface IMonopolyRequests
{
public Task GetReadyInfo();
public Task PlayerSelectLocation(int locationId);
public Task PlayerSelectRole(string roleId);
}
4 changes: 3 additions & 1 deletion Server/Hubs/MonopolyHub.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
19 changes: 13 additions & 6 deletions Server/Repositories/InMemoryRepository.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,32 +5,39 @@ namespace Server.Repositories;

public class InMemoryRepository : ICommandRepository, IQueryRepository
{
private readonly Dictionary<string, Monopoly> Games = new();
private readonly Dictionary<string, Monopoly> 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;
}
}
2 changes: 1 addition & 1 deletion Test/ServerTests/Utils.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down

0 comments on commit ededac9

Please sign in to comment.