@@ -12,11 +12,12 @@ public partial class MatchZy
12
12
private void InitPlayerDamageInfo ( )
13
13
{
14
14
foreach ( var key in playerData . Keys ) {
15
+ if ( ! playerData [ key ] . IsValid ) continue ;
15
16
if ( playerData [ key ] . IsBot ) continue ;
16
17
int attackerId = key ;
17
18
foreach ( var key2 in playerData . Keys ) {
18
19
if ( key == key2 ) continue ;
19
- if ( playerData [ key2 ] . IsBot ) continue ;
20
+ if ( ! playerData [ key2 ] . IsValid || playerData [ key2 ] . IsBot ) continue ;
20
21
if ( playerData [ key ] . TeamNum == playerData [ key2 ] . TeamNum ) continue ;
21
22
if ( playerData [ key ] . TeamNum == 2 ) {
22
23
if ( playerData [ key2 ] . TeamNum != 3 ) continue ;
@@ -54,50 +55,65 @@ private void UpdatePlayerDamageInfo(EventPlayerHurt @event, int targetId)
54
55
}
55
56
56
57
private void ShowDamageInfo ( )
57
- {
58
- HashSet < ( int , int ) > processedPairs = new HashSet < ( int , int ) > ( ) ;
59
-
60
- foreach ( var entry in playerDamageInfo )
61
- {
62
- int attackerId = entry . Key ;
63
- foreach ( var ( targetId , targetEntry ) in entry . Value )
64
- {
65
- if ( processedPairs . Contains ( ( attackerId , targetId ) ) || processedPairs . Contains ( ( targetId , attackerId ) ) )
66
- continue ;
67
-
68
- // Access and use the damage information as needed.
69
- int damageGiven = targetEntry . DamageHP ;
70
- int hitsGiven = targetEntry . Hits ;
71
- int damageTaken = 0 ;
72
- int hitsTaken = 0 ;
73
-
74
- if ( playerDamageInfo . TryGetValue ( targetId , out var targetInfo ) && targetInfo . TryGetValue ( attackerId , out var takenInfo ) )
75
- {
76
- damageTaken = takenInfo . DamageHP ;
77
- hitsTaken = takenInfo . Hits ;
78
- }
79
-
80
- var attackerController = Utilities . GetPlayerFromUserid ( attackerId ) ;
81
- var targetController = Utilities . GetPlayerFromUserid ( targetId ) ;
82
-
83
- if ( attackerController != null && targetController != null )
84
- {
85
- int attackerHP = attackerController . PlayerPawn . Value . Health < 0 ? 0 : attackerController . PlayerPawn . Value . Health ;
86
- string attackerName = attackerController . PlayerName ;
87
-
88
- int targetHP = targetController . PlayerPawn . Value . Health < 0 ? 0 : targetController . PlayerPawn . Value . Health ;
89
- string targetName = targetController . PlayerName ;
90
-
91
- attackerController . PrintToChat ( $ "{ chatPrefix } { ChatColors . Green } To: [{ damageGiven } / { hitsGiven } hits] From: [{ damageTaken } / { hitsTaken } hits] - { targetName } - ({ targetHP } hp){ ChatColors . Default } ") ;
92
- targetController . PrintToChat ( $ "{ chatPrefix } { ChatColors . Green } To: [{ damageTaken } / { hitsTaken } hits] From: [{ damageGiven } / { hitsGiven } hits] - { attackerName } - ({ attackerHP } hp){ ChatColors . Default } ") ;
93
- }
94
-
95
- // Mark this pair as processed to avoid duplicates.
96
- processedPairs . Add ( ( attackerId , targetId ) ) ;
97
- }
98
- }
99
- playerDamageInfo . Clear ( ) ;
100
- }
58
+ {
59
+ try
60
+ {
61
+ HashSet < ( int , int ) > processedPairs = new HashSet < ( int , int ) > ( ) ;
62
+
63
+ foreach ( var entry in playerDamageInfo )
64
+ {
65
+ int attackerId = entry . Key ;
66
+ foreach ( var ( targetId , targetEntry ) in entry . Value )
67
+ {
68
+ if ( processedPairs . Contains ( ( attackerId , targetId ) ) || processedPairs . Contains ( ( targetId , attackerId ) ) )
69
+ continue ;
70
+
71
+ // Access and use the damage information as needed.
72
+ int damageGiven = targetEntry . DamageHP ;
73
+ int hitsGiven = targetEntry . Hits ;
74
+ int damageTaken = 0 ;
75
+ int hitsTaken = 0 ;
76
+
77
+ if ( playerDamageInfo . TryGetValue ( targetId , out var targetInfo ) && targetInfo . TryGetValue ( attackerId , out var takenInfo ) )
78
+ {
79
+ damageTaken = takenInfo . DamageHP ;
80
+ hitsTaken = takenInfo . Hits ;
81
+ }
82
+
83
+ if ( ! playerData . ContainsKey ( attackerId ) || ! playerData . ContainsKey ( targetId ) ) continue ;
84
+
85
+ var attackerController = playerData [ attackerId ] ;
86
+ var targetController = playerData [ targetId ] ;
87
+
88
+ if ( attackerController != null && targetController != null )
89
+ {
90
+ if ( ! attackerController . IsValid || ! targetController . IsValid ) continue ;
91
+ if ( attackerController . Connected != PlayerConnectedState . PlayerConnected ) continue ;
92
+ if ( targetController . Connected != PlayerConnectedState . PlayerConnected ) continue ;
93
+ if ( ! attackerController . PlayerPawn . IsValid || ! targetController . PlayerPawn . IsValid ) continue ;
94
+
95
+ int attackerHP = attackerController . PlayerPawn . Value . Health < 0 ? 0 : attackerController . PlayerPawn . Value . Health ;
96
+ string attackerName = attackerController . PlayerName ;
97
+
98
+ int targetHP = targetController . PlayerPawn . Value . Health < 0 ? 0 : targetController . PlayerPawn . Value . Health ;
99
+ string targetName = targetController . PlayerName ;
100
+
101
+ attackerController . PrintToChat ( $ "{ chatPrefix } { ChatColors . Green } To: [{ damageGiven } / { hitsGiven } hits] From: [{ damageTaken } / { hitsTaken } hits] - { targetName } - ({ targetHP } hp){ ChatColors . Default } ") ;
102
+ targetController . PrintToChat ( $ "{ chatPrefix } { ChatColors . Green } To: [{ damageTaken } / { hitsTaken } hits] From: [{ damageGiven } / { hitsGiven } hits] - { attackerName } - ({ attackerHP } hp){ ChatColors . Default } ") ;
103
+ }
104
+
105
+ // Mark this pair as processed to avoid duplicates.
106
+ processedPairs . Add ( ( attackerId , targetId ) ) ;
107
+ }
108
+ }
109
+ playerDamageInfo . Clear ( ) ;
110
+ }
111
+ catch ( Exception e )
112
+ {
113
+ Log ( $ "[ShowDamageInfo FATAL] An error occurred: { e . Message } ") ;
114
+ }
115
+
116
+ }
101
117
}
102
118
103
119
public class DamagePlayerInfo
0 commit comments