Skip to content

Commit

Permalink
fix: quest time percent
Browse files Browse the repository at this point in the history
  • Loading branch information
DorielRivalet committed Aug 18, 2023
1 parent 16c2d45 commit 97b603b
Show file tree
Hide file tree
Showing 4 changed files with 103 additions and 69 deletions.
14 changes: 14 additions & 0 deletions MHFZ_Overlay/Models/Constant/Numbers.cs
Original file line number Diff line number Diff line change
Expand Up @@ -218,6 +218,8 @@ TODO replace the numbers in source code as necessary

public const int QuestIDMultiplayerRoad = 23527;

public const int QuestIDFirstDistrictDuremudira = 21731;

public const int QuestIDSecondDistrictDuremudira = 21746;

public const int QuestIDTwinheadRajangsHistoric = 55937;
Expand All @@ -241,4 +243,16 @@ TODO replace the numbers in source code as necessary
public const int MezFesSRankPanicHoney = 100;

public const int MezFesSRankGuukuScoop = 106980;

/// <summary>
/// Not musou.
/// </summary>
public const int DuremudiraTimeLimitMinutes = 20;

public const int ArrogantDuremudiraTimeLimitMinutes = 10;

/// <summary>
/// Not musou.
/// </summary>
public const decimal DuremudiraTimeLimitFrames = 36_000M;
}
7 changes: 7 additions & 0 deletions MHFZ_Overlay/Models/Structures/Enums.cs
Original file line number Diff line number Diff line change
Expand Up @@ -399,3 +399,10 @@ public enum BingoLineCompletionType
/// </summary>
Diagonal,
}

// TODO enums for settings
public enum TimerMode
{
TimeLeft,
Elapsed,
}
111 changes: 71 additions & 40 deletions MHFZ_Overlay/Services/TimeService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,9 @@ namespace MHFZ_Overlay.Services;
using System.Text;
using System.Threading.Tasks;
using MHFZ_Overlay.Models.Constant;
using MHFZ_Overlay.Models.Structures;
using NLog;
using static System.Windows.Forms.VisualStyles.VisualStyleElement.TaskbarClock;

/// <summary>
/// A service for doing time and date manipulation. Consult the benchmarks project for the performance.
Expand All @@ -31,6 +33,34 @@ public static TimeSpan GetTimeSpanFromFrames(decimal frames)
return TimeSpan.FromSeconds((double)frames / (double)Numbers.FramesPerSecond);
}

public static string GetTimeLeftPercent(decimal timeDefInt, decimal timeInt, bool isDure)
{
if (timeDefInt < timeInt)
{
return " (?)";
}
else
{
return string.Format(CultureInfo.InvariantCulture, " ({0:0}%)", timeInt / timeDefInt * 100.0M);
}
}

public static decimal GetTimeValue(TimerMode mode, decimal timeDefInt, decimal timeInt)
{
decimal time;

if (mode == TimerMode.Elapsed)
{
time = timeDefInt - timeInt;
}
else // default to Time Left mode
{
time = timeInt;
}

return time;
}

/// <summary>
/// Test the timer methods for equality up until the specified max time in frames.
/// </summary>
Expand All @@ -39,50 +69,58 @@ public static TimeSpan GetTimeSpanFromFrames(decimal frames)
public static string TestTimerMethods(decimal timeDefInt)
{
decimal timeInt = timeDefInt;
var maxTime = timeDefInt.ToString(TimeFormats.HoursMinutesSecondsMilliseconds, CultureInfo.InvariantCulture);
for (decimal i = timeDefInt; i > 0M; i--)
var maxTime = TimeSpan.FromSeconds((double)(timeDefInt / Numbers.FramesPerSecond));
string timer1Result = string.Empty;
string timer2Result = string.Empty;
string timer3Result = string.Empty;

for (decimal i = timeInt; i >= 0M; i--)
{
var timer1Result = StringBuilderTimer(timeInt, timeDefInt);
var timer2Result = TimeSpanTimer(timeInt, timeDefInt);
var fastestResult = FastestTimer(timeDefInt - timeInt);
timer1Result = StringBuilderTimer(timeInt, 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);

if (timer1Result != timer2Result || fastestResult != timer1Result || fastestResult != timer2Result)
if (timer1Result != timer2Result || timer3Result != timer1Result || timer3Result != timer2Result)
{
return $"timeDefInt: {timeDefInt} ({maxTime}) timeInt: {timeInt} | StringBuilder: {timer1Result} | TimeSpan: {timer2Result}";
return @$"timeDefInt: {timeDefInt} ({maxTime}) | timeInt: {timeInt}
StringBuilder: {timer1Result}
TimeSpan: {timer2Result}
Simple: {timer3Result}";
}

timeInt--;
}

return $"No inequalities found.";
return @$"No inequalities found.
timeDefInt: {timeDefInt} ({maxTime}) | timeInt: {timeInt}
StringBuilder: {timer1Result}
TimeSpan: {timer2Result}
Simple: {timer3Result}";
}

