๐Ÿ“ฆ sleepyfran / duets

๐Ÿ“„ Stats.fs ยท 96 lines
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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96module Duets.Agents.Stats

open System
open System.Diagnostics
open Duets.Common
open Microsoft.FSharp.Data.UnitSystems.SI.UnitNames

type Stats = { SecondsPlayed: int<second> }

let private empty = { SecondsPlayed = 0<second> }

let private loadStats () =
    Files.statsPath ()
    |> Files.readAll
    |> Option.bind Serializer.deserialize
    |> Option.defaultValue empty

let private writeStats (stats: Stats) =
    stats |> Serializer.serialize |> Files.write (Files.statsPath ())

let private addElapsedToStats (elapsed: TimeSpan) =
    let stats = loadStats ()
    let elapsedSeconds = elapsed.Seconds |> (*) 1<second>

    { SecondsPlayed = stats.SecondsPlayed + elapsedSeconds } |> writeStats

let private startTracking (sw: Stopwatch) =
    if sw.IsRunning then
        Log.appendMessage
            "Attempted to start a timewatch that was already running"
    else
        sw.Start()

let private stopTracking (sw: Stopwatch) =
    match sw with
    | stopWatch when stopWatch.IsRunning ->
        stopWatch.Stop()
        stopWatch.Elapsed |> addElapsedToStats
    | _ ->
        Log.appendMessage "Attempted to stop a timewatch that was not running"

type private SavegameAgentMessage =
    | Read of AsyncReplyChannel<Stats>
    | StartTracking
    | StopTracking of AsyncReplyChannel<unit>

/// Agent in charge of writing and loading the stats of the game.
type private StatsAgent() =
    let agent =
        MailboxProcessor.Start
        <| fun inbox ->
            let rec loop stopWatch =
                async {
                    let! msg = inbox.Receive()

                    match msg with
                    | Read channel ->
                        try
                            loadStats () |> channel.Reply
                        with _ ->
                            channel.Reply empty

                        return! loop stopWatch
                    | StartTracking ->
                        startTracking stopWatch
                        return! loop stopWatch
                    | StopTracking channel ->
                        try
                            stopTracking stopWatch
                        with ex ->
                            Log.appendMessage
                                $"Saving stats failed with error {ex.Message}"

                        channel.Reply()

                        return! loop stopWatch
                }

            loop (Stopwatch())

    member _.Read() = agent.PostAndReply Read
    member _.StartTracking() = agent.Post StartTracking
    member _.StopTracking() = agent.PostAndReply StopTracking

let private statsAgent = StatsAgent()

/// Attempts to load the game stats from the file if it exist.
let retrieve = statsAgent.Read

/// Starts tracking the elapsed time.
let startTrackingTime = statsAgent.StartTracking

/// Stops tracking the elapsed time and saves it in the stats file, adding the
/// elapsed time to the current amount of seconds saved on the file.
let stopTrackingAndSave = statsAgent.StopTracking