generated from Game-as-a-Service/Gaas-repo-template
-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
2 additions
and
93 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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> | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters