-
Notifications
You must be signed in to change notification settings - Fork 0
/
frmPlayerStats.vb
1817 lines (1431 loc) · 79.6 KB
/
frmPlayerStats.vb
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
993
994
995
996
997
998
999
1000
Option Strict Off
Option Explicit On
Imports System.Collections.Generic
Imports System.IO 'Imports VB = Microsoft.VisualBasic
Friend Class frmPlayerStats
Inherits Form
' -------------------------------------------------------------------------------
' Dart Scorekeeper
'
' Written by Matthew Monroe
' Started in July 1999
' Ported to .NET in 2011
'
' E-mail: [email protected] or [email protected]
' Repository: https://github.com/alchemistmatt
'
' -------------------------------------------------------------------------------
'
' Licensed under the Apache License, Version 2.0; you may not use this file except
' in compliance with the License. You may obtain a copy of the License at
' http://www.apache.org/licenses/LICENSE-2.0
#Region "Constants"
Private Const RequiredMinGamesPlayed As Short = 2
#End Region
#Region "Structures"
Public Structure udtPlayerStatsGameInfoType
Public GameDate As DateTime
Public GameWon As Boolean ' True if the game was won; false if lost
Public GameOpponentsIndex As List(Of Short)
End Structure
Public Structure udtGameStatsType
Public GameDateTime As DateTime
Public PlayerName As String
Public GameWon As Boolean
Public PartnerName As String
Public GamePoints As Short
End Structure
Public Structure udtExtendedStats
Public GameDateTime As DateTime
Public TeamNumber As Short
Public PlayerName As String
Public DartValue As Short
Public Multiplier As Short
Public ValidHits As Short
End Structure
Public Structure udtPlayerRankingStats
Public OverallRanking As Single ' Was index 0 in a 2D array
Public WinPercent As Single ' Was index 1 in a 2D array
Public MostRecentRanking As Single ' Was index 2 in a 2D array
Public TotalGamesPlayed As Single ' Was index 3 in a 2D array
End Structure
Public Structure udtPlayerStatsType
Public GameDateTime As DateTime
Public GameTimeElapsed As String
Public PlayerName As String
Public GameWon As Boolean
Public Points As Short
Public PartnerName As String
Public GameName As String
End Structure
Public Structure usr301Stats
Public HighestScoringFirstTurn As Short
Public MostDartsUntilDoubleIn As Short
Public GamesLostWithoutDoublingIn As Short
End Structure
Public Structure usrGolfStats
Public LowestScoringGame As Short
Public HighestScoringGame As Short
Public TotalPointsAllGames As Short
Public GameCount As Short
End Structure
Public Structure udtGeneralExtendedStats
Public HighestScoringTurn As Short
Public HighestScoringFirstTurn As Short
Public ShortestGameLengthToWin As Short
Public LongestGameLengthToWin As Short
Public LongestScoringDrought As Short
Public AllTimeDartsThrown As Integer
Public AllTimeTotalScore As Integer
Public GameInfo As List(Of udtExtendedStatGameInfoType)
Public Sub Initialize()
GameInfo = New List(Of udtExtendedStatGameInfoType)
End Sub
End Structure
Public Structure udtPartnerGameStatsType
Public GamesWon As Integer
Public GamesPlayed As Integer
End Structure
Public Structure udtExtendedStatGameInfoType
Public GameDate As DateTime
Public GameMeanScorePerThrow As Single
Public GameThrowCount As Short
End Structure
Public Structure udtPlayerStatsParsed
Public PlayerNameIndex As Short
Public GamesWonAlone As Integer
Public GamesPlayedAlone As Integer
Public GamesWonWithPartner As Integer
Public GamesPlayedWithPartner As Integer
Public FirstGameDate As DateTime
Public LastGameDate As DateTime
Public GamesPlayedPerMonth As Single
Public PartnerNameCount As Short
Public PartnerNameIndex() As Short
Public GamesWonByPartner() As udtPartnerGameStatsType
Public GameInfo As List(Of udtPlayerStatsGameInfoType)
Public StatsFor301 As udtGeneralExtendedStats
Public StatsForCricket As udtGeneralExtendedStats
Public StatsForGolf As udtGeneralExtendedStats
Public OverallGolfStats As usrGolfStats
Public Overall301Stats As usr301Stats
End Structure
#End Region
#Region "Module-wide Variables"
Private mGameStats As List(Of udtGameStatsType)
' Note: mExtendedStats() is 1 based, not 0 based
Private mExtendedStatsCount As Integer
Private mExtendedStats() As udtExtendedStats
Private mStatsMode As frmStatOptionsDialog.smStatsMode
Private MasterPlayerListCount As Short
Private MasterPlayerList() As String ' 0-based (was previously 1-based)
' Likely parallel with MasterPlayerList()
Private mPlayerRanking(MAX_PLAYER_COUNT) As udtPlayerRankingStats ' 0-based (was previously 1-based)
' Likely parallel with MasterPlayerList()
Private mPlayerStats() As udtPlayerStatsParsed
Private mFormLoaded As Boolean
#End Region
Public ReadOnly Property PlayerStatsCount() As Integer
Get
Return MasterPlayerListCount
End Get
End Property
Private Sub AddToGameStats(udtPlayerStatEntry As udtPlayerStatsType)
Dim udtGameStatsEntry As udtGameStatsType
With udtPlayerStatEntry
udtGameStatsEntry.GameDateTime = .GameDateTime
udtGameStatsEntry.PlayerName = .PlayerName
udtGameStatsEntry.PartnerName = .PartnerName
udtGameStatsEntry.GameWon = .GameWon
udtGameStatsEntry.GamePoints = .Points
End With
mGameStats.Add(udtGameStatsEntry)
End Sub
Public Sub ParseGamePlayStats()
Dim x, y As Short
Dim GamesPlayedTotal, TotalPartnerGames As Short
Dim BestWinIndex As Short
Dim BestWinValue As Single
Dim WorstWinIndex As Short
Dim WorstWinValue, CompareValue As Single
' Compute stats and fill list boxes
For x = 0 To MasterPlayerListCount - 1
With mPlayerStats(x)
lstPlayers.Items.Add(MasterPlayerList(.PlayerNameIndex))
GamesPlayedTotal = .GamesPlayedAlone + .GamesPlayedWithPartner
If GamesPlayedTotal > 0 Then
lstGamesWon.Items.Add((.GamesWonAlone + .GamesWonWithPartner).ToString & "/" & GamesPlayedTotal.ToString & " (" & ((.GamesWonAlone + .GamesWonWithPartner) / GamesPlayedTotal * 100).ToString("##0") & "%)")
mPlayerRanking(x).OverallRanking = 0.5
lstRank.Items.Add("0.5")
Else
lstGamesWon.Items.Add("N/A")
End If
If .GamesPlayedWithPartner > 0 Then
lstGamesWonWithPartner.Items.Add(.GamesWonWithPartner.ToString & "/" & .GamesPlayedWithPartner.ToString & " (" & ((.GamesWonWithPartner) / .GamesPlayedWithPartner * 100).ToString("##0") & "%)")
Else
lstGamesWonWithPartner.Items.Add("N/A")
End If
' Compute Games played per Month
' Make sure at least 1 week of stats is defined
If .LastGameDate.Subtract(.FirstGameDate).TotalDays >= 7 Then
' ToDo: Check this math after converting from VB6
.GamesPlayedPerMonth = Math.Round((.GamesPlayedAlone + .GamesPlayedWithPartner) / (.LastGameDate.Subtract(.FirstGameDate).TotalDays * 12 / 365), 1)
lstGamesPlayedPerMonth.Items.Add(.GamesPlayedPerMonth.ToString("0.0"))
Else
.GamesPlayedPerMonth = -1
lstGamesPlayedPerMonth.Items.Add("N/A")
End If
' Find best and worst partner
If .PartnerNameCount >= 0 Then
' Count up total partner games played
TotalPartnerGames = 0
For y = 0 To .PartnerNameCount
TotalPartnerGames += .GamesWonByPartner(y).GamesPlayed
Next y
If TotalPartnerGames > 0 Then
BestWinIndex = 0
BestWinValue = .GamesWonByPartner(0).GamesWon / TotalPartnerGames
WorstWinIndex = 0
WorstWinValue = BestWinValue
' Step through the partners and determine win percentage with that partner
For y = 0 To .PartnerNameCount
CompareValue = .GamesWonByPartner(y).GamesWon / TotalPartnerGames
If CompareValue > BestWinValue Then
BestWinValue = CompareValue
BestWinIndex = y
End If
If CompareValue < WorstWinValue Then
WorstWinValue = CompareValue
WorstWinIndex = y
End If
Next y
lstBestPartner.Items.Add(MasterPlayerList(.PartnerNameIndex(BestWinIndex)) & " - (" & .GamesWonByPartner(BestWinIndex).GamesWon.ToString() & "/" & .GamesWonByPartner(BestWinIndex).GamesPlayed.ToString() & ")")
lstWorstPartner.Items.Add(MasterPlayerList(.PartnerNameIndex(WorstWinIndex)) & " - (" & .GamesWonByPartner(WorstWinIndex).GamesWon.ToString() & "/" & .GamesWonByPartner(WorstWinIndex).GamesPlayed.ToString() & ")")
Else
lstBestPartner.Items.Add("N/A")
lstWorstPartner.Items.Add("N/A")
End If
Else
lstBestPartner.Items.Add("N/A")
lstWorstPartner.Items.Add("N/A")
End If
End With
Next x
End Sub
Private Sub DoRankings(StopDate As DateTime)
Dim x As Short
Dim StartingDate, EndingDate As DateTime
Dim DateWeight As Single
' Erase values in PlayerRanking(x)
For x = 0 To MasterPlayerListCount - 1
mPlayerRanking(x).OverallRanking = 0
mPlayerRanking(x).TotalGamesPlayed = 0
Next x
' Compute ranking, lowering the weight over time
' Exit if StartingDate is before the StopDate
For x = 1 To 4
Select Case x
Case 1 ' 1 month ago to today
StartingDate = DateTime.Now.AddDays(-30)
EndingDate = DateTime.Now
DateWeight = 0.6
Case 2 ' 4 months ago to 1 month ago
StartingDate = DateTime.Now.AddDays(-120)
EndingDate = DateTime.Now.AddDays(-30)
DateWeight = 0.25
Case 3 ' 8 months ago to 4 months ago
StartingDate = DateTime.Now.AddDays(-240)
EndingDate = DateTime.Now.AddDays(-120)
DateWeight = 0.1
Case Else ' 12 months ago to 8 months ago
StartingDate = DateTime.Now.AddDays(-365)
EndingDate = DateTime.Now.AddDays(-240)
DateWeight = 0.05
End Select
If StartingDate < StopDate Then
Exit For
Else
RankPlayers(StartingDate, EndingDate, DateWeight)
End If
Next x
End Sub
Private Sub FindBestWorstPlayer(StartingDate As DateTime)
' Finds the best and worst player simply by looking at the rankings list
Dim x As Short
Dim MaxRank, MinRank As Single
Dim MaxRankIndex, MinRankIndex As Short
Dim RecentGamesPlayed(1) As Short
Dim RecentGamesWon(1) As Short
Dim RecentGamesPlayedTemp, RecentGamesWonTemp As Short
Dim intPlayerIndex As Integer
Dim sngPlayerRank As Single
Try
MaxRank = -1
MaxRankIndex = -1
MinRank = 1.1
MinRankIndex = -1
' Find player with highest and lowest ranking simply by looking at the ranking list
For x = 0 To lstRank.Items.Count - 1
If Single.TryParse(lstRank.Items(x), sngPlayerRank) Then
' Make sure player has played Recently
intPlayerIndex = GetPlayerIndex(CStr(lstPlayers.Items(x)))
If intPlayerIndex >= 0 Then
' Find the players' recent win record
RecentGamesPlayedTemp = 0
RecentGamesWonTemp = 0
For Each udtGameDetails As udtPlayerStatsGameInfoType In mPlayerStats(intPlayerIndex).GameInfo
If udtGameDetails.GameDate >= StartingDate AndAlso StartingDate <= DateTime.Now Then
RecentGamesPlayedTemp += 1
If udtGameDetails.GameWon Then
RecentGamesWonTemp += 1
End If
End If
Next
If RecentGamesPlayedTemp > 0 Then
' Only consider the player if they've played recently
If sngPlayerRank > MaxRank Then
MaxRank = sngPlayerRank
MaxRankIndex = x
RecentGamesPlayed(0) = RecentGamesPlayedTemp
RecentGamesWon(0) = RecentGamesWonTemp
End If
If sngPlayerRank < MinRank Then
MinRank = sngPlayerRank
MinRankIndex = x
RecentGamesPlayed(1) = RecentGamesPlayedTemp
RecentGamesWon(1) = RecentGamesWonTemp
End If
End If
End If
End If
Next x
If MaxRankIndex >= 0 And RecentGamesPlayed(0) <> 0 Then
lblBestPlayerName.Text = lstPlayers.Items(MaxRankIndex).ToString() & ": " & RecentGamesWon(0).ToString() & "/" & RecentGamesPlayed(0).ToString & " (" & (RecentGamesWon(0) / RecentGamesPlayed(0) * 100).ToString("##0") & "%)"
Else
lblBestPlayerName.Text = "N/A"
End If
If MinRankIndex >= 0 And RecentGamesPlayed(1) <> 0 Then
lblWorstPlayerName.Text = lstPlayers.Items(MinRankIndex).ToString() & ": " & RecentGamesWon(1).ToString() & "/" & RecentGamesPlayed(1).ToString() & " (" & (RecentGamesWon(1) / RecentGamesPlayed(1) * 100).ToString("##0") & "%)"
Else
lblWorstPlayerName.Text = "N/A"
End If
Catch ex As Exception
HandleException("FindBestWorstPlayer", ex)
End Try
End Sub
Private Function FindMatchingFiles(strFilePathBase As String, ByRef strFileMatchList As SortedList(Of String, String)) As Integer
Dim ioDirInfo As DirectoryInfo
Dim ioFileInfo As FileInfo
Dim strFileSpecToFind As String
Dim strFileMatchCheck As String
Dim strFileBaseCompare1 As String
Dim strFileBaseCompare2 As String
Try
strFileMatchList.Clear()
ioDirInfo = New DirectoryInfo(Path.GetDirectoryName(strFilePathBase))
If Not ioDirInfo.Exists() Then
' Directory not found
Return 0
End If
' Step through all of the .Ini files in the folder
strFileSpecToFind = Path.GetFileNameWithoutExtension(strFilePathBase) & "*.ini"
' If the name matches the base exactly, or if it matches the base plus an underscore, then count this as a match
strFileBaseCompare1 = (Path.GetFileNameWithoutExtension(strFilePathBase) & ".ini").ToLower()
strFileBaseCompare2 = (Path.GetFileNameWithoutExtension(strFilePathBase) & "_").ToLower()
For Each ioFileInfo In ioDirInfo.GetFiles(strFileSpecToFind)
strFileMatchCheck = ioFileInfo.Name.ToLower()
If strFileMatchCheck = strFileBaseCompare1 Or strFileMatchCheck.StartsWith(strFileBaseCompare2) Then
strFileMatchList.Add(ioFileInfo.FullName, "")
End If
Next
Return strFileMatchList.Count
Catch ex As Exception
HandleException("FindMatchingFiles", ex)
End Try
End Function
Public Function GetPlayerGameInfo(strPlayerName As String, ByRef udtGameInfo As List(Of udtPlayerStatsGameInfoType)) As Boolean
Dim intPlayerIndex As Integer
intPlayerIndex = GetPlayerIndex(strPlayerName)
If intPlayerIndex >= 0 Then
Return GetPlayerGameInfo(intPlayerIndex, udtGameInfo)
Else
Return False
End If
End Function
Public Function GetPlayerGameInfo(intPlayerIndex As Integer, ByRef udtGameInfo As List(Of udtPlayerStatsGameInfoType)) As Boolean
If intPlayerIndex >= 0 Then
udtGameInfo = mPlayerStats(intPlayerIndex).GameInfo
Return True
Else
Return False
End If
End Function
''' <summary>
''' Looks for PlayerName in MasterPlayerList()
''' </summary>
''' <param name="PlayerName"></param>
''' <returns>Index if found; -1 if not found</returns>
''' <remarks></remarks>
Protected Function GetPlayerIndex(ByRef PlayerName As String) As Short
Dim x As Short
For x = 0 To MasterPlayerListCount - 1
If MasterPlayerList(x) = PlayerName Then
Return x
End If
Next x
Return -1
End Function
Public Function GetPlayerNameByIndex(intPlayerIndex As Integer) As String
If intPlayerIndex >= 0 And intPlayerIndex < MasterPlayerListCount Then
Return MasterPlayerList(intPlayerIndex)
Else
Return String.Empty
End If
End Function
Public Function GetPlayerStatsByIndex(intPlayerIndex As Integer) As udtPlayerStatsParsed
If intPlayerIndex >= 0 And intPlayerIndex < MasterPlayerListCount Then
Return mPlayerStats(intPlayerIndex)
Else
Return New udtPlayerStatsParsed
End If
End Function
Protected Sub InitializePlayerStats()
Dim intIndex As Short
ReDim mPlayerRanking(MAX_PLAYER_COUNT)
ReDim MasterPlayerList(MAX_PLAYER_COUNT)
ReDim mPlayerStats(MAX_PLAYER_COUNT)
For intIndex = 0 To MAX_PLAYER_COUNT - 1
With mPlayerStats(intIndex)
ReDim .PartnerNameIndex(MAX_PLAYER_COUNT)
ReDim .GamesWonByPartner(MAX_PLAYER_COUNT)
.GameInfo = New List(Of udtPlayerStatsGameInfoType)
.StatsFor301.Initialize()
.StatsForCricket.Initialize()
.StatsForGolf.Initialize()
End With
Next intIndex
End Sub
Private Sub ParseExtendedGameStats(lngCurrentTurnStartIndex As Integer,
lngCurrentTurnStopIndex As Integer,
ByRef ThisPlayerStats As udtPlayerStatsParsed,
ByRef intDartsThrownThisGame As Short,
ByRef intDartsSinceLastScore As Short,
ByRef lngTotalScoreThisGame As Integer,
eGameType As gtGameTypeConstants,
blnRequireDoubleInFor301 As Boolean,
ByRef blnHasScoredThisGame As Boolean)
' This sub gets called for each recorded "turn", typically every 3 darts thrown (though not necessarily true for golf)
Dim intTurnScoreValid As Short
Dim intTurnScoreRaw, intThisThrowScoreRaw As Short
Dim lngExtStatIndex As Integer
Dim objThisPlayerExtStats As udtGeneralExtendedStats
Dim intDartsThrownThisGameTemp As Short
Dim boolFirstScoreOfGame As Boolean
Select Case eGameType
Case gtGameTypeConstants.gt301
objThisPlayerExtStats = ThisPlayerStats.StatsFor301
Case gtGameTypeConstants.gtCricket
objThisPlayerExtStats = ThisPlayerStats.StatsForCricket
Case gtGameTypeConstants.gtGolf
objThisPlayerExtStats = ThisPlayerStats.StatsForGolf
Case Else
' Unknown mode
MessageBox.Show("Unknown game mode in ParseExtendedGameStats: " & LookupGameStringByType(eGameType), "Error", MessageBoxButtons.OK, MessageBoxIcon.Exclamation)
objThisPlayerExtStats = New udtGeneralExtendedStats
End Select
intTurnScoreValid = 0
intTurnScoreRaw = 0
For lngExtStatIndex = lngCurrentTurnStartIndex To lngCurrentTurnStopIndex
With mExtendedStats(lngExtStatIndex)
If .ValidHits = 0 Then
intDartsSinceLastScore = intDartsSinceLastScore + 1
If intDartsSinceLastScore > objThisPlayerExtStats.LongestScoringDrought Then
objThisPlayerExtStats.LongestScoringDrought = intDartsSinceLastScore
End If
Else
If eGameType = gtGameTypeConstants.gt301 Then
' Record # of darts until double in if not doubled in yet
If blnRequireDoubleInFor301 And Not blnHasScoredThisGame Then
If .ValidHits <> 2 Then
' This spot should not be reached
MessageBox.Show(".ValidHits <> 2 in ParseExtendedGameStats; this is unexpected", "Code error", MessageBoxButtons.OK, MessageBoxIcon.Exclamation)
End If
blnHasScoredThisGame = True
boolFirstScoreOfGame = True
intDartsThrownThisGameTemp = intDartsThrownThisGame + lngExtStatIndex - lngCurrentTurnStartIndex + 1
If intDartsThrownThisGameTemp > ThisPlayerStats.Overall301Stats.MostDartsUntilDoubleIn Then
ThisPlayerStats.Overall301Stats.MostDartsUntilDoubleIn = intDartsThrownThisGameTemp
End If
End If
Else
' For all games, set boolFirstScoreOfGame to true for first scoring points
If Not blnHasScoredThisGame Then
blnHasScoredThisGame = True
boolFirstScoreOfGame = True
End If
End If
' Find total turn score
intTurnScoreValid = intTurnScoreValid + .DartValue * .ValidHits
intDartsSinceLastScore = 0
End If
intThisThrowScoreRaw = .DartValue * .Multiplier
intTurnScoreRaw = intTurnScoreRaw + intThisThrowScoreRaw
objThisPlayerExtStats.AllTimeDartsThrown = objThisPlayerExtStats.AllTimeDartsThrown + 1
objThisPlayerExtStats.AllTimeTotalScore = objThisPlayerExtStats.AllTimeTotalScore + intThisThrowScoreRaw
End With
Next lngExtStatIndex
intDartsThrownThisGame = intDartsThrownThisGame + lngCurrentTurnStopIndex - lngCurrentTurnStartIndex + 1
lngTotalScoreThisGame = lngTotalScoreThisGame + intTurnScoreRaw
If intTurnScoreValid > objThisPlayerExtStats.HighestScoringTurn Then
objThisPlayerExtStats.HighestScoringTurn = intTurnScoreValid
End If
If boolFirstScoreOfGame Then
If eGameType = gtGameTypeConstants.gt301 Then
' Check for highest scoring first turn for 301
If intTurnScoreValid > ThisPlayerStats.Overall301Stats.HighestScoringFirstTurn Then
ThisPlayerStats.Overall301Stats.HighestScoringFirstTurn = intTurnScoreValid
End If
End If
' Check for highest scoring first turn for all games
If intTurnScoreValid > objThisPlayerExtStats.HighestScoringFirstTurn Then
objThisPlayerExtStats.HighestScoringFirstTurn = intTurnScoreValid
End If
End If
' Copy the stats from objThisPlayerExtStats back to ThisPlayerStats
Select Case eGameType
Case gtGameTypeConstants.gt301
ThisPlayerStats.StatsFor301 = objThisPlayerExtStats
Case gtGameTypeConstants.gtCricket
ThisPlayerStats.StatsForCricket = objThisPlayerExtStats
Case gtGameTypeConstants.gtGolf
ThisPlayerStats.StatsForGolf = objThisPlayerExtStats
End Select
End Sub
Private Sub ParseGameStats(boolParseExtendedStatFile As Boolean,
strGameTypeName As String,
eStatsMode As frmStatOptionsDialog.smStatsMode)
' When eStatsMode is 0, just show Cricket stats
' When eStatsMode is 1, just show 301 stats
' When eStatsMode is 2, just show Golf stats
' When eStatsMode is 3, show cumulative stats for all games (excluding golf from the rankings)
Dim x, playeri, z As Short
Dim y As Integer
Dim PartnerNameSwap As String
Dim PlayerFound, PartnerFound As Boolean
Dim PlayerNameWork As String
Dim boolExcludeGolfGame As Boolean
Dim PlayerIndexWork, PartnerIndexWork As Short
Dim OpponentIndex1, OpponentIndex2 As Short
Dim WinningTeamIndex As Short
Dim lngExtStatStartIndex, lngExtStatStopIndex As Integer
Dim intThisGamePoints As Short
Dim lngCurrentTurnStartIndex As Integer
Dim strPreviousTurnPlayerName As String
Dim intDartsThrownThisGame, intDartsSinceLastScore As Short
Dim lngTotalScoreThisGame As Integer
Dim boolRequireDoubleIn, boolHasScoredThisGame As Boolean
Dim strPlayersExaminedForDoubleIn As String
Dim eGameType As gtGameTypeConstants
Try
eGameType = LookupGameTypeByString(strGameTypeName)
' First go through and register all Players
' Also, record which team/player won this game
WinningTeamIndex = -1
For Each udtGameStatEntry As udtGameStatsType In mGameStats
If udtGameStatEntry.GameWon Then WinningTeamIndex = x
For playeri = 1 To 2
If playeri = 1 Then
PlayerNameWork = udtGameStatEntry.PlayerName
Else
PlayerNameWork = udtGameStatEntry.PartnerName
If String.IsNullOrEmpty(PlayerNameWork) Then Exit For
End If
PlayerFound = False
For y = 0 To MasterPlayerListCount - 1
If MasterPlayerList(y) = PlayerNameWork Then
PlayerFound = True
Exit For
End If
Next y
' Add Player if not found
If Not PlayerFound Then
MasterPlayerList(MasterPlayerListCount) = String.Copy(PlayerNameWork)
With mPlayerStats(MasterPlayerListCount)
.PlayerNameIndex = MasterPlayerListCount
.PartnerNameCount = -1
.FirstGameDate = udtGameStatEntry.GameDateTime
.OverallGolfStats.HighestScoringGame = Short.MinValue
.OverallGolfStats.LowestScoringGame = Short.MaxValue
End With
MasterPlayerListCount += 1
End If
Next playeri
Next
' Next determine the start and stop indices in the mExtendedStats() array for this game
lngExtStatStartIndex = 0
lngExtStatStopIndex = 0
If boolParseExtendedStatFile AndAlso mGameStats.Count > 0 Then
For y = 1 To mExtendedStatsCount
If mGameStats(0).GameDateTime = mExtendedStats(y).GameDateTime Then
' Found the first record for this game in the extended stats file
' Record the index
If lngExtStatStartIndex = 0 Then lngExtStatStartIndex = y
ElseIf lngExtStatStartIndex > 0 Then
' If previously on the current game and now on a new game, record index
lngExtStatStopIndex = y - 1
Exit For
End If
Next y
End If
If lngExtStatStartIndex > 0 And lngExtStatStopIndex = 0 Then
' Matched the current game in the extended stats file, but didn't find a game
' after it. Thus, must be the last game in the Extended Stats file
' Record appropriate lngExtStatStopIndex
lngExtStatStopIndex = mExtendedStatsCount
End If
' Now go through the Players for this game and add their statistics
For Each udtGameStatEntry As udtGameStatsType In mGameStats
For playeri = 1 To 2
' ParseStats
PlayerIndexWork = GetPlayerIndex(udtGameStatEntry.PlayerName)
If PlayerIndexWork >= 0 Then
' Add Stats for player stored in mPlayerStats(PlayerIndexWork)
With mPlayerStats(PlayerIndexWork)
If eStatsMode = frmStatOptionsDialog.smStatsMode.AllGames AndAlso eGameType = gtGameTypeConstants.gtGolf Then
boolExcludeGolfGame = True
Else
boolExcludeGolfGame = False
End If
If Not boolExcludeGolfGame Then
' Do not parse these stats if parsing all games and this game is golf
' Increment GamesPlayed counters (separate alone & with partner; sum is total)
If String.IsNullOrEmpty(udtGameStatEntry.PartnerName) Then
.GamesPlayedAlone += 1
Else
.GamesPlayedWithPartner += 1
End If
If udtGameStatEntry.GameWon = True Then
If String.IsNullOrEmpty(udtGameStatEntry.PartnerName) Then
.GamesWonAlone += 1
Else
.GamesWonWithPartner += 1
End If
End If
' Create a new entry to add to .GamesPlayed for this player
Dim udtGameDetails As udtPlayerStatsGameInfoType
If udtGameStatEntry.GameWon = True Then
' Record Game Win Status
udtGameDetails.GameWon = True
Else
udtGameDetails.GameWon = False
End If
udtGameDetails.GameDate = udtGameStatEntry.GameDateTime
' Record Opponents -- all if player won, or only the winner if player lost
udtGameDetails.GameOpponentsIndex = New List(Of Short)
For Each udtGameStatCompare As udtGameStatsType In mGameStats
OpponentIndex1 = GetPlayerIndex(udtGameStatCompare.PlayerName)
If String.IsNullOrEmpty(udtGameStatCompare.PartnerName) Then
OpponentIndex2 = -1
Else
OpponentIndex2 = GetPlayerIndex(udtGameStatCompare.PartnerName)
End If
If PlayerIndexWork = OpponentIndex1 OrElse PlayerIndexWork = OpponentIndex2 Then
' Current team, don't count
Else
' Again, if .GameWinStatus(CompareGameCount) = 1 then record all
' otherwise, when .GameWinStatus(CompareGameCount) = 0 only record winner
If udtGameDetails.GameWon OrElse y = WinningTeamIndex Then
If Not udtGameDetails.GameOpponentsIndex.Contains(OpponentIndex1) Then
udtGameDetails.GameOpponentsIndex.Add(OpponentIndex1)
End If
If OpponentIndex2 >= 0 Then
If Not udtGameDetails.GameOpponentsIndex.Contains(OpponentIndex2) Then
udtGameDetails.GameOpponentsIndex.Add(OpponentIndex2)
End If
End If
End If
End If
Next udtGameStatCompare
' Add the new game details entry to .GamesPlayed
.GameInfo.Add(udtGameDetails)
If .LastGameDate < udtGameStatEntry.GameDateTime Then .LastGameDate = udtGameStatEntry.GameDateTime
End If
' If Playing Golf, look for new stats
If eGameType = gtGameTypeConstants.gtGolf Then
intThisGamePoints = udtGameStatEntry.GamePoints
If intThisGamePoints < .OverallGolfStats.LowestScoringGame Then
.OverallGolfStats.LowestScoringGame = intThisGamePoints
End If
If intThisGamePoints > .OverallGolfStats.HighestScoringGame Then
.OverallGolfStats.HighestScoringGame = intThisGamePoints
End If
.OverallGolfStats.TotalPointsAllGames = .OverallGolfStats.TotalPointsAllGames + intThisGamePoints
.OverallGolfStats.GameCount = .OverallGolfStats.GameCount + 1
End If
' Parse the extended stats for this game, assigning new statistics
' for this player as necessary
If boolParseExtendedStatFile And lngExtStatStartIndex > 0 Then
lngCurrentTurnStartIndex = 0
strPreviousTurnPlayerName = String.Empty
intDartsSinceLastScore = 0
intDartsThrownThisGame = 0
lngTotalScoreThisGame = 0
strPlayersExaminedForDoubleIn = String.Empty
If eGameType = gtGameTypeConstants.gt301 Then
' First determine if doubling in was required for this game
' Do this by looking to see if any of the player's 1st dart that scored points was a double
' Assume we will require double in
boolRequireDoubleIn = True
For y = lngExtStatStartIndex To lngExtStatStopIndex
If mExtendedStats(y).ValidHits > 0 Then
' See if player has been examined yet
If Not strPlayersExaminedForDoubleIn.Contains(";" & mExtendedStats(y).PlayerName & ";") Then
strPlayersExaminedForDoubleIn &= ";" & mExtendedStats(y).PlayerName & ";"
If mExtendedStats(y).ValidHits <> 2 Then
boolRequireDoubleIn = False
Exit For
End If
End If
End If
Next y
Else
boolRequireDoubleIn = False
End If
boolHasScoredThisGame = False
For y = lngExtStatStartIndex To lngExtStatStopIndex
If mExtendedStats(y).PlayerName = udtGameStatEntry.PlayerName Then
If strPreviousTurnPlayerName <> mExtendedStats(y).PlayerName Then
' Start of a new turn
lngCurrentTurnStartIndex = y
End If
ElseIf strPreviousTurnPlayerName = udtGameStatEntry.PlayerName Then
' Completed previous turn, parse the previous turn
ParseExtendedGameStats(lngCurrentTurnStartIndex, y - 1, mPlayerStats(PlayerIndexWork), intDartsThrownThisGame, intDartsSinceLastScore, lngTotalScoreThisGame, eGameType, boolRequireDoubleIn, boolHasScoredThisGame)
lngCurrentTurnStartIndex = 0
End If
strPreviousTurnPlayerName = mExtendedStats(y).PlayerName
Next y
If lngCurrentTurnStartIndex > 0 Then
' Stats remaining to be parsed
ParseExtendedGameStats(lngCurrentTurnStartIndex, y - 1, mPlayerStats(PlayerIndexWork), intDartsThrownThisGame, intDartsSinceLastScore, lngTotalScoreThisGame, eGameType, boolRequireDoubleIn, boolHasScoredThisGame)
End If
' See if never doubled in
' Make sure at least 6 darts have been thrown by this player
If boolRequireDoubleIn And Not boolHasScoredThisGame Then
If intDartsThrownThisGame >= 6 Then
mPlayerStats(PlayerIndexWork).Overall301Stats.GamesLostWithoutDoublingIn = mPlayerStats(PlayerIndexWork).Overall301Stats.GamesLostWithoutDoublingIn + 1
End If
End If
' Record game-wide stats for this player
If udtGameStatEntry.GameWon And intDartsThrownThisGame > 0 Then
Select Case eGameType
Case gtGameTypeConstants.gt301
RecordExtendedGameStats(mPlayerStats(PlayerIndexWork).StatsFor301, intDartsThrownThisGame)
Case gtGameTypeConstants.gtCricket
RecordExtendedGameStats(mPlayerStats(PlayerIndexWork).StatsForCricket, intDartsThrownThisGame)
Case gtGameTypeConstants.gtGolf
RecordExtendedGameStats(mPlayerStats(PlayerIndexWork).StatsForGolf, intDartsThrownThisGame)
End Select
End If
Select Case eGameType
Case gtGameTypeConstants.gt301
RecordMeanThrowGameStats(mPlayerStats(PlayerIndexWork).StatsFor301, udtGameStatEntry.GameDateTime, intDartsThrownThisGame, lngTotalScoreThisGame)
Case gtGameTypeConstants.gtCricket
RecordMeanThrowGameStats(mPlayerStats(PlayerIndexWork).StatsForCricket, udtGameStatEntry.GameDateTime, intDartsThrownThisGame, lngTotalScoreThisGame)
Case gtGameTypeConstants.gtGolf
RecordMeanThrowGameStats(mPlayerStats(PlayerIndexWork).StatsForGolf, udtGameStatEntry.GameDateTime, intDartsThrownThisGame, lngTotalScoreThisGame)
End Select
End If
If String.IsNullOrEmpty(udtGameStatEntry.PartnerName) Then
Exit For
Else
If Not boolExcludeGolfGame Then
' Record partner stats
PartnerIndexWork = GetPlayerIndex(udtGameStatEntry.PartnerName)
PartnerFound = False
If PartnerIndexWork >= 0 Then
For z = 0 To .PartnerNameCount
If .PartnerNameIndex(z) = PartnerIndexWork Then
PartnerFound = True
Exit For
End If
Next z
End If
If Not PartnerFound Then
' Add Partner
.PartnerNameCount = .PartnerNameCount + 1
.PartnerNameIndex(.PartnerNameCount) = PartnerIndexWork
z = .PartnerNameCount
End If
' If game won, increment for this partner
If udtGameStatEntry.GameWon Then .GamesWonByPartner(z).GamesWon += 1
' Increment total games played with this partner
.GamesWonByPartner(z).GamesPlayed += 1
' Swap Player and Partner and add stats
PartnerNameSwap = udtGameStatEntry.PlayerName
udtGameStatEntry.PlayerName = udtGameStatEntry.PartnerName
udtGameStatEntry.PartnerName = PartnerNameSwap
End If
End If
End With
End If
Next playeri
Next udtGameStatEntry
Catch ex As Exception
HandleException("ParseGameStats", ex)
End Try
End Sub
''' <summary>
''' Splits strLineToParse on commas
''' </summary>
''' <param name="strLineToParse"></param>
''' <param name="KeyString"></param>
''' <returns></returns>
''' <remarks>The number of items parsed out of strLineToParse</remarks>
Private Function ParseLine(strLineToParse As String, ByRef KeyString() As String) As Integer
Static chSepChars() As Char = New Char() {","c}
Dim x As Short
Dim strItems() As String
Dim intKeyStringItemCount As Integer
intKeyStringItemCount = 0
x = strLineToParse.IndexOf("="c)
' ToDo: Check this code, ported from VB6
If x > 0 AndAlso x < strLineToParse.Length - 1 Then
strLineToParse = strLineToParse.Substring(x + 1).Trim()
If strLineToParse.Length > 0 Then
strItems = strLineToParse.Split(chSepChars, MAX_PARSELINE_COUNT)
intKeyStringItemCount = strItems.Length
Array.Copy(strItems, KeyString, intKeyStringItemCount)
End If
End If
Return intKeyStringItemCount
End Function
Private Sub RankPlayers(StartingDate As DateTime, EndingDate As DateTime, DateWeight As Single)
Dim x As Short
Dim OpponentRankSum, OpponentRank As Single
Dim RecentGamesWonRelative As Single