Skip to content

Commit ac394ce

Browse files
committed
Added the remove command and cleaned up a few others.
1 parent 7b41791 commit ac394ce

File tree

3 files changed

+174
-7
lines changed

3 files changed

+174
-7
lines changed

Beef/Application.cs

+57-4
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77

88
namespace Beef {
99
class Application {
10+
private readonly String _version = "1.1";
1011
private BeefConfig _config;
1112
private String _botPrefix;
1213
private PresentationManager _manager;
@@ -128,12 +129,61 @@ private void HandleCommand(SocketMessage userInput) {
128129
return;
129130
}
130131

131-
code = _manager.RenamePlayer(arguments[2], arguments[3]);
132+
String playerOrRankToRename = arguments[2];
133+
String newName = arguments[3];
134+
int rank;
135+
if (!int.TryParse(playerOrRankToRename, out rank)) rank = -1;
136+
137+
if (rank != -1) {
138+
BeefEntry existingPlayer;
139+
code = _manager.RenamePlayer(rank - 1, newName, out existingPlayer);
140+
141+
if (existingPlayer != null) {
142+
playerOrRankToRename = existingPlayer.PlayerName;
143+
if (String.IsNullOrEmpty(playerOrRankToRename)) {
144+
playerOrRankToRename = "Rank " + rank;
145+
}
146+
}
147+
} else {
148+
code = _manager.RenamePlayer(playerOrRankToRename, newName);
149+
}
150+
132151
if (code.Ok())
133-
MessageChannel(channel, "**" + arguments[2] + "** has been renamed to **" + arguments[3] + "**").GetAwaiter().GetResult();
152+
MessageChannel(channel, "**" + playerOrRankToRename + "** has been renamed to **" + newName + "**").GetAwaiter().GetResult();
134153
}
135154
} else if (arguments.Length >= 2) {
136-
if (arguments[1] == "bracket" || arguments[1] == "list" || arguments[1] == "ladder") {
155+
if (arguments[1] == "version" && arguments.Length == 2) {
156+
MessageChannel(channel, "BeefBot version " + _version).GetAwaiter().GetResult();
157+
return;
158+
} else if (arguments[1] == "remove") {
159+
if (!IsLeader(author)) {
160+
MessageChannel(channel, "You don't have permission to do that.").GetAwaiter().GetResult();
161+
return;
162+
}
163+
164+
if (arguments.Length != 3) {
165+
MessageChannel(channel, "Read the instructions for once in your life, that's not how you use this command.").GetAwaiter().GetResult();
166+
return;
167+
}
168+
169+
String playerToRemove = arguments[2];
170+
int rank;
171+
if (!int.TryParse(playerToRemove, out rank)) rank = -1;
172+
173+
if (rank != -1) {
174+
BeefEntry removedPlayerEntry;
175+
code = _manager.RemovePlayer(rank - 1, out removedPlayerEntry);
176+
177+
if (code.Ok())
178+
playerToRemove = removedPlayerEntry.PlayerName;
179+
} else {
180+
code = _manager.RemovePlayer(playerToRemove);
181+
}
182+
183+
if (code.Ok()) {
184+
MessageChannel(channel, "Removed " + playerToRemove + " from the ladder.").GetAwaiter().GetResult();
185+
}
186+
} else if (arguments[1] == "bracket" || arguments[1] == "list" || arguments[1] == "ladder") {
137187
IsLeader(userInput.Author);
138188

139189
List<BeefEntry> entries = _manager.ReadBracket();
@@ -177,6 +227,7 @@ private void HandleCommand(SocketMessage userInput) {
177227

178228
String help = "";
179229
help += "The Beef ladder is maintained on Google Docs as a Slide presentation. This bot makes it more convenient to update it. Each Beef command is prefixed with \"%beef%\".\n";
230+
help += "Note, commands that have parameters in them surrounded in [] means that parameter is optional. As an example \"all\" is a common suffix you can add to make a command print to the chat instead of whispering the response to you.\n";
180231
help += "The following commands are available:\n";
181232
help += "\t **%beef%** - Prints the link to the ladder.\n";
182233
help += "\t **%beef% help [all]** - Prints this message to the user who typed it or the channel if [all] is specified.\n";
@@ -191,7 +242,9 @@ private void HandleCommand(SocketMessage userInput) {
191242
help += "\t\t\t\t **%beef% bum beat GamerRichy**. -- Will put bum in rank 1, GamerRichy in rank 2, and shuffle everyone else accordingly.\n";
192243
help += "\t **%beef% _<WinningPlayerOrRank>_ beats _<LosingPlayerOrRank>_**. - Same as %beef% X beat Y (It accepts beats and beat)\n";
193244
help += "\t **%beef% rename _<OldPlayerName>_ _<NewPlayerName>_**. - Renames a player on the ladder to the new name.\n";
245+
help += "\t **%beef% remove _<PlayerOrRank>_**. - Removes the given player or rank from the ladder..\n";
194246
help += "\t **%beef% undo**. - Undoes the last change to the ladder (renames, wins, etc..).\n";
247+
help += "\t **%beef% version**. - Prints the version of BeefBot\n";
195248
help = help.Replace("%beef%", _botPrefix + "beef");
196249

197250
// Send the help message to all if requested. Otherwise
@@ -204,7 +257,7 @@ private void HandleCommand(SocketMessage userInput) {
204257
}
205258

206259
if (!code.Ok()) {
207-
MessageErrorToChannel(channel, code);
260+
MessageErrorToChannel(channel, code).GetAwaiter().GetResult();
208261
}
209262
} // end beef
210263
}

Beef/ErrorCodes.cs

+6-1
Original file line numberDiff line numberDiff line change
@@ -22,9 +22,12 @@ public enum ErrorCode {
2222
YouCantBeatYourself,
2323
LoserIsNotOnTheLadder,
2424

25-
// Renaming
25+
// Renaming/Removing
2626
NoExistingPlayerByThatName,
2727

28+
// Removing
29+
RankNotOnLadder,
30+
2831
// Undo
2932
NothingToUndo,
3033
LadderDifferentSize,
@@ -63,6 +66,8 @@ public static String GetUserMessage(this ErrorCode code) {
6366
return "That loser isn't even on the ladder. Is this the level of proficiency you have in other things in life too?";
6467
case ErrorCode.NoExistingPlayerByThatName:
6568
return "How can I rename a player that doesn't exist? Seriously, check the ladder. That name's not on it.";
69+
case ErrorCode.RankNotOnLadder:
70+
return "Someone can't read numbers. Pick a rank that actually is on the ladder.";
6671
case ErrorCode.NothingToUndo:
6772
return "You haven't done anything yet wtf!?";
6873
case ErrorCode.LadderDifferentSize:

Beef/PresentationManager.cs

+111-2
Original file line numberDiff line numberDiff line change
@@ -235,9 +235,118 @@ public ErrorCode RenamePlayer(String oldName, String newName) {
235235
if (existingPlayerEntry == null)
236236
return ErrorCode.NoExistingPlayerByThatName; // Player not found
237237

238+
return RenamePlayer(existingPlayerEntry, newName);
239+
}
240+
241+
/// <summary>
242+
/// Renames the player at the given index with the given name.
243+
/// </summary>
244+
/// <param name="index">The index of the player (the rank - 1).</param>
245+
/// <param name="newName">The new name for the player.</param>
246+
/// <param name="existingPlayer">Set to the existing player if there wasn't an error. Set to null if there was an error.</param>
247+
/// <returns>Returns Success or an error code for what went wrong.</returns>
248+
public ErrorCode RenamePlayer(int index, String newName, out BeefEntry existingPlayer) {
249+
existingPlayer = null;
250+
List<BeefEntry> entries = ReadBracket();
251+
if (entries.Count == 0)
252+
return ErrorCode.CouldNotReadTheLadder; // There was an error reading the bracket.
253+
254+
if (index <= 0 || index >= entries.Count) {
255+
return ErrorCode.RankNotOnLadder;
256+
}
257+
258+
BeefEntry playerToRename = entries[index];
259+
ErrorCode result = RenamePlayer(playerToRename, newName);
260+
261+
if (result.Ok())
262+
existingPlayer = playerToRename;
263+
264+
return result;
265+
}
266+
267+
/// <summary>
268+
/// Renames the given entry to the new name.
269+
/// </summary>
270+
/// <param name="existingEntry">The existing entry in the ladder.</param>
271+
/// <param name="newName">The new name to give the player</param>
272+
/// <returns>Returns Success or what went wrong if it failed.</returns>
273+
private ErrorCode RenamePlayer(BeefEntry existingEntry, String newName) {
238274
// Remove the existing and add the new one
239-
AddDeleteRequest(existingPlayerEntry);
240-
AddInsertRequest(existingPlayerEntry, newName);
275+
AddDeleteRequest(existingEntry);
276+
AddInsertRequest(existingEntry, newName);
277+
278+
return SubmitRequests();
279+
}
280+
281+
/// <summary>
282+
/// Removes the player from the ladder with the given name and shuffles everyone else down.
283+
/// </summary>
284+
/// <param name="name">The name of the player to remove.</param>
285+
/// <returns>Returns Success if it was successful or the error code if there was an issue.</returns>
286+
public ErrorCode RemovePlayer(String name) {
287+
List<BeefEntry> entries = ReadBracket();
288+
if (entries.Count == 0)
289+
return ErrorCode.CouldNotReadTheLadder; // There was an error reading the bracket.
290+
291+
// Get the object ID for this player
292+
BeefEntry playerToRemove = null;
293+
for (int i = 0; i < entries.Count; i++) {
294+
BeefEntry entry = entries[i];
295+
if (name.Equals(entry.PlayerName)) {
296+
playerToRemove = entry;
297+
break;
298+
}
299+
}
300+
301+
if (playerToRemove == null) {
302+
return ErrorCode.NoExistingPlayerByThatName;
303+
}
304+
305+
return RemovePlayer(entries, playerToRemove);
306+
}
307+
308+
/// <summary>
309+
/// Removes the player at the given index.
310+
/// </summary>
311+
/// <param name="index">The index of the player to remove. Note this is the INDEX not the RANK.</param>
312+
/// <param name="playerToRemove">This is set to the player being removed if successful and null otherwise.</param>
313+
/// <returns>Returns Success if it was successful or the error code if there was an issue.</returns>
314+
public ErrorCode RemovePlayer(int index, out BeefEntry playerToRemove) {
315+
playerToRemove = null;
316+
List<BeefEntry> entries = ReadBracket();
317+
if (entries.Count == 0)
318+
return ErrorCode.CouldNotReadTheLadder; // There was an error reading the bracket.
319+
320+
if (index <= 0 || index >= entries.Count) {
321+
return ErrorCode.RankNotOnLadder;
322+
}
323+
324+
playerToRemove = entries[index];
325+
ErrorCode result = RemovePlayer(entries, playerToRemove);
326+
if (result.Ok()) {
327+
playerToRemove = entries[index];
328+
}
329+
return result;
330+
}
331+
332+
/// <summary>
333+
/// Builds the list of requests to remove the given BeefEntry (player) and submits it.
334+
/// </summary>
335+
/// <param name="entries">The list of entries</param>
336+
/// <param name="playerToRemove">The player to remove</param>
337+
/// <returns>Returns Success if it worked or the error code if it didn't.</returns>
338+
private ErrorCode RemovePlayer(List<BeefEntry> entries, BeefEntry playerToRemove) {
339+
// Add a delete request for each player and insert the player one rank above
340+
// so it shuffles everyone up the bracket leaving the last one black.
341+
for (int index = playerToRemove.PlayerRank - 1; index < entries.Count; index++) {
342+
AddDeleteRequest(entries[index]);
343+
344+
if (index + 1 < entries.Count) {
345+
AddInsertRequest(entries[index], entries[index + 1].PlayerName);
346+
} else {
347+
AddInsertRequest(entries[index], "");
348+
}
349+
}
241350

242351
return SubmitRequests();
243352
}

0 commit comments

Comments
 (0)