aboutsummaryrefslogtreecommitdiffstats
path: root/WarframeClock/AppData.cs
blob: ea59d1f5c01f56f57d49a4730a382a68d8c5a929 (plain)
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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
using System;
using System.Diagnostics;
using System.Globalization;
using System.Linq;
using System.Text.RegularExpressions;
using System.Threading;

namespace WarframeClock
{
    public class AppData : IDisposable
    {
        public DateTimeOffset NextBountiesResetByWorldState { get; set; } =
            DateTimeOffset.FromUnixTimeMilliseconds(1548159123053);
        public DateTimeOffset? NextBountiesReset { get; set; }

        public DateTimeOffset ArbitrationSolNodeUpdated { get; set; } =
            DateTimeOffset.FromUnixTimeMilliseconds(0);
        public WfcdWorldStateData.SolNode ArbitrationSolNode { get; set; }

        public DateTimeOffset KuvaFloodSolNodeUpdated { get; set; } =
            DateTimeOffset.FromUnixTimeMilliseconds(0);
        public WfcdWorldStateData.SolNode KuvaFloodSolNode { get; set; }

        private Timer _worldStateFetchTimer, _eeLogFetchTimer;

        public AppData()
        {
            StartWorldStateFetchTimer();
            StartEeLogFetchTimer();
        }

        public void Dispose()
        {
            _worldStateFetchTimer.Change(Timeout.Infinite, Timeout.Infinite);
            _worldStateFetchTimer.Dispose();
            _eeLogFetchTimer.Change(Timeout.Infinite, Timeout.Infinite);
            _eeLogFetchTimer.Dispose();
        }

        private void StartWorldStateFetchTimer()
        {
            _worldStateFetchTimer = new Timer(state =>
            {
                try
                {
                    var worldState = WorldState.Fetch().Result;
                    var cetusSyndicate =
                        worldState.SyndicateMissions.FirstOrDefault(mi => mi.Tag == "CetusSyndicate");
                    if (cetusSyndicate == null)
                    {
                        Trace.WriteLine("WorldState: CetusSyndicate missions not found");
                        return;
                    }

                    var cetusExpiry = cetusSyndicate.Expiry.Date.NumberLong;
                    Trace.WriteLine($"WorldState: CetusSyndicate missions expire at {cetusExpiry}");
                    NextBountiesResetByWorldState = DateTimeOffset.FromUnixTimeMilliseconds(cetusExpiry);
                }
                catch (Exception ex)
                {
                    Trace.WriteLine($"WorldState: Failed to fetch or parse worldState.php: {ex}");
                    Trace.WriteLine(ex.StackTrace);
                }
            }, null, TimeSpan.Zero, TimeSpan.FromMinutes(10));
        }

        private void StartEeLogFetchTimer()
        {
            var parser = new EeLogParser();

            var regexSyncingWorldStateJobs = new Regex(@"^syncing world state jobs in ([0-9.]+)$");
            var regexWorldStateRefreshedFromDb = new Regex(@"^Background: world state refreshed from db$");
            var regexCetusJoin = new Regex(@"^IRC out: JOIN #H_CETUSHUB4_(.+)$");
            var regexArbitration = new Regex(@"^EliteAlertMission at (.+) \(.+\)$");
            var regexKuvaFlood = new Regex(@"^KuvaMission(\d+) at (.+)\((.+) - (.+)\)$");
            parser.OnLogEntry += (sender, args) =>
            {
                Match match;

                if ((match = regexSyncingWorldStateJobs.Match(args.Content)).Success)
                {
                    var reset = args.DateTime.AddSeconds(
                        double.Parse(match.Groups[1].Value, CultureInfo.InvariantCulture));
                    if (reset < DateTimeOffset.UtcNow)
                        return;
                    NextBountiesReset = reset;
                    Trace.WriteLine($"EE.log: 'syncing world state jobs in'; next bounties reset: {reset}");
                }
                else if ((match = regexWorldStateRefreshedFromDb.Match(args.Content)).Success)
                {
                    if (NextBountiesReset == null)
                        return;
                    NextBountiesReset = null;
                    Trace.WriteLine($"EE.log: 'world state refreshed from db'; resetting next bounties reset");
                }
                else if ((match = regexCetusJoin.Match(args.Content)).Success)
                {
                    Trace.WriteLine($"EE.log: Loaded Cetus: {match.Groups[1].Value}");
                }
                else if ((match = regexArbitration.Match(args.Content)).Success)
                {
                    Trace.WriteLine($"EE.log: Arbitration: {match.Groups[1].Value}");
                    try
                    {
                        var solNode = WfcdWorldStateData.GetSolNode(match.Groups[1].Value);
                        ArbitrationSolNodeUpdated = args.DateTime;
                        ArbitrationSolNode = solNode;
                    }
                    catch (Exception ex)
                    {
                        Trace.WriteLine($"EE.log: Unknown SolNode");
                        Trace.WriteLine(ex);
                    }
                }
                else if ((match = regexKuvaFlood.Match(args.Content)).Success)
                {
                    if (match.Groups[1].Value == "6" || match.Groups[1].Value == "12")
                    {
                        Trace.WriteLine($"EE.log: Arbitration: {match.Groups[1].Value}");
                        try
                        {
                            Trace.WriteLine($"EE.log: Kuva Flood: {match.Groups[2].Value} " +
                                $"({match.Groups[3].Value} - {match.Groups[4].Value})");
                            var solNode = WfcdWorldStateData.GetSolNodeByName(match.Groups[3].Value,
                                match.Groups[4].Value);
                            KuvaFloodSolNodeUpdated = args.DateTime;
                            KuvaFloodSolNode = solNode;
                        }
                        catch (Exception ex)
                        {
                            Trace.WriteLine($"EE.log: Unknown SolNode");
                            Trace.WriteLine(ex);
                        }
                    }
                }
            };

            parser.OnLogFileReset += (sender, args) =>
            {
                NextBountiesReset = null;
            };

            _eeLogFetchTimer = new Timer(state =>
            {
                try
                {
                    parser.Update();
                }
                catch (Exception ex)
                {
                    Trace.WriteLine($"EE.log: Failed to parse EE.log: {ex}");
                    Trace.WriteLine(ex.StackTrace);
                }
            }, null, TimeSpan.Zero, TimeSpan.FromSeconds(5));
        }
    }
}