aboutsummaryrefslogtreecommitdiffstats
path: root/WarframeClock/App.xaml.cs
blob: 5302dbc8fe2b4cd0ff570508129630e86fc8724e (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
158
159
160
161
162
163
164
165
166
167
using System;
using System.Diagnostics;
using System.Globalization;
using System.IO;
using System.Linq;
using System.Text.RegularExpressions;
using System.Threading;
using System.Windows;

namespace WarframeClock
{
    /// <summary>
    /// Interaction logic for App.xaml
    /// </summary>
    public partial class App : Application
    {
        public static readonly string VersionString = "Warframe Clock Overlay 2019-07-20";
        public static readonly string WebsiteUriString = "https://poepoe.org/warframe/clock/";
        private static readonly bool DEBUG = false;
        public static readonly Uri WebsiteUri = new Uri(WebsiteUriString);

        public static readonly AppData AppData = new AppData();
        private Timer _worldStateFetchTimer, _eeLogFetchTimer;
        private string _debugTracePath;

        protected override void OnStartup(StartupEventArgs e)
        {
            base.OnStartup(e);
            if (DEBUG)
            {
                _debugTracePath = Path.GetTempFileName();
                Trace.Listeners.Add(new TextWriterTraceListener(File.Create(_debugTracePath)));
            }
            StartWorldStateFetchTimer();
            StartEeLogFetchTimer();
        }

        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}");
                    AppData.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;
                    AppData.NextBountiesReset = reset;
                    Trace.WriteLine($"EE.log: 'syncing world state jobs in'; next bounties reset: {reset}");
                }
                else if ((match = regexWorldStateRefreshedFromDb.Match(args.Content)).Success)
                {
                    if (AppData.NextBountiesReset == null)
                        return;
                    AppData.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);
                        AppData.ArbitrationSolNodeUpdated = args.DateTime;
                        AppData.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);
                            AppData.KuvaFloodSolNodeUpdated = args.DateTime;
                            AppData.KuvaFloodSolNode = solNode;
                        }
                        catch (Exception ex)
                        {
                            Trace.WriteLine($"EE.log: Unknown SolNode");
                            Trace.WriteLine(ex);
                        }
                    }
                }
            };

            parser.OnLogFileReset += (sender, args) =>
            {
                AppData.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));
        }

        internal void Terminate()
        {
            _worldStateFetchTimer.Change(Timeout.Infinite, Timeout.Infinite);
            _eeLogFetchTimer.Change(Timeout.Infinite, Timeout.Infinite);
            MainWindow?.Close();
            if (DEBUG)
            {
                Trace.Flush();
                MessageBox.Show("Debug log file is located at: " + _debugTracePath);
            }
        }
    }
}