aboutsummaryrefslogtreecommitdiff
path: root/src/Ryujinx.Ui.Common/App/ApplicationMetadata.cs
blob: 43647feef154547b0c6b8709c303ca9d43594b10 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
using System;
using System.Text.Json.Serialization;

namespace Ryujinx.Ui.App.Common
{
    public class ApplicationMetadata
    {
        public string Title { get; set; }
        public bool Favorite { get; set; }

        [JsonPropertyName("timespan_played")]
        public TimeSpan TimePlayed { get; set; } = TimeSpan.Zero;

        [JsonPropertyName("last_played_utc")]
        public DateTime? LastPlayed { get; set; } = null;

        [JsonPropertyName("time_played")]
        [JsonIgnore(Condition = JsonIgnoreCondition.WhenWritingDefault)]
        public double TimePlayedOld { get; set; }

        [JsonPropertyName("last_played")]
        [JsonIgnore(Condition = JsonIgnoreCondition.WhenWritingDefault)]
        public string LastPlayedOld { get; set; }

        /// <summary>
        /// Updates <see cref="LastPlayed"/>. Call this before launching a game.
        /// </summary>
        public void UpdatePreGame()
        {
            LastPlayed = DateTime.UtcNow;
        }

        /// <summary>
        /// Updates <see cref="LastPlayed"/> and <see cref="TimePlayed"/>. Call this after a game ends.
        /// </summary>
        public void UpdatePostGame()
        {
            DateTime? prevLastPlayed = LastPlayed;
            UpdatePreGame();

            if (!prevLastPlayed.HasValue)
            {
                return;
            }

            TimeSpan diff = DateTime.UtcNow - prevLastPlayed.Value;
            double newTotalSeconds = TimePlayed.Add(diff).TotalSeconds;
            TimePlayed = TimeSpan.FromSeconds(Math.Round(newTotalSeconds, MidpointRounding.AwayFromZero));
        }
    }
}