Ejemplo n.º 1
0
            public async Task Trivia(int winReq = 10, [Remainder] string additionalArgs = "")
            {
                var channel = (ITextChannel)Context.Channel;

                var showHints = !additionalArgs.Contains("nohint");

                TriviaGame trivia = new TriviaGame(channel.Guild, channel, showHints, winReq);

                if (RunningTrivias.TryAdd(channel.Guild.Id, trivia))
                {
                    try
                    {
                        await trivia.StartGame().ConfigureAwait(false);
                    }
                    finally
                    {
                        RunningTrivias.TryRemove(channel.Guild.Id, out trivia);
                        await trivia.EnsureStopped().ConfigureAwait(false);
                    }
                    return;
                }
                else
                {
                    await Context.Channel.SendErrorAsync("Trivia game is already running on this server.\n" + trivia.CurrentQuestion).ConfigureAwait(false);
                }
            }
Ejemplo n.º 2
0
            public async Task InternalTrivia(params string[] args)
            {
                var channel = (ITextChannel)Context.Channel;

                var(opts, _) = OptionsParser.Default.ParseFrom(new TriviaOptions(), args);

                var trivia = new TriviaGame(_strings, _client, _bc, _cache, _cs, channel.Guild, channel, opts);

                if (_service.RunningTrivias.TryAdd(channel.Guild.Id, trivia))
                {
                    try
                    {
                        await trivia.StartGame().ConfigureAwait(false);
                    }
                    finally
                    {
                        _service.RunningTrivias.TryRemove(channel.Guild.Id, out trivia);
                        await trivia.EnsureStopped().ConfigureAwait(false);
                    }
                    return;
                }

                await Context.Channel.SendErrorAsync(GetText("trivia_already_running") + "\n" + trivia.CurrentQuestion)
                .ConfigureAwait(false);
            }
Ejemplo n.º 3
0
            public async Task Trivia(IUserMessage umsg, params string[] args)
            {
                var channel = (ITextChannel)umsg.Channel;

                TriviaGame trivia;

                if (!RunningTrivias.TryGetValue(channel.Guild.Id, out trivia))
                {
                    var showHints = !args.Contains("nohint");
                    var number    = args.Select(s =>
                    {
                        int num;
                        return(new Tuple <bool, int>(int.TryParse(s, out num), num));
                    }).Where(t => t.Item1).Select(t => t.Item2).FirstOrDefault();
                    if (number < 0)
                    {
                        return;
                    }
                    var triviaGame = new TriviaGame(channel.Guild, (ITextChannel)umsg.Channel, showHints, number == 0 ? 10 : number);
                    if (RunningTrivias.TryAdd(channel.Guild.Id, triviaGame))
                    {
                        await channel.SendConfirmAsync($"**Trivia game started! {triviaGame.WinRequirement} points needed to win.**").ConfigureAwait(false);
                    }
                    else
                    {
                        await triviaGame.StopGame().ConfigureAwait(false);
                    }
                }
                else
                {
                    await channel.SendErrorAsync("Trivia game is already running on this server.\n" + trivia.CurrentQuestion).ConfigureAwait(false);
                }
            }
Ejemplo n.º 4
0
        public void DBConnect()
        {
            AppDomain currentDomain = AppDomain.CurrentDomain;

            currentDomain.SetData("DataDirectory", "E:\\Year 2\\Semester 2\\Windows Programming C#\\Projects\\CSharpProject");
            tGame = new TriviaGame();
        }
Ejemplo n.º 5
0
            public async Task InternalTrivia(int winReq, string additionalArgs = "")
            {
                var channel = (ITextChannel)Context.Channel;

                additionalArgs = additionalArgs?.Trim()?.ToLowerInvariant();

                var showHints = !additionalArgs.Contains("nohint");
                var isPokemon = additionalArgs.Contains("pokemon");

                var trivia = new TriviaGame(channel.Guild, channel, showHints, winReq, isPokemon);

                if (RunningTrivias.TryAdd(channel.Guild.Id, trivia))
                {
                    try
                    {
                        await trivia.StartGame().ConfigureAwait(false);
                    }
                    finally
                    {
                        RunningTrivias.TryRemove(channel.Guild.Id, out trivia);
                        await trivia.EnsureStopped().ConfigureAwait(false);
                    }
                    return;
                }

                await Context.Channel.SendErrorAsync(GetText("trivia_already_running") + "\n" + trivia.CurrentQuestion)
                .ConfigureAwait(false);
            }
