-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathIndex.razor
More file actions
143 lines (132 loc) · 5.53 KB
/
Index.razor
File metadata and controls
143 lines (132 loc) · 5.53 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
@page "/"
@using System.Net.Http
@using Homestead.Client.ViewModels
@using Homestead.Shared;
@using Microsoft.AspNetCore.SignalR.Client
@inject HttpClient Http
@inject NavigationManager NavManager
@inject Blazored.LocalStorage.ILocalStorageService localStorage
@inject LobbyVm Lobby;
@inject HubConnection hubConnection
<PageTitle>Homestead game by IntelliTect</PageTitle>
<h1 class="pinewood">
Homestead Games
</h1>
<div>
<button class="btn btn-success" @onclick="CreateGame" type="button">Create a New Game</button>
<button class="btn btn-primary" @onclick="GetGames" type="button">Refresh Games</button>
</div>
<hr />
<div class="container-fluid">
@if (games is null || !games.Any())
{
<p>No games found.</p>
}
else
{
<div class="row">
@{
int counter = 0;
}
@foreach (var game in games)
{
counter++;
<div class="col-12 col-sm-12 col-md-6 col-lg-4 col-xl-3">
<div class="card">
<div class="card-header tree-background" style="padding-top: 15px;">
<button class="btn btn-success" style="float:right;" disabled="@(GetHumanPlayerCount(game) > 3)" type="button" @onclick="()=>JoinGame(game.GameId)">
@if (game.Players.Any(f => f.PlayerId == Lobby.PlayerId))
{
<span>Rejoin</span>
}
else
{
<span>Join </span>
}
</button>
<h2>Game #@counter</h2>
</div>
<div class="card-body green-background">
<div class="row">
<div class="col-12">
<div style="float:right">
@if (game.State == Game.GameState.Playing)
{
<span>In Progress</span>
}
else
{
<span>Looking for Players</span>
}
</div>
<h3>Players</h3>
<ul class="list-group">
@foreach (var player in game.Players)
{
<li class="list-group-item">
<img class="player" src="@PlayerVm.GetImageUrl(player)" />
@player.Name
@if (player.PlayerId == Lobby.PlayerId)
{
<span> (You)</span>
}
<span style="padding-top:7px;float: right;">
@if (player.IsBot)
{
<span>🤖</span>
}
else
{
<span>😃</span>
}
</span>
</li>
}
</ul>
</div>
</div>
</div>
<div class="card-footer">
Last Play: @(Math.Round((DateTime.UtcNow - game.LastPlayDate).TotalMinutes, 1)) minutes ago
</div>
</div>
</div>
}
</div>
}
</div>
@code {
private Game[]? games;
private int GetHumanPlayerCount(Game game) => game.Players.Where(x => !x.IsBot).Count();
protected override async Task OnInitializedAsync()
{
Lobby.PlayerId = await localStorage.GetItemAsync<string>("PlayerId") ?? string.Empty;
if (string.IsNullOrEmpty(Lobby.PlayerId))
{
Lobby.PlayerId = Guid.NewGuid().ToString();
await localStorage.SetItemAsync("PlayerId", Lobby.PlayerId);
}
Console.WriteLine($"Player's Id: {Lobby.PlayerId}");
await GetGames();
}
private async Task GetGames()
{
games = await Http.GetFromJsonAsync<Game[]>("Lobby");
}
private async Task CreateGame()
{
await Lobby.CreateGame(Http);
await hubConnection.InvokeAsync("SubscribeToGame", Lobby.Board.GameId);
NavManager.NavigateTo("/gameboardpage");
}
private void DisplayInstructions()
{
NavManager.NavigateTo("/instructions");
}
private async Task JoinGame(string gameId)
{
var result = await Lobby.JoinGame(gameId, Http);
await hubConnection.InvokeAsync("SubscribeToGame", Lobby.Board.GameId);
if (result) NavManager.NavigateTo("/gameboardpage");
}
}