@implements IDisposable; @using Shogi.Contracts.Socket; @using Shogi.Contracts.Types; @using System.ComponentModel.DataAnnotations; @using System.Net; @using System.Text.Json; @inject IShogiApi ShogiApi; @inject ShogiSocket ShogiSocket; @inject AccountState Account;

Games you're seated at

@if (!joinedSessions.Any()) {

You have not joined any games.

} @foreach (var session in joinedSessions) { }

Other games

@if (!otherSessions.Any()) {

You have not joined any games.

} @foreach (var session in otherSessions) { }

Start a new session

@if (createSessionStatusCode == HttpStatusCode.Created) { } else if (createSessionStatusCode == HttpStatusCode.Conflict) { }
@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 void OnInitialized() { base.OnInitialized(); ShogiSocket.OnSessionCreated += FetchSessions; ShogiSocket.OnSessionJoined += OnSessionJoined_FetchSessions; Account.LoginChangedEvent += LoginChangedEvent_FetchSessions; } string ActiveCss(SessionMetadata s) => s == activeSession ? "active" : string.Empty; void OnClickSession(SessionMetadata s) { activeSession = s; ActiveSessionChanged?.Invoke(s); } Task OnSessionJoined_FetchSessions(SessionJoinedByPlayerSocketMessage args) => FetchSessions(); Task LoginChangedEvent_FetchSessions(LoginEventArgs args) { if (args.User != null) { return FetchSessions(); } return Task.CompletedTask; } 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); } public void Dispose() { ShogiSocket.OnSessionCreated -= FetchSessions; ShogiSocket.OnSessionJoined -= OnSessionJoined_FetchSessions; Account.LoginChangedEvent -= LoginChangedEvent_FetchSessions; } private class CreateForm { [Required] public string Name { get; set; } = string.Empty; public bool IsPrivate { get; set; } } }