Ejemplo n.º 6
0
            public async Task InternalTrivia(params string[] args)
            {
                var channel = (ITextChannel)ctx.Channel;

                var(opts, _) = OptionsParser.ParseFrom(new TriviaOptions(), args);

                var config = _gamesConfig.Data;

                if (config.Trivia.MinimumWinReq > 0 && config.Trivia.MinimumWinReq > opts.WinRequirement)
                {
                    return;
                }
                var trivia = new TriviaGame(Strings, _client, config, _cache, _cs, channel.Guild, channel, opts, Prefix + "tq");

                if (_service.RunningTrivias.TryAdd(channel.Guild.Id, trivia))
                {
                    try
                    {
                        await trivia.StartGame().ConfigureAwait(false);
                    }
                    finally
                    {
                        _service.RunningTrivias.TryRemove(channel.Guild.Id, out trivia);
                        await trivia.EnsureStopped().ConfigureAwait(false);
                    }
                    return;
                }

                await ctx.Channel.SendErrorAsync(GetText("trivia_already_running") + "\n" + trivia.CurrentQuestion)
                .ConfigureAwait(false);
            }
Ejemplo n.º 7
0
        internal override void Init(CommandGroupBuilder cgb)
        {
            cgb.CreateCommand(Module.Prefix + "t")
            .Description($"Starts a game of trivia. You can add nohint to prevent hints." +
                         "First player to get to 10 points wins. 30 seconds per question." +
                         $"\n**Usage**:`{Module.Prefix}t nohint`")
            .Parameter("args", ParameterType.Multiple)
            .Do(async e =>
            {
                TriviaGame trivia;
                if (!RunningTrivias.TryGetValue(e.Server.Id, out trivia))
                {
                    var showHints  = !e.Args.Contains("nohint");
                    var triviaGame = new TriviaGame(e, showHints);
                    if (RunningTrivias.TryAdd(e.Server.Id, triviaGame))
                    {
                        await e.Channel.SendMessage("**Trivia game started!**").ConfigureAwait(false);
                    }
                    else
                    {
                        await triviaGame.StopGame().ConfigureAwait(false);
                    }
                }
                else
                {
                    await e.Channel.SendMessage("Trivia game is already running on this server.\n" + trivia.CurrentQuestion).ConfigureAwait(false);
                }
            });

            cgb.CreateCommand(Module.Prefix + "tl")
            .Description("Shows a current trivia leaderboard.")
            .Do(async e =>
            {
                TriviaGame trivia;
                if (RunningTrivias.TryGetValue(e.Server.Id, out trivia))
                {
                    await e.Channel.SendMessage(trivia.GetLeaderboard()).ConfigureAwait(false);
                }
                else
                {
                    await e.Channel.SendMessage("No trivia is running on this server.").ConfigureAwait(false);
                }
            });

            cgb.CreateCommand(Module.Prefix + "tq")
            .Description("Quits current trivia after current question.")
            .Do(async e =>
            {
                TriviaGame trivia;
                if (RunningTrivias.TryGetValue(e.Server.Id, out trivia))
                {
                    await trivia.StopGame().ConfigureAwait(false);
                }
                else
                {
                    await e.Channel.SendMessage("No trivia is running on this server.").ConfigureAwait(false);
                }
            });
        }
Ejemplo n.º 8
0
        public async Task StartAsync(IDialogContext context)
        {
            await context.PostAsync($"Welcome to Trivia, Let's play...");

            // post the question and choices as a hero card
            _game = new TriviaGame("");
            await context.PostAsync(_game.CurrentQuestion().Question);

            await context.PostAsync(MakeChoiceCard(context, _game.CurrentQuestion()));

            // wait for input
            context.Wait(MessageReceivedAsync);
        }
