public async Task SendNextRound(string group) { try { // check that this is initiated by the owner if (!GroupDetails.TryGetOwner(group, out string ownerconnectionid) || !string.Equals(Context.ConnectionId, ownerconnectionid, StringComparison.OrdinalIgnoreCase)) { return; } // choose details about the next round if (!GroupDetails.StartNextRound(group, out RoundDetails round)) { await Clients.Group(group).SendAsync("ReceiveMessage", "failed to start round"); return; } // give credit for the drawer GroupDetails.AddToScore(group, round.ConnectionId, round.Timeout / 3f); GroupDetails.SetHasAnswered(group, round.ConnectionId); // notify everyone except the drawer await Clients.GroupExcept(group, round.ConnectionId).SendAsync("ReceiveNextRound", round.Username, round.Timeout, round.ObfuscatedWord, false /* candraw */); // notify the drawer await Clients.Client(round.ConnectionId).SendAsync("ReceiveNextRound", round.Username, round.Timeout, round.Word, true /* candraw */); } catch (Exception e) { await Clients.Group(group).SendAsync("ReceiveMessage", $"failed to start next round: {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}"); } }