Exemple #1
0
        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}");
            }
        }