-
Notifications
You must be signed in to change notification settings - Fork 0
/
Main.cs
364 lines (308 loc) · 7.95 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
using ChunkPriorityCalculator.Model;
using ChunkPriorityCalculator.Rules;
using System.Reflection;
using System.Text.Json;
namespace ChunkPriorityCalculator
{
public partial class Main : Form
{
#region Fields and Properties
private RuleEngine? _ruleEngine;
private Dictionary<OreType, OreMarketPrice>? _marketPrices;
private ChunkSpeedRule? _chunkSpeedRule;
private OrePriceRule? _orePriceRule;
private ChunkPriceRule? _chunkPriceRule;
/// <summary>
/// To avoid firing unnecesary events.
/// </summary>
private bool _fillingValues = false;
#endregion
#region Constructors
public Main()
{
InitializeComponent();
// This will simplify other parts of the code:
nudPriceBe.Tag = OreType.Be;
nudPriceFe.Tag = OreType.Fe;
nudPricePd.Tag = OreType.Pd;
nudPricePt.Tag = OreType.Pt;
nudPriceV.Tag = OreType.V;
nudPriceW.Tag = OreType.W;
var exeDir = Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location);
ofd.InitialDirectory = sfd.InitialDirectory = Path.Combine(exeDir!, "Examples");
CreateSampleValues();
Recalculate();
}
#endregion
#region Methods
private void CreateSampleValues(bool fillValues = true)
{
_fillingValues = true;
// Market prices
var marketPrices = new List<OreMarketPrice>
{
new() { OreType = OreType.Be, Price = 20618.4M },
new() { OreType = OreType.Fe, Price = 881.28M },
new() { OreType = OreType.Pd, Price = 2524.42M },
new() { OreType = OreType.Pt, Price = 2732.13M },
new() { OreType = OreType.V, Price = 3034.67M },
new() { OreType = OreType.W, Price = 6274.49M },
};
_marketPrices = new();
foreach (var mp in marketPrices)
{
_marketPrices[mp.OreType] = mp;
}
// Chunks
var chunk1 = new OreChunk
{
OreType = OreType.Be,
OreWeightKg = 500,
WaterWeightKg = 200,
RelativeSpeed = 15,
};
uiChunk1.Chunk = chunk1;
var chunk2 = new OreChunk
{
OreType = OreType.Fe,
OreWeightKg = 1500,
WaterWeightKg = 3700,
RelativeSpeed = -20,
};
uiChunk2.Chunk = chunk2;
// Rules
_chunkSpeedRule = new ChunkSpeedRule();
uisChunkSpeed.Rule = _chunkSpeedRule;
_orePriceRule = new OrePriceRule
{
MarketPrices = marketPrices
};
_orePriceRule.Precalculate();
uisOrePrice.Rule = _orePriceRule;
_chunkPriceRule = new ChunkPriceRule
{
MarketPrices = _marketPrices
};
uisChunkPrice.Rule = _chunkPriceRule;
_ruleEngine = new()
{
Rules = new IPriorityRule[] {
_chunkSpeedRule,
_orePriceRule,
_chunkPriceRule,
},
};
txtDescription.Text = "Example with default values for all rules.";
_fillingValues = false;
if (fillValues)
{
FillValues(true);
}
}
private void FillPrices(IEnumerable<OreMarketPrice>? marketPrices)
{
if (marketPrices is null)
{
return;
}
var nuds = new NumericUpDown[] { nudPriceBe, nudPriceFe, nudPricePd, nudPricePt, nudPriceV, nudPriceW };
var dic = new Dictionary<OreType, NumericUpDown>();
foreach (var nud in nuds)
{
nud.Value = 0; // Just in case
var oreType = (OreType)nud.Tag!;
dic[oreType] = nud;
}
foreach (var mp in marketPrices)
{
if (dic.TryGetValue(mp.OreType, out var nud))
{
nud.Value = Math.Clamp(mp.Price, nud.Minimum, nud.Maximum);
}
}
}
private void FillValues(bool fillPrices = false)
{
_fillingValues = true;
if (fillPrices)
{
FillPrices(_marketPrices?.Values);
}
// Comparative Priority
if (uiChunk1.Priority == uiChunk2.Priority)
{
trkComparativePriority.Value = 0;
}
else if (uiChunk1.Priority > uiChunk2.Priority)
{
trkComparativePriority.Value = trkComparativePriority.Minimum;
}
else
{
trkComparativePriority.Value = trkComparativePriority.Maximum;
}
_fillingValues = false;
}
private void Recalculate()
{
if (_ruleEngine is not null)
{
uiChunk1.Priority = _ruleEngine.CalculatePriority(uiChunk1.Chunk);
uiChunk2.Priority = _ruleEngine.CalculatePriority(uiChunk2.Chunk);
}
if (_marketPrices is not null)
{
uiChunk1.OreValue = uiChunk1.Chunk?.OreType is not null && _marketPrices.TryGetValue(uiChunk1.Chunk.OreType.Value, out var mp1) ? mp1.Price * ((decimal)uiChunk1.Chunk.OreWeightKg / 1000) : 0;
uiChunk2.OreValue = uiChunk2.Chunk?.OreType is not null && _marketPrices.TryGetValue(uiChunk2.Chunk.OreType.Value, out var mp2) ? mp2.Price * ((decimal)uiChunk2.Chunk.OreWeightKg / 1000) : 0;
}
FillValues();
}
private void SaveFile(string fileName)
{
var saveData = new SaveData()
{
Description = txtDescription.Text,
Chunk1 = uiChunk1.Chunk,
Chunk2 = uiChunk2.Chunk,
MarketPrices = _marketPrices?.Values,
RuleData = new(),
};
if (_ruleEngine?.Rules is not null)
{
foreach (var rule in _ruleEngine.Rules)
{
rule.Save(saveData.RuleData);
}
}
var json = JsonSerializer.Serialize(saveData, new JsonSerializerOptions() { WriteIndented = true });
try
{
File.WriteAllText(fileName, json);
ofd.InitialDirectory = sfd.InitialDirectory = Path.GetDirectoryName(fileName);
ofd.FileName = sfd.FileName;
}
catch (Exception ex)
{
MessageBox.Show(ex.Message, "Could not save", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
private void LoadFile(string fileName)
{
try
{
var json = File.ReadAllText(fileName);
var saveData = JsonSerializer.Deserialize<SaveData>(json);
// Use the sample values for data missing from the json.
CreateSampleValues(false);
if (saveData is null)
{
FillValues(true);
return;
}
_fillingValues = true;
txtDescription.Text = saveData.Description;
if (saveData.Chunk1 is not null)
{
uiChunk1.Chunk = saveData.Chunk1;
}
if (saveData.Chunk2 is not null)
{
uiChunk2.Chunk = saveData.Chunk2;
}
if (saveData.MarketPrices is not null)
{
_marketPrices = new();
foreach (var mp in saveData.MarketPrices)
{
_marketPrices[mp.OreType] = mp;
}
if (_orePriceRule is not null)
{
_orePriceRule.MarketPrices = saveData.MarketPrices;
}
}
if (saveData.RuleData is not null && _ruleEngine?.Rules is not null)
{
foreach (var rule in _ruleEngine.Rules)
{
rule.Load(saveData.RuleData);
}
}
var uiss = new SlidingRuleUI[] {
uisChunkPrice,
uisChunkSpeed,
uisOrePrice,
};
foreach (var uis in uiss)
{
uis.FillValues();
}
FillValues(true);
Recalculate();
ofd.InitialDirectory = sfd.InitialDirectory = Path.GetDirectoryName(fileName);
sfd.FileName = ofd.FileName;
}
catch (Exception ex)
{
_fillingValues = false;
MessageBox.Show(ex.Message, "Could not load", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
#endregion
#region Event handlers
private void nudPrice_ValueChanged(object sender, EventArgs e)
{
if (_fillingValues)
{
return;
}
var nud = (NumericUpDown)sender;
var oreType = (OreType)nud.Tag!;
if (_marketPrices?.TryGetValue(oreType, out var marketPrice) ?? false)
{
marketPrice.Price = nud.Value;
}
Recalculate();
}
private void ui_ChunkChanged(object sender, EventArgs e)
{
if (_fillingValues)
{
return;
}
Recalculate();
}
private void ui_RuleChanged(object sender, EventArgs e)
{
if (_fillingValues)
{
return;
}
Recalculate();
}
private void btnNew_Click(object sender, EventArgs e)
{
var result = MessageBox.Show("Fill in sample values?", "Sample values", MessageBoxButtons.OKCancel, MessageBoxIcon.Question);
if (result == DialogResult.OK)
{
CreateSampleValues(true);
Recalculate();
}
}
private void btnLoad_Click(object sender, EventArgs e)
{
if (ofd.ShowDialog() == DialogResult.OK)
{
LoadFile(ofd.FileName);
}
}
private void btnSaveAs_Click(object sender, EventArgs e)
{
if (sfd.ShowDialog() == DialogResult.OK)
{
SaveFile(sfd.FileName);
}
}
#endregion
}
}