Skip to content

Commit

Permalink
Fixed playtime formatting
Browse files Browse the repository at this point in the history
  • Loading branch information
stalengd authored and MilonPL committed Oct 24, 2024
1 parent 318aa45 commit c36ca84
Show file tree
Hide file tree
Showing 9 changed files with 34 additions and 9 deletions.
3 changes: 2 additions & 1 deletion Content.Client/Info/PlaytimeStats/PlaytimeStatsEntry.cs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
using Content.Shared.Localizations;
using Robust.Client.AutoGenerated;
using Robust.Client.Graphics;
using Robust.Client.UserInterface.Controls;
Expand All @@ -16,7 +17,7 @@ public PlaytimeStatsEntry(string role, TimeSpan playtime, StyleBox styleBox)

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

Expand Down
3 changes: 1 addition & 2 deletions Content.Client/Info/PlaytimeStats/PlaytimeStatsWindow.cs
Original file line number Diff line number Diff line change
Expand Up @@ -104,8 +104,7 @@ private void PopulatePlaytimeData()
{
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();

Expand Down
21 changes: 21 additions & 0 deletions Content.Shared/Localizations/ContentLocalizationManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ public void Initialize()
_loc.AddFunction(culture, "LOC", FormatLoc);
_loc.AddFunction(culture, "NATURALFIXED", FormatNaturalFixed);
_loc.AddFunction(culture, "NATURALPERCENT", FormatNaturalPercent);
_loc.AddFunction(culture, "PLAYTIME", FormatPlaytime);


/*
Expand Down Expand Up @@ -141,6 +142,16 @@ public static string FormatDirection(Direction dir)
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;
Expand Down Expand Up @@ -229,5 +240,15 @@ private static ILocValue FormatUnits(LocArgs args)

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));
}
}
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System.Diagnostics.CodeAnalysis;
using Content.Shared.Localizations;
using Content.Shared.Preferences;
using JetBrains.Annotations;
using Robust.Shared.Prototypes;
Expand Down Expand Up @@ -50,7 +51,7 @@ public override bool Check(IEntityManager entManager,

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))
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System.Diagnostics.CodeAnalysis;
using Content.Shared.Localizations;
using Content.Shared.Players.PlayTimeTracking;
using Content.Shared.Preferences;
using JetBrains.Annotations;
Expand Down Expand Up @@ -28,7 +29,7 @@ public override bool Check(IEntityManager entManager,
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)
{
Expand Down
3 changes: 2 additions & 1 deletion Content.Shared/Roles/JobRequirement/RoleTimeRequirement.cs
Original file line number Diff line number Diff line change
@@ -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;
Expand Down Expand Up @@ -37,7 +38,7 @@ public override bool Check(IEntityManager entManager,
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))
Expand Down
3 changes: 3 additions & 0 deletions Resources/Locale/en-US/_lib.ftl
Original file line number Diff line number Diff line change
Expand Up @@ -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
3 changes: 1 addition & 2 deletions Resources/Locale/en-US/info/playtime-stats.ftl
Original file line number Diff line number Diff line change
Expand Up @@ -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
1 change: 0 additions & 1 deletion Resources/Locale/en-US/job/role-requirements.ftl
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down

0 comments on commit c36ca84

Please sign in to comment.