diff options
author | TrueDoctor <d-kobert@web.de> | 2018-04-09 14:13:28 +0200 |
---|---|---|
committer | TrueDoctor <d-kobert@web.de> | 2018-04-09 14:13:28 +0200 |
commit | 5cb8346a6a3ccc55295c00ac333e410629ff7c42 (patch) | |
tree | 1603fdf24f1ead61cf7dd23c09a9784cfb1c780e /DiscoBot/Auxiliary | |
parent | e49ac2778bb6d38517f14447c0675df354552528 (diff) | |
parent | 4ca7291ebee440d9f3ef2a1643d8d01b29006459 (diff) |
Merge branch 'Reset'
Diffstat (limited to 'DiscoBot/Auxiliary')
-rw-r--r-- | DiscoBot/Auxiliary/CommandExtension.cs | 46 | ||||
-rw-r--r-- | DiscoBot/Auxiliary/Dice.cs | 12 | ||||
-rw-r--r-- | DiscoBot/Auxiliary/KampfTalent.cs | 18 | ||||
-rw-r--r-- | DiscoBot/Auxiliary/Misc.cs | 51 | ||||
-rw-r--r-- | DiscoBot/Auxiliary/SpellCorrect.cs | 134 | ||||
-rw-r--r-- | DiscoBot/Auxiliary/Talent.cs | 37 | ||||
-rw-r--r-- | DiscoBot/Auxiliary/Vorteil.cs | 15 |
7 files changed, 313 insertions, 0 deletions
diff --git a/DiscoBot/Auxiliary/CommandExtension.cs b/DiscoBot/Auxiliary/CommandExtension.cs new file mode 100644 index 0000000..6690d03 --- /dev/null +++ b/DiscoBot/Auxiliary/CommandExtension.cs @@ -0,0 +1,46 @@ +namespace DiscoBot.Auxiliary +{ + using System; + using System.Collections.Generic; + using System.ComponentModel; + using System.Linq; + using System.Threading; + using System.Threading.Tasks; + + using Discord; + using Discord.Commands; + + public static class CommandExtension + { + public static async Task ReplyTimedAsync(this ModuleBase m, string message, TimeSpan time) + { + var token = message.GetHashCode(); + var send = m.Context.Channel.SendMessageAsync($"#{token}\n```xl\n{message}```", true); + + var barInvoker = new BackgroundWorker(); + barInvoker.DoWork += delegate + { + Thread.Sleep(time); + Delete(token, m); + }; + + await send; + barInvoker.RunWorkerAsync(); + } + + private static void Delete(int token, ModuleBase m) + { + var messagesAsync = m.Context.Channel.GetMessagesAsync(); + Task.WaitAll(messagesAsync.ToArray()); + var list = messagesAsync.ToEnumerable().ToList(); + var messages = new List<IMessage>(); + foreach (var task in list) + { + messages.AddRange(task.ToList()); + } + + m.Context.Channel.DeleteMessagesAsync( + messages.Where(x => x.Content.StartsWith($"#{token}\n") && x.Author.IsBot)); + } + } +} diff --git a/DiscoBot/Auxiliary/Dice.cs b/DiscoBot/Auxiliary/Dice.cs new file mode 100644 index 0000000..16a8a77 --- /dev/null +++ b/DiscoBot/Auxiliary/Dice.cs @@ -0,0 +1,12 @@ +namespace DiscoBot.Auxiliary +{ + public static class Dice // roll it! + { + private static readonly System.Random Rnd = new System.Random(); + + public static int Roll(int d = 20) + { + return Rnd.Next(d) + 1; + } + } +} diff --git a/DiscoBot/Auxiliary/KampfTalent.cs b/DiscoBot/Auxiliary/KampfTalent.cs new file mode 100644 index 0000000..05b7c9e --- /dev/null +++ b/DiscoBot/Auxiliary/KampfTalent.cs @@ -0,0 +1,18 @@ +namespace DiscoBot.Auxiliary +{ + public class KampfTalent + { + public KampfTalent(string name, int at, int pa) + { + this.Name = name; + this.At = at; + this.Pa = pa; + } + + public string Name { get; set; } + + public int At { get; set; } + + public int Pa { get; set; } + } +} diff --git a/DiscoBot/Auxiliary/Misc.cs b/DiscoBot/Auxiliary/Misc.cs new file mode 100644 index 0000000..2531f12 --- /dev/null +++ b/DiscoBot/Auxiliary/Misc.cs @@ -0,0 +1,51 @@ +namespace DiscoBot.Auxiliary +{ + using System; + using System.Linq; + using System.Text; + + public static class Misc + { + private static readonly Random Rand = new Random(); + + // use: 4w6 +4 + public static string Roll(string input) + { + var output = new StringBuilder(); + var strings = input.Split('w', 'd').ToList(); + int count = Convert.ToInt32(strings[0]); + strings = strings[1].Split(' ').ToList(); + int d = Convert.ToInt32(strings[0]); + + if (strings.Count > 0) + { + } + + int sum = 0; + for (int i = 0; i < count; i++) + { + var roll = Dice.Roll(d); + sum += roll; + output.Append("[" + roll + "] "); + } + + if (count > 1) + { + output.Append("sum: " + sum); + } + + return output.ToString(); + } + + public static double Random(double stdDev = 1, double mean = 0) + { + double u1 = Rand.NextDouble(); // uniform(0,1) random doubles + double u2 = Rand.NextDouble(); + double randStdNormal = Math.Sqrt(-2.0 * Math.Log(u1)) * + Math.Sin(2.0 * Math.PI * u2); // random normal(0,1) + double randNormal = + mean + stdDev * randStdNormal; // random normal(mean,stdDev^2) + return randNormal; + } + } +} diff --git a/DiscoBot/Auxiliary/SpellCorrect.cs b/DiscoBot/Auxiliary/SpellCorrect.cs new file mode 100644 index 0000000..8c5741b --- /dev/null +++ b/DiscoBot/Auxiliary/SpellCorrect.cs @@ -0,0 +1,134 @@ +namespace DiscoBot.Auxiliary +{ + using System; + using System.Diagnostics; + using System.Linq; + + public class SpellCorrect : StringComparer + { + public override int Compare(string x, string y) + { + if (string.IsNullOrEmpty(x)) + { + throw new ArgumentException("message", nameof(x)); + } + + if (string.IsNullOrEmpty(y)) + { + throw new ArgumentException("message", nameof(y)); + } + + if (x.Equals(y)) + { + return 0; + } + + x = x.ToLower(); + y = y.ToLower(); + if (x.Equals(y)) + { + return 1; + } + + var subs = y.Split(' ', '/'); + int score = subs.Count(); + foreach (string s in subs) + { + if (s.Equals(x)) + { + score--; + } + } + + if (score < subs.Count()) + { + return score + 1; + } + + return 100000 - (int)(this.CompareExact(x, y) * 1000.0); + /*if (y.Contains(x)) + return 6;*/ + } + + public override bool Equals(string x, string y) + { + Debug.Assert(x != null, nameof(x) + " != null"); + return x.Equals(y); + } + + public override int GetHashCode(string obj) + { + throw new NotImplementedException(); + } + + public double CompareExact(string s, string q) + { + int i, j; + const double Match = 3.0; + const double Gap = -2.0; + const double Mismatch = -2.0; + + double decay; + + double[,] matrix = new double[s.Length + 1, q.Length + 1]; + double max = 0.0; + matrix[0, 0] = 0.0; + + for (i = 1; i < s.Length; i++) + { + matrix[i, 0] = 0.0; + } + + for (i = 1; i < q.Length; i++) + { + matrix[0, i] = 0.0; + } + + for (i = 1; i <= s.Length; i++) + { + for (j = 1; j <= q.Length; j++) + { + decay = j / (double)(s.Length * 1000); + double add = s[i - 1] == q[j - 1] ? (Match - decay) : Mismatch; + double score = matrix[i - 1, j - 1] + add; + + if (score < (matrix[i - 1, j] + Gap)) + { + score = matrix[i - 1, j] + Gap; + } + + if (score < (matrix[i, j - 1] + Gap)) + { + score = matrix[i, j - 1] + Gap; + } + + if (i > 1 && j > 1) + { + if (s[i - 1] == q[j - 2] && s[i - 2] == q[j - 1]) + { + add = (3 / 2.0) * Match - decay; + if (score < matrix[i - 2, j - 2] + add) + { + score = matrix[i - 2, j - 2] + add; + } + } + } + + if (score < 0) + { + score = 0; + } + + if (max < score) + { + max = score; + } + + matrix[i, j] = score; + } + } + + return max; + } + } +} diff --git a/DiscoBot/Auxiliary/Talent.cs b/DiscoBot/Auxiliary/Talent.cs new file mode 100644 index 0000000..969304c --- /dev/null +++ b/DiscoBot/Auxiliary/Talent.cs @@ -0,0 +1,37 @@ +namespace DiscoBot.Auxiliary +{ + using System; + + public class Talent // talent objekt + { + public Talent(string name, string probe, int value) + { + this.Name = name; + this.Probe = probe; + this.Value = value; + } + + public string Name { get; set; } + + public string Probe { get; set; } + + public int Value { get; set; } + + public string[] Test() // turn XX/XX/XX into string[]{XX,XX,XX} + { + var temp = this.Probe.Split('/'); + for (var index = 0; index < temp.Length; index++) + { + temp[index] = temp[index].Replace("/", string.Empty); + } + + return temp; + } + + public int CheckName(string quarry) + { + var sc = (StringComparer)new SpellCorrect(); + return sc.Compare(quarry, this.Name); + } + } +} diff --git a/DiscoBot/Auxiliary/Vorteil.cs b/DiscoBot/Auxiliary/Vorteil.cs new file mode 100644 index 0000000..823305c --- /dev/null +++ b/DiscoBot/Auxiliary/Vorteil.cs @@ -0,0 +1,15 @@ +namespace DiscoBot.Auxiliary +{ + public class Vorteil // talent objekt + { + public Vorteil(string name, int value = 0) + { + this.Name = name; + this.Value = value; + } + + public string Name { get; set; } + + public int Value { get; set; } + } +} |