From 24f79c3ecc6dca77e6096e760be1b4c6e2c0bf5a Mon Sep 17 00:00:00 2001 From: Stalen <33173619+stalengd@users.noreply.github.com> Date: Mon, 28 Oct 2024 21:00:00 +0300 Subject: [PATCH] Fix playtime formatting (#32974) --- .../Info/PlaytimeStats/PlaytimeStatsEntry.cs | 3 ++- .../Info/PlaytimeStats/PlaytimeStatsWindow.cs | 3 +-- .../ContentLocalizationManager.cs | 21 +++++++++++++++++++ .../DepartmentTimeRequirement.cs | 3 ++- .../OverallPlaytimeRequirement.cs | 3 ++- .../JobRequirement/RoleTimeRequirement.cs | 3 ++- Resources/Locale/en-US/_lib.ftl | 3 +++ .../Locale/en-US/info/playtime-stats.ftl | 3 +-- .../Locale/en-US/job/role-requirements.ftl | 1 - 9 files changed, 34 insertions(+), 9 deletions(-) diff --git a/Content.Client/Info/PlaytimeStats/PlaytimeStatsEntry.cs b/Content.Client/Info/PlaytimeStats/PlaytimeStatsEntry.cs index 16e8f55a7e..632ad8de4a 100644 --- a/Content.Client/Info/PlaytimeStats/PlaytimeStatsEntry.cs +++ b/Content.Client/Info/PlaytimeStats/PlaytimeStatsEntry.cs @@ -1,3 +1,4 @@ +using Content.Shared.Localizations; using Robust.Client.AutoGenerated; using Robust.Client.Graphics; using Robust.Client.UserInterface.Controls; @@ -16,7 +17,7 @@ public sealed partial class PlaytimeStatsEntry : ContainerButton RoleLabel.Text = role; Playtime = playtime; // store the TimeSpan value directly - PlaytimeLabel.Text = playtime.ToString(Loc.GetString("ui-playtime-time-format")); // convert to string for display + PlaytimeLabel.Text = ContentLocalizationManager.FormatPlaytime(playtime); // convert to string for display BackgroundColorPanel.PanelOverride = styleBox; } diff --git a/Content.Client/Info/PlaytimeStats/PlaytimeStatsWindow.cs b/Content.Client/Info/PlaytimeStats/PlaytimeStatsWindow.cs index 1a530d950f..98241b2cca 100644 --- a/Content.Client/Info/PlaytimeStats/PlaytimeStatsWindow.cs +++ b/Content.Client/Info/PlaytimeStats/PlaytimeStatsWindow.cs @@ -104,8 +104,7 @@ public sealed partial class PlaytimeStatsWindow : FancyWindow { var overallPlaytime = _jobRequirementsManager.FetchOverallPlaytime(); - var formattedPlaytime = overallPlaytime.ToString(Loc.GetString("ui-playtime-time-format")); - OverallPlaytimeLabel.Text = Loc.GetString("ui-playtime-overall", ("time", formattedPlaytime)); + OverallPlaytimeLabel.Text = Loc.GetString("ui-playtime-overall", ("time", overallPlaytime)); var rolePlaytimes = _jobRequirementsManager.FetchPlaytimeByRoles(); diff --git a/Content.Shared/Localizations/ContentLocalizationManager.cs b/Content.Shared/Localizations/ContentLocalizationManager.cs index ad8890ae0f..e60ca74a37 100644 --- a/Content.Shared/Localizations/ContentLocalizationManager.cs +++ b/Content.Shared/Localizations/ContentLocalizationManager.cs @@ -36,6 +36,7 @@ namespace Content.Shared.Localizations _loc.AddFunction(culture, "LOC", FormatLoc); _loc.AddFunction(culture, "NATURALFIXED", FormatNaturalFixed); _loc.AddFunction(culture, "NATURALPERCENT", FormatNaturalPercent); + _loc.AddFunction(culture, "PLAYTIME", FormatPlaytime); /* @@ -141,6 +142,16 @@ namespace Content.Shared.Localizations return Loc.GetString($"zzzz-fmt-direction-{dir.ToString()}"); } + /// + /// Formats playtime as hours and minutes. + /// + public static string FormatPlaytime(TimeSpan time) + { + var hours = (int)time.TotalHours; + var minutes = time.Minutes; + return Loc.GetString($"zzzz-fmt-playtime", ("hours", hours), ("minutes", minutes)); + } + private static ILocValue FormatLoc(LocArgs args) { var id = ((LocValueString) args.Args[0]).Value; @@ -229,5 +240,15 @@ namespace Content.Shared.Localizations return new LocValueString(res); } + + private static ILocValue FormatPlaytime(LocArgs args) + { + var time = TimeSpan.Zero; + if (args.Args is { Count: > 0 } && args.Args[0].Value is TimeSpan timeArg) + { + time = timeArg; + } + return new LocValueString(FormatPlaytime(time)); + } } } diff --git a/Content.Shared/Roles/JobRequirement/DepartmentTimeRequirement.cs b/Content.Shared/Roles/JobRequirement/DepartmentTimeRequirement.cs index 78c6bd2517..8c86299210 100644 --- a/Content.Shared/Roles/JobRequirement/DepartmentTimeRequirement.cs +++ b/Content.Shared/Roles/JobRequirement/DepartmentTimeRequirement.cs @@ -1,4 +1,5 @@ using System.Diagnostics.CodeAnalysis; +using Content.Shared.Localizations; using Content.Shared.Preferences; using JetBrains.Annotations; using Robust.Shared.Prototypes; @@ -49,7 +50,7 @@ public sealed partial class DepartmentTimeRequirement : JobRequirement var deptDiffSpan = Time - playtime; var deptDiff = deptDiffSpan.TotalMinutes; - var formattedDeptDiff = deptDiffSpan.ToString(Loc.GetString("role-timer-time-format")); + var formattedDeptDiff = ContentLocalizationManager.FormatPlaytime(deptDiffSpan); var nameDepartment = "role-timer-department-unknown"; if (protoManager.TryIndex(Department, out var departmentIndexed)) diff --git a/Content.Shared/Roles/JobRequirement/OverallPlaytimeRequirement.cs b/Content.Shared/Roles/JobRequirement/OverallPlaytimeRequirement.cs index ed985cadfb..67b3938e1a 100644 --- a/Content.Shared/Roles/JobRequirement/OverallPlaytimeRequirement.cs +++ b/Content.Shared/Roles/JobRequirement/OverallPlaytimeRequirement.cs @@ -1,4 +1,5 @@ using System.Diagnostics.CodeAnalysis; +using Content.Shared.Localizations; using Content.Shared.Players.PlayTimeTracking; using Content.Shared.Preferences; using JetBrains.Annotations; @@ -27,7 +28,7 @@ public sealed partial class OverallPlaytimeRequirement : JobRequirement var overallTime = playTimes.GetValueOrDefault(PlayTimeTrackingShared.TrackerOverall); var overallDiffSpan = Time - overallTime; var overallDiff = overallDiffSpan.TotalMinutes; - var formattedOverallDiff = overallDiffSpan.ToString(Loc.GetString("role-timer-time-format")); + var formattedOverallDiff = ContentLocalizationManager.FormatPlaytime(overallDiffSpan); if (!Inverted) { diff --git a/Content.Shared/Roles/JobRequirement/RoleTimeRequirement.cs b/Content.Shared/Roles/JobRequirement/RoleTimeRequirement.cs index 23498ab91a..e75a18f011 100644 --- a/Content.Shared/Roles/JobRequirement/RoleTimeRequirement.cs +++ b/Content.Shared/Roles/JobRequirement/RoleTimeRequirement.cs @@ -1,4 +1,5 @@ using System.Diagnostics.CodeAnalysis; +using Content.Shared.Localizations; using Content.Shared.Players.PlayTimeTracking; using Content.Shared.Preferences; using Content.Shared.Roles.Jobs; @@ -36,7 +37,7 @@ public sealed partial class RoleTimeRequirement : JobRequirement playTimes.TryGetValue(proto, out var roleTime); var roleDiffSpan = Time - roleTime; var roleDiff = roleDiffSpan.TotalMinutes; - var formattedRoleDiff = roleDiffSpan.ToString(Loc.GetString("role-timer-time-format")); + var formattedRoleDiff = ContentLocalizationManager.FormatPlaytime(roleDiffSpan); var departmentColor = Color.Yellow; if (entManager.EntitySysManager.TryGetEntitySystem(out SharedJobSystem? jobSystem)) diff --git a/Resources/Locale/en-US/_lib.ftl b/Resources/Locale/en-US/_lib.ftl index c901d0f461..5c6f73af66 100644 --- a/Resources/Locale/en-US/_lib.ftl +++ b/Resources/Locale/en-US/_lib.ftl @@ -31,3 +31,6 @@ zzzz-fmt-power-joules = { TOSTRING($divided, "F1") } { $places -> [4] TJ *[5] ??? } + +# Used internally by the PLAYTIME() function. +zzzz-fmt-playtime = {$hours}H {$minutes}M \ No newline at end of file diff --git a/Resources/Locale/en-US/info/playtime-stats.ftl b/Resources/Locale/en-US/info/playtime-stats.ftl index 85508c1d09..b4925176a7 100644 --- a/Resources/Locale/en-US/info/playtime-stats.ftl +++ b/Resources/Locale/en-US/info/playtime-stats.ftl @@ -2,9 +2,8 @@ ui-playtime-stats-title = User Playtime Stats ui-playtime-overall-base = Overall Playtime: -ui-playtime-overall = Overall Playtime: {$time} +ui-playtime-overall = Overall Playtime: {PLAYTIME($time)} ui-playtime-first-time = First Time Playing ui-playtime-roles = Playtime per Role -ui-playtime-time-format = %h\H\ %m\M ui-playtime-header-role-type = Role ui-playtime-header-role-time = Time diff --git a/Resources/Locale/en-US/job/role-requirements.ftl b/Resources/Locale/en-US/job/role-requirements.ftl index 79a216fcca..686fcb93cb 100644 --- a/Resources/Locale/en-US/job/role-requirements.ftl +++ b/Resources/Locale/en-US/job/role-requirements.ftl @@ -4,7 +4,6 @@ role-timer-overall-insufficient = You require [color=yellow]{$time}[/color] more role-timer-overall-too-high = You require [color=yellow]{$time}[/color] less overall playtime to play this role. (Are you trying to play a trainee role?) role-timer-role-insufficient = You require [color=yellow]{$time}[/color] more playtime with [color={$departmentColor}]{$job}[/color] to play this role. role-timer-role-too-high = You require[color=yellow] {$time}[/color] less playtime with [color={$departmentColor}]{$job}[/color] to play this role. (Are you trying to play a trainee role?) -role-timer-time-format = %h\H\ %m\M role-timer-age-too-old = Your character must be under the age of [color=yellow]{$age}[/color] to play this role. role-timer-age-too-young = Your character must be over the age of [color=yellow]{$age}[/color] to play this role. role-timer-whitelisted-species = Your character must be one of the following species to play this role: -- 2.52.0