-
Notifications
You must be signed in to change notification settings - Fork 0
/
SettingsControl.xaml.cs
992 lines (853 loc) · 42.7 KB
/
SettingsControl.xaml.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
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using SimHub.Plugins.Styles;
using System.IO;
using Newtonsoft.Json;
using SimHub;
using Newtonsoft.Json.Linq;
using System.Reflection.Emit;
using System.Globalization;
using System.Reflection;
namespace Aaron.PluginRacenetReceiver
{
/// <summary>
/// Interaction logic for SettingsControl.xaml
/// </summary>
public partial class SettingsControl : UserControl
{
private readonly RacenetDataReceiver plugin;
// Add fields to save user's selections
private string selectedLocation;
private string selectedStage;
private string currentSelectedStage;
private string selectedCarClass;
private string selectedSurfaceCondition;
private dynamic currentLeaderboardData;
private dynamic currentClubLeaderboardData;
private int stageId;
private int vehicleClassId;
private int surfaceConditionId;
public string CurrentPlayerName { get; set; }
public string SelectedEventLocation { get; set; }
//private readonly bool hasLoaded = false;
public SettingsControl(RacenetDataReceiver plugin)
{
InitializeComponent();
this.plugin = plugin;
// Load settings from file
if (File.Exists("settings.json"))
{
string jsonSettings = File.ReadAllText("settings.json");
plugin.Settings = JsonConvert.DeserializeObject<Settings>(jsonSettings);
}
else
{
plugin.Settings = new Settings
{
RefreshToken = plugin.RefreshToken,
ClubName = plugin.ClubName
};
}
if (!string.IsNullOrEmpty(plugin.Settings.SelectedStage))
{
currentSelectedStage = plugin.Settings.SelectedStage;
}
FillClubList();
plugin.RefreshToken = plugin.Settings.RefreshToken;
// If ClubID is not empty, fetch club championship info
if (!string.IsNullOrEmpty(plugin.Settings.ClubID))
{
Task.Run(async () =>
{
await plugin.FetchClubChampionshipInfoAsync(plugin.Settings.ClubID);
});
}
// Attach the Loaded event handler
this.Loaded += SettingsControl_Loaded;
}
private void SettingsControl_Loaded(object sender, RoutedEventArgs e)
{
FillLocationComboBox();
if (locationComboBox.SelectedItem != null)
{
FillStageComboBox(locationComboBox.SelectedItem.ToString());
}
FillCarClassComboBox();
FillSurfaceConditionComboBox();
Logging.Current.Info($"Selected Location: {selectedLocation}");
Logging.Current.Info($"Selected Stage: {selectedStage}");
Logging.Current.Info($"Selected Car Class: {selectedCarClass}");
Logging.Current.Info($"Selected Surface Condition: {selectedSurfaceCondition}");
// Save the default selections if the settings file does not exist or the selections are null
if (!File.Exists("settings.json") || plugin.Settings.SelectedLocation == null || plugin.Settings.SelectedStage == null || plugin.Settings.SelectedCarClass == null || plugin.Settings.SelectedSurfaceCondition == null)
{
SaveSettings();
}
}
private void FillLocationComboBox()
{
// Assuming pluginManager is accessible in this context
var timeTrialPreInfoData = (string)(plugin.PluginManager.GetPropertyValue("RacenetDataReceiver.Racenet.rawData.timeTrialPreInfo"));
if (timeTrialPreInfoData != null)
{
// Parse timeTrialPreInfoData to a JObject
JObject timeTrialPreInfoDataJson = JObject.Parse(timeTrialPreInfoData);
// Get the locations from the JSON data
var locations = timeTrialPreInfoDataJson["locations"];
var orderedLocations = timeTrialPreInfoDataJson["orderedLocations"];
// Save the currently selected item
//var selectedItem = locationComboBox.SelectedItem;
// Fill the locationComboBox with the location names in the order specified by orderedLocations
locationComboBox.Items.Clear();
foreach (var orderedLocation in orderedLocations.Children<JObject>())
{
var locationId = orderedLocation["id"].Value<string>();
var location = locations[locationId];
if (location != null)
{
locationComboBox.Items.Add(location.Value<string>());
}
}
if (plugin.Settings.SelectedLocation != null)
{
locationComboBox.SelectedItem = plugin.Settings.SelectedLocation;
}
else if (locationComboBox.HasItems)
{
// Set the default selected item
locationComboBox.SelectedIndex = 0;
}
}
}
private void LocationComboBox_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
if(locationComboBox.SelectedItem == null) return;
// Save the current selected stage before changing the location
if (stageComboBox.SelectedItem != null)
{
currentSelectedStage = stageComboBox.SelectedItem.ToString();
}
string selectedLocation = locationComboBox.SelectedItem.ToString();
// Save the user's selection
this.selectedLocation = selectedLocation;
plugin.Settings.SelectedLocation = selectedLocation;
// Reset the selected stage in the settings
plugin.Settings.SelectedStage = null;
FillStageComboBox(selectedLocation);
Logging.Current.Info($"Selected Location Changed To: {selectedLocation}");
SaveSettings();
CheckAndFillLeaderboardDataGrid();
}
private void FillStageComboBox(string selectedLocation)
{
// Assuming timeTrialPreInfoData is accessible in this context
var timeTrialPreInfoData = (string)(plugin.PluginManager.GetPropertyValue("RacenetDataReceiver.Racenet.rawData.timeTrialPreInfo"));
// Parse timeTrialPreInfoData to a JObject
JObject timeTrialPreInfoDataJson = JObject.Parse(timeTrialPreInfoData);
// Get the location ID for the selected location
string selectedLocationId = timeTrialPreInfoDataJson["locations"]
.Children<JProperty>()
.FirstOrDefault(x => x.Value.ToString() == selectedLocation)?.Name;
if (string.IsNullOrEmpty(selectedLocationId))
{
Logging.Current.Info($"Location: {selectedLocation} not found in locations dictionary.");
return;
}
// Get the route IDs for the selected location
var routeIds = timeTrialPreInfoDataJson["locationRoute"][selectedLocationId];
// Clear the stageComboBox
stageComboBox.Items.Clear();
// Fill the stageComboBox with the route names
foreach (var routeId in routeIds)
{
var routeName = timeTrialPreInfoDataJson["routes"][routeId.ToString()];
stageComboBox.Items.Add(routeName);
}
// Try to restore the selected stage after filling the stageComboBox
if (!string.IsNullOrEmpty(currentSelectedStage))
{
foreach (var item in stageComboBox.Items)
{
if (item.ToString() == currentSelectedStage)
{
stageComboBox.SelectedItem = item;
break;
}
}
}
else if (stageComboBox.HasItems)
{
// Set the default selected item
stageComboBox.SelectedIndex = 0;
}
// If no item is selected, select the first item
if (stageComboBox.SelectedItem == null && stageComboBox.HasItems)
{
stageComboBox.SelectedIndex = 0;
}
}
private void StageComboBox_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
if(stageComboBox.SelectedItem == null) return;
string selectedStage = stageComboBox.SelectedItem.ToString();
// Save the user's selection
this.selectedStage = selectedStage;
plugin.Settings.SelectedStage = selectedStage;
Logging.Current.Info($"Selected Stage Changed To: {selectedStage}");
SaveSettings();
CheckAndFillLeaderboardDataGrid();
}
private void FillCarClassComboBox()
{
// Assuming pluginManager is accessible in this context
var timeTrialPreInfoData = (string)(plugin.PluginManager.GetPropertyValue("RacenetDataReceiver.Racenet.rawData.timeTrialPreInfo"));
if (timeTrialPreInfoData != null)
{
// Parse timeTrialPreInfoData to a JObject
JObject timeTrialPreInfoDataJson = JObject.Parse(timeTrialPreInfoData);
// Get the orderedVehicleClasses from the JSON data
var orderedVehicleClasses = timeTrialPreInfoDataJson["orderedVehicleClasses"];
// Fill the carClassComboBox with the vehicle class names in the order specified by orderedVehicleClasses
carClassComboBox.Items.Clear();
foreach (var orderedVehicleClass in orderedVehicleClasses.Children<JObject>())
{
var vehicleClassName = orderedVehicleClass["value"].Value<string>();
carClassComboBox.Items.Add(vehicleClassName);
}
if (plugin.Settings.SelectedCarClass != null)
{
carClassComboBox.SelectedItem = plugin.Settings.SelectedCarClass;
}
else if (carClassComboBox.HasItems)
{
// Set the default selected item
carClassComboBox.SelectedIndex = 0;
}
}
}
private void CarClassComboBox_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
if(carClassComboBox.SelectedItem == null) return;
string selectedCarClass = carClassComboBox.SelectedItem.ToString();
// Save the user's selection
this.selectedCarClass = selectedCarClass;
plugin.Settings.SelectedCarClass = selectedCarClass;
Logging.Current.Info($"Selected Car Class Changed To: {selectedCarClass}");
SaveSettings();
CheckAndFillLeaderboardDataGrid();
}
private void FillSurfaceConditionComboBox()
{
// Assuming pluginManager is accessible in this context
var timeTrialPreInfoData = (string)(plugin.PluginManager.GetPropertyValue("RacenetDataReceiver.Racenet.rawData.timeTrialPreInfo"));
if (timeTrialPreInfoData != null)
{
// Parse timeTrialPreInfoData to a JObject
JObject timeTrialPreInfoDataJson = JObject.Parse(timeTrialPreInfoData);
// Get the surface conditions from the JSON data
var surfaceConditions = timeTrialPreInfoDataJson["surfaceConditions"];
// Fill the surfaceConditionComboBox with the surface condition names
surfaceConditionComboBox.Items.Clear();
foreach (var surfaceCondition in surfaceConditions.Children<JProperty>())
{
surfaceConditionComboBox.Items.Add(surfaceCondition.Value.Value<string>());
}
if (plugin.Settings.SelectedSurfaceCondition != null)
{
surfaceConditionComboBox.SelectedItem = plugin.Settings.SelectedSurfaceCondition;
}
else if (surfaceConditionComboBox.HasItems)
{
// Set the default selected item
surfaceConditionComboBox.SelectedIndex = 0;
}
}
}
private void SurfaceConditionComboBox_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
if(surfaceConditionComboBox.SelectedItem == null) return;
string selectedSurfaceCondition = surfaceConditionComboBox.SelectedItem.ToString();
// Save the user's selection
this.selectedSurfaceCondition = selectedSurfaceCondition;
plugin.Settings.SelectedSurfaceCondition = selectedSurfaceCondition;
Logging.Current.Info($"Selected Surface Condition Changed To: {selectedSurfaceCondition}");
SaveSettings();
CheckAndFillLeaderboardDataGrid();
}
private void FillClubList()
{
if (clubNameComboBox.IsLoaded)
{
//clean up the list
clubNameComboBox.Items.Clear();
// Sort the club list by club name
var selectedIndex = 0;
var sortedClubList = plugin.ClubListData.OrderBy(club => club.clubName.ToString()).ToList();
var idx = 0;
foreach (var club in sortedClubList)
{
var isActiveNow = false;
if (club.currentChampionshipSummary != null)
{
isActiveNow = club.currentChampionshipSummary.isActiveNow;
}
clubNameComboBox.Items.Add(new{club.clubName, club.clubID, isActiveNow});
if(club.clubName == plugin.Settings.ClubName)
{
selectedIndex = idx;
}
idx++;
}
// Set the selected item to the saved club name
clubNameComboBox.SelectedIndex = selectedIndex;
// Set the DisplayMemberPath to "clubName" to only display the club name
// clubNameComboBox.DisplayMemberPath = "clubName";
}
else
{
clubNameComboBox.Loaded += (s, e) => FillClubList();
}
}
private void OpenDialogButton_Click(object sender, RoutedEventArgs e)
{
var dialog = new TokenInputDialog();
if (dialog.ShowDialog() == true)
{
string refreshToken = dialog.Token;
plugin.Settings.RefreshToken = refreshToken;
plugin.RefreshToken = refreshToken;
plugin.DoRefreshToken(false);
SaveSettings();
}
}
private void ClubNameComboBox_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
if (clubNameComboBox.SelectedItem == null) return;
// string clubName = clubNameComboBox.SelectedItem.ToString();
var selectedItem = (dynamic)clubNameComboBox.SelectedItem;
string clubName = selectedItem.clubName;
string clubID = selectedItem.clubID;
plugin.Settings.ClubName = clubName;
plugin.Settings.ClubID = clubID;
plugin.Settings.RefreshToken = plugin.RefreshToken;
plugin.ClubName = clubName;
// plugin.Settings.SelectedEventLocation = null;
// Fetch the club championship info
Task.Run(async () =>
{
await plugin.FetchClubChampionshipInfoAsync(clubID);
// Update the event list for the selected club
this.Dispatcher.Invoke(() =>
{
FillClubEventList();
// if(clubEventComboBox.HasItems)
// {
// clubEventComboBox.SelectedIndex = 0;
// }
});
});
SaveSettings();
// Log the selected club name
Logging.Current.Info($"Selected Club Name: {clubName}");
CheckAndFillClubLeaderboardDataGrid();
}
private void ClubNameComboBox_DropDownOpened(object sender, EventArgs e)
{
plugin.FetchClubListAsync().ContinueWith(t =>
{
if (t.Status == TaskStatus.RanToCompletion)
{
this.Dispatcher.Invoke(() =>
{
FillClubList();
});
}
});
}
private void FillClubEventList()
{
if (clubEventComboBox.IsLoaded)
{
// Assuming pluginManager is accessible in this context
var clubChampionshipInfoData = (string)(plugin.PluginManager.GetPropertyValue("RacenetDataReceiver.Racenet.rawData.clubChampionshipInfo"));
try
{
// Unsubscribe from the SelectionChanged event
// clubEventComboBox.SelectionChanged -= ClubEventComboBox_SelectionChanged;
// Parse clubChampionshipInfoData to a JObject
JObject clubChampionshipInfoDataJson = JObject.Parse(clubChampionshipInfoData);
// Get the events from the JSON data
var clubChampionship = clubChampionshipInfoDataJson["currentChampionship"];
var clubChampionshipEvents = clubChampionshipInfoDataJson["currentChampionship"]["events"];
// Clear the clubEventComboBox
clubEventComboBox.Items.Clear();
// Fill the clubEventComboBox with the club event>eventSettings>location value
foreach (var clubChampionshipEvent in clubChampionshipEvents.Children<JObject>())
{
var clubEventStatus = clubChampionshipEvent["status"].Value<int>();
var clubEventLocationName = clubChampionshipEvent["eventSettings"]["location"].Value<string>();
// clubEventComboBox.Items.Add(clubEventLocationName);
clubEventComboBox.Items.Add(new { Location = clubEventLocationName, Status = clubEventStatus });
}
clubStageComboBox.DisplayMemberPath = "Location";
// Set the selected item to the saved event location
if (plugin.Settings.SelectedEventLocation != null)
{
foreach (var item in clubEventComboBox.Items)
{
if (item.ToString() == plugin.Settings.SelectedEventLocation)
{
clubEventComboBox.SelectedItem = item;
break;
}
}
// clubEventComboBox.SelectedItem = plugin.Settings.SelectedEventLocation;
}
if (clubEventComboBox.SelectedItem == null && clubEventComboBox.HasItems)
{
// clubEventComboBox.SelectedIndex = 0;
// Set the default selected item
// If there is an item with status 1, select it. If there are multiple, select the first one.
// If there are no items with status 1, select the first item in the list.
var firstActiveItem = clubEventComboBox.Items.Cast<dynamic>().FirstOrDefault(item => item.Status == 1);
clubEventComboBox.SelectedItem = firstActiveItem ?? clubEventComboBox.Items[0];
}
// Resubscribe to the SelectionChanged event
// clubEventComboBox.SelectionChanged += ClubEventComboBox_SelectionChanged;
}
catch (JsonReaderException ex)
{
// Log the error or handle it appropriately
Logging.Current.Info($"Error parsing JSON: {ex.Message}");
}
}
else
{
clubEventComboBox.Loaded += (s, e) => FillClubEventList();
}
}
private void ClubEventComboBox_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
if (clubEventComboBox.SelectedItem == null) return;
var selectedItem = (dynamic)clubEventComboBox.SelectedItem;
string selectedEventLocation = selectedItem.Location;
int clubEventStatus = selectedItem.Status;
// Save the user's selection
plugin.Settings.SelectedEventLocation = selectedEventLocation;
plugin.Settings.SelectedEventStatus = clubEventStatus.ToString();
// Reset the selected club stage
// plugin.Settings.SelectedClubStage = null;、
SaveSettings();
// Fill the clubStageComboBox with the stages of the selected event
FillClubStageList(selectedEventLocation);
}
private void FillClubStageList(string selectedEventLocation)
{
if (clubStageComboBox.IsLoaded)
{
// Assuming pluginManager is accessible in this context
var clubChampionshipInfoData = (string)(plugin.PluginManager.GetPropertyValue("RacenetDataReceiver.Racenet.rawData.clubChampionshipInfo"));
try
{
// Parse clubChampionshipInfoData to a JObject
JObject clubChampionshipInfoDataJson = JObject.Parse(clubChampionshipInfoData);
// Get the selected event from the JSON data
var clubChampionshipEvent = clubChampionshipInfoDataJson["currentChampionship"]["events"].FirstOrDefault(e => e["eventSettings"]["location"].Value<string>() == selectedEventLocation);
if (clubChampionshipEvent != null)
{
// Unsubscribe from the SelectionChanged event
// clubStageComboBox.SelectionChanged -= ClubStageComboBox_SelectionChanged;
// Clear the clubStageComboBox
clubStageComboBox.Items.Clear();
// Fill the clubStageComboBox with the club event>stages>stageSettings>route value
foreach (var stage in clubChampionshipEvent["stages"].Children<JObject>())
{
var stageRoute = stage["stageSettings"]["route"].Value<string>();
var leaderboardID = stage["leaderboardID"].Value<string>();
clubStageComboBox.Items.Add(new { Route = stageRoute, LeaderboardID = leaderboardID });
}
// Set the DisplayMemberPath to "Route" to only display the route
clubStageComboBox.DisplayMemberPath = "Route";
// Set the selected item to the saved event location
if (plugin.Settings.SelectedClubStage != null)
{
foreach (var item in clubStageComboBox.Items)
{
if (((dynamic)item).Route == plugin.Settings.SelectedClubStage)
{
clubStageComboBox.SelectedItem = item;
break;
}
}
// clubStageComboBox.SelectedItem = clubStageComboBox.Items.Cast<dynamic>().FirstOrDefault(item => item.Route == plugin.Settings.SelectedClubStage);
}
if (clubStageComboBox.SelectedItem == null && clubStageComboBox.HasItems)
{
// Set the default selected item
clubStageComboBox.SelectedIndex = 0;
}
// Resubscribe to the SelectionChanged event
// clubStageComboBox.SelectionChanged += ClubStageComboBox_SelectionChanged;
}
}
catch (JsonReaderException ex)
{
// Log the error or handle it appropriately
Logging.Current.Info($"Error parsing JSON: {ex.Message}");
}
}
else
{
clubStageComboBox.Loaded += (s, e) => FillClubStageList(selectedEventLocation);
}
}
private void ClubStageComboBox_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
if (clubStageComboBox.SelectedItem == null) return;
// Save the selected stage and leaderboard ID to Settings
var selectedItem = (dynamic)clubStageComboBox.SelectedItem;
plugin.Settings.SelectedClubStage = selectedItem.Route;
plugin.Settings.SelectedClubLeaderboardID = selectedItem.LeaderboardID;
SaveSettings();
CheckAndFillClubLeaderboardDataGrid();
}
private void SaveSettings()
{
// Check if the token and club name are not empty before saving
if (!string.IsNullOrEmpty(plugin.Settings.RefreshToken) && !string.IsNullOrEmpty(plugin.Settings.ClubName))
{
// settings.RefreshToken = plugin.RefreshToken;
string json = JsonConvert.SerializeObject(plugin.Settings, Formatting.Indented);
File.WriteAllText("settings.json", json);
}
else
{
// Log an error message or throw an exception
Logging.Current.Info("Error: Token or Club Name is empty. Settings not saved.");
}
}
// Define a class to represent a row in the leaderboardDataGrid
public class LeaderboardRow
{
public int Position { get; set; }
public string Player { get; set; }
public string Nation { get; set; }
public BitmapImage NationFlag { get; set; }
public string Vehicle { get; set; }
public string Assists { get; set; }
public string Penalty { get; set; }
public string Time { get; set; }
public string DiffFirst { get; set; }
public bool IsCurrentUser { get; set; }
// public List<BitmapImage> AssistIcons { get; set; }
public BitmapImage[] AssistIcons { get; set; }
public int Points { get; set; }
public string Overall { get; set; }
}
private void FillLeaderboardDataGrid(dynamic leaderboardData)
{
// Update the current leaderboard data
currentLeaderboardData = leaderboardData;
// Get the current player's name
var personalInfoData = (string)(plugin.PluginManager.GetPropertyValue("RacenetDataReceiver.Racenet.rawData.personalInfo"));
CurrentPlayerName = JObject.Parse(personalInfoData)["displayName"] != null ? JObject.Parse(personalInfoData)["displayName"].ToString() : string.Empty;
// Convert the leaderboardData to a list of LeaderboardRow
List<LeaderboardRow> rows = new List<LeaderboardRow>();
foreach (var entry in leaderboardData.entries)
{
string time = entry.time != null ? FormatTime(entry.time.ToString()) : string.Empty;
string diffFirst = entry.differenceToFirst != null ? FormatDiffTime(entry.differenceToFirst.ToString()) : string.Empty;
// string penalty = entry.timePenalty != null ? FormatTime(entry.timePenalty.ToString()) : string.Empty;
// Convert assistFlags from integer array to string array
int[] assistFlagsIntArray = entry.assistFlags.ToObject<int[]>();
if (assistFlagsIntArray.Length == 0)
{
assistFlagsIntArray = new int[] { 0 };
}
string[] assistFlagsStrArray = Array.ConvertAll(assistFlagsIntArray, x => x.ToString());
// Create a new LeaderboardRow
LeaderboardRow row = new LeaderboardRow
{
Position = entry.rank,
Player = entry.displayName,
Nation = entry.nationalityID,
NationFlag = GetNationFlag(entry.nationalityID.ToString()),
Vehicle = entry.vehicle,
Assists = string.Join("- ", assistFlagsStrArray),
AssistIcons = GetAssistIcons(assistFlagsIntArray),
Penalty = entry.timePenalty,
Time = time,
DiffFirst = diffFirst,
IsCurrentUser = entry.displayName == CurrentPlayerName
};
rows.Add(row);
}
// Fill the leaderboardDataGrid with the rows
leaderboardDataGrid.ItemsSource = rows;
}
private void FillClubLeaderboardDataGrid(dynamic leaderboardData)
{
// Update the current leaderboard data
currentClubLeaderboardData = leaderboardData;
// Get the current player's name
var personalInfoData = (string)(plugin.PluginManager.GetPropertyValue("RacenetDataReceiver.Racenet.rawData.personalInfo"));
CurrentPlayerName = JObject.Parse(personalInfoData)["displayName"] != null ? JObject.Parse(personalInfoData)["displayName"].ToString() : string.Empty;
// Convert the leaderboardData to a list of LeaderboardRow
List<LeaderboardRow> rows = new List<LeaderboardRow>();
foreach (var entry in leaderboardData.entries)
{
string time = entry.time != null ? FormatTime(entry.time.ToString()) : string.Empty;
string diffFirst = entry.differenceToFirst != null ? FormatDiffTime(entry.differenceToFirst.ToString()) : string.Empty;
// Convert assistFlags from integer array to string array
int[] assistFlagsIntArray = entry.assists.ToObject<int[]>();
string[] assistFlagsStrArray = Array.ConvertAll(assistFlagsIntArray, x => x.ToString());
// Add Points and Overall to the row
int points = entry.points != null ? int.Parse(entry.points.ToString()) : 0;
string overall = entry.timeAccumulated != null ? FormatTime(entry.timeAccumulated.ToString()) : string.Empty;
rows.Add(new LeaderboardRow
{
Position = entry.rank,
Player = entry.displayName,
Nation = entry.nationalityID,
NationFlag = GetNationFlag(entry.nationalityID.ToString()),
Vehicle = entry.vehicle,
Assists = string.Join("- ", assistFlagsStrArray),
AssistIcons = GetAssistIcons(assistFlagsIntArray),
Penalty = entry.timePenalty,
Time = time,
DiffFirst = diffFirst,
IsCurrentUser = entry.displayName == CurrentPlayerName,
Points = points,
Overall = overall
});
}
// Set the ItemsSource of the DataGrid
clubDataGrid.ItemsSource = rows;
}
private BitmapImage GetNationFlag(string nationalityId)
{
{
// Load the JSON file
string json = LoadJsonFromResources("Aaron.PluginRacenetReceiver.nationalityID.json");
// Parse the JSON file
var jsonObject = JObject.Parse(json);
// Get the image file name
string imageFileName = (string)jsonObject[nationalityId];
// Load the image from resources
if (imageFileName == null)
{
return null;
}
BitmapImage image = LoadImageFromResources($"Aaron.PluginRacenetReceiver.icons.flags.{imageFileName}");
return image;
}
}
private string FormatTime(string timeStr)
{
TimeSpan timeSpan = TimeSpan.Parse(timeStr);
// If the time is less than one second, only display the milliseconds
if (timeSpan.TotalSeconds < 1)
{
return "0." + timeSpan.Milliseconds.ToString("D3").Substring(0, 3);
}
// If the time is less than one minute, only display the seconds and milliseconds
else if (timeSpan.TotalMinutes < 1)
{
return string.Format("{0}.{1}",
timeSpan.Seconds.ToString("D2"),
timeSpan.Milliseconds.ToString("D3").Substring(0, 3));
}
// If the time is less than one hour, only display the minutes, seconds, and milliseconds
else if (timeSpan.TotalHours < 1)
{
return string.Format("{0}:{1}.{2}",
timeSpan.Minutes.ToString("D2"),
timeSpan.Seconds.ToString("D2"),
timeSpan.Milliseconds.ToString("D3").Substring(0, 3));
}
// Otherwise, display the hours, minutes, seconds, and milliseconds
else
{
return string.Format("{0}:{1}:{2}.{3}",
timeSpan.Hours.ToString(),
timeSpan.Minutes.ToString("D2"),
timeSpan.Seconds.ToString("D2"),
timeSpan.Milliseconds.ToString("D3").Substring(0, 3));
}
}
private string FormatDiffTime(string timeStr)
{
TimeSpan timeSpan = TimeSpan.Parse(timeStr);
// If the time is exactly zero, display "="
if (timeSpan.TotalSeconds == 0)
{
return "=";
}
// If the time is less than one second, display "0." followed by the milliseconds
else if (timeSpan.TotalSeconds < 1)
{
return "+ 0." + timeSpan.Milliseconds.ToString("D3").Substring(0, 3);
}
// If the time is less than one minute, only display the seconds and milliseconds
else if (timeSpan.TotalMinutes < 1)
{
return string.Format("+ {0}.{1}",
timeSpan.Seconds.ToString("D2"),
timeSpan.Milliseconds.ToString("D3").Substring(0, 3));
}
// If the time is less than one hour, only display the minutes, seconds, and milliseconds
else if (timeSpan.TotalHours < 1)
{
return string.Format("+ {0}:{1}.{2}",
timeSpan.Minutes.ToString("D2"),
timeSpan.Seconds.ToString("D2"),
timeSpan.Milliseconds.ToString("D3").Substring(0, 3));
}
// Otherwise, display the hours, minutes, seconds, and milliseconds
else
{
return string.Format("+ {0}:{1}:{2}.{3}",
timeSpan.Hours.ToString(),
timeSpan.Minutes.ToString("D2"),
timeSpan.Seconds.ToString("D2"),
timeSpan.Milliseconds.ToString("D3").Substring(0, 3));
}
}
private async void CheckAndFillLeaderboardDataGrid()
{
// Check if all the combo boxes have a selected item
if (stageComboBox.SelectedItem != null && carClassComboBox.SelectedItem != null && surfaceConditionComboBox.SelectedItem != null)
{
// Get the selected values
string selectedStage = stageComboBox.SelectedItem.ToString();
string selectedCarClass = carClassComboBox.SelectedItem.ToString();
string selectedSurfaceCondition = surfaceConditionComboBox.SelectedItem.ToString();
// Get the timeTrialPreInfoData
var timeTrialPreInfoData = (string)(plugin.PluginManager.GetPropertyValue("RacenetDataReceiver.Racenet.rawData.timeTrialPreInfo"));
// Create a JsonDataHelper instance
JsonDataHelper jsonDataHelper = new JsonDataHelper(timeTrialPreInfoData);
// Convert the selected values to the parameters needed for FetchTimeTrialLeaderboardDataAsync
stageId = int.Parse(jsonDataHelper.GetKeyByValue(selectedStage));
vehicleClassId = int.Parse(jsonDataHelper.GetKeyByValue(selectedCarClass));
surfaceConditionId = int.Parse(jsonDataHelper.GetKeyByValue(selectedSurfaceCondition));
// Fetch the leaderboard data
var leaderboardData = await plugin.FetchTimeTrialLeaderboardDataAsync(stageId, vehicleClassId, surfaceConditionId);
// Update the current leaderboard data
currentLeaderboardData = leaderboardData;
// Fill the leaderboardDataGrid with the fetched data
FillLeaderboardDataGrid(leaderboardData);
}
}
private async void CheckAndFillClubLeaderboardDataGrid()
{
// Check if all the combo boxes have a selected item
if (clubNameComboBox.SelectedItem != null && clubEventComboBox.SelectedItem != null && clubStageComboBox.SelectedItem != null)
{
// Fetch the leaderboard data
var leaderboardData = await plugin.FetchClubChampionshipLeaderboardDataAsync(plugin.Settings.ClubID, plugin.Settings.SelectedClubLeaderboardID);
// Update the current leaderboard data
currentClubLeaderboardData = leaderboardData;
// Fill the clubLeaderboardDataGrid with the fetched data
FillClubLeaderboardDataGrid(leaderboardData);
}
}
private async void PreviousPageButton_Click(object sender, RoutedEventArgs e)
{
// Get the previous cursor from the current leaderboard data
string previousCursor = currentLeaderboardData["previous"].ToString();
// Fetch the previous page of leaderboard data
var leaderboardData = await plugin.FetchTimeTrialLeaderboardDataAsync(stageId, vehicleClassId, surfaceConditionId, cursor:previousCursor);
// Fill the leaderboardDataGrid with the fetched data
FillLeaderboardDataGrid(leaderboardData);
}
private async void NextPageButton_Click(object sender, RoutedEventArgs e)
{
// Get the next cursor from the current leaderboard data
string nextCursor = currentLeaderboardData["next"].ToString();
// Fetch the next page of leaderboard data
var leaderboardData = await plugin.FetchTimeTrialLeaderboardDataAsync(stageId, vehicleClassId, surfaceConditionId, cursor:nextCursor);
// Fill the leaderboardDataGrid with the fetched data
FillLeaderboardDataGrid(leaderboardData);
}
private async void PreviousClubPageButton_Click(object sender, RoutedEventArgs e)
{
// Get the previous cursor from the current club leaderboard data
string previousCursor = currentClubLeaderboardData["previous"].ToString();
// Fetch the previous page of club leaderboard data
var leaderboardData = await plugin.FetchClubChampionshipLeaderboardDataAsync(plugin.Settings.ClubID, plugin.Settings.SelectedClubLeaderboardID, cursor:previousCursor);
// Fill the clubLeaderboardDataGrid with the fetched data
FillClubLeaderboardDataGrid(leaderboardData);
}
private async void NextClubPageButton_Click(object sender, RoutedEventArgs e)
{
// Get the next cursor from the current club leaderboard data
string nextCursor = currentClubLeaderboardData["next"].ToString();
// Fetch the next page of club leaderboard data
var leaderboardData = await plugin.FetchClubChampionshipLeaderboardDataAsync(plugin.Settings.ClubID, plugin.Settings.SelectedClubLeaderboardID, cursor:nextCursor);
// Fill the clubLeaderboardDataGrid with the fetched data
FillClubLeaderboardDataGrid(leaderboardData);
}
private BitmapImage[] GetAssistIcons(int[] assistFlags)
{
// Set default icons
BitmapImage[] icons = new BitmapImage[3]
{
LoadImageFromResources("Aaron.PluginRacenetReceiver.icons.0_1.png"),
LoadImageFromResources("Aaron.PluginRacenetReceiver.icons.0_2.png"),
LoadImageFromResources("Aaron.PluginRacenetReceiver.icons.0_4.png")
};
foreach (int flag in assistFlags)
{
switch (flag)
{
case 1:
icons[0] = LoadImageFromResources("Aaron.PluginRacenetReceiver.icons.1.png");
break;
case 2:
icons[1] = LoadImageFromResources("Aaron.PluginRacenetReceiver.icons.2.png");
break;
case 4:
icons[2] = LoadImageFromResources("Aaron.PluginRacenetReceiver.icons.4.png");
break;
}
}
return icons;
}
private BitmapImage LoadImageFromResources(string resourcePath)
{
var stream = Assembly.GetExecutingAssembly().GetManifestResourceStream(resourcePath);
var image = new BitmapImage();
image.BeginInit();
image.StreamSource = stream;
image.EndInit();
return image;
}
public string LoadJsonFromResources(string resourcePath)
{
var assembly = Assembly.GetExecutingAssembly();
using (var stream = assembly.GetManifestResourceStream(resourcePath))
{
if (stream == null)
{
throw new Exception($"Could not find resource {resourcePath}");
}
using (var reader = new StreamReader(stream))
{
return reader.ReadToEnd();
}
}
}
}
}