-
Notifications
You must be signed in to change notification settings - Fork 14
/
Copy pathMain.cs
414 lines (335 loc) · 16.9 KB
/
Main.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
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
using System;
using System.Collections.Generic;
using System.Data;
using System.Drawing;
using System.IO;
using System.Windows.Forms;
using System.Xml.Serialization;
namespace EliteDangerousTradingAssistant
{
public partial class Main : Form
{
private GameData gameData;
private DataTable bindingTable;
public Main()
{
InitializeComponent();
this.Icon = new Icon("Graphics\\EliteDangerousIcon.ico");
gameData = new GameData();
SystemComboBox.Items.Clear();
StationComboBox.Items.Clear();
}
private void AddSystemButton_Click(object sender, System.EventArgs e)
{
AddSystemDialog dialog = new AddSystemDialog();
dialog.ShowDialog();
if (dialog.Result == null)
return;
StarSystem newSystem = dialog.Result;
gameData.StarSystems.Add(newSystem);
SystemComboBox.Items.Add(newSystem.Name);
SystemComboBox.SelectedIndex = SystemComboBox.Items.Count - 1;
}
private void RemoveSystemButton_Click(object sender, System.EventArgs e)
{
if (gameData.StarSystems.Count == 0)
return;
int selectedIndex = SystemComboBox.SelectedIndex;
SystemComboBox.Items.RemoveAt(selectedIndex);
gameData.StarSystems.RemoveAt(selectedIndex);
if (gameData.StarSystems.Count > 0)
SystemComboBox.SelectedIndex = Math.Min(selectedIndex, Math.Max(gameData.StarSystems.Count - 1, 0));
else
{
SystemComboBox.Text = string.Empty;
StationComboBox.Items.Clear();
StationComboBox.Text = string.Empty;
BindCommodities();
}
}
private void SystemComboBox_SelectedIndexChanged(object sender, EventArgs e)
{
if (SystemComboBox.SelectedIndex < 0)
return;
StarSystem selectedSystem = gameData.StarSystems[SystemComboBox.SelectedIndex];
StationComboBox.Items.Clear();
foreach (Station station in selectedSystem.Stations)
StationComboBox.Items.Add(station.Name);
if (StationComboBox.Items.Count > 0)
StationComboBox.SelectedIndex = 0;
else
{
StationComboBox.Text = string.Empty;
BindCommodities();
}
}
private void AddStationButton_Click(object sender, EventArgs e)
{
if (SystemComboBox.Items.Count == 0)
return;
AddStationDialog dialog = new AddStationDialog(gameData.StarSystems[SystemComboBox.SelectedIndex].Name);
dialog.ShowDialog();
if (dialog.Result == null)
return;
gameData.StarSystems[SystemComboBox.SelectedIndex].Stations.Add(dialog.Result);
StationComboBox.Items.Add(dialog.Result.Name);
StationComboBox.SelectedIndex = StationComboBox.Items.Count - 1;
}
private void RemoveStationButton_Click(object sender, EventArgs e)
{
if (gameData.StarSystems.Count == 0 || gameData.StarSystems[SystemComboBox.SelectedIndex].Stations.Count == 0)
return;
int selectedStationIndex = StationComboBox.SelectedIndex;
StarSystem selectedSystem = gameData.StarSystems[SystemComboBox.SelectedIndex];
StationComboBox.Items.RemoveAt(selectedStationIndex);
selectedSystem.Stations.RemoveAt(selectedStationIndex);
if (selectedSystem.Stations.Count > 0)
StationComboBox.SelectedIndex = Math.Min(selectedStationIndex, Math.Max(selectedSystem.Stations.Count - 1, 0));
else
{
StationComboBox.Text = string.Empty;
BindCommodities();
}
}
private void StationComboBox_SelectedIndexChanged(object sender, EventArgs e)
{
BindCommodities();
}
private void BindCommodities()
{
bindingTable = new DataTable();
bindingTable.Columns.Add("Index");
bindingTable.Columns.Add("Commodity");
bindingTable.Columns.Add("SellPrice");
bindingTable.Columns.Add("BuyPrice");
bindingTable.Columns.Add("Supply");
bindingTable.Columns.Add("LastUpdated");
bindingTable.Columns["Index"].DataType = System.Type.GetType("System.Decimal");
bindingTable.Columns["SellPrice"].DataType = System.Type.GetType("System.Decimal");
bindingTable.Columns["BuyPrice"].DataType = System.Type.GetType("System.Decimal");
bindingTable.Columns["Supply"].DataType = System.Type.GetType("System.Decimal");
bindingTable.Columns["Supply"].DataType = System.Type.GetType("System.Decimal");
bindingTable.Columns["LastUpdated"].DataType = System.Type.GetType("System.DateTime");
CommoditiesGrid.Columns.Clear();
if (SystemComboBox.SelectedIndex < 0 || gameData.StarSystems.Count == 0)
return;
StarSystem selectedSystem = gameData.StarSystems[SystemComboBox.SelectedIndex];
if (StationComboBox.SelectedIndex < 0 || selectedSystem.Stations.Count == 0)
return;
Station selectedStation = selectedSystem.Stations[StationComboBox.SelectedIndex];
int index = 0;
foreach (Commodity commodity in selectedStation.Commodities)
{
DataRow newRow = bindingTable.NewRow();
newRow["Index"] = index;
newRow["Commodity"] = commodity.Name;
newRow["BuyPrice"] = commodity.BuyPrice;
newRow["SellPrice"] = commodity.SellPrice;
newRow["Supply"] = commodity.Supply;
newRow["LastUpdated"] = commodity.LastUpdated;
bindingTable.Rows.Add(newRow);
index++;
}
CommoditiesGrid.AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.Fill;
CommoditiesGrid.DataSource = bindingTable;
CommoditiesGrid.Columns["Index"].Visible = false;
DataGridViewButtonColumn deleteColumn = new DataGridViewButtonColumn();
deleteColumn.Name = "Delete";
deleteColumn.HeaderText = "Delete";
deleteColumn.Text = "Delete";
deleteColumn.UseColumnTextForButtonValue = true;
CommoditiesGrid.Columns.Insert(0, deleteColumn);
DataGridViewButtonColumn upColumn = new DataGridViewButtonColumn();
upColumn.Name = "Up";
upColumn.HeaderText = "Up";
upColumn.Text = "▲";
upColumn.UseColumnTextForButtonValue = true;
CommoditiesGrid.Columns.Insert(7, upColumn);
DataGridViewButtonColumn downColumn = new DataGridViewButtonColumn();
downColumn.Name = "Down";
downColumn.HeaderText = "Down";
downColumn.Text = "▼";
downColumn.UseColumnTextForButtonValue = true;
CommoditiesGrid.Columns.Insert(8, downColumn);
}
private void AddCommodityButton_Click(object sender, EventArgs e)
{
if (gameData.StarSystems.Count == 0 || gameData.StarSystems[SystemComboBox.SelectedIndex].Stations.Count == 0)
return;
StarSystem selectedSystem = gameData.StarSystems[SystemComboBox.SelectedIndex];
Station selectedStation = selectedSystem.Stations[StationComboBox.SelectedIndex];
AddEditCommodityDialog dialog = new AddEditCommodityDialog(selectedSystem.Name, selectedStation.Name, gameData.StarSystems);
dialog.ShowDialog();
if (dialog.Result == null)
return;
Commodity newCommodity = dialog.Result;
newCommodity.LastUpdated = DateTime.Now;
selectedStation.Commodities.Add(newCommodity);
BindCommodities();
}
private void CommoditiesGrid_CellEndEdit(object sender, DataGridViewCellEventArgs e)
{
switch(CommoditiesGrid.Columns[e.ColumnIndex].Name)
{
case "Commodity":
case "BuyPrice":
case "SellPrice":
case "Supply":
UpdateSelectedCommodity(CommoditiesGrid.Rows[e.RowIndex]);
break;
}
}
private void CalculateAllTradesManifestsAndRoutesButton_Click(object sender, EventArgs e)
{
gameData.Capital = CapitalNumericUpDown.Value;
gameData.CargoSlots = CargoSlotsNumericUpDown.Value;
if (gameData.Capital <= 0 || gameData.CargoSlots <= 0)
{
MessageBox.Show("Please provide your available capital and cargo slots.", "Error", MessageBoxButtons.OK);
return;
}
Adviser.CalculateAll(gameData);
if (gameData.Trades.Count > 0)
{
TradesManifestsRoutesDialog dialog = new TradesManifestsRoutesDialog(gameData);
dialog.Show();
}
else
MessageBox.Show("No trades found. Make sure more than one system is up to date. Check your spelling on the commodities.", "Error: No trades found.", MessageBoxButtons.OK);
}
private void SaveButton_Click(object sender, EventArgs e)
{
SaveFileDialog saveFileDialog = new SaveFileDialog();
saveFileDialog.AddExtension = true;
saveFileDialog.DefaultExt = ".edassistant";
saveFileDialog.InitialDirectory = string.Concat(System.IO.Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly().Location), @"\SaveData");
saveFileDialog.RestoreDirectory = true;
saveFileDialog.Filter = "Elite: Dangerous Assistant (.edassistant)|*.edassistant";
saveFileDialog.FilterIndex = 0;
if (saveFileDialog.ShowDialog() == DialogResult.OK)
{
Stream fileStream = saveFileDialog.OpenFile();
XmlSerializer serializer = new XmlSerializer(typeof(GameData));
serializer.Serialize(fileStream, gameData);
fileStream.Close();
}
}
private void LoadButton_Click(object sender, EventArgs e)
{
OpenFileDialog openFileDialog = new OpenFileDialog();
openFileDialog.AddExtension = true;
openFileDialog.DefaultExt = ".edassistant";
openFileDialog.InitialDirectory = string.Concat(System.IO.Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly().Location), @"\SaveData");
openFileDialog.RestoreDirectory = true;
openFileDialog.Filter = "Elite: Dangerous Assistant (.edassistant)|*.edassistant";
openFileDialog.FilterIndex = 0;
TryOpenSaveFile_Current(openFileDialog);
}
private void TryOpenSaveFile_Current(OpenFileDialog openFileDialog)
{
try
{
if (openFileDialog.ShowDialog() == DialogResult.OK)
{
Stream fileStream = openFileDialog.OpenFile();
XmlSerializer serializer = new XmlSerializer(typeof(GameData));
gameData = serializer.Deserialize(fileStream) as GameData;
if (gameData == null)
throw new Exception();
SystemComboBox.Items.Clear();
StationComboBox.Items.Clear();
foreach (StarSystem starSystem in gameData.StarSystems)
SystemComboBox.Items.Add(starSystem.Name);
SystemComboBox.SelectedIndex = 0;
}
}
catch
{
TryOpenSaveFile_0004(openFileDialog);
}
}
private void TryOpenSaveFile_0004(OpenFileDialog openFileDialog)
{
try
{
Stream fileStream = openFileDialog.OpenFile();
XmlSerializer serializer = new XmlSerializer(typeof(List<StarSystem>));
List<StarSystem> starSystems = serializer.Deserialize(fileStream) as List<StarSystem>;
if (starSystems == null || starSystems.Count == 0)
throw new Exception();
gameData = new GameData();
SystemComboBox.Items.Clear();
StationComboBox.Items.Clear();
foreach (StarSystem starSystem in starSystems)
{
SystemComboBox.Items.Add(starSystem.Name);
gameData.StarSystems.Add(starSystem);
}
SystemComboBox.SelectedIndex = 0;
}
catch
{
MessageBox.Show("The save file selected cannot be loaded. There are two possible reasons:\n\n1. It is corrupted\n2. It is from a version earlier than 0.5 and is not compatible.", "Error", MessageBoxButtons.OK);
}
}
private void UpdateSelectedCommodity(DataGridViewRow commodity)
{
if (gameData.StarSystems.Count == 0 || SystemComboBox.SelectedIndex < 0)
return;
StarSystem selectedSystem = gameData.StarSystems[SystemComboBox.SelectedIndex];
if (selectedSystem.Stations.Count == 0 || StationComboBox.SelectedIndex < 0)
return;
Station selectedStation = selectedSystem.Stations[StationComboBox.SelectedIndex];
int editedCommodityIndex = -1;
int.TryParse(commodity.Cells["Index"].Value.ToString(), out editedCommodityIndex);
if (editedCommodityIndex < 0)
return;
Commodity editedCommodity = selectedStation.Commodities[editedCommodityIndex];
string editedName = (string)commodity.Cells["Commodity"].Value;
decimal editedBuy = (decimal)commodity.Cells["BuyPrice"].Value;
decimal editedSell = (decimal)commodity.Cells["SellPrice"].Value;
decimal editedSupply = (decimal)commodity.Cells["Supply"].Value;
editedCommodity.Name = editedName;
editedCommodity.BuyPrice = editedBuy;
editedCommodity.SellPrice = editedSell;
editedCommodity.Supply = editedSupply;
editedCommodity.LastUpdated = DateTime.Now;
commodity.Cells["LastUpdated"].Value = editedCommodity.LastUpdated;
}
private void CommoditiesGrid_CellContentClick(object sender, DataGridViewCellEventArgs e)
{
if (CommoditiesGrid.Columns[e.ColumnIndex].Name != "Delete" &&
CommoditiesGrid.Columns[e.ColumnIndex].Name != "Up" &&
CommoditiesGrid.Columns[e.ColumnIndex].Name != "Down")
return;
int selectedSystemIndex = SystemComboBox.SelectedIndex;
int selectedStationIndex = StationComboBox.SelectedIndex;
int selectedCommodityIndex = int.Parse(CommoditiesGrid.Rows[e.RowIndex].Cells["Index"].Value.ToString());
switch (CommoditiesGrid.Columns[e.ColumnIndex].Name.ToString())
{
case "Delete":
gameData.StarSystems[selectedSystemIndex].Stations[selectedStationIndex].Commodities.RemoveAt(selectedCommodityIndex);
break;
case "Up":
if (selectedCommodityIndex > 0)
{
Commodity temp = gameData.StarSystems[selectedSystemIndex].Stations[selectedStationIndex].Commodities[selectedCommodityIndex - 1];
gameData.StarSystems[selectedSystemIndex].Stations[selectedStationIndex].Commodities[selectedCommodityIndex - 1] = gameData.StarSystems[selectedSystemIndex].Stations[selectedStationIndex].Commodities[selectedCommodityIndex];
gameData.StarSystems[selectedSystemIndex].Stations[selectedStationIndex].Commodities[selectedCommodityIndex] = temp;
}
break;
case "Down":
if (selectedCommodityIndex < gameData.StarSystems[selectedSystemIndex].Stations[selectedStationIndex].Commodities.Count - 1)
{
Commodity temp = gameData.StarSystems[selectedSystemIndex].Stations[selectedStationIndex].Commodities[selectedCommodityIndex];
gameData.StarSystems[selectedSystemIndex].Stations[selectedStationIndex].Commodities[selectedCommodityIndex] = gameData.StarSystems[selectedSystemIndex].Stations[selectedStationIndex].Commodities[selectedCommodityIndex + 1];
gameData.StarSystems[selectedSystemIndex].Stations[selectedStationIndex].Commodities[selectedCommodityIndex + 1] = temp;
}
break;
default:
break;
}
BindCommodities();
}
}
}