internal async Task StopQuiz() { //wrap quiz up here QuizUtil trivia = null; _hamTestService.RunningTests.TryRemove(Id, out trivia); ShouldStopTest = true; //_client.MessageReceived -= ListenForAnswer; _client.ReactionAdded += ListenForReactionAdded; var quiz = _db.Quiz.Where(q => q.QuizId == Quiz.QuizId).FirstOrDefault(); quiz.TimeEnded = DateTime.Now; quiz.IsActive = false; await _db.SaveChangesAsync(); var embed = new EmbedBuilder(); embed.Title = $"[{CurrentQuestion.Test.TestName}] [{CurrentQuestion.Test.FromDate.ToShortDateString()} -> {CurrentQuestion.Test.ToDate.ToShortDateString()}] Test Results!"; embed.WithColor(new Color(0, 255, 0)); var sb = new StringBuilder(); sb.AppendLine($"Number of questions -> [**{_totalQuestions}**]"); embed.ThumbnailUrl = "https://github.com/gngrninja/SevenThree/raw/master/media/73.png?raw=true"; embed.WithFooter(new EmbedFooterBuilder { Text = "SevenThree, your local ham radio Discord bot!", IconUrl = "https://github.com/gngrninja/SevenThree/raw/master/media/73.png?raw=true" }); var users = await _db.UserAnswer.Where(u => u.Quiz.QuizId == Quiz.QuizId).ToListAsync(); if (users != null) { sb.AppendLine(); sb.AppendLine($"**__Leaderboard:__**"); var userResults = await GetTopUsers(); if (userResults.Count > 0) { int i = 0; string passFailEmoji = string.Empty; foreach (var user in userResults) { i++; decimal percentage; percentage = ((decimal)user.Item2 / (decimal)_totalQuestions) * 100; passFailEmoji = _quizHelper.GetPassFail(percentage); sb.AppendLine($"{_quizHelper.GetNumberEmojiFromInt(i)} [**{users.Where(u => (ulong)u.UserId == user.Item1).FirstOrDefault().UserName}**] with [**{user.Item2}**] [{passFailEmoji}] ({Math.Round(percentage, 0)}%)"); } sb.AppendLine(); sb.AppendLine($"Thanks for taking the test! Happy learning."); embed.Description = sb.ToString(); await SendReplyAsync(embed, false); await ClearChannel(); return; } } embed.Description = "Nobody scored!"; await SendReplyAsync(embed, false, false); await ClearChannel(); }
public async Task StopQuiz() { ulong id = GetId(); var quiz = await _db.Quiz.Where(q => q.ServerId == id && q.IsActive).FirstOrDefaultAsync(); if (quiz != null) { var gUser = Context.User as IGuildUser; if (gUser != null && Context.User.Id != quiz.StartedById && !gUser.GuildPermissions.KickMembers) { await ReplyAsync($"Sorry, {Context.User.Mention}, a test can only be stopped by the person who started it, or by someone with at least **KickMembers** permissions in {Context.Guild.Name}!"); return; } else if (quiz != null && gUser != null && gUser.GuildPermissions.KickMembers) { QuizUtil trivia = null; if (_hamTestService.RunningTests.TryRemove(id, out trivia)) { await trivia.StopQuiz().ConfigureAwait(false); } else { await ReplyAsync("No quiz to end!"); } return; } if (Context.User.Id == quiz.StartedById) { QuizUtil trivia = null; if (_hamTestService.RunningTests.TryRemove(id, out trivia)) { await trivia.StopQuiz().ConfigureAwait(false); } else { await ReplyAsync("No quiz to end!"); } } else { await ReplyAsync($"Sorry, {Context.User.Mention}, a test can only be stopped by the person who started it! (or by a moderator)"); } } else { await ReplyAsync("No quiz to end!"); } }
private async Task StartTest( int numQuestions, int questionDelay, string directMessage, string testName, ulong id ) { if (!(Context.Channel is IDMChannel) || !string.IsNullOrEmpty(directMessage)) { var channelInfo = await _db.QuizSettings.Where(q => q.DiscordGuildId == Context.Guild.Id).FirstOrDefaultAsync(); switch (testName) { case "tech": { if (channelInfo != null && channelInfo.TechChannelId != null && channelInfo.TechChannelId != Context.Channel.Id) { var goodChan = await Context.Guild.GetChannelAsync((ulong)channelInfo.TechChannelId); await ReplyAsync($"Tech test commands cannot be used in this channel, please use them in [#{goodChan.Name}]!"); return; } break; } case "general": { if (channelInfo != null && channelInfo.GeneralChannelId != null && channelInfo.GeneralChannelId != Context.Channel.Id) { var goodChan = await Context.Guild.GetChannelAsync((ulong)channelInfo.GeneralChannelId); await ReplyAsync($"General test commands cannot be used in this channel, please use them in [#{goodChan.Name}]!"); return; } break; } case "extra": { if (channelInfo != null && channelInfo.ExtraChannelId != null && channelInfo.ExtraChannelId != Context.Channel.Id) { var goodChan = await Context.Guild.GetChannelAsync((ulong)channelInfo.ExtraChannelId); await ReplyAsync($"Extra test commands cannot be used in this channel, please use them in [#{goodChan.Name}]!"); return; } break; } } } var checkQuiz = _db.Quiz.Where(q => q.ServerId == id && q.IsActive).FirstOrDefault(); if (checkQuiz == null) { await _db.Quiz.AddAsync( new Quiz { ServerId = id, IsActive = true, TimeStarted = DateTime.Now, StartedById = Context.User.Id, StartedByName = Context.User.Username, StartedByIconUrl = Context.User.GetAvatarUrl() }); await _db.SaveChangesAsync(); QuizUtil startQuiz = null; if (Context.Channel is IDMChannel || directMessage != null) { startQuiz = new QuizUtil( user: Context.User as IUser, services: _services, guild: Context.Guild as IGuild, id: id ); } else { startQuiz = new QuizUtil( channel: Context.Channel as ITextChannel, services: _services, guild: Context.Guild as IGuild, id: id ); } if (_hamTestService.RunningTests.TryAdd(id, startQuiz)) { var quiz = await _db.Quiz.Where(q => q.ServerId == id && q.IsActive).FirstOrDefaultAsync(); try { await startQuiz.StartGame(quiz, numQuestions, testName, questionDelay * 1000).ConfigureAwait(false); } catch (Exception ex) { _logger.LogError($"{ex.Message}"); } } else { _logger.LogInformation($"server n{Context.Guild.Name} i{Context.Guild.Id} -> Dictionary"); await ReplyAsync("There is already an active quiz!"); } } else { _logger.LogInformation($"server n{Context.Guild.Name} i{Context.Guild.Id} -> DB"); await ReplyAsync("There is already an active quiz!"); } }