-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathUtil.cs
277 lines (259 loc) · 14.5 KB
/
Util.cs
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
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Net;
using System.Reflection;
using VatsimData;
namespace VatBoardCons
{
public static class Util
{
public const string ascVATSIM = @"██╗ ██╗ █████╗ ████████╗██████╗ ██████╗ █████╗ ██████╗ ██████╗
██║ ██║██╔══██╗╚══██╔══╝██╔══██╗██╔═══██╗██╔══██╗██╔══██╗██╔══██╗
██║ ██║███████║ ██║ ██████╔╝██║ ██║███████║██████╔╝██║ ██║
╚██╗ ██╔╝██╔══██║ ██║ ██╔══██╗██║ ██║██╔══██║██╔══██╗██║ ██║
╚████╔╝ ██║ ██║ ██║ ██████╔╝╚██████╔╝██║ ██║██║ ██║██████╔╝
╚═══╝ ╚═╝ ╚═╝ ╚═╝ ╚═════╝ ╚═════╝ ╚═╝ ╚═╝╚═╝ ╚═╝╚═════╝
";
public const string ascARRIVALS = @" █████╗ ██████╗ ██████╗ ██╗██╗ ██╗ █████╗ ██╗ ███████╗
██╔══██╗██╔══██╗██╔══██╗██║██║ ██║██╔══██╗██║ ██╔════╝
███████║██████╔╝██████╔╝██║██║ ██║███████║██║ ███████╗
██╔══██║██╔══██╗██╔══██╗██║╚██╗ ██╔╝██╔══██║██║ ╚════██║
██║ ██║██║ ██║██║ ██║██║ ╚████╔╝ ██║ ██║███████╗███████║
╚═╝ ╚═╝╚═╝ ╚═╝╚═╝ ╚═╝╚═╝ ╚═══╝ ╚═╝ ╚═╝╚══════╝╚══════╝
"
;
public const string ascDEPARTURES = @"██████╗ ███████╗██████╗ █████╗ ██████╗ ████████╗██╗ ██╗██████╗ ███████╗███████╗
██╔══██╗██╔════╝██╔══██╗██╔══██╗██╔══██╗╚══██╔══╝██║ ██║██╔══██╗██╔════╝██╔════╝
██║ ██║█████╗ ██████╔╝███████║██████╔╝ ██║ ██║ ██║██████╔╝█████╗ ███████╗
██║ ██║██╔══╝ ██╔═══╝ ██╔══██║██╔══██╗ ██║ ██║ ██║██╔══██╗██╔══╝ ╚════██║
██████╔╝███████╗██║ ██║ ██║██║ ██║ ██║ ╚██████╔╝██║ ██║███████╗███████║
╚═════╝ ╚══════╝╚═╝ ╚═╝ ╚═╝╚═╝ ╚═╝ ╚═╝ ╚═════╝ ╚═╝ ╚═╝╚══════╝╚══════╝
";
//:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
//::: :::
//::: This routine calculates the distance between two points (given the :::
//::: latitude/longitude of those points). It is being used to calculate :::
//::: the distance between two locations using GeoDataSource(TM) products :::
//::: :::
//::: Definitions: :::
//::: South latitudes are negative, east longitudes are positive :::
//::: :::
//::: Passed to function: :::
//::: lat1, lon1 = Latitude and Longitude of point 1 (in decimal degrees) :::
//::: lat2, lon2 = Latitude and Longitude of point 2 (in decimal degrees) :::
//::: unit = the unit you desire for results :::
//::: where: 'M' is statute miles (default) :::
//::: 'K' is kilometers :::
//::: 'N' is nautical miles :::
//::: :::
//::: Worldwide cities and other features databases with latitude longitude :::
//::: are available at https://www.geodatasource.com :::
//::: :::
//::: For enquiries, please contact [email protected] :::
//::: :::
//::: Official Web site: https://www.geodatasource.com :::
//::: :::
//::: GeoDataSource.com (C) All Rights Reserved 2018 :::
//::: :::
//:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
public static double distance(double lat1, double lon1, double lat2, double lon2, char unit)
{
if ((lat1 == lat2) && (lon1 == lon2))
{
return 0;
}
else
{
double theta = lon1 - lon2;
double dist = Math.Sin(deg2rad(lat1)) * Math.Sin(deg2rad(lat2)) + Math.Cos(deg2rad(lat1)) * Math.Cos(deg2rad(lat2)) * Math.Cos(deg2rad(theta));
dist = Math.Acos(dist);
dist = rad2deg(dist);
dist = dist * 60 * 1.1515;
if (unit == 'K')
{
dist = dist * 1.609344;
}
else if (unit == 'N')
{
dist = dist * 0.8684;
}
return (dist);
}
}
//:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
//:: This function converts decimal degrees to radians :::
//:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
public static double deg2rad(double deg)
{
return (deg * Math.PI / 180.0);
}
//:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
//:: This function converts radians to decimal degrees :::
//:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
public static double rad2deg(double rad)
{
return (rad / Math.PI * 180.0);
}
public static void typeWriter(string _str, int speed, ConsoleColor _colFG = ConsoleColor.White, ConsoleColor _colBG = ConsoleColor.Black)
{
ConsoleColor currentForeground = Console.ForegroundColor;
ConsoleColor currentBackground = Console.BackgroundColor;
Console.ForegroundColor = _colFG;
Console.BackgroundColor = _colBG;
for (int i = 0; i < _str.Length; i++)
{
Console.Write(_str[i]);
System.Threading.Thread.Sleep(speed);
if (Console.CursorTop == Console.WindowHeight - 1 && Console.CursorLeft == 0)
{
System.Threading.Thread.Sleep(4000);
for (int _i = 6; _i <= Console.WindowHeight - 1; _i++)
{
Console.SetCursorPosition(0, _i);
Console.Write(new string(' ', Console.WindowWidth - 2));
}
Console.SetCursorPosition(0, 6);
}
}
Console.ForegroundColor = currentForeground;
Console.BackgroundColor = currentBackground;
}
public static List<Airport> LoadAirports()
{
var _ret = new List<Airport>();
string[] _data = File.ReadAllLines("Airports.txt");
foreach (string _d in _data)
{
string[] col;
string[] col1 =
col = _d.Split(":");
if (col[1].Contains(","))
{
col1 = col[1].Split(",");
}
else
{
col1[0] = "0.0";
col1[1] = "0.0";
}
_ret.Add(new Airport()
{
code = col[0],
lat = col1[1],
lon = col1[0]
});
}
return _ret;
}
public static void WriteLn(string _str, ConsoleColor _background, ConsoleColor _foreground, bool _lf = true)
{
ConsoleColor currentBackground = Console.BackgroundColor;
ConsoleColor currentForeground = Console.ForegroundColor;
Console.BackgroundColor = _background;
Console.ForegroundColor = _foreground;
if (_lf)
{
Console.WriteLine(_str);
}
else
{
Console.Write(_str);
}
Console.BackgroundColor = currentBackground;
Console.ForegroundColor = currentForeground;
}
public static List<VatLine> DownloadVatsimData(string _uri, string dataFile, string _interval)
{
var Airports = new List<Airport>();
List<VatLine> dataList = new List<VatLine>();
DateTime lastDownload = File.GetLastWriteTime(dataFile);
TimeSpan span = DateTime.Now.Subtract(lastDownload);
Airports = LoadAirports();
if (span.TotalMinutes > Convert.ToDouble(_interval))
{
WriteLn("\nDownloading VATSIM data ...", ConsoleColor.Black, ConsoleColor.Yellow, false);
try
{
VatsimData.VatsimData.GetData(_uri);
WriteLn("DONE!", ConsoleColor.Black, ConsoleColor.Yellow);
SetLastDownload(false, dataFile);
}
catch (WebException wex)
{
WriteLn(string.Format("\nERROR!! => [{0}]]", wex.Message), ConsoleColor.Red, ConsoleColor.White);
WriteLn(string.Format("Press any key to exit.", wex.Message), ConsoleColor.Red, ConsoleColor.White);
Console.ReadKey();
System.Environment.Exit(-1);
}
}
var fp = new FlightPlan();
foreach (var pilot in VatsimData.VatsimData.Data.Pilots)
{
string user_lat_dep = "0.0";
string user_lon_dep = "0.0";
string user_lat_dest = "0.0";
string user_lon_dest = "0.0";
if (pilot.FlightPlan != null)
{
fp = (FlightPlan)pilot.FlightPlan;
if (Airports.Any(x => x.code == fp.Departure))
{
user_lat_dep = Airports.Find(x => x.code == fp.Departure).lat;
user_lon_dep = Airports.Find(x => x.code == fp.Departure).lon;
}
if (Airports.Any(x => x.code == fp.Arrival))
{
user_lat_dest = Airports.Find(x => x.code == fp.Arrival).lat;
user_lon_dest = Airports.FirstOrDefault(x => x.code == fp.Arrival).lon;
}
}
dataList.Add(new VatLine
{
callsign = pilot.Callsign,
planned_depairport = fp.Departure,
planned_destairport = fp.Arrival,
planned_aircraft = fp.Aircraft,
planned_tascruise = fp.CruiseTAS,
altitude = fp.Altitude,
lat = pilot.Latitude.ToString(),
lon = pilot.Longitude.ToString(),
DistanceTo = distance(
Convert.ToDouble(user_lat_dest.Replace(".", ",")),
Convert.ToDouble(user_lon_dest.Replace(".", ",")),
Convert.ToDouble(pilot.Latitude.ToString().Replace(".", ",")),
Convert.ToDouble(pilot.Longitude.ToString().Replace(".", ",")), 'N'),
DistanceFrom = distance(
Convert.ToDouble(user_lat_dep.Replace(".", ",")),
Convert.ToDouble(user_lon_dep.Replace(".", ",")),
Convert.ToDouble(pilot.Latitude.ToString().Replace(".", ",")),
Convert.ToDouble(pilot.Longitude.ToString().Replace(".", ",")), 'N'),
});
}
return dataList;
}
public static string GetVersion()
{
string gitVersion = String.Empty;
using (Stream stream = Assembly.GetExecutingAssembly()
.GetManifestResourceStream("VatBoard.version.txt"))
using (StreamReader reader = new StreamReader(stream))
{
gitVersion = reader.ReadLine();
}
return gitVersion;
}
public static void SetLastDownload(Boolean isFirstTime, string dataFile)
{
if (isFirstTime)
{
File.SetLastWriteTimeUtc(dataFile, new DateTime(1974, 3, 25));
}
else
{
File.SetLastWriteTimeUtc(dataFile, DateTime.UtcNow);
}
}
}
}