wpm calculation, time scales with text length

in typing contest
This commit is contained in:
Master Kwoth 2016-01-24 07:53:02 +01:00
parent 2851a763f6
commit f857d39ff5
2 changed files with 23 additions and 11 deletions

View File

@ -7,48 +7,54 @@ using Discord;
using Discord.Commands; using Discord.Commands;
using NadekoBot.Extensions; using NadekoBot.Extensions;
using System.Threading; using System.Threading;
using System.Diagnostics;
namespace NadekoBot { namespace NadekoBot {
public static class SentencesProvider { public static class SentencesProvider {
internal static string GetRandomSentence() { internal static string GetRandomSentence() {
return "Random test sentence."; return "Random ultra long test sentence that i have to type every time.";
} }
} }
public class TypingGame { public class TypingGame {
public static float WORD_VALUE { get; } = 4.5f;
private Channel channel; private Channel channel;
public string currentSentence; public string currentSentence;
public bool IsActive; public bool IsActive;
private Stopwatch sw;
public TypingGame(Channel channel) { public TypingGame(Channel channel) {
this.channel = channel; this.channel = channel;
currentSentence = SentencesProvider.GetRandomSentence(); currentSentence = SentencesProvider.GetRandomSentence();
IsActive = false; IsActive = false;
sw = new Stopwatch();
} }
public Channel Channell { get; internal set; } public Channel Channell { get; internal set; }
internal async Task<bool> Stop() { internal async Task<bool> Stop() {
if (!IsActive) return false; if (!IsActive) return false;
IsActive = false;
NadekoBot.client.MessageReceived -= AnswerReceived; NadekoBot.client.MessageReceived -= AnswerReceived;
IsActive = false;
sw.Stop();
sw.Reset();
await channel.Send("Typing contest stopped"); await channel.Send("Typing contest stopped");
return true; return true;
} }
internal async Task Start() { internal async Task Start() {
IsActive = true; IsActive = true;
var msg = await channel.SendMessage("Starting new typing contest in **3**...."); var msg = await channel.SendMessage("Starting new typing contest in **3**...");
await Task.Delay(1000); await Task.Delay(1000);
await msg.Edit("Starting new typing contest in **2**...."); await msg.Edit("Starting new typing contest in **2**...");
await Task.Delay(1000); await Task.Delay(1000);
await msg.Edit("Starting new typing contest in **1**...."); await msg.Edit("Starting new typing contest in **1**...");
await Task.Delay(1000); await Task.Delay(1000);
await msg.Edit($"**{currentSentence}**"); await msg.Edit($"**{currentSentence}**");
sw.Start();
HandleAnswers(); HandleAnswers();
int i = 10; int i = (int)(currentSentence.Length / WORD_VALUE * 1.7f);
while (i > 0) { while (i > 0) {
await Task.Delay(1000); await Task.Delay(1000);
i--; i--;
@ -69,7 +75,7 @@ namespace NadekoBot {
var guess = e.Message.RawText; var guess = e.Message.RawText;
if (currentSentence == guess) { if (currentSentence == guess) {
await channel.Send(e.User.Mention + " finished!"); await channel.Send($"{e.User.Mention} finished in **{sw.Elapsed.Seconds}** seconds, **{ currentSentence.Length / TypingGame.WORD_VALUE / sw.Elapsed.Seconds * 60 }** WPM!");
} }
} }
} }
@ -109,7 +115,6 @@ namespace NadekoBot {
}; };
public override void Init(CommandGroupBuilder cgb) { public override void Init(CommandGroupBuilder cgb) {
/*
cgb.CreateCommand("typestart") cgb.CreateCommand("typestart")
.Description("Starts a typing contest.") .Description("Starts a typing contest.")
.Do(DoFunc()); .Do(DoFunc());
@ -117,7 +122,6 @@ namespace NadekoBot {
cgb.CreateCommand("typestop") cgb.CreateCommand("typestop")
.Description("Stops a typing contest on the current channel.") .Description("Stops a typing contest on the current channel.")
.Do(QuitFunc()); .Do(QuitFunc());
*/
} }
} }
} }

View File

@ -6,6 +6,7 @@ using System.Diagnostics;
using System.Linq; using System.Linq;
using System.Timers; using System.Timers;
using NadekoBot.Extensions; using NadekoBot.Extensions;
using System.Threading.Tasks;
namespace NadekoBot.Modules namespace NadekoBot.Modules
{ {
@ -232,7 +233,14 @@ namespace NadekoBot.Modules
cgb.CreateCommand(".stats") cgb.CreateCommand(".stats")
.Description("Shows some basic stats for nadeko") .Description("Shows some basic stats for nadeko")
.Do(async e => await e.Send("```" + NadekoBot.GetStats() + "\n" + Music.GetMusicStats() + "```")); .Do(async e => {
var t = Task.Run(() => {
return "```" + NadekoBot.GetStats() + "\n" + Music.GetMusicStats() + "```";
});
await e.Send(await t);
});
cgb.CreateCommand(".leaveall") cgb.CreateCommand(".leaveall")
.Description("Nadeko leaves all servers") .Description("Nadeko leaves all servers")