public static string FastestTimer(decimal time)
public static string SimpleTimer(decimal timeInt, decimal timeDefInt, bool timeLeftPercentShown = false, string timeLeftPercentNumber = "", TimerMode timerMode = TimerMode.Elapsed)
{
decimal frameValue = time; // Example frame value

decimal milliseconds = frameValue / 30 * 1000;
decimal minutes = Math.Floor(milliseconds / 60000);
decimal time = timerMode == TimerMode.Elapsed || timeInt >= timeDefInt ? time = timeDefInt - timeInt : time = timeInt;
decimal milliseconds = time / 30 * 1000;
decimal totalMinutes = Math.Floor(milliseconds / 60000);
decimal minutes = totalMinutes >= 60 ? totalMinutes : Math.Floor(milliseconds / 60000);
decimal seconds = Math.Floor((milliseconds - (minutes * 60000)) / 1000);
decimal remainingMilliseconds = milliseconds - (minutes * 60000) - (seconds * 1000);
var timeLeftPercent = timeLeftPercentShown ? timeLeftPercentNumber : string.Empty;

return $"{minutes:00}:{seconds:00}.{remainingMilliseconds:000}";
return $"{minutes:00}:{seconds:00}.{remainingMilliseconds:000}" + timeLeftPercent;
}

public static string StringBuilderTimer(decimal timeInt, decimal timeDefInt, bool timeLeftPercentShown = false, string timeLeftPercentNumber = "")
public static string StringBuilderTimer(decimal timeInt, decimal timeDefInt, bool timeLeftPercentShown = false, string timeLeftPercentNumber = "", TimerMode timerMode = TimerMode.Elapsed)
{
if (timeInt <= 0M)
{
return "00:00.000";
}

decimal time = timeDefInt - timeInt;
decimal time = timerMode == TimerMode.Elapsed || timeInt >= timeDefInt ? time = timeDefInt - timeInt : time = timeInt;
decimal framesPerSecond = Numbers.FramesPerSecond;
decimal totalSeconds = time / framesPerSecond;
decimal minutes = Math.Floor(totalSeconds / 60);
decimal totalMinutes = Math.Floor(totalSeconds / 60);
decimal minutes = totalMinutes >= 60 ? totalMinutes : Math.Floor(totalSeconds / 60);
decimal seconds = Math.Floor(totalSeconds % 60);
decimal milliseconds = Math.Round((time % framesPerSecond) * (1000M / framesPerSecond));

var timeLeftPercent = timeLeftPercentShown ? timeLeftPercentNumber : string.Empty;

StringBuilder sb = new StringBuilder();
Expand All @@ -92,25 +130,18 @@ public static string StringBuilderTimer(decimal timeInt, decimal timeDefInt, boo
return sb.ToString();
}

public static string TimeSpanTimer(decimal timeInt, decimal timeDefInt)
public static string TimeSpanTimer(decimal timeInt, decimal timeDefInt, bool timeLeftPercentShown = false, string timeLeftPercentNumber = "", TimerMode timerMode = TimerMode.Elapsed)
{
if (timeInt <= 0M)
{
return "00:00.000";
}

decimal totalQuestDurationSeconds = timeDefInt / Numbers.FramesPerSecond; // Total duration of the quest in seconds
decimal timeRemainingInSeconds = timeInt / Numbers.FramesPerSecond; // Time left in the quest in seconds

// Calculate the elapsed time by subtracting the time left from the total duration
decimal elapsedTimeSeconds = totalQuestDurationSeconds - timeRemainingInSeconds;

// Create a TimeSpan object directly from elapsed time in seconds
TimeSpan elapsedTime = TimeSpan.FromSeconds((double)elapsedTimeSeconds);
decimal time = timerMode == TimerMode.Elapsed || timeInt >= timeDefInt ? time = timeDefInt - timeInt : time = timeInt;
decimal timeInSeconds = time / Numbers.FramesPerSecond;
TimeSpan timeInSecondsSpan = TimeSpan.FromSeconds((double)timeInSeconds);
int roundedMilliseconds = (int)(Math.Round(timeInSecondsSpan.TotalMilliseconds) % 1000);
var totalMinutes = Math.Floor(timeInSecondsSpan.TotalSeconds / 60);
var minutes = totalMinutes >= 60 ? totalMinutes : timeInSecondsSpan.Minutes;
var timeLeftPercent = timeLeftPercentShown ? timeLeftPercentNumber : string.Empty;

// Format the TimeSpan object as a string
string formattedElapsedTime = elapsedTime.ToString(TimeFormats.MinutesSecondsMilliseconds, CultureInfo.InvariantCulture);

return formattedElapsedTime;
return $"{minutes:00}:{timeInSecondsSpan.Seconds:00}.{roundedMilliseconds:000}" + timeLeftPercent;
}

}
40 changes: 11 additions & 29 deletions MHFZ_Overlay/ViewModels/Windows/AddressModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,13 @@ namespace MHFZ_Overlay.ViewModels.Windows;
using MHFZ_Overlay.Models;
using MHFZ_Overlay.Models.Collections;
using MHFZ_Overlay.Models.Constant;
using MHFZ_Overlay.Models.Structures;
using MHFZ_Overlay.Services;
using MHFZ_Overlay.Services.Converter;
using RESTCountries.NET.Models;
using RESTCountries.NET.Services;
using SkiaSharp;
using static System.Windows.Forms.VisualStyles.VisualStyleElement.TaskbarClock;
using Application = System.Windows.Application;

