Skip to content

Commit 5a27c5e

Browse files
committed
Added the switch and refresh commands. One caveat is I noticed the switch command seemed to duplicate all the names on the ladder for some reason. Undo fixed it so it can be recovered but I wasnt able to reproduce or see how it could happen. My only hunch is the MMR was being updated at the same time but even that shouldnt break it.
1 parent 3dfba07 commit 5a27c5e

File tree

4 files changed

+136
-8
lines changed

4 files changed

+136
-8
lines changed

Beef/Application.cs

+44-5
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010

1111
namespace Beef {
1212
class Application : ProfileInfoProvider, MmrListener {
13-
private readonly String _version = "1.4.1";
13+
private readonly String _version = "1.5";
1414
private BeefConfig _config;
1515
private String _botPrefix;
1616
private BeefUserConfigManager _userManager;
@@ -355,6 +355,31 @@ private void HandleCommand(SocketMessage userInput) {
355355

356356
MessageChannel(channel, "**" + playerOrRankToRename + "** has been renamed to **" + newName + "**").GetAwaiter().GetResult();
357357
}
358+
} else if (arguments[1] == "switch") {
359+
if (!IsLeader(author)) {
360+
MessageChannel(channel, "You don't have permission to do that.").GetAwaiter().GetResult();
361+
return;
362+
}
363+
364+
String playerOrRank1 = arguments[2];
365+
String playerOrRank2 = arguments[3];
366+
367+
BeefEntry player1 = null;
368+
BeefEntry player2 = null;
369+
code = _presentationManager.SwitchPlayers(playerOrRank1, playerOrRank2, out player1, out player2);
370+
371+
if (code.Ok()) {
372+
MessageChannel(channel, "**" + player1.PlayerName + "** has been switched with **" + player2.PlayerName + "**").GetAwaiter().GetResult();
373+
} else {
374+
if (code == ErrorCode.Player1DoesNotExist) {
375+
MessageChannel(channel, "**" + playerOrRank1 + "** isn't a valid rank or beef name. When was the last time you had your eyes checked?").GetAwaiter().GetResult();
376+
} else if (code == ErrorCode.Player2DoesNotExist) {
377+
MessageChannel(channel, "**" + playerOrRank2 + "** isn't a valid rank or beef name. When was the last time you had your eyes checked?").GetAwaiter().GetResult();
378+
} else {
379+
MessageChannel(channel, "Something went wrong. I'm not sure what but I'm pretty sure it's your fault.").GetAwaiter().GetResult();
380+
}
381+
return;
382+
}
358383
}
359384
} else if (arguments.Length >= 2) {
360385
if (arguments[1] == "version" && arguments.Length == 2) {
@@ -389,8 +414,6 @@ private void HandleCommand(SocketMessage userInput) {
389414
MessageChannel(channel, "Removed **" + playerToRemove + "** from the ladder.").GetAwaiter().GetResult();
390415
}
391416
} else if (arguments[1] == "bracket" || arguments[1] == "list" || arguments[1] == "ladder") {
392-
IsLeader(userInput.Author);
393-
394417
List<BeefEntry> entries = _presentationManager.ReadBracket();
395418

396419
if (entries.Count == 0)
@@ -405,11 +428,25 @@ private void HandleCommand(SocketMessage userInput) {
405428
}
406429
// Send the help message to all if requested. Otherwise
407430
// just DM it to the user that asked.
408-
if (arguments.Length >= 3 && arguments[2] == "all")
431+
if (arguments.Length >= 3 && arguments[2] == "all") {
432+
if (!IsLeader(author)) {
433+
MessageChannel(channel, "You don't have permission to do that.").GetAwaiter().GetResult();
434+
return;
435+
}
436+
409437
MessageChannel(channel, bracket).GetAwaiter().GetResult();
410-
else
438+
} else
411439
MessageUser(userInput.Author, bracket).GetAwaiter().GetResult();
412440
}
441+
} else if (arguments[1].Equals("refresh")) {
442+
if (!IsLeader(author)) {
443+
MessageChannel(channel, "You don't have permission to do that.").GetAwaiter().GetResult();
444+
return;
445+
}
446+
447+
_mmrReader.RequestUpdate();
448+
code = ErrorCode.Success;
449+
MessageChannel(channel, "MMR refresh requested.").GetAwaiter().GetResult();
413450
} else if (arguments[1].Equals("undo")) {
414451
if (!IsLeader(author)) {
415452
MessageChannel(channel, "You don't have permission to do that.").GetAwaiter().GetResult();
@@ -454,6 +491,8 @@ private void HandleCommand(SocketMessage userInput) {
454491
help += "\t **%beef% unregister <PlayerLadderName>** - Unregisters the given ladder name.\n";
455492
help += "\t **%beef% link <PlayerLadderName> <PlayerBattleNetProfileLink>** - Links the given ladder name to the given Battle.net profile link. This will enable their best MMR and race to be displayed on the ladder.\n";
456493
help += "\t **%beef% unlink <PlayerLadderName>** - Unlinks the given ladder name from their associated Battle.net Profile.\n";
494+
help += "\t **%beef% switch <PlayerOrRank> <OtherPlayerOrRank>** - Switches the two players on the ladder leaving everyone else in place.\n";
495+
help += "\t **%beef% refresh** - Requests a refresh to the MMRs for each player. Note that this can take a minute.\n";
457496
help += "\t **%beef% undo** - Undoes the last change to the ladder (renames, wins, etc..).\n";
458497
help += "\t **%beef% version** - Prints the version of BeefBot\n";
459498
help = help.Replace("%beef%", _botPrefix + "beef");

Beef/ErrorCodes.cs

+4
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,10 @@ public enum ErrorCode {
3434
LadderDifferentSize,
3535
CouldNotRevertBackupFile,
3636

37+
// Switching
38+
Player1DoesNotExist,
39+
Player2DoesNotExist,
40+
3741
// Commands
3842
CommandNotRecognized,
3943

Beef/MmrReader/MmrReader.cs

+4-3
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,6 @@ private long GetNowInMs() {
7878
/// <returns>Returns the number of milliseconds until the next refresh or 0 if it should refresh now.</returns>
7979
private long GetTimeToNextRefresh(long nextRefreshTimeMs) {
8080
if (_manualUpdateRequested) {
81-
_manualUpdateRequested = false;
8281
return 0; // Update immediately if requested
8382
}
8483

@@ -383,10 +382,12 @@ private void ReadMmrLoop() {
383382

384383
int timeToNextRefresh = (int)GetTimeToNextRefresh(nextRefreshTimeMs);
385384
lock (_lock) {
386-
if (_manualUpdateRequested)
385+
if (_manualUpdateRequested) {
387386
forceRefresh = true;
388-
else
387+
_manualUpdateRequested = false;
388+
} else {
389389
Monitor.Wait(_lock, timeToNextRefresh);
390+
}
390391
}
391392
}
392393

Beef/PresentationManager.cs

+84
Original file line numberDiff line numberDiff line change
@@ -296,6 +296,63 @@ private ErrorCode RenamePlayer(BeefEntry existingEntry, String newName) {
296296
return UpdatePresentationFromEntries(_entries, true);
297297
}
298298

299+
/// <summary>
300+
/// Switches the 2 players in the beef ladder.
301+
/// </summary>
302+
/// <param name="nameOrRank1">A rank or beef name for the first player.</param>
303+
/// <param name="nameOrRank2">A rank or beef name for the second player.</param>
304+
/// <param name="player1">This is set to the BeefEntry if the first player is found.</param>
305+
/// <param name="player2">This is set to the BeefEntry if the second player is found.</param>
306+
/// <returns>Returns success if it succeeded or an error code identifying what happened.</returns>
307+
public ErrorCode SwitchPlayers(String nameOrRank1, String nameOrRank2, out BeefEntry player1, out BeefEntry player2) {
308+
// Init the out params
309+
player1 = null;
310+
player2 = null;
311+
312+
List<BeefEntry> entries = ReadBracket();
313+
player1 = GetBeefEntryFromNameOrRank(nameOrRank1, entries);
314+
if (player1 == null) {
315+
return ErrorCode.Player1DoesNotExist;
316+
}
317+
318+
player2 = GetBeefEntryFromNameOrRank(nameOrRank2, entries);
319+
if (player2 == null) {
320+
return ErrorCode.Player2DoesNotExist;
321+
}
322+
323+
return SwitchPlayers(player1, player2);
324+
}
325+
326+
private ErrorCode SwitchPlayers(BeefEntry player1, BeefEntry player2) {
327+
// Assumes the beef ladder has already been updated
328+
/*
329+
List<BeefEntry> newList = new List<BeefEntry>(_entries.Count);
330+
for (int i = 0; i < _entries.Count; i++) {
331+
if (i == (player1.PlayerRank - 1)) {
332+
newList.Add(player2);
333+
} else if (i == (player2.PlayerRank - 1)) {
334+
newList.Add(player1);
335+
} else {
336+
newList.Add(_entries[i]);
337+
}
338+
}
339+
340+
int player1PrevRank = player1.PlayerRank;
341+
String player1PrevObjectId = player1.
342+
player1.PlayerRank = player2.PlayerRank;
343+
player2.PlayerRank = player1PrevRank;
344+
345+
return UpdatePresentationFromEntries(newList, true);
346+
*/
347+
348+
String player1Name = player1.PlayerName;
349+
String player2Name = player2.PlayerName;
350+
player1.PlayerName = player2Name;
351+
player2.PlayerName = player1Name;
352+
353+
return UpdatePresentationFromEntries(_entries, true);
354+
}
355+
299356
/// <summary>
300357
/// Removes the player from the ladder with the given name and shuffles everyone else down.
301358
/// </summary>
@@ -654,5 +711,32 @@ private String GetStringFromElements(IList<TextElement> elements) {
654711

655712
return result;
656713
}
714+
715+
/// <summary>
716+
/// Gets the BeefEntry corresponding to the given name or rank.
717+
/// </summary>
718+
/// <param name="nameOrRank">A Beef Name or rank on the ladder (RANK not INDEX)</param>
719+
/// <param name="entries">The list of current entries to check</param>
720+
/// <returns>Returns the entry that was found or null</returns>
721+
private BeefEntry GetBeefEntryFromNameOrRank(String nameOrRank, List<BeefEntry> entries) {
722+
int rank;
723+
if (!int.TryParse(nameOrRank, out rank)) rank = -1;
724+
725+
if (rank == -1) {
726+
foreach (BeefEntry entry in entries) {
727+
if (entry.PlayerName.Equals(nameOrRank)) {
728+
return entry;
729+
}
730+
}
731+
return null; // Not found
732+
} else {
733+
int index = rank - 1;
734+
if (index < 0 || index >= entries.Count) {
735+
return null;
736+
}
737+
738+
return entries[index];
739+
}
740+
}
657741
}
658742
}

0 commit comments

Comments
 (0)