Skip to content

Commit

Permalink
refactor: 將骰子顯示的邏輯搬進 DiceBox
Browse files Browse the repository at this point in the history
  • Loading branch information
aa89227 committed Oct 13, 2024
1 parent f624ba1 commit fcfc312
Show file tree
Hide file tree
Showing 4 changed files with 16 additions and 26 deletions.
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<dlv class="DiceBox">
@foreach (var Dice in GetShowDices())
@foreach (var dice in Dices)
{
<img class="Dice" src="@($"images/gamepage/Dice{Dice}.svg")" alt="@($"Dice {Dice}")" />
<img class="Dice" src="@($"images/gamepage/Dice{dice}.svg")" alt="@($"Dice {dice}")" />
}
</dlv>
Original file line number Diff line number Diff line change
@@ -1,15 +1,16 @@
using Microsoft.AspNetCore.Components;

namespace Client.Pages.Gaming.Components;
namespace Client.Pages.Gaming.Components;

public partial class DiceBox
{
[CascadingParameter] public GamingPage Parent { get; set; } = default!;

public List<int> ShowDice => Parent.ShowDice;
private IEnumerable<int> Dices { get; set; } = [];

public List<int> GetShowDices()
public async Task ShowDices(IEnumerable<int> dices)
{
return ShowDice.TrueForAll(x => x > 0) ? ShowDice : [];
Console.WriteLine("ShowDices");
Dices = dices;
StateHasChanged();
await Task.Delay(3000);
Dices = [];
StateHasChanged();
}
}
}
2 changes: 1 addition & 1 deletion Clients/Monopoly.Clients.Web/Pages/Gaming/GamingPage.razor
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,6 @@
<PlayerInfo></PlayerInfo>
<Go OnRolledDice="OnRolledDice"></Go>

<DiceBox></DiceBox>
<DiceBox @ref="DiceBox" />
</CascadingValue>
</div>
17 changes: 3 additions & 14 deletions Clients/Monopoly.Clients.Web/Pages/Gaming/GamingPage.razor.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using Client.Options;
using Client.Pages.Enums;
using Client.Pages.Gaming.Components;
using Client.Pages.Gaming.Entities;
using Microsoft.AspNetCore.Components;
using Microsoft.AspNetCore.SignalR.Client;
Expand Down Expand Up @@ -40,7 +41,7 @@ public partial class GamingPage
private GamingHubConnection Connection { get; set; } = default!;
public IEnumerable<Player> Players { get; set; } = [];

public List<int> ShowDice { get; set; } = [];
private DiceBox DiceBox { get; set; } = default!;

protected override void OnInitialized()
{
Expand Down Expand Up @@ -106,19 +107,7 @@ protected override async Task OnInitializedAsync()

private async Task OnRolledDiceEvent(PlayerRolledDiceEventArgs e)
{
// 顯示骰子點數
ShowDice = [.. e.DicePoints];
StateHasChanged();

await Task.Delay(3000);

// TODO 角色移動

// 關閉骰子點數
ShowDice = [];
StateHasChanged();

await Task.CompletedTask;
await DiceBox.ShowDices(e.DicePoints);
}

private async Task OnRolledDice()
Expand Down

0 comments on commit fcfc312

Please sign in to comment.