Ejemplo n.º 9
0
        public async Task <IActionResult> EditGame([FromQuery] int id = -1)
        {
            if (id == -1)
            {
                return(RedirectToAction("CreateGame"));
            }
            TriviaGame game = await _context.TriviaGame.FirstAsync(x => x.Id == id);

            if (game.Host != ((User)ViewData[Global.UserDataKey]).Id)
            {
                return(Forbid());
            }
            return(View(game));
        }
Ejemplo n.º 10
0
        public static async Task <TriviaGame?> StartGame(ulong gameChannel, int questionCount = 1, QuestionCategory questionCategory = QuestionCategory.All)
        {
            if (ActiveTriviaGames.ContainsKey(gameChannel))
            {
                return(null);
            }

            var game = new TriviaGame(gameChannel, questionCount, questionCategory);

            await game.LoadQuestionsAsync();

            ActiveTriviaGames[gameChannel] = game;

            return(game);
        }
Ejemplo n.º 11
0
        public async Task StartTriviaGame([Remainder] string args)
        {
            var cat        = TriviaGameHelper.ConvertCategory(args);
            var questions  = TriviaGameHelper.MakeQuestionRequest(cat);
            var _questions = TriviaGameHelper.HandleQuestionResponse(questions);

            TriviaGame game = new TriviaGame(_questions);

            game.NewGame(Context);

            if (_questions.Count > 0)
            {
                //while (_questions.Any())
                //{
                //    var rnd = new Random();
                //    var q = _questions[rnd.Next(1, _questions.Count)];
                //    _questions.Remove(q);
                //    var shuffled = new List<string>();

                //    for (int i = 0; i < q.Answers.Count; i++)
                //    {
                //        shuffled.Add(q.Answers[i].ToString());
                //    }
                //    shuffled.Add(q.Correct_Answer);
                //    var newShuffled = shuffled.Shuffle();

                //    var sb = new StringBuilder();
                //    sb.AppendLine(String.Format("Question: {0}", q._Question));
                //    sb.AppendLine(String.Format("Answers: {0}, {1}, {2}, {3}", newShuffled[0], newShuffled[1], newShuffled[2], newShuffled[3]));
                //    sb.AppendLine(String.Format("Correct Answer: {0}", q.Correct_Answer));
                //    sb.AppendLine("you have 30 seconds to answer the question correctly!");

                //    var embed = EmbedBuilderHelper.BuildEmbed("Trivia Quest - " + q.Category, sb.ToString(), DateTime.Now.ToLongTimeString(), "darkorange");
                //    await Context.Channel.SendMessageAsync(null, false, embed);

                //    await Task.Delay(20 * 1000);

                //    await Context.Channel.SendMessageAsync("```there are 10 seconds remaining to answer correctly!```");

                //    await Task.Delay(10 * 1000);

                //    await Context.Channel.SendMessageAsync("```Times Up!```");
                //}
            }
        }
Ejemplo n.º 12
0
        private async Task MessageReceivedAsync(IDialogContext context, IAwaitable <object> result)
        {
            var    activity    = (IMessageActivity)await result;
            int    usersAnswer = -1;
            string answer      = activity.Text.Replace("trivia", string.Empty);

            if (int.TryParse(answer, out usersAnswer))
            {
                await context.PostAsync($"You chose: {answer}");

                if (_game.Answer(usersAnswer))
                {
                    await context.PostAsync("Correct!");
                }
                else
                {
                    await context.PostAsync("Sorry, that's wrong :-(");
                }

                await context.PostAsync($"Your score is: {_game.Score()}/{_game._questions.Count}. Next question!");

                TriviaQuestion nextQuestion = _game.MoveToNextQuestion();
                if (nextQuestion != null)
                {
                    await context.PostAsync(nextQuestion.Question);

                    await context.PostAsync(MakeChoiceCard(context, nextQuestion));

                    context.Wait(MessageReceivedAsync);
                }
                else
                {
                    await context.PostAsync("That's it! :-)");

                    context.Done("");
                    _game = null;
                }
            }
            else if (activity.Text != "trivia")
            {
                await context.PostAsync("I didn't quite get that, I am only programmed to accept numbers :-(");

                context.Wait(MessageReceivedAsync);
            }
        }
