public async Task SendRoundComplete(string group) { try { // this method is callable by any connection GroupDetails.TryGetInRound(group, out bool inround, out bool atendofround, out RoundDetails round); // stop round GroupDetails.SetInRound(group, inround: false); // send a round done notification await Clients.Group(group).SendAsync("ReceiveRoundComplete", round != null ? round.Word : ""); // refresh the user list (with scores) await Clients.Group(group).SendAsync("ReceiveJoin", GetSortedUsers(GroupDetails.GetUsers(group))); } catch (Exception e) { await Clients.Group(group).SendAsync("ReceiveMessage", $"failed to complete round: {e.Message}"); } }
public async Task SendJoin(string group, string username) { try { // associate username with group if (!GroupDetails.AddUser(group: group, connectionId: Context.ConnectionId, username)) { await Clients.Group(group).SendAsync("ReceiveMessage", $"failed to add {username}"); return; } // associate connection with group await Groups.AddToGroupAsync(Context.ConnectionId, group); // broadcast current players to everyone in the group await Clients.Group(group).SendAsync("ReceiveJoin", GetSortedUsers(GroupDetails.GetUsers(group))); // check if a round is in flight GroupDetails.TryGetInRound(group, out bool inround, out bool atendofround, out RoundDetails round); if (inround && !atendofround && round != null) { await Clients.Client(Context.ConnectionId).SendAsync("ReceiveNextRound", round.Username, round.Timeout, round.ObfuscatedWord, false /* candraw */); return; } // make sure the player get the game start notification if (GroupDetails.IsGroupStarted(group)) { // send the game start inidcator await Clients.Client(Context.ConnectionId).SendAsync("ReceiveStartGame"); } } catch (Exception e) { await Clients.Group(group).SendAsync("ReceiveMessage", $"failed to join game: {e.Message}"); } }
public override async Task OnDisconnectedAsync(Exception exception) { try { // get the group and username associated with this connectionid GroupDetails.TryGetUsername(Context.ConnectionId, out string group, out string username); try { // what if the owner drops? if (!string.IsNullOrWhiteSpace(group) && GroupDetails.TryGetOwner(group, out string ownerconnectionid)) { if (string.Equals(Context.ConnectionId, ownerconnectionid, StringComparison.OrdinalIgnoreCase)) { // ugh - we cannot proceed await SendMessage(group, "I left the game and no more rounds can be played (sorry)"); } else { // notify that you left await SendMessage(group, $"I left the game"); } } // clean up GroupDetails.Purge(Context.ConnectionId); // broadcast the updated group if (!string.IsNullOrWhiteSpace(group)) { await Clients.Group(group).SendAsync("ReceiveJoin", GetSortedUsers(GroupDetails.GetUsers(group))); } await base.OnDisconnectedAsync(exception); } catch (Exception e) { await Clients.Group(group).SendAsync("ReceiveMessage", $"failed to disconnect: {e.Message}"); } } catch (Exception e) { await Clients.All.SendAsync("ReceiveMessage", $"Catastrophic failure: {e.Message}"); } }