Skip to content

Commit

Permalink
fix: timer methods
Browse files Browse the repository at this point in the history
  • Loading branch information
DorielRivalet committed Aug 31, 2023
1 parent 7a2ae3f commit 5e5b50b
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 30 deletions.
37 changes: 14 additions & 23 deletions MHFZ_Overlay/Services/DatabaseService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3897,15 +3897,6 @@ public void StoreAchievement(int achievementID)
}
}

// TODO put somewhere else and test
public static string FormatTime(int framesElapsed)
{
var minutes = framesElapsed / (Numbers.FramesPerSecond * 60);
var seconds = (framesElapsed % (Numbers.FramesPerSecond * 60)) / Numbers.FramesPerSecond;
var milliseconds = ((framesElapsed % ((double)Numbers.FramesPerSecond * 60)) % (double)Numbers.FramesPerSecond) / double.Parse(Numbers.FramesPerSecond.ToString(CultureInfo.InvariantCulture), CultureInfo.InvariantCulture);
return $"{minutes:D2}:{seconds:D2}.{(int)(milliseconds * 1000):D3}";
}

private void CreateDatabaseTables(SQLiteConnection conn, DataLoader dataLoader)
{
using (var transaction = conn.BeginTransaction())
Expand Down Expand Up @@ -10556,59 +10547,59 @@ GROUP BY
switch (weaponType)
{
case "Sword and Shield":
configWindow.SwordAndShieldBestTimeTextBlock.Text = FormatTime(bestTime);
configWindow.SwordAndShieldBestTimeTextBlock.Text = TimeService.GetMinutesSecondsMillisecondsFromFrames(bestTime);
configWindow.SwordAndShieldRunIDTextBlock.Text = string.Format(CultureInfo.InvariantCulture, "Run ID: {0}", runID);
break;
case "Great Sword":
configWindow.GreatSwordBestTimeTextBlock.Text = FormatTime(bestTime);
configWindow.GreatSwordBestTimeTextBlock.Text = TimeService.GetMinutesSecondsMillisecondsFromFrames(bestTime);
configWindow.GreatSwordRunIDTextBlock.Text = string.Format(CultureInfo.InvariantCulture, "Run ID: {0}", runID);
break;
case "Dual Swords":
configWindow.DualSwordsBestTimeTextBlock.Text = FormatTime(bestTime);
configWindow.DualSwordsBestTimeTextBlock.Text = TimeService.GetMinutesSecondsMillisecondsFromFrames(bestTime);
configWindow.DualSwordsRunIDTextBlock.Text = string.Format(CultureInfo.InvariantCulture, "Run ID: {0}", runID);
break;
case "Long Sword":
configWindow.LongSwordBestTimeTextBlock.Text = FormatTime(bestTime);
configWindow.LongSwordBestTimeTextBlock.Text = TimeService.GetMinutesSecondsMillisecondsFromFrames(bestTime);
configWindow.LongSwordRunIDTextBlock.Text = string.Format(CultureInfo.InvariantCulture, "Run ID: {0}", runID);
break;
case "Lance":
configWindow.LanceBestTimeTextBlock.Text = FormatTime(bestTime);
configWindow.LanceBestTimeTextBlock.Text = TimeService.GetMinutesSecondsMillisecondsFromFrames(bestTime);
configWindow.LanceRunIDTextBlock.Text = string.Format(CultureInfo.InvariantCulture, "Run ID: {0}", runID);
break;
case "Gunlance":
configWindow.GunlanceBestTimeTextBlock.Text = FormatTime(bestTime);
configWindow.GunlanceBestTimeTextBlock.Text = TimeService.GetMinutesSecondsMillisecondsFromFrames(bestTime);
configWindow.GunlanceRunIDTextBlock.Text = string.Format(CultureInfo.InvariantCulture, "Run ID: {0}", runID);
break;
case "Hammer":
configWindow.HammerBestTimeTextBlock.Text = FormatTime(bestTime);
configWindow.HammerBestTimeTextBlock.Text = TimeService.GetMinutesSecondsMillisecondsFromFrames(bestTime);
configWindow.HammerRunIDTextBlock.Text = string.Format(CultureInfo.InvariantCulture, "Run ID: {0}", runID);
break;
case "Hunting Horn":
configWindow.HuntingHornBestTimeTextBlock.Text = FormatTime(bestTime);
configWindow.HuntingHornBestTimeTextBlock.Text = TimeService.GetMinutesSecondsMillisecondsFromFrames(bestTime);
configWindow.HuntingHornRunIDTextBlock.Text = string.Format(CultureInfo.InvariantCulture, "Run ID: {0}", runID);
break;
case "Tonfa":
configWindow.TonfaBestTimeTextBlock.Text = FormatTime(bestTime);
configWindow.TonfaBestTimeTextBlock.Text = TimeService.GetMinutesSecondsMillisecondsFromFrames(bestTime);
configWindow.TonfaRunIDTextBlock.Text = string.Format(CultureInfo.InvariantCulture, "Run ID: {0}", runID);
break;
case "Switch Axe F":
configWindow.SwitchAxeFBestTimeTextBlock.Text = FormatTime(bestTime);
configWindow.SwitchAxeFBestTimeTextBlock.Text = TimeService.GetMinutesSecondsMillisecondsFromFrames(bestTime);
configWindow.SwitchAxeFRunIDTextBlock.Text = string.Format(CultureInfo.InvariantCulture, "Run ID: {0}", runID);
break;
case "Magnet Spike":
configWindow.MagnetSpikeBestTimeTextBlock.Text = FormatTime(bestTime);
configWindow.MagnetSpikeBestTimeTextBlock.Text = TimeService.GetMinutesSecondsMillisecondsFromFrames(bestTime);
configWindow.MagnetSpikeRunIDTextBlock.Text = string.Format(CultureInfo.InvariantCulture, "Run ID: {0}", runID);
break;
case "Light Bowgun":
configWindow.LightBowgunBestTimeTextBlock.Text = FormatTime(bestTime);
configWindow.LightBowgunBestTimeTextBlock.Text = TimeService.GetMinutesSecondsMillisecondsFromFrames(bestTime);
configWindow.LightBowgunRunIDTextBlock.Text = string.Format(CultureInfo.InvariantCulture, "Run ID: {0}", runID);
break;
case "Heavy Bowgun":
configWindow.HeavyBowgunBestTimeTextBlock.Text = FormatTime(bestTime);
configWindow.HeavyBowgunBestTimeTextBlock.Text = TimeService.GetMinutesSecondsMillisecondsFromFrames(bestTime);
configWindow.HeavyBowgunRunIDTextBlock.Text = string.Format(CultureInfo.InvariantCulture, "Run ID: {0}", runID);
break;
case "Bow":
configWindow.BowBestTimeTextBlock.Text = FormatTime(bestTime);
configWindow.BowBestTimeTextBlock.Text = TimeService.GetMinutesSecondsMillisecondsFromFrames(bestTime);
configWindow.BowRunIDTextBlock.Text = string.Format(CultureInfo.InvariantCulture, "Run ID: {0}", runID);
break;
default:
Expand Down
24 changes: 17 additions & 7 deletions MHFZ_Overlay/Services/TimeService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -78,8 +78,8 @@ public static string TestTimerMethods(decimal timeDefInt)
for (decimal i = timeInt; i >= 0M; i--)
{
timer1Result = StringBuilderTimer(timeInt, TimerFormat.MinutesSecondsMilliseconds, true, timeDefInt, true, GetTimeLeftPercent(timeDefInt, timeInt, true), TimerMode.Elapsed);
timer2Result = TimeSpanTimer(timeInt, timeDefInt, true, GetTimeLeftPercent(timeDefInt, timeInt, true), TimerMode.Elapsed);
timer3Result = SimpleTimer(timeInt, timeDefInt, true, GetTimeLeftPercent(timeDefInt, timeInt, true), TimerMode.Elapsed);
timer2Result = TimeSpanTimer(timeInt, TimerFormat.MinutesSecondsMilliseconds, timeDefInt, true, GetTimeLeftPercent(timeDefInt, timeInt, true), TimerMode.Elapsed);
timer3Result = SimpleTimer(timeInt, TimerFormat.MinutesSecondsMilliseconds, timeDefInt, true, GetTimeLeftPercent(timeDefInt, timeInt, true), TimerMode.Elapsed);

if (timer1Result != timer2Result || timer3Result != timer1Result || timer3Result != timer2Result)
{
Expand All @@ -100,7 +100,7 @@ public static string TestTimerMethods(decimal timeDefInt)
Simple: {timer3Result}";
}

private static string SimpleTimer(decimal timeInt, decimal timeDefInt = 0, bool timeLeftPercentShown = false, string timeLeftPercentNumber = "", TimerMode timerMode = TimerMode.Elapsed)
private static string SimpleTimer(decimal timeInt, TimerFormat timerFormat, decimal timeDefInt = 0, bool timeLeftPercentShown = false, string timeLeftPercentNumber = "", TimerMode timerMode = TimerMode.Elapsed)
{
// TODO wrong conditionals for timeint >= timedefint?
decimal time = timerMode == TimerMode.Elapsed && timeInt <= timeDefInt ? time = timeDefInt - timeInt : time = timeInt;
Expand All @@ -111,7 +111,12 @@ private static string SimpleTimer(decimal timeInt, decimal timeDefInt = 0, bool
decimal remainingMilliseconds = milliseconds - (minutes * 60000) - (seconds * 1000);
var timeLeftPercent = timeLeftPercentShown ? timeLeftPercentNumber : string.Empty;

return $"{minutes:00}:{seconds:00}.{remainingMilliseconds:000}" + timeLeftPercent;
return timerFormat switch
{
TimerFormat.MinutesSeconds => $"{minutes:00}:{seconds:00}" + timeLeftPercent,
TimerFormat.MinutesSecondsMilliseconds => $"{minutes:00}:{seconds:00}.{remainingMilliseconds:000}" + timeLeftPercent,
_ => $"{minutes:00}:{seconds:00}.{remainingMilliseconds:000}" + timeLeftPercent,
};
}

private static string StringBuilderTimer(decimal timeInt, TimerFormat timerFormat, bool isFrames = true, decimal timeDefInt = 0, bool timeLeftPercentShown = false, string timeLeftPercentNumber = "", TimerMode timerMode = TimerMode.Elapsed)
Expand All @@ -138,12 +143,12 @@ private static string StringBuilderTimer(decimal timeInt, TimerFormat timerForma
sb.AppendFormat(CultureInfo.InvariantCulture, "{0:00}:{1:00}.{2:000}", minutes, seconds, milliseconds);
break;
}
sb.Append(timeLeftPercent);

sb.Append(timeLeftPercent);
return sb.ToString();
}

private static string TimeSpanTimer(decimal timeInt, decimal timeDefInt = 0, bool timeLeftPercentShown = false, string timeLeftPercentNumber = "", TimerMode timerMode = TimerMode.Elapsed)
private static string TimeSpanTimer(decimal timeInt, TimerFormat timerFormat, decimal timeDefInt = 0, bool timeLeftPercentShown = false, string timeLeftPercentNumber = "", TimerMode timerMode = TimerMode.Elapsed)
{
decimal time = timerMode == TimerMode.Elapsed && timeInt <= timeDefInt ? time = timeDefInt - timeInt : time = timeInt;
decimal timeInSeconds = time / Numbers.FramesPerSecond;
Expand All @@ -154,7 +159,12 @@ private static string TimeSpanTimer(decimal timeInt, decimal timeDefInt = 0, boo
var timeLeftPercent = timeLeftPercentShown ? timeLeftPercentNumber : string.Empty;

// Format the TimeSpan object as a string
return $"{minutes:00}:{timeInSecondsSpan.Seconds:00}.{roundedMilliseconds:000}" + timeLeftPercent;
return timerFormat switch
{
TimerFormat.MinutesSeconds => $"{minutes:00}:{timeInSecondsSpan.Seconds:00}" + timeLeftPercent,
TimerFormat.MinutesSecondsMilliseconds => $"{minutes:00}:{timeInSecondsSpan.Seconds:00}.{roundedMilliseconds:000}" + timeLeftPercent,
_ => $"{minutes:00}:{timeInSecondsSpan.Seconds:00}.{roundedMilliseconds:000}" + timeLeftPercent,
};
}

/// <summary>
Expand Down

0 comments on commit 5e5b50b

Please sign in to comment.