-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathGNALib.cs
222 lines (172 loc) · 6.16 KB
/
GNALib.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
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
using BattleBitAPI.Common;
using BBRAPIModules;
using System.Text.RegularExpressions;
using System.Threading.Tasks;
using System.Collections.Generic;
using System.Linq;
using System;
namespace BBRModules
{
[Module("GNA\'s core library.", "1.0.0")]
public class GNALib : BattleBitModule
{
public short GetPlayerCountInRoleOnTeam(RunnerServer server, Team team, GameRole role)
{
int index = 0;
short count = 0;
IEnumerable<RunnerPlayer> teamPlayers = team == Team.TeamA ? server.AllTeamAPlayers : server.AllTeamBPlayers;
do
{
RunnerPlayer player = teamPlayers.ElementAt(index);
if (player.Role == role)
count++;
index++;
} while (index < teamPlayers.Count());
return count;
}
public List<RunnerPlayer> GetPlayersInTeam(RunnerServer server, Team team)
{
return team == Team.TeamA ? server.AllTeamAPlayers.ToList() : server.AllTeamBPlayers.ToList();
}
public string GetTimestringAsUnitResult(string timestring)
{
Dictionary<string, string> conversions = new Dictionary<string, string> {
{ "y", "year" },
{ "mo", "month" },
{ "w", "week" },
{ "d", "day" },
{ "h", "hour" },
{ "m", "minute" }
};
string unitResult = "";
bool isNumber = int.TryParse(timestring, out int secondsTime);
bool valid = true;
if (isNumber)
return secondsTime + " seconds";
MatchCollection collection = Regex.Matches(timestring, "(\\d+)([A-Za-z]+)");
int i = 0;
foreach (Match match in collection)
{
valid = int.TryParse(match.Groups[1].Value, out int num);
string time = match.Groups[2].Value;
if (!conversions.ContainsKey(time.ToLower()))
valid = false;
if (!valid)
return "";
bool result = conversions.TryGetValue(time, out string unit);
if (result)
{
if (num != 1)
unit += "s";
string beginning = "";
string end = "";
if (collection.Count > i + 1)
{
end += " ";
}
else
{
beginning = "and ";
}
unitResult += beginning + num + " " + unit + end;
}
i++;
}
return unitResult;
}
public int GetTimestringAsSeconds(string timestring)
{
bool valid = false;
int total = 0;
Dictionary<string, int> conversions = new Dictionary<string, int> {
{ "y", 31556952 },
{ "mo", 2629746 },
{ "w", 604800 },
{ "d", 86400 },
{ "h", 3600 },
{ "m", 60 }
};
bool isNumber = int.TryParse(timestring, out int secondsTime);
if (isNumber)
return secondsTime;
foreach (Match match in Regex.Matches(timestring, "(\\d+)([A-Za-z]+)"))
{
valid = int.TryParse(match.Groups[1].Value, out int num);
string time = match.Groups[2].Value;
if (!conversions.ContainsKey(time.ToLower()))
valid = false;
if (!valid)
return -1;
conversions.TryGetValue(time, out int seconds);
total += seconds * num;
}
return total;
}
public string GetSecondsAsTimeUnit(int seconds)
{
string timeunit = "Never";
Dictionary<string, int> conversions = new Dictionary<string, int> {
{ "y", 31556952 },
{ "mo", 2629746 },
{ "w", 604800 },
{ "d", 86400 },
{ "h", 3600 },
{ "m", 60 }
};
if (seconds != -1)
{
timeunit = "";
int remaining = seconds;
int quantity = 0;
for (int i = 0; i < conversions.Count; i++)
{
KeyValuePair<string, int> pair = conversions.ElementAt(i);
if (remaining == 0)
break;
if (pair.Value > remaining)
continue;
if (remaining / pair.Value >= 1)
{
quantity = (int)Math.Floor((decimal)remaining / pair.Value);
remaining -= quantity * pair.Value;
}
timeunit += $"{quantity}{pair.Key}";
}
}
return GetTimestringAsUnitResult(timeunit);
}
public List<string> GetAfter(string[] split, int index)
{
List<string> newSplit = new(split);
newSplit.RemoveRange(0, index - 1);
return newSplit;
}
public string Join(string[] strings, string separator)
{
string result = "";
for (int i = 0; i < strings.Length; i++)
{
if (i == strings.Length - 1)
separator = "";
string value = strings[i];
result += value + separator;
}
return result;
}
public string Join(string[] strings, string separator, int fromIndex)
{
string result = "";
for (int i = 0; i < strings.Length; i++)
{
if (i >= fromIndex)
{
if (i == strings.Length - 1)
separator = "";
string value = strings[i];
result += value + separator;
}
}
return result;
}
}
}