Skip to content

Commit

Permalink
client: dev page增加能夠讀取房間的功能
Browse files Browse the repository at this point in the history
  • Loading branch information
aa89227 committed Aug 15, 2023
1 parent 1e3b069 commit 4324289
Show file tree
Hide file tree
Showing 5 changed files with 67 additions and 27 deletions.
1 change: 1 addition & 0 deletions Application/Common/IRepository.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ namespace Application.Common;
public interface IRepository
{
public Monopoly FindGameById(string id);
public string[] GetRooms();
public bool IsExist(string id);
public string Save(Monopoly game);
}
41 changes: 31 additions & 10 deletions Client/Pages/Index.razor
Original file line number Diff line number Diff line change
@@ -1,18 +1,39 @@
@page "/"
<MudStack>
@foreach (var item in Users)
<MudCard>
<MudCardHeader>
<CardHeaderContent>
<MudText Typo="Typo.h5">玩家列表</MudText>
</CardHeaderContent>
</MudCardHeader>
<MudCardContent>
@foreach (var item in users)
{
<MudPaper Class="pa-3" Outlined Elevation="5">@item.Id</MudPaper>
}
</MudStack>
</MudCardContent>
</MudCard>
<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"
AdornmentIcon="@Icons.Material.Filled.PlayArrow"
OnAdornmentClick="JoinGame" />
<MudStack Row Style="height:100vh">
<MudCard>
<MudCardHeader>
<CardHeaderContent>
<MudText Typo="Typo.h5">房間列表</MudText>
</CardHeaderContent>
</MudCardHeader>
<MudCardContent>
@foreach (var id in rooms!)
{
<MudButton Class="pa-3" Color="Color.Secondary" Variant="Variant.Outlined" OnClick="() => JoinGame(id)">@id</MudButton>
}
</MudCardContent>
</MudCard>
<MudDivider />
<MudText Typo="Typo.h5">遊戲視窗</MudText>

<MudGrid Justify="Justify.SpaceEvenly" Style="height:100vh;width:100%;" Spacing="1">
@foreach (var game in games)
{
<iframe src="/@[email protected]"></iframe>
<MudItem xs="6" Class="pa-0 border-2">
<iframe src="/@[email protected]" Style="width: 100%; height: 100%;"></iframe>
</MudItem>
}
</MudStack>
</MudGrid>
27 changes: 18 additions & 9 deletions Client/Pages/Index.razor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,11 @@ namespace Client.Pages;

public partial class Index
{
private List<User> Users { get; set; } = new();
private string GameId { get; set; } = string.Empty;
private readonly List<User> users = new();
private readonly List<GameData> games = new();
private List<string>? rooms = new();
[Inject] private ISnackbar Snackbar { get; set; } = default!;
[Inject] private NavigationManager NavigationManager { get; set; } = default!;
[Inject] private IOptions<BackendApiOptions> BackendApiOptions { get; set; } = default!;
private Uri BackendApiBaseUri => new(BackendApiOptions.Value.BaseUrl);

Expand All @@ -36,10 +37,10 @@ protected override async Task OnInitializedAsync()

private async void CreateGame()
{
CreateGameBodyPayload bodyPayload = new(Users.Select(user => new Player(user.Id)).ToArray());
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();
var host = users.FirstOrDefault();
if (host is null)
{
Snackbar.Add("請先加入使用者", Severity.Error);
Expand All @@ -50,7 +51,8 @@ private async void CreateGame()
if (response.IsSuccessStatusCode)
{
var content = await response.Content.ReadAsStringAsync();
Snackbar.Add($"遊戲建立成功! Url: {content}", Severity.Success);
Snackbar.Add($"遊戲建立成功! Url: {content}", Severity.Normal);
await RefleshRoomListAsync();
}
else
{
Expand Down Expand Up @@ -93,7 +95,7 @@ private async void AddUser(string token)
Snackbar.Add("連線成功!", Severity.Success);
await hubConnection.SendAsync("WhoAmI");
var tcst = await tcs.Task;
Users.Add(new(tcst, token));
users.Add(new(tcst, token));
await hubConnection.StopAsync();
StateHasChanged();
}
Expand All @@ -103,13 +105,20 @@ private async void AddUser(string token)
}
}

private void JoinGame()
private async Task RefleshRoomListAsync()
{
rooms = await new HttpClient().GetFromJsonAsync<List<string>>(new Uri(BackendApiBaseUri, "/rooms"));
StateHasChanged();
}

private void JoinGame(string id)
{
games.Clear();
Users.ForEach(user =>
users.ForEach(user =>
{
games.Add(new(GameId, user.Token));
games.Add(new(id, user.Token));
});
NavigationManager.NavigateTo(NavigationManager.Uri + "#game-view");
StateHasChanged();
}

Expand Down
20 changes: 12 additions & 8 deletions Server/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -84,17 +84,21 @@
string projectDirectory = AppDomain.CurrentDomain.BaseDirectory;
string jsonFilePath = Path.Combine(projectDirectory, "Maps", $"{mapId}.json");
if (File.Exists(jsonFilePath))
{
// read json file
string json = File.ReadAllText(jsonFilePath);
var data = MonopolyMap.Parse(json);
return Results.Json(data, MonopolyMap.JsonSerializerOptions);
}
else
if (!File.Exists(jsonFilePath))
{
return Results.NotFound();
}
// read json file
string json = File.ReadAllText(jsonFilePath);
var data = MonopolyMap.Parse(json);
return Results.Json(data, MonopolyMap.JsonSerializerOptions);
});

app.MapGet("/rooms", () =>
{
var repository = app.Services.CreateScope().ServiceProvider.GetRequiredService<IRepository>();
return Results.Json(repository.GetRooms());
});

#if DEBUG
Expand Down
5 changes: 5 additions & 0 deletions Server/Repositories/InMemoryRepository.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,11 @@ public Monopoly FindGameById(string id)
return game;
}

public string[] GetRooms()
{
return Games.Keys.ToArray();
}

public bool IsExist(string id)
{
return Games.ContainsKey(id);
Expand Down

0 comments on commit 4324289

Please sign in to comment.