Skip to content

Commit

Permalink
allow SECS_AS_MINS formatting for leaderboards
Browse files Browse the repository at this point in the history
  • Loading branch information
Jamiras committed Apr 12, 2024
1 parent a94e452 commit 057a37f
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 14 deletions.
29 changes: 19 additions & 10 deletions app/Platform/Enums/ValueFormat.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,9 @@ abstract class ValueFormat
// hhhmm
public const TimeMinutes = 'MINUTES';

// hhhmm calculated by taking value / 60
public const TimeSecondsAsMinutes = 'SECS_AS_MINS';

// number followed by three zeroes
public const ValueThousands = 'THOUSANDS';

Expand All @@ -45,6 +48,9 @@ abstract class ValueFormat
// n.nnn calculated by taking number / 1000
public const Fixed3 = 'FIXED3';

// NOTE: RichPresence supports additional types for floats, but leaderboards can't
// store floats.

public static function cases(): array
{
return [
Expand All @@ -53,11 +59,12 @@ public static function cases(): array
self::TimeCentiseconds,
self::TimeSeconds,
self::TimeMinutes,
self::ValueThousands,
self::ValueHundreds,
self::ValueTens,
self::TimeSecondsAsMinutes,
self::Value,
self::ValueUnsigned,
self::ValueTens,
self::ValueHundreds,
self::ValueThousands,
self::Fixed1,
self::Fixed2,
self::Fixed3,
Expand All @@ -77,14 +84,15 @@ public static function toString(string $format): string
self::TimeCentiseconds => 'Time (Centiseconds)',
self::TimeSeconds => 'Time (Seconds)',
self::TimeMinutes => 'Time (Minutes)',
self::ValueThousands => 'Value (Thousands)',
self::ValueHundreds => 'Value (Hundreds)',
self::ValueTens => 'Value (Tens)',
self::TimeSecondsAsMinutes => 'Time (Seconds as Minutes)',
self::Value => 'Value',
self::ValueUnsigned => 'Value (unsigned)',
self::Fixed1 => 'Fixed1',
self::Fixed2 => 'Fixed2',
self::Fixed3 => 'Fixed3',
self::ValueUnsigned => 'Value (Unsigned)',
self::ValueTens => 'Value (Tens)',
self::ValueHundreds => 'Value (Hundreds)',
self::ValueThousands => 'Value (Thousands)',
self::Fixed1 => 'Value (Fixed1)',
self::Fixed2 => 'Value (Fixed2)',
self::Fixed3 => 'Value (Fixed3)',
default => 'Unknown',
};
}
Expand All @@ -104,6 +112,7 @@ public static function format(int $value, string $format): string
self::TimeCentiseconds => sprintf("%s.%02d", ValueFormat::formatSeconds((int) ($value / 100)), $value % 100),
self::TimeSeconds => ValueFormat::formatSeconds($value),
self::TimeMinutes => sprintf("%01dh%02d", (int) $value / 60, $value % 60),
self::TimeSecondsAsMinutes => sprintf("%01dh%02d", (int) $value / 60 / 60, ($value / 60) % 60),
self::ValueThousands => localized_number($value * 1000),
self::ValueHundreds => localized_number($value * 100),
self::ValueTens => localized_number($value * 10),
Expand Down
18 changes: 14 additions & 4 deletions tests/Unit/ValueFormatTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ public function testFormatValue(): void

public function testFormatUnsignedValue(): void
{
$this->assertEquals("Value (unsigned)", ValueFormat::toString(ValueFormat::ValueUnsigned));
$this->assertEquals("Value (Unsigned)", ValueFormat::toString(ValueFormat::ValueUnsigned));

$this->assertEquals("12,345", ValueFormat::format(12345, ValueFormat::ValueUnsigned));
$this->assertEquals("4,294,954,951", ValueFormat::format(-12345, ValueFormat::ValueUnsigned));
Expand Down Expand Up @@ -83,6 +83,16 @@ public function testFormatMinutes(): void
$this->assertEquals("0h01", ValueFormat::format(1, ValueFormat::TimeMinutes));
}

public function testFormatSecondsAsMinutes(): void
{
$this->assertEquals("Time (Seconds as Minutes)", ValueFormat::toString(ValueFormat::TimeSecondsAsMinutes));

$this->assertEquals("3h25", ValueFormat::format(12345, ValueFormat::TimeSecondsAsMinutes));
$this->assertEquals("0h05", ValueFormat::format(345, ValueFormat::TimeSecondsAsMinutes));
$this->assertEquals("0h00", ValueFormat::format(0, ValueFormat::TimeSecondsAsMinutes));
$this->assertEquals("0h00", ValueFormat::format(1, ValueFormat::TimeSecondsAsMinutes));
}

public function testFormatThousands(): void
{
$this->assertEquals("Value (Thousands)", ValueFormat::toString(ValueFormat::ValueThousands));
Expand Down Expand Up @@ -121,7 +131,7 @@ public function testFormatTens(): void

public function testFormatFixed1(): void
{
$this->assertEquals("Fixed1", ValueFormat::toString(ValueFormat::Fixed1));
$this->assertEquals("Value (Fixed1)", ValueFormat::toString(ValueFormat::Fixed1));

$this->assertEquals("1,234.5", ValueFormat::format(12345, ValueFormat::Fixed1));
$this->assertEquals("-1,234.5", ValueFormat::format(-12345, ValueFormat::Fixed1));
Expand All @@ -133,7 +143,7 @@ public function testFormatFixed1(): void

public function testFormatFixed2(): void
{
$this->assertEquals("Fixed2", ValueFormat::toString(ValueFormat::Fixed2));
$this->assertEquals("Value (Fixed2)", ValueFormat::toString(ValueFormat::Fixed2));

$this->assertEquals("123.45", ValueFormat::format(12345, ValueFormat::Fixed2));
$this->assertEquals("-123.45", ValueFormat::format(-12345, ValueFormat::Fixed2));
Expand All @@ -145,7 +155,7 @@ public function testFormatFixed2(): void

public function testFormatFixed3(): void
{
$this->assertEquals("Fixed3", ValueFormat::toString(ValueFormat::Fixed3));
$this->assertEquals("Value (Fixed3)", ValueFormat::toString(ValueFormat::Fixed3));

$this->assertEquals("12.345", ValueFormat::format(12345, ValueFormat::Fixed3));
$this->assertEquals("-12.345", ValueFormat::format(-12345, ValueFormat::Fixed3));
Expand Down

0 comments on commit 057a37f

Please sign in to comment.