コード例 #1
0
        public async Task JoinQuiz(string name)
        {
            // Check if quiz exists
            var exists = Quizzes.Exists(q => q.Name == name);

            // Check if the quiz has started
            if (exists && !Quizzes.Find(q => q.Name == name).Started)
            {
                var quiz = Quizzes.Find(q => q.Name == name);

                if (quiz.MaxPlayers > quiz.Players.Count)
                {
                    // Leave room if already in another quiz
                    if (IsInRoom())
                    {
                        LeaveQuiz();
                    }

                    var player = GetCurrentPlayer();

                    await Groups.Add(Context.ConnectionId, name);

                    quiz.Players.Add(player);
                    Clients.Caller.inRoom(true, name, quiz.MaxPlayers);
                    PlayersReadyCaller();
                    MessageGroup(player.Name + " joined the room");
                    PlayersInLobby(quiz);
                }
                else
                {
                    Clients.Caller.message("Player count reached");
                }
            }
            // Quiz has already started
            else if (exists)
            {
                Clients.Caller.message("Quiz has already started");
            }
            // Quiz does not exist
            else
            {
                Clients.Caller.message("Room does not exist");
            }
        }