示例#1
0
        public async Task CreateQuiz(bool priv, string name, string max)
        {
            string trimmedName = name == null ? "" : name.Trim();

            if (Quizzes.FirstOrDefault(quiz => quiz.Name == trimmedName) == null)
            {
                int maxPlayers;
                if (trimmedName == "none")
                {
                    Clients.Caller.message("Can not make room with name of 'none'");
                }
                else if (!int.TryParse(max, out maxPlayers))
                {
                    Clients.Caller.message("Room creation failed: max must be an integer");
                }
                else if (maxPlayers < 2)
                {
                    Clients.Caller.message("Room creation failed: maximum players must be greater than 1");
                }
                else
                {
                    var player = GetCurrentPlayer();

                    if (trimmedName == "")
                    {
                        trimmedName = player.Name + "'s room";
                    }
                    else if (trimmedName.Length > 35)
                    {
                        trimmedName = trimmedName.Substring(0, 35);
                    }

                    if (IsInRoom())
                    {
                        LeaveQuiz();
                    }

                    if (maxPlayers > 20)
                    {
                        maxPlayers = 20;
                    }

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

                    Quizzes.Add(new Quiz(priv, trimmedName, maxPlayers, player));
                    Clients.Caller.message("Room '" + trimmedName + "' created");
                    Clients.Caller.inRoom(true, trimmedName, maxPlayers);
                    PlayersInLobby(GetCurrentQuiz());
                }
            }
            else
            {
                Clients.Caller.message("Room already exists");
            }
        }