-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathEdRouter.cs
More file actions
245 lines (209 loc) · 4.61 KB
/
EdRouter.cs
File metadata and controls
245 lines (209 loc) · 4.61 KB
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
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using libspanch;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
namespace ED_Router
{
public class EdRouter : INotifyPropertyChanged
{
private static readonly EdRouter _instance = new EdRouter();
private static string settingsFile = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData), "ed-router\\settings.json");
public static EdRouter Instance
{
get
{
return _instance;
}
}
private EdRouter()
{
settingsFile = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData), "ed-router\\settings.json");
_api = new SpanchApi();
_jMon = new JournalMonitor();
_jMon.NewLocation += _jMon_NewLocation;
var backgroundTask = Task.Factory.StartNew(() =>
{
_jMon.Start();
//do something with the result
});
_range = 20;
_efficiency = 60;
LoadSettings();
}
private void _jMon_NewLocation(string obj)
{
Start = obj;
}
private SpanchApi _api;
private int _currentWaypoint;
private JournalMonitor _jMon;
private string _destination;
private double _range;
private int _efficiency;
private Route _route;
private SystemJump _currentWaypoint1;
public event PropertyChangedEventHandler PropertyChanged;
public Route Route
{
get => _route;
set
{
_route = value;
OnPropertyChanged("Route");
}
}
private string _start;
public string Start
{
get { return _start; }
set
{
if (_start == value)
return;
var list = _api.GetSystems(value);
if (list.Contains(value))
_start = value;
else
_start = string.Empty;
OnPropertyChanged("Start");
SaveSettings();
}
}
public string Destination
{
get { return _destination; }
set
{
if (_destination == value)
return;
var list = _api.GetSystems(value);
if (list.Contains(value))
_destination = value;
else
_destination = string.Empty;
SaveSettings();
OnPropertyChanged("Destination");
}
}
public double Range
{
get => _range;
set
{
if (_range == value)
return;
_range = value;
SaveSettings();
OnPropertyChanged("Range");
}
}
public int Efficiency
{
get => _efficiency;
set
{
if (_efficiency == value)
return;
_efficiency = value;
SaveSettings();
OnPropertyChanged("Efficiency");
}
}
public void CalculateRoute()
{
if (Start?.Length > 0 && Destination?.Length > 0)
{
var route = _api.PlotRoute(Start, Destination, Range, Efficiency);
if (route.TotalJumps > 0)
{
Route = route;
_currentWaypoint = 0;
CurrentWaypoint = Route.SystemJumps.ElementAt(0);
}
}
else
throw new Exception("Start or Destination is empty.");
}
public string GetCurrentSystem()
{
return "";
}
public SystemJump NextWaypoint()
{
if (Route.TotalJumps > 0 && _currentWaypoint + 1 < Route.SystemJumps.Count)
{
var next = Route.SystemJumps[++_currentWaypoint];
CurrentWaypoint = next;
}
return CurrentWaypoint;
}
public SystemJump PreviousWaypoint()
{
if (Route.TotalJumps > 0 && _currentWaypoint - 1 >= 0)
{
var next = Route.SystemJumps[--_currentWaypoint];
CurrentWaypoint = next;
}
return CurrentWaypoint;
}
public SystemJump CurrentWaypoint
{
get => _currentWaypoint1;
private set
{
if (_currentWaypoint1 == value)
return;
_currentWaypoint1 = value;
OnPropertyChanged("CurrentWaypoint");
}
}
public List<string> GetSystems(string value)
{
return _api.GetSystems(value);
}
public void SaveSettings()
{
Directory.CreateDirectory(Path.GetDirectoryName(settingsFile));
using (StreamWriter file = File.CreateText(settingsFile))
{
JObject o = JObject.FromObject(new
{
settings = new
{
destination = Destination,
start = Start,
efficiency = Efficiency,
range = Range
}
});
file.Write(o.ToString());
}
}
public void LoadSettings()
{
try
{
if (File.Exists(settingsFile))
{
dynamic o1 = JObject.Parse(File.ReadAllText(settingsFile));
_start = o1.settings.start;
_destination = o1.settings.destination;
_range = o1.settings.range;
_efficiency = o1.settings.efficiency;
}
}
catch (Exception) { }
}
// Create the OnPropertyChanged method to raise the event
protected void OnPropertyChanged(string name)
{
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(name));
}
}
}