Ejemplo n.º 13
0
        public async Task <IActionResult> CreateGame(string name, int maxPlayers)
        {
            _context.TriviaGame.Add(new TriviaGame()
            {
                Name       = name,
                MaxPlayers = (byte)maxPlayers,
                Host       = (ViewData[Global.UserDataKey] as User).Id
            });
            await _context.SaveChangesAsync();

            TriviaGame myGame = await _context.TriviaGame
                                .Include(x => x.HostNavigation)
                                .FirstAsync(
                x => x.Name.Equals(name) &&
                x.MaxPlayers.Equals(maxPlayers) &&
                x.HostNavigation.Token.Equals(ViewData[Global.UserDataKey]
                                              ));

            return(RedirectToAction("EditGame", new { id = myGame.Id }));
        }
Ejemplo n.º 14
0
        /// <summary>
        ///     Here we'll set the players name returned from the prompt and then initialize the game
        /// </summary>
        /// <param name="context"></param>
        /// <param name="result"></param>
        /// <returns></returns>
        private async Task AfterName(IDialogContext context, IAwaitable <object> result)
        {
            // we stored the user's name in the bot session data in RootDialog, now we will retrieve it
            IMessageActivity activity    = (IMessageActivity)await result;
            StateClient      stateClient = activity.GetStateClient();
            // get the current user data
            BotData userData = await stateClient.BotState.GetUserDataAsync(activity.ChannelId, activity.From.Id);

            // upate/set a property called 'name'
            string name = userData.GetProperty <string>("Name");

            _game = new TriviaGame(name);

            await context.PostAsync($"Ready or not, {name}, Let's play!");

            // post the question in separate message so that it doesn't get cut off
            await context.PostAsync(_game.CurrentQuestion().Question);

            // most the question and choices as a hero card
            await context.PostAsync(MakeChoiceCard(context, _game.CurrentQuestion()));

            // wait for the answer
            context.Wait(MessageReceivedAsync);
        }
Ejemplo n.º 15
0
        public static void Main(String[] args)
        {
            var triviaGame = new TriviaGame();

            triviaGame.Execute();
        }
Ejemplo n.º 16
0
        internal override void Init(CommandGroupBuilder cgb)
        {
            cgb.CreateCommand(Module.Prefix + "t")
            .Description($"Startet ein Quiz. Du kannst nohint hinzufügen um Tipps zu verhindern." +
                         "Erster Spieler mit 10 Punkten gewinnt. 30 Sekunden je Frage." +
                         $" |`{Module.Prefix}t nohint` oder `{Module.Prefix}t 5 nohint`")
            .Parameter("args", ParameterType.Multiple)
            .AddCheck(SimpleCheckers.ManageMessages())
            .Do(async e =>
            {
                TriviaGame trivia;
                if (!RunningTrivias.TryGetValue(e.Server.Id, out trivia))
                {
                    var showHints = !e.Args.Contains("nohint");
                    var number    = e.Args.Select(s =>
                    {
                        int num;
                        return(new Tuple <bool, int> (int.TryParse(s, out num), num));
                    }).Where(t => t.Item1).Select(t => t.Item2).FirstOrDefault();
                    if (number < 0)
                    {
                        return;
                    }
                    var triviaGame = new TriviaGame(e, showHints, number == 0 ? 10 : number);
                    if (RunningTrivias.TryAdd(e.Server.Id, triviaGame))
                    {
                        await e.Channel.SendMessage($"**Trivia Game gestartet! {triviaGame.WinRequirement} Punkte benötigt um zu gewinnen.**").ConfigureAwait(false);
                    }
                    else
                    {
                        await triviaGame.StopGame().ConfigureAwait(false);
                    }
                }
                else
                {
                    await e.Channel.SendMessage("Auf diesem Server läuft bereits ein Quiz.\n" + trivia.CurrentQuestion).ConfigureAwait(false);
                }
            });

            cgb.CreateCommand(Module.Prefix + "tl")
            .Description($"Zeigt eine Rangliste des derzeitigen Quiz. | `{Prefix}tl`")
            .Do(async e =>
            {
                TriviaGame trivia;
                if (RunningTrivias.TryGetValue(e.Server.Id, out trivia))
                {
                    await e.Channel.SendMessage(trivia.GetLeaderboard()).ConfigureAwait(false);
                }
                else
                {
                    await e.Channel.SendMessage("Es läuft kein Quiz auf diesem Server.").ConfigureAwait(false);
                }
            });

            cgb.CreateCommand(Module.Prefix + "tq")
            .Description($"Beendet Quiz nach der derzeitgen Frage. | `{Prefix}tq`")
            .AddCheck(SimpleCheckers.ManageMessages())
            .Do(async e =>
            {
                TriviaGame trivia;
                if (RunningTrivias.TryGetValue(e.Server.Id, out trivia))
                {
                    await trivia.StopGame().ConfigureAwait(false);
                }
                else
                {
                    await e.Channel.SendMessage("Es läuft kein Quiz auf diesem Server.").ConfigureAwait(false);
                }
            });
        }
