private Task AnswerReceived(SocketMessage imsg) { var _ = Task.Run(async() => { try { if (imsg.Author.IsBot) { return; } var msg = imsg as SocketUserMessage; if (msg == null) { return; } if (this.Channel == null || this.Channel.Id != msg.Channel.Id) { return; } var guess = msg.Content; var distance = CurrentSentence.LevenshteinDistance(guess); var decision = Judge(distance, guess.Length); if (decision && !finishedUserIds.Contains(msg.Author.Id)) { var elapsed = sw.Elapsed; var wpm = CurrentSentence.Length / WORD_VALUE / elapsed.TotalSeconds * 60; finishedUserIds.Add(msg.Author.Id); await this.Channel.EmbedAsync(new EmbedBuilder().WithOkColor() .WithTitle($"{msg.Author} finished the race!") .AddField(efb => efb.WithName("Place").WithValue($"#{finishedUserIds.Count}").WithIsInline(true)) .AddField(efb => efb.WithName("WPM").WithValue($"{wpm:F1} *[{elapsed.TotalSeconds:F2}sec]*") .WithIsInline(true)) .AddField(efb => efb.WithName("Errors").WithValue(distance.ToString()).WithIsInline(true))) .ConfigureAwait(false); if (finishedUserIds.Count % 4 == 0) { await this.Channel.SendConfirmAsync( $":exclamation: A lot of people finished, here is the text for those still typing:" + $"\n\n**{Format.Sanitize(CurrentSentence.Replace(" ", " \x200B", StringComparison.InvariantCulture)).SanitizeMentions(true)}**") .ConfigureAwait(false); } } } catch (Exception ex) { Log.Warning(ex.ToString()); } }); return(Task.CompletedTask); }
public async Task Start() { if (IsActive) { return; // can't start running game } IsActive = true; CurrentSentence = GetRandomSentence(); var i = (int)(CurrentSentence.Length / WORD_VALUE * 1.7f); try { await Channel.SendConfirmAsync($@":clock2: Next contest will last for {i} seconds. Type the bolded text as fast as you can.").ConfigureAwait(false); var time = _options.StartTime; var msg = await Channel.SendMessageAsync($"Starting new typing contest in **{time}**...", options : new RequestOptions() { RetryMode = RetryMode.AlwaysRetry }).ConfigureAwait(false); do { await Task.Delay(2000).ConfigureAwait(false); time -= 2; try { await msg.ModifyAsync(m => m.Content = $"Starting new typing contest in **{time}**..").ConfigureAwait(false); } catch { } } while (time > 2); await msg.ModifyAsync(m => { m.Content = CurrentSentence.Replace(" ", " \x200B"); }).ConfigureAwait(false); sw.Start(); HandleAnswers(); while (i > 0) { await Task.Delay(1000).ConfigureAwait(false); i--; if (!IsActive) { return; } } } catch { } finally { await Stop().ConfigureAwait(false); } }
public async Task Start() { if (IsActive) { return; // can't start running game } IsActive = true; CurrentSentence = GetRandomSentence(); var i = (int)(CurrentSentence.Length / WORD_VALUE * 1.7f); try { await Channel.SendConfirmAsync($@":clock2: Next contest will last for {i} seconds. Type the bolded text as fast as you can.").ConfigureAwait(false); var msg = await Channel.SendMessageAsync("Starting new typing contest in **3**...").ConfigureAwait(false); await Task.Delay(1000).ConfigureAwait(false); try { await msg.ModifyAsync(m => m.Content = "Starting new typing contest in **2**...").ConfigureAwait(false); await Task.Delay(1000).ConfigureAwait(false); await msg.ModifyAsync(m => m.Content = "Starting new typing contest in **1**...").ConfigureAwait(false); await Task.Delay(1000).ConfigureAwait(false); } catch (Exception ex) { _log.Warn(ex); } await msg.ModifyAsync(m => m.Content = Format.Bold(Format.Sanitize(CurrentSentence.Replace(" ", " \x200B")).SanitizeMentions())).ConfigureAwait(false); sw.Start(); HandleAnswers(); while (i > 0) { await Task.Delay(1000).ConfigureAwait(false); i--; if (!IsActive) { return; } } } catch { } finally { await Stop().ConfigureAwait(false); } }
private Task AnswerReceived(IMessage imsg) { if (imsg.Author.IsBot) { return(Task.CompletedTask); } var msg = imsg as IUserMessage; if (msg == null) { return(Task.CompletedTask); } var t = Task.Run(async() => { try { if (this.Channel == null || this.Channel.Id != this.Channel.Id) { return; } var guess = msg.Content; var distance = CurrentSentence.LevenshteinDistance(guess); var decision = Judge(distance, guess.Length); if (decision && !finishedUserIds.Contains(msg.Author.Id)) { var wpm = CurrentSentence.Length / WORD_VALUE / sw.Elapsed.Seconds * 60; finishedUserIds.Add(msg.Author.Id); await Extensions.Extensions.EmbedAsync(this.Channel, (Discord.API.Embed) new EmbedBuilder().WithColor((uint)NadekoBot.OkColor) .WithTitle((string)$"{msg.Author} finished the race!") .AddField(efb => efb.WithName("Place").WithValue($"#{finishedUserIds.Count}").WithIsInline(true)) .AddField(efb => efb.WithName("WPM").WithValue($"{wpm:F2} *[{sw.Elapsed.Seconds.ToString()}sec]*").WithIsInline(true)) .AddField(efb => efb.WithName((string)"Errors").WithValue((string)distance.ToString()).WithIsInline((bool)true)) .Build()).ConfigureAwait(false); if (finishedUserIds.Count % 4 == 0) { await Extensions.Extensions.SendConfirmAsync(this.Channel, (string)$":exclamation: A lot of people finished, here is the text for those still typing:\n\n**{Format.Sanitize((string)CurrentSentence.Replace((string)" ", (string)" \x200B")).SanitizeMentions()}**").ConfigureAwait(false); } } } catch { } }); return(Task.CompletedTask); }
private async void AnswerReceived(SocketMessage imsg) { try { if (imsg.Author.IsBot) { return; } var msg = imsg as SocketUserMessage; if (msg == null) { return; } if (this.Channel == null || this.Channel.Id != this.Channel.Id) { return; } var guess = msg.Content; var distance = CurrentSentence.LevenshteinDistance(guess); var decision = Judge(distance, guess.Length); if (decision && !finishedUserIds.Contains(msg.Author.Id)) { var wpm = CurrentSentence.Length / WORD_VALUE / sw.Elapsed.Seconds * 60; finishedUserIds.Add(msg.Author.Id); await this.Channel.EmbedAsync(new EmbedBuilder().WithOkColor() .WithTitle((string)$"{msg.Author} finished the race!") .AddField(efb => efb.WithName("Place").WithValue($"#{finishedUserIds.Count}").WithIsInline(true)) .AddField(efb => efb.WithName("WPM").WithValue($"{wpm:F2} *[{sw.Elapsed.Seconds.ToString()}sec]*").WithIsInline(true)) .AddField(efb => efb.WithName((string)"Errors").WithValue((string)distance.ToString()).WithIsInline((bool)true))) .ConfigureAwait(false); if (finishedUserIds.Count % 4 == 0) { await this.Channel.SendConfirmAsync($":exclamation: A lot of people finished, here is the text for those still typing:\n\n**{Format.Sanitize(CurrentSentence.Replace(" ", " \x200B")).SanitizeMentions()}**").ConfigureAwait(false); } } } catch (Exception ex) { _log.Warn(ex); } }