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 async Task SendMessage(string group, string message) { if (string.IsNullOrWhiteSpace(message)) { return; } // todo there is a corner case with only 1 player where the end will not end by answering the right answer // the reason is that the round is over but the detection logic does not catch that try { // get username GroupDetails.TryGetUsername(group, Context.ConnectionId, out string username); // check if we are in a round, and if so then check if this is a valid guess GroupDetails.TryGetInRound(group, out bool inround, out bool atendofround, out RoundDetails round); if (inround && round != null) { // check if this guess is currect if (message.IndexOf(round.Word, StringComparison.OrdinalIgnoreCase) >= 0) { // cannot guess for your own question or if you have already gotten it right if (!atendofround) { if (GroupDetails.TryGetHasAnswered(group, Context.ConnectionId, out bool answered) && !answered) { // give credit GroupDetails.AddToScore(group, Context.ConnectionId, (float)(round.End - DateTime.UtcNow).TotalSeconds); // mark that you answered GroupDetails.SetHasAnswered(group, Context.ConnectionId); } // check again GroupDetails.TryGetInRound(group, out inround, out atendofround, out round); } // remove the correct word from the phrase message = message.Replace(round.Word, "correct :)", StringComparison.OrdinalIgnoreCase); } } // send the message to everyone in this group await Clients.Group(group).SendAsync("ReceiveMessage", $"{username}: {message}"); // finish early if done if (atendofround) { await SendRoundComplete(group); } } catch (Exception e) { await Clients.Group(group).SendAsync("ReceiveMessage", $"failed to send message: {e.Message}"); } }