Skip to content

Commit

Permalink
Bug fix
Browse files Browse the repository at this point in the history
  • Loading branch information
qubqub committed Nov 3, 2023
1 parent c4454f1 commit fc5e68d
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 14 deletions.
8 changes: 6 additions & 2 deletions Entities/LogFileWatcher.cs
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ public class LogFileWatcher {
private bool useShareCode;
private string sessionId;

// private readonly object lockObject = new object();
private readonly object lockObject = new object();
private bool toggleCountryInfoApi;
private bool toggleFgdbCreativeApi;
private string creativeShareCode;
Expand Down Expand Up @@ -726,7 +726,11 @@ private bool ParseLine(LogLine line, List<RoundInfo> round, LogRound logRound) {

if ((DateTime.UtcNow - Stats.ConnectedToServerDate).TotalMinutes <= 40) {
if (!logRound.Info.PrivateLobby && logRound.Info.Finish.HasValue) {
this.OnPersonalBestNotification?.Invoke(logRound.Info);
lock (this.lockObject) {
if (!this.StatsForm.ExistsPersonalBestLog(logRound.Info.SessionId, logRound.Info.ShowNameId, logRound.Info.Name)) {
this.OnPersonalBestNotification?.Invoke(logRound.Info);
}
}
}
}
}
Expand Down
20 changes: 8 additions & 12 deletions Views/Stats.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2762,8 +2762,6 @@ private void LogFile_OnError(string error) {

private void LogFile_OnPersonalBestNotification(RoundInfo info) {
if (LevelStats.ALL.TryGetValue(info.Name, out LevelStats level) && level.Type == LevelType.Race) {
bool isExists = this.ExistsPersonalBestLog(info.SessionId, info.ShowNameId, info.Name);

double currentRecord = (info.Finish.Value - info.Start).TotalMilliseconds;
BsonExpression recordQuery = Query.And(
Query.Not("Finish", null),
Expand All @@ -2773,18 +2771,16 @@ private void LogFile_OnPersonalBestNotification(RoundInfo info) {
List<RoundInfo> queryResult = this.RoundDetails.Find(recordQuery).ToList();
double record = queryResult.Count > 0 ? queryResult.Min(r => (r.Finish.Value - r.Start).TotalMilliseconds) : Double.MaxValue;

if (!isExists && currentRecord < record) {
this.InsertPersonalBestLog(info.SessionId, info.ShowNameId, info.Name, currentRecord, info.Finish.Value, currentRecord < record);
if (!IsGameRunning) { return; }

this.UpsertPersonalBestLog(info.SessionId, info.ShowNameId, info.Name, currentRecord, info.Finish.Value, currentRecord < record);
if (IsGameRunning && currentRecord < record) {
string timeDiffContent = String.Empty;
if (record != Double.MaxValue) {
TimeSpan timeDiff = TimeSpan.FromMilliseconds(record - currentRecord);
timeDiffContent = timeDiff.Minutes > 0 ? $"{Multilingual.GetWord("message_new_personal_best_timediff_by_minute_prefix")}{timeDiff.Minutes}{Multilingual.GetWord("message_new_personal_best_timediff_by_minute_infix")} {timeDiff.Seconds}.{timeDiff.Milliseconds}{Multilingual.GetWord("message_new_personal_best_timediff_by_minute_suffix")}"
: $"{timeDiff.Seconds}.{timeDiff.Milliseconds}{Multilingual.GetWord("message_new_personal_best_timediff_by_second")}";
timeDiffContent = timeDiff.Minutes > 0 ? $" ⏱️{Multilingual.GetWord("message_new_personal_best_timediff_by_minute_prefix")}{timeDiff.Minutes}{Multilingual.GetWord("message_new_personal_best_timediff_by_minute_infix")} {timeDiff.Seconds}.{timeDiff.Milliseconds}{Multilingual.GetWord("message_new_personal_best_timediff_by_minute_suffix")}"
: $" ⏱️{timeDiff.Seconds}.{timeDiff.Milliseconds}{Multilingual.GetWord("message_new_personal_best_timediff_by_second")}";
}
string showName = $" {(Multilingual.GetShowName(this.GetAlternateShowId(info.ShowNameId)).Equals(Multilingual.GetRoundName(info.Name)) ? $"({Multilingual.GetRoundName(info.Name)})" : $"({Multilingual.GetShowName(this.GetAlternateShowId(info.ShowNameId))}{Multilingual.GetRoundName(info.Name)})")}";
string description = $"{Multilingual.GetWord("message_new_personal_best_prefix")}{showName}{Multilingual.GetWord("message_new_personal_best_suffix")} {timeDiffContent}";
string description = $"{Multilingual.GetWord("message_new_personal_best_prefix")}{showName}{Multilingual.GetWord("message_new_personal_best_suffix")}{timeDiffContent}";
ToastPosition toastPosition = this.CurrentSettings.NotificationWindowPosition == 0 ? ToastPosition.BottomRight : ToastPosition.TopRight;
ToastTheme toastTheme = this.Theme == MetroThemeStyle.Light ? ToastTheme.Light : ToastTheme.Dark;
ToastAnimation toastAnimation = this.CurrentSettings.NotificationWindowAnimation == 0 ? ToastAnimation.FADE : ToastAnimation.SLIDE;
Expand Down Expand Up @@ -3137,7 +3133,7 @@ private void LogFile_OnParsedLogLines(List<RoundInfo> round) {
}
}

private bool ExistsPersonalBestLog(string sessionId, string showId, string roundId) {
public bool ExistsPersonalBestLog(string sessionId, string showId, string roundId) {
if (string.IsNullOrEmpty(sessionId) || string.IsNullOrEmpty(showId)) return false;
BsonExpression condition = Query.And(
Query.EQ("_id", sessionId),
Expand All @@ -3156,10 +3152,10 @@ private PersonalBestLog SelectPersonalBestLog(string sessionId, string showId, s
return this.PersonalBestLog.FindOne(condition);
}

private void InsertPersonalBestLog(string sessionId, string showId, string roundId, double record, DateTime finish, bool isPb) {
private void UpsertPersonalBestLog(string sessionId, string showId, string roundId, double record, DateTime finish, bool isPb) {
lock (this.StatsDB) {
this.StatsDB.BeginTrans();
this.PersonalBestLog.Insert(new PersonalBestLog { SessionId = sessionId, ShowId = showId, RoundId = roundId, Record = record, PbDate = finish, IsPb = isPb,
this.PersonalBestLog.Upsert(new PersonalBestLog { SessionId = sessionId, ShowId = showId, RoundId = roundId, Record = record, PbDate = finish, IsPb = isPb,
CountryCode = HostCountryCode, OnlineServiceType = (int)OnlineServiceType, OnlineServiceId = OnlineServiceId, OnlineServiceNickname = OnlineServiceNickname
});
this.StatsDB.Commit();
Expand Down

0 comments on commit fc5e68d

Please sign in to comment.