aboutsummaryrefslogtreecommitdiffstats
path: root/WarframeClock/Program.cs
blob: 1e9c658c56acc219a68bcc571b13c004a0be7256 (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
using System;
using System.Net.Http;
using System.Threading;
using System.Windows.Forms;
using Newtonsoft.Json;

namespace WarframeClock
{
    class Program
    {
        private static bool UseWorldStatePhp = false;
        private static string VersionString = "Warframe Clock Overlay 2019-01-15";

        public class WorldStateDataStruct
        {
            public long CetusExpiry { get; set; } = -1;

            public string CetusExpiryString()
            {
                double currentTime = GetCurrentTime();
                double cetusLen = 8998.8748;
                double cetusDayLen = 8248.9686 - 2249.7187;
                double cetusBegin = 1509371722 + 2249.7187;

                if (UseWorldStatePhp)
                {
                    if (CetusExpiry < 0)
                        return "Plains: <NOT READY>";
                    cetusBegin = CetusExpiry / 1000 - cetusLen;
                }
                cetusBegin += Math.Floor((currentTime - cetusBegin) / cetusLen) * cetusLen;

                if (currentTime < cetusBegin + cetusDayLen)
                    return $"Plains: ☀ {GetReadableTimeSpan(cetusBegin + cetusDayLen - currentTime)}";
                else
                    return $"Plains: 🌙 {GetReadableTimeSpan(cetusBegin + cetusLen - currentTime)}";
            }

            public string VallisExpiryString()
            {
                double currentTime = GetCurrentTime();
                double dayStart = 1542131224;
                dayStart += Math.Floor((currentTime - dayStart) / 1600) * 1600;

                if (currentTime - dayStart < 400)
                    return $"Vallis: ❄️[→☁] {GetReadableTimeSpan(dayStart + 400 - currentTime)}";
                else if (currentTime - dayStart < 800)
                    return $"Vallis: ☁[→🌣] {GetReadableTimeSpan(dayStart + 800 - currentTime)}";
                else if (currentTime - dayStart < 1200)
                    return $"Vallis: 🌣[→☁] {GetReadableTimeSpan(dayStart + 1200 - currentTime)}";
                else
                    return $"Vallis: ☁[→❄️] {GetReadableTimeSpan(dayStart + 1600 - currentTime)}";
            }

            private double GetCurrentTime() => DateTime.UtcNow.Subtract(new DateTime(1970, 1, 1)).TotalMilliseconds / 1000;

            private string GetReadableTimeSpan(double span) =>
                $"{Math.Floor(span / 60)}m{Math.Floor(span % 60)}s";
        }

        public static WorldStateDataStruct WorldStateData = new WorldStateDataStruct();

        [STAThread]
        static void Main(string[] args)
        {
            StartNotifyIcon();
            StartOverlay();

            if (UseWorldStatePhp)
            {
                StartWorldStateFetch();
            }
        }

        private static void StartNotifyIcon()
        {
            var thread = new Thread(delegate ()
            {
                var iconMenu = new ContextMenu();
                var icon = new NotifyIcon()
                {
                    Text = "Warframe Clock",
                    ContextMenu = iconMenu,
                    Icon = Properties.Resources.TrayIcon
                };

                iconMenu.MenuItems.Add(new MenuItem(VersionString) { Enabled = false });
                iconMenu.MenuItems.Add(new MenuItem("Quit", (_, __) =>
                {
                    icon.Dispose();
                    Environment.Exit(0);
                }));
                icon.Visible = true;

                Application.Run();
            });
            thread.Start();
        }

        private static void StartWorldStateFetch()
        {
            var httpClient = new HttpClient();

            var period = 10 * 60 * 1000; // 10 Minutes
            var timer = new System.Threading.Timer(state =>
            {
                lock (state)
                {
                    var res = httpClient.GetAsync("http://content.warframe.com/dynamic/worldState.php").Result;
                    if (!res.IsSuccessStatusCode)
                    {
                        Console.WriteLine("WorldState: Could not fetch worldState.php");
                        return;
                    }

                    var json = res.Content.ReadAsStringAsync().Result;
                    dynamic obj = JsonConvert.DeserializeObject(json);

                    dynamic cetusSyndicate = null;
                    foreach (var syndicateMission in obj.SyndicateMissions)
                    {
                        if (syndicateMission.Tag == "CetusSyndicate")
                            cetusSyndicate = syndicateMission;
                    }
                    if (cetusSyndicate == null)
                    {
                        Console.WriteLine("WorldState: CetusSyndicate missions not found");
                        return;
                    }

                    dynamic cetusExpiry = cetusSyndicate.Expiry["$date"]["$numberLong"];
                    Console.WriteLine($"WorldState: CetusSyndicate missions expire at {cetusExpiry}");

                    WorldStateData.CetusExpiry = cetusExpiry;
                }
            }, new object(), 0, period);
        }

        private static void StartOverlay()
        {
            var plugin = new ClockOverlay(WorldStateData, "Warframe.x64");
            while (true)
            {
                plugin.Update();
                Thread.Sleep(16);
            }
        }
    }
}