/// <summary>
Expand Down Expand Up @@ -2114,22 +2117,6 @@ public static string GetMetadata
}
}

public string TimeLeftPercentNumber
{
get
{
// TODO: Does this work with dure/road?
if (this.TimeDefInt() < this.TimeInt() || this.TimeDefInt() <= 0)
{
return string.Empty;
}
else
{
return string.Format(CultureInfo.InvariantCulture, " ({0:0}%)", (float)this.TimeInt() / this.TimeDefInt() * 100.0);
}
}
}

public string SharpnessPercentNumber
{
get
Expand Down Expand Up @@ -2159,38 +2146,33 @@ public string SharpnessPercentNumber

private StringBuilder sbForTimer = new StringBuilder();


/// <summary>
/// Gets quest time in the format of mm:ss.fff. This should only be used for display purposes.
/// </summary>
public string Time
{
get
{
decimal time;

if (GetTimerMode() == "Time Elapsed")
{
time = this.TimeDefInt() - this.TimeInt();
}
else // default to Time Left mode
{
time = this.TimeInt();
}
// check for 1st and 2nd district dure
// TODO: find timedefint address for dures
var isDure = QuestID() == 21731 || QuestID() == 21746;
decimal timeDefInt = isDure ? Numbers.DuremudiraTimeLimitFrames : TimeDefInt();

var timerMode = GetTimerMode() == "Time Elapsed" ? TimerMode.Elapsed : TimerMode.TimeLeft;
decimal time = TimeService.GetTimeValue(timerMode, timeDefInt, (decimal)TimeInt());
decimal framesPerSecond = Numbers.FramesPerSecond;
decimal totalSeconds = time / framesPerSecond;
decimal minutes = Math.Floor(totalSeconds / 60);
decimal seconds = Math.Floor(totalSeconds % 60);
decimal milliseconds = Math.Round((time % framesPerSecond) * (1000M / framesPerSecond));

this.timeLeftPercent = ShowTimeLeftPercentage() ? this.TimeLeftPercentNumber : string.Empty;
this.timeLeftPercent = ShowTimeLeftPercentage() ? TimeService.GetTimeLeftPercent(timeDefInt, TimeInt(), isDure) : string.Empty;

StringBuilder sb = new StringBuilder();
sb.AppendFormat(CultureInfo.InvariantCulture, "{0:00}:{1:00}.{2:000}", minutes, seconds, milliseconds);
sb.Append(this.timeLeftPercent);

MessageBox.Show(TimeService.TestTimerMethods(324_000M)); // 1 hour
// MessageBox.Show(TimeService.TestTimerMethods(216_000 * 10)); // 2 hours at 30 fps

return sb.ToString();
}
Expand Down

0 comments on commit 97b603b

Please sign in to comment.