@using Shogi.Contracts.Types @using System.ComponentModel.DataAnnotations @using System.Net @inject IShogiApi ShogiApi; @inject ShogiSocket ShogiSocket; Search Create @if (!sessions.Any()) { No games exist } @foreach (var session in sessions) { 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; } CreateForm createForm = new(); SessionMetadata[] sessions = Array.Empty(); SessionMetadata? activeSession; HttpStatusCode? createSessionStatusCode; protected override async Task OnInitializedAsync() { ShogiSocket.OnCreateGameMessage += async (sender, message) => await FetchSessions(); 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.sessions = sessions.PlayerHasJoinedSessions.Concat(sessions.AllOtherSessions).ToArray(); } } 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; } } }
No games exist