/ src / Ryujinx.UI.Common / App / ApplicationMetadata.cs
ApplicationMetadata.cs
 1  using System;
 2  using System.Text.Json.Serialization;
 3  
 4  namespace Ryujinx.UI.App.Common
 5  {
 6      public class ApplicationMetadata
 7      {
 8          public string Title { get; set; }
 9          public bool Favorite { get; set; }
10  
11          [JsonPropertyName("timespan_played")]
12          public TimeSpan TimePlayed { get; set; } = TimeSpan.Zero;
13  
14          [JsonPropertyName("last_played_utc")]
15          public DateTime? LastPlayed { get; set; } = null;
16  
17          [JsonPropertyName("time_played")]
18          [JsonIgnore(Condition = JsonIgnoreCondition.WhenWritingDefault)]
19          public double TimePlayedOld { get; set; }
20  
21          [JsonPropertyName("last_played")]
22          [JsonIgnore(Condition = JsonIgnoreCondition.WhenWritingDefault)]
23          public string LastPlayedOld { get; set; }
24  
25          /// <summary>
26          /// Updates <see cref="LastPlayed"/>. Call this before launching a game.
27          /// </summary>
28          public void UpdatePreGame()
29          {
30              LastPlayed = DateTime.UtcNow;
31          }
32  
33          /// <summary>
34          /// Updates <see cref="LastPlayed"/> and <see cref="TimePlayed"/>. Call this after a game ends.
35          /// </summary>
36          public void UpdatePostGame()
37          {
38              DateTime? prevLastPlayed = LastPlayed;
39              UpdatePreGame();
40  
41              if (!prevLastPlayed.HasValue)
42              {
43                  return;
44              }
45  
46              TimeSpan diff = DateTime.UtcNow - prevLastPlayed.Value;
47              double newTotalSeconds = TimePlayed.Add(diff).TotalSeconds;
48              TimePlayed = TimeSpan.FromSeconds(Math.Round(newTotalSeconds, MidpointRounding.AwayFromZero));
49          }
50      }
51  }