Ejemplo n.º 17
0
 public TriviaCommands(TriviaGame trivia)
 {
     _Trivia = trivia;
 }
Ejemplo n.º 18
0
        internal override void Init(CommandGroupBuilder cgb)
        {
            cgb.CreateCommand(Module.Prefix + "t")
            .Description($"Starts a game of trivia. You can add nohint to prevent hints." +
                         "First player to get to 10 points wins by default. You can specify a different number. 30 seconds per question." +
                         $" |`{Module.Prefix}t nohint` or `{Module.Prefix}t 5 nohint`")
            .Parameter("args", ParameterType.Multiple)
            .Do(async e =>
            {
                TriviaGame trivia;
                if (!RunningTrivias.TryGetValue(e.Server.Id, out trivia))
                {
                    var showHints = !e.Args.Contains("nohint");
                    var number    = e.Args.Select(s =>
                    {
                        int num;
                        return(new Tuple <bool, int>(int.TryParse(s, out num), num));
                    }).Where(t => t.Item1).Select(t => t.Item2).FirstOrDefault();
                    if (number < 3)
                    {
                        await e.Channel.SendMessage("Number too small.");
                        return;
                    }
                    var triviaGame = new TriviaGame(e, showHints, number == 0 ? 10 : number);
                    if (RunningTrivias.TryAdd(e.Server.Id, triviaGame))
                    {
                        await e.Channel.SendMessage($"**Trivia game started! {triviaGame.WinRequirement} points needed to win.**").ConfigureAwait(false);
                    }
                    else
                    {
                        await triviaGame.StopGame().ConfigureAwait(false);
                    }
                }
                else
                {
                    await e.Channel.SendMessage("Trivia game is already running on this server.\n" + trivia.CurrentQuestion).ConfigureAwait(false);
                }
            });

            cgb.CreateCommand(Module.Prefix + "tl")
            .Description($"Shows a current trivia leaderboard. | `{Prefix}tl`")
            .Do(async e =>
            {
                TriviaGame trivia;
                if (RunningTrivias.TryGetValue(e.Server.Id, out trivia))
                {
                    await e.Channel.SendMessage(trivia.GetLeaderboard()).ConfigureAwait(false);
                }
                else
                {
                    await e.Channel.SendMessage("No trivia is running on this server.").ConfigureAwait(false);
                }
            });

            cgb.CreateCommand(Module.Prefix + "tq")
            .Description($"Quits current trivia after current question. | `{Prefix}tq`")
            .Do(async e =>
            {
                TriviaGame trivia;
                if (RunningTrivias.TryGetValue(e.Server.Id, out trivia))
                {
                    await trivia.StopGame().ConfigureAwait(false);
                }
                else
                {
                    await e.Channel.SendMessage("No trivia is running on this server.").ConfigureAwait(false);
                }
            });
        }