Skip to content

Commit

Permalink
feat: 移除不需要的程式碼
Browse files Browse the repository at this point in the history
  • Loading branch information
aa89227 committed Jun 2, 2024
1 parent 30ac443 commit e816b71
Show file tree
Hide file tree
Showing 3 changed files with 2 additions and 93 deletions.
14 changes: 1 addition & 13 deletions Clients/Monopoly.Clients.Web/Pages/Index.razor
Original file line number Diff line number Diff line change
@@ -1,13 +1 @@
@page "/games/{gameid}"
@using Client.Pages.Gaming
@using Client.Pages.Ready
<CascadingValue Value="Connection">
@if (IsGameStarted)
{
<GamingPage />
}
else
{
<ReadyPage UserId="@UserId" RoomId="@GameId" AccessToken="@AccessToken" />
}
</CascadingValue>

66 changes: 1 addition & 65 deletions Clients/Monopoly.Clients.Web/Pages/Index.razor.cs
Original file line number Diff line number Diff line change
@@ -1,65 +1 @@
using Client.Options;
using Microsoft.AspNetCore.Components;
using Microsoft.AspNetCore.SignalR.Client;
using Microsoft.Extensions.Options;
using SharedLibrary.ResponseArgs.ReadyRoom;

namespace Client.Pages;

public partial class Index
{
[Parameter] public string GameId { get; set; } = null!;

[Parameter, SupplyParameterFromQuery(Name = "token")]
public string AccessToken { get; set; } = default!;
[Inject] private IOptions<MonopolyApiOptions> BackendApiOptions { get; set; } = default!;
private string UserId { get; set; } = string.Empty;
private List<string> Messages { get; } = [];
public bool IsGameStarted { get; set; } = false;
private TypedHubConnection Connection { get; set; } = default!;

protected override async Task OnInitializedAsync()
{
await SetupHubConnectionAsync();
}

private async Task SetupHubConnectionAsync()
{
var baseUri = new Uri(BackendApiOptions.Value.BaseUrl);
var url = new Uri(baseUri, $"/monopoly?gameid={GameId}");
var client = new HubConnectionBuilder()
.WithUrl(url, options =>
{
options.AccessTokenProvider = async () => await Task.FromResult(AccessToken);
})
.Build();
Connection = new TypedHubConnection(client);
Connection.WelcomeEventHandler += OnWelcomeEventHandler;
client.Closed += OnConnectionClosed;
try
{
await client.StartAsync();
Messages.Add("連線成功!");
}
catch (Exception ex)
{
Messages.Add(ex.Message);
}
}

private async Task OnConnectionClosed(Exception? exception)
{
var errorMessage = exception?.Message;
Messages.Add($"中斷連線: {errorMessage}");

await Task.CompletedTask;
}

private Task OnWelcomeEventHandler(WelcomeEventArgs e)
{
UserId = e.PlayerId;
Messages.Add($"歡迎 {e.PlayerId} 加入遊戲!");
StateHasChanged();
return Task.CompletedTask;
}
}

15 changes: 0 additions & 15 deletions Clients/Monopoly.Clients.Web/Pages/Ready/ReadyPage.razor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
using Microsoft.AspNetCore.Components;
using Microsoft.AspNetCore.SignalR.Client;
using Microsoft.Extensions.Options;
using SharedLibrary.ResponseArgs.Monopoly;
using SharedLibrary.ResponseArgs.ReadyRoom;
using SharedLibrary.ResponseArgs.ReadyRoom.Models;
using Player = Client.Pages.Ready.Entities.Player;
Expand Down Expand Up @@ -100,20 +99,6 @@ private Task OnPlayerReadyEvent(PlayerReadyEventArgs e)
return Task.CompletedTask;
}

private async Task OnPlayerCannotSelectLocationEvent(PlayerCannotSelectLocationEventArgs e)
{
if (Popup is null)
{
return;
}

await Popup.Show(new Popup.PopupParameter
{
Message = "Cannot select location.",
Delay = 500
});
}

private async Task OnGameStartEvent(GameStartedEventArgs e)
{
if (Popup is null)
Expand Down

0 comments on commit e816b71

Please sign in to comment.