Skip to content

Commit c1e6c99

Browse files
committed
Version 1.6. This update fixes a crash that can occur if authentication with the Google presentation API fails. It also adds support for caching the previously retrieved MMR/league/race information from the Blizzard API so that if it goes down, BeefBot remembers the last values and can still display them.
1 parent 301e411 commit c1e6c99

File tree

3 files changed

+50
-4
lines changed

3 files changed

+50
-4
lines changed

Beef/Application.cs

+22-2
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.5";
13+
private readonly String _version = "1.6";
1414
private BeefConfig _config;
1515
private String _botPrefix;
1616
private BeefUserConfigManager _userManager;
@@ -694,8 +694,28 @@ public void OnMmrRead(List<Tuple<ProfileInfo, LadderInfo>> mmrList) {
694694
if (user == null)
695695
continue;
696696

697+
// If the LadderInfo for a user is null, we failed to update their information.
698+
// In this case, use the last known values so they are still displayed.
699+
Tuple<ProfileInfo, LadderInfo> mmrDictionaryEntry = entry;
700+
if (entry.Item2 == null) {
701+
// Also make sure we have a previous MMR to show
702+
// If there isn't, leave it null so it displays correctly.
703+
if (!String.IsNullOrEmpty(user.LastKnownMmr)) {
704+
LadderInfo previousInfo = new LadderInfo();
705+
previousInfo.League = user.LastKnownLeague;
706+
previousInfo.Mmr = user.LastKnownMmr;
707+
previousInfo.Race = user.LastKnownMainRace;
708+
709+
mmrDictionaryEntry = new Tuple<ProfileInfo, LadderInfo>(entry.Item1, previousInfo);
710+
}
711+
} else {
712+
// If the update was successful, record it for later too.
713+
// (Note: Should this be done on the MMR reading thread? Seems silly to block the main context for this.)
714+
_userManager.UpdateUserLadderInfo(user.BeefName, entry.Item2);
715+
}
716+
697717
if (!profileIdToMaxMmrDict.ContainsKey(user.BeefName))
698-
profileIdToMaxMmrDict.Add(user.BeefName, entry);
718+
profileIdToMaxMmrDict.Add(user.BeefName, mmrDictionaryEntry);
699719
else
700720
Console.WriteLine("Warning: User " + user.BeefName + " already exists in the profileIdToMaxMmrDict!");
701721
}

Beef/BeefUserConfig.cs

+9-1
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ namespace Beef {
77
/// </summary>
88
class BeefUserConfig {
99
// This is the current version and should always be incremented when changing the config file format.
10-
public static int BeefUserVersion = 0;
10+
public static int BeefUserVersion = 1;
1111

1212
// This is the persisted version so we know if it's not the latest when we start up.
1313
public int Version = BeefUserVersion;
@@ -16,6 +16,11 @@ class BeefUserConfig {
1616
public String BeefName = ""; // This is the name that is displayed on the Beef ladder. It must be unique on the ladder.
1717
public String DiscordName = ""; // This is the discord name including the #1234 tag.
1818

19+
// Save previous MMR information in case the servers go down
20+
public String LastKnownMmr = "";
21+
public String LastKnownMainRace = "";
22+
public String LastKnownLeague = "";
23+
1924
public ProfileInfo ProfileInfo = null; // This is information about their profile so we can update their MMR.
2025

2126
public BeefUserConfig(String beefName, String discordName) {
@@ -26,6 +31,9 @@ public BeefUserConfig(String beefName, String discordName) {
2631
public BeefUserConfig(BeefUserConfig other) {
2732
BeefName = other.BeefName;
2833
DiscordName = other.DiscordName;
34+
LastKnownMmr = other.LastKnownMmr;
35+
LastKnownMainRace = other.LastKnownMainRace;
36+
LastKnownLeague = other.LastKnownLeague;
2937

3038
if (other.ProfileInfo != null)
3139
ProfileInfo = new ProfileInfo(other.ProfileInfo);

Beef/BeefUserConfigManager.cs

+19-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
using System;
1+
using Beef.MmrReader;
2+
using System;
23
using System.Collections.Generic;
34
using System.IO;
45
using System.Text.RegularExpressions;
@@ -126,6 +127,23 @@ public BeefUserConfig GetUserByDiscordId(String discordId) {
126127
}
127128
}
128129

130+
/// <summary>
131+
/// Updates the last known ladder information for a given beef user.
132+
/// </summary>
133+
/// <param name="beefName">The user's beef name.</param>
134+
/// <param name="ladderInfo">The ladder info for the user.</param>
135+
/// <returns>Returns ErrorCode.Success if it succeeded, otherwise an error indicating the problem.</returns>
136+
public ErrorCode UpdateUserLadderInfo(String beefName, LadderInfo ladderInfo) {
137+
lock (_userConfigsLock) {
138+
BeefUserConfig user = _userNameToBeefConfigMap[beefName];
139+
user.LastKnownLeague = ladderInfo.League;
140+
user.LastKnownMmr = ladderInfo.Mmr;
141+
user.LastKnownMainRace = ladderInfo.Race;
142+
ErrorCode error = WriteBeefUserToFile(user, GetBeefUserFilePath(user.BeefName));
143+
return error;
144+
}
145+
}
146+
129147
/// <summary>
130148
/// Links the given beef name to the given battle.net account.
131149
/// </summary>

0 commit comments

Comments
 (0)