@using Shogi.Contracts.Types; @using System.ComponentModel.DataAnnotations; @using System.Net; @using System.Text.Json; @inject IShogiApi ShogiApi; @inject ShogiSocket ShogiSocket; @inject AccountState Account; Search Create Games you're seated at @if (!joinedSessions.Any()) { You have not joined any games. } @foreach (var session in joinedSessions) { OnClickSession(session)"> @session.Name (@session.PlayerCount/2) } Other games @if (!otherSessions.Any()) { You have not joined any games. } @foreach (var session in otherSessions) { OnClickSession(session)"> @session.Name (@session.PlayerCount/2) } Start a new session Session name Private? Submit @if (createSessionStatusCode == HttpStatusCode.Created) { Session started. View it in the search tab. } else if (createSessionStatusCode == HttpStatusCode.Conflict) { The name you chose is taken; choose another. } @code { [Parameter] public Action? ActiveSessionChanged { get; set; } private CreateForm createForm = new(); private SessionMetadata[] joinedSessions = Array.Empty(); private SessionMetadata[] otherSessions = Array.Empty(); private SessionMetadata? activeSession; private HttpStatusCode? createSessionStatusCode; protected override async Task OnInitializedAsync() { ShogiSocket.OnCreateGameMessage += async (sender, message) => await FetchSessions(); Account.LoginChangedEvent += async (sender, message) => { Console.WriteLine($"LoginEvent. Message={JsonSerializer.Serialize(message)}."); if (message.User != null) { await FetchSessions(); } }; } string ActiveCss(SessionMetadata s) => s == activeSession ? "active" : string.Empty; void OnClickSession(SessionMetadata s) { activeSession = s; ActiveSessionChanged?.Invoke(s); } async Task FetchSessions() { var sessions = await ShogiApi.GetSessionsPlayerCount(); if (sessions != null) { this.joinedSessions = sessions.PlayerHasJoinedSessions.ToArray(); this.otherSessions = sessions.AllOtherSessions.ToArray(); StateHasChanged(); } } async Task CreateSession() { createSessionStatusCode = await ShogiApi.PostSession(createForm.Name, createForm.IsPrivate); } private class CreateForm { [Required] public string Name { get; set; } = string.Empty; public bool IsPrivate { get; set; } } }
You have not joined any games.