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.
之後應該會對頁面的url規劃過一遍,還要想辦法把Dev Page藏起來
- Loading branch information
Showing
11 changed files
with
225 additions
and
119 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 |
---|---|---|
|
@@ -3,4 +3,8 @@ | |
public class BackendApiOptions | ||
{ | ||
public string BaseUrl { get; set; } = default!; | ||
public BackendApiOptions() | ||
{ | ||
|
||
} | ||
} |
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
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,20 +1,10 @@ | ||
@page "/" | ||
@using Client.Options; | ||
@using Microsoft.AspNetCore.SignalR.Client; | ||
@using System.Security.Claims; | ||
@using System.Net.Http.Headers; | ||
@using Microsoft.Extensions.Options; | ||
<MudStack> | ||
@foreach (var item in Users) | ||
{ | ||
<MudPaper Class="pa-3" Outlined Elevation="5">@item.Id</MudPaper> | ||
} | ||
</MudStack> | ||
<MudTextField @bind-Value="Token" Label="Jwt Token" Variant="Variant.Outlined" | ||
Adornment="Adornment.End" | ||
AdornmentIcon="@Icons.Material.Filled.Add" | ||
AdornmentColor="Color.Info" | ||
OnAdornmentClick="AddUser" /> | ||
<MudButton OnClick="CreateGame" Variant="Variant.Filled" Color="Color.Primary">Create Game</MudButton> | ||
<MudTextField @bind-Value="GameId" Label="Game Id" Variant="Variant.Outlined" | ||
Adornment="Adornment.End" | ||
|
@@ -25,101 +15,4 @@ | |
{ | ||
<iframe src="/@[email protected]"></iframe> | ||
} | ||
</MudStack> | ||
|
||
@code { | ||
private string Token { get; set; } = ""; | ||
private List<string> Messages { get; set; } = new(); | ||
private List<User> Users { get; set; } = new(); | ||
private string UserId { get; set; } = string.Empty; | ||
private string GameId { get; set; } = string.Empty; | ||
private List<GameData> games = new(); | ||
[Inject] private ISnackbar Snackbar { get; set; } = default!; | ||
[Inject] private IOptions<BackendApiOptions> BackendApiOptions { get; set; } = default!; | ||
|
||
private async void CreateGame() | ||
{ | ||
CreateGameBodyPayload bodyPayload = new(Users.Select(user => new Player(user.Id)).ToArray()); | ||
var url = new Uri(new Uri(BackendApiOptions.Value.BaseUrl), "/create-game"); | ||
var httpClient = new HttpClient(); | ||
var host = Users.FirstOrDefault(); | ||
if (host is null) | ||
{ | ||
Snackbar.Add("請先加入使用者", Severity.Error); | ||
return; | ||
} | ||
httpClient.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", host.Token); | ||
var response = await httpClient.PostAsJsonAsync(url, bodyPayload); | ||
if (response.IsSuccessStatusCode) | ||
{ | ||
var content = await response.Content.ReadAsStringAsync(); | ||
Snackbar.Add($"遊戲建立成功! Url: {content}", Severity.Success); | ||
} | ||
else | ||
{ | ||
Snackbar.Add($"遊戲建立失敗! {response.StatusCode}", Severity.Error); | ||
} | ||
} | ||
|
||
private async void AddUser() | ||
{ | ||
var url = new Uri(new Uri(BackendApiOptions.Value.BaseUrl), "/whoami"); | ||
HubConnection hubConnection = new HubConnectionBuilder() | ||
.WithAutomaticReconnect() | ||
.WithUrl(url, opt => | ||
{ | ||
opt.AccessTokenProvider = () => Task.FromResult<string?>(Token); | ||
}) | ||
.Build(); | ||
hubConnection.Closed += async (exception) => | ||
{ | ||
if (exception == null) | ||
{ | ||
Snackbar.Add("中斷連線", Severity.Error); | ||
} | ||
else | ||
{ | ||
Snackbar.Add($"中斷連線: {exception.Message}", Severity.Error); | ||
} | ||
await Task.CompletedTask; | ||
}; | ||
var tcs = new TaskCompletionSource<string>(TaskCreationOptions.RunContinuationsAsynchronously); | ||
hubConnection.On<List<string>>("WhoAmI", data => | ||
{ | ||
var userId = data.Single(d => d.StartsWith(ClaimTypes.Sid)).Substring(ClaimTypes.Sid.Length + 1); | ||
tcs.SetResult(userId); | ||
}); | ||
|
||
try | ||
{ | ||
await hubConnection.StartAsync(); | ||
Snackbar.Add("連線成功!", Severity.Success); | ||
await hubConnection.SendAsync("WhoAmI"); | ||
var tcst = await tcs.Task; | ||
Users.Add(new(tcst, Token)); | ||
await hubConnection.StopAsync(); | ||
Token = string.Empty; | ||
StateHasChanged(); | ||
} | ||
catch (Exception ex) | ||
{ | ||
Snackbar.Add(ex.Message, Severity.Error); | ||
} | ||
} | ||
|
||
private void JoinGame() | ||
{ | ||
games.Clear(); | ||
Users.ForEach(user => | ||
{ | ||
games.Add(new(GameId, user.Token)); | ||
}); | ||
StateHasChanged(); | ||
} | ||
|
||
private record CreateGameBodyPayload(Player[] Players); | ||
private record Player(string Id); | ||
|
||
record GameData(string Id, string Token); | ||
record User(string Id, string Token); | ||
} | ||
</MudStack> |
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 |
---|---|---|
@@ -0,0 +1,121 @@ | ||
using Client.Options; | ||
using Microsoft.AspNetCore.Components; | ||
using Microsoft.AspNetCore.SignalR.Client; | ||
using Microsoft.Extensions.Options; | ||
using MudBlazor; | ||
using System.Net.Http.Headers; | ||
using System.Net.Http.Json; | ||
using System.Security.Claims; | ||
|
||
namespace Client.Pages; | ||
|
||
public partial class Index | ||
{ | ||
private List<User> Users { get; set; } = new(); | ||
private string GameId { get; set; } = string.Empty; | ||
private readonly List<GameData> games = new(); | ||
[Inject] private ISnackbar Snackbar { get; set; } = default!; | ||
[Inject] private IOptions<BackendApiOptions> BackendApiOptions { get; set; } = default!; | ||
private Uri BackendApiBaseUri => new(BackendApiOptions.Value.BaseUrl); | ||
|
||
protected override async Task OnInitializedAsync() | ||
{ | ||
var tokens = await new HttpClient().GetFromJsonAsync<string[]>(new Uri(BackendApiBaseUri, "/tokens")); | ||
if (tokens is null) | ||
{ | ||
Snackbar.Add("取得 Token 失敗", Severity.Error); | ||
return; | ||
} | ||
|
||
foreach(var token in tokens) | ||
{ | ||
AddUser(token); | ||
} | ||
await base.OnInitializedAsync(); | ||
} | ||
|
||
private async void CreateGame() | ||
{ | ||
CreateGameBodyPayload bodyPayload = new(Users.Select(user => new Player(user.Id)).ToArray()); | ||
var url = new Uri(BackendApiBaseUri, "/create-game"); | ||
var httpClient = new HttpClient(); | ||
var host = Users.FirstOrDefault(); | ||
if (host is null) | ||
{ | ||
Snackbar.Add("請先加入使用者", Severity.Error); | ||
return; | ||
} | ||
httpClient.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", host.Token); | ||
var response = await httpClient.PostAsJsonAsync(url, bodyPayload); | ||
if (response.IsSuccessStatusCode) | ||
{ | ||
var content = await response.Content.ReadAsStringAsync(); | ||
Snackbar.Add($"遊戲建立成功! Url: {content}", Severity.Success); | ||
} | ||
else | ||
{ | ||
Snackbar.Add($"遊戲建立失敗! {response.StatusCode}", Severity.Error); | ||
} | ||
} | ||
|
||
private async void AddUser(string token) | ||
{ | ||
var url = new Uri(BackendApiBaseUri, "/whoami"); | ||
HubConnection hubConnection = new HubConnectionBuilder() | ||
.WithAutomaticReconnect() | ||
.WithUrl(url, opt => | ||
{ | ||
opt.AccessTokenProvider = () => Task.FromResult<string?>(token); | ||
}) | ||
.Build(); | ||
hubConnection.Closed += async (exception) => | ||
{ | ||
if (exception == null) | ||
{ | ||
Snackbar.Add("中斷連線", Severity.Error); | ||
} | ||
else | ||
{ | ||
Snackbar.Add($"中斷連線: {exception.Message}", Severity.Error); | ||
} | ||
await Task.CompletedTask; | ||
}; | ||
var tcs = new TaskCompletionSource<string>(TaskCreationOptions.RunContinuationsAsynchronously); | ||
hubConnection.On<List<string>>("WhoAmI", data => | ||
{ | ||
var userId = data.Single(d => d.StartsWith(ClaimTypes.Sid))[(ClaimTypes.Sid.Length + 1)..]; | ||
tcs.SetResult(userId); | ||
}); | ||
|
||
try | ||
{ | ||
await hubConnection.StartAsync(); | ||
Snackbar.Add("連線成功!", Severity.Success); | ||
await hubConnection.SendAsync("WhoAmI"); | ||
var tcst = await tcs.Task; | ||
Users.Add(new(tcst, token)); | ||
await hubConnection.StopAsync(); | ||
StateHasChanged(); | ||
} | ||
catch (Exception ex) | ||
{ | ||
Snackbar.Add(ex.Message, Severity.Error); | ||
} | ||
} | ||
|
||
private void JoinGame() | ||
{ | ||
games.Clear(); | ||
Users.ForEach(user => | ||
{ | ||
games.Add(new(GameId, user.Token)); | ||
}); | ||
StateHasChanged(); | ||
} | ||
|
||
private record CreateGameBodyPayload(Player[] Players); | ||
private record Player(string Id); | ||
|
||
record GameData(string Id, string Token); | ||
record User(string Id, string Token); | ||
} |
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
File renamed without changes.
File renamed without changes.
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
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
Oops, something went wrong.