This repository has been archived by the owner on May 2, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Dice.cs
122 lines (107 loc) · 4 KB
/
Dice.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
using System.Linq;
using System.Threading.Tasks;
using System.Collections.Generic;
using Discord.WebSocket;
using System;
namespace csharp
{
class Dice : IDisposable
{
public static readonly Dice instance = new Dice();
private readonly DiceExpression die;
private Dice()
{
die = new DiceExpression();
}
public async Task RollDice(SocketMessage message, string[] args)
{
if (args.Length < 2)
{
await message.Channel.SendMessageAsync("You must specify a number of dice to roll");
return;
}
var joinedArgs = (new ArraySegment<string>(args, 1, args.Length - 1));
var toRoll = string.Join(' ', joinedArgs).ToLower();
List<long> totalRolls = new List<long>();
long result = die.R(toRoll, ref totalRolls);
string eachRoll = string.Join(", ", totalRolls.Select(x => x.ToString()).ToArray());
if (eachRoll.Length > 1900)
{
eachRoll = "There were too many die rolls to show the result of each one";
}
await message.Channel.SendMessageAsync(string.Format("{0}, you rolled: {1}, ({2})", message.Author.Mention, result, eachRoll));
}
public uint GetRandomNum()
{
return die.GetRandNum();
}
public async Task RollStats(SocketMessage message, string[] args = null)
{
long[] rolls = new long[4];
long[] stats = new long[6];
var ignoreList = new List<long>();
for (int i = 0; i < 6; i++)
{
ignoreList.Clear();
for (int j = 0; j < 4; j++)
{
rolls[j] = die.R("1d6", ref ignoreList);
}
int toSkip = FindIndexOfSmallest(ref rolls);
long sum = 0;
for (int j = 0; j < 4; j++)
{
if (j == toSkip) continue;
sum += rolls[j];
}
stats[i] = sum;
}
await message.Channel.SendMessageAsync(
string.Format("{0}, 4d6 drop the lowest:\n{1}",
message.Author.Mention,
string.Join(", ", stats.Select(x => x.ToString()).ToArray())));
}
private int FindIndexOfSmallest<T>(ref T[] toCheck) where T : IComparable<T>
{
int index = 0;
for (int i = 1; i < toCheck.Length; i++)
{
//if (toCheck[i] < toCheck[index])
if (toCheck[i].CompareTo(toCheck[index]) < 0)
index = i;
}
return index;
}
#region IDisposable Support
private bool disposedValue = false; // To detect redundant calls
protected virtual void Dispose(bool disposing)
{
if (!disposedValue)
{
if (disposing)
{
die.Dispose();
// TODO: dispose managed state (managed objects).
}
// TODO: free unmanaged resources (unmanaged objects) and override a finalizer below.
// TODO: set large fields to null.
disposedValue = true;
}
}
// TODO: override a finalizer only if Dispose(bool disposing) above has code to free unmanaged resources.
~Dice()
{
// Do not change this code. Put cleanup code in Dispose(bool disposing) above.
Dispose(false);
}
// This code added to correctly implement the disposable pattern.
public void Dispose()
{
// Do not change this code. Put cleanup code in Dispose(bool disposing) above.
Dispose(true);
// TODO: uncomment the following line if the finalizer is overridden above.
GC.SuppressFinalize(this);
}
#endregion
}
}