Fix playtime formatting (#32974)

This commit is contained in:
Stalen
2024-10-28 21:00:00 +03:00
committed by GitHub
parent ee445c4938
commit 24f79c3ecc
9 changed files with 34 additions and 9 deletions

View File

@@ -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;
}

View File

@@ -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();

View File

@@ -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()}");
}
/// <summary>
/// Formats playtime as hours and minutes.
/// </summary>
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));
}
}
}

View File

@@ -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))

View File

@@ -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)
{

View File

@@ -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))

View File

@@ -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

View File

@@ -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

View File

@@ -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: