forked from shobhit-pathak/MatchZy
-
Notifications
You must be signed in to change notification settings - Fork 0
/
DamageInfo.cs
125 lines (103 loc) · 5.9 KB
/
DamageInfo.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
using CounterStrikeSharp.API;
using CounterStrikeSharp.API.Core;
using CounterStrikeSharp.API.Modules.Utils;
namespace MatchZy
{
public partial class MatchZy
{
private void InitPlayerDamageInfo()
{
foreach (var key in playerData.Keys) {
if (!playerData[key].IsValid) continue;
if (playerData[key].IsBot) continue;
int attackerId = key;
foreach (var key2 in playerData.Keys) {
if (key == key2) continue;
if (!playerData[key2].IsValid || playerData[key2].IsBot) continue;
if (playerData[key].TeamNum == playerData[key2].TeamNum) continue;
if (playerData[key].TeamNum == 2) {
if (playerData[key2].TeamNum != 3) continue;
int targetId = key2;
if (!playerDamageInfo.TryGetValue(attackerId, out var attackerInfo))
playerDamageInfo[attackerId] = attackerInfo = new Dictionary<int, DamagePlayerInfo>();
if (!attackerInfo.TryGetValue(targetId, out var targetInfo))
attackerInfo[targetId] = targetInfo = new DamagePlayerInfo();
} else if (playerData[key].TeamNum == 3) {
if (playerData[key2].TeamNum != 2) continue;
int targetId = key2;
if (!playerDamageInfo.TryGetValue(attackerId, out var attackerInfo))
playerDamageInfo[attackerId] = attackerInfo = new Dictionary<int, DamagePlayerInfo>();
if (!attackerInfo.TryGetValue(targetId, out var targetInfo))
attackerInfo[targetId] = targetInfo = new DamagePlayerInfo();
}
}
}
}
public Dictionary<int, Dictionary<int, DamagePlayerInfo>> playerDamageInfo = new Dictionary<int, Dictionary<int, DamagePlayerInfo>>();
private void UpdatePlayerDamageInfo(EventPlayerHurt @event, int targetId)
{
int attackerId = (int)@event.Attacker.UserId!;
if (!playerDamageInfo.TryGetValue(attackerId, out var attackerInfo))
playerDamageInfo[attackerId] = attackerInfo = new Dictionary<int, DamagePlayerInfo>();
if (!attackerInfo.TryGetValue(targetId, out var targetInfo))
attackerInfo[targetId] = targetInfo = new DamagePlayerInfo();
targetInfo.DamageHP += @event.DmgHealth;
targetInfo.Hits++;
}
private void ShowDamageInfo()
{
try
{
HashSet<(int, int)> processedPairs = new HashSet<(int, int)>();
foreach (var entry in playerDamageInfo)
{
int attackerId = entry.Key;
foreach (var (targetId, targetEntry) in entry.Value)
{
if (processedPairs.Contains((attackerId, targetId)) || processedPairs.Contains((targetId, attackerId)))
continue;
// Access and use the damage information as needed.
int damageGiven = targetEntry.DamageHP;
int hitsGiven = targetEntry.Hits;
int damageTaken = 0;
int hitsTaken = 0;
if (playerDamageInfo.TryGetValue(targetId, out var targetInfo) && targetInfo.TryGetValue(attackerId, out var takenInfo))
{
damageTaken = takenInfo.DamageHP;
hitsTaken = takenInfo.Hits;
}
if (!playerData.ContainsKey(attackerId) || !playerData.ContainsKey(targetId)) continue;
var attackerController = playerData[attackerId];
var targetController = playerData[targetId];
if (attackerController != null && targetController != null)
{
if (!attackerController.IsValid || !targetController.IsValid) continue;
if (attackerController.Connected != PlayerConnectedState.PlayerConnected) continue;
if (targetController.Connected != PlayerConnectedState.PlayerConnected) continue;
if (!attackerController.PlayerPawn.IsValid || !targetController.PlayerPawn.IsValid) continue;
if (attackerController.PlayerPawn.Value == null || targetController.PlayerPawn.Value == null) continue;
int attackerHP = attackerController.PlayerPawn.Value.Health < 0 ? 0 : attackerController.PlayerPawn.Value.Health;
string attackerName = attackerController.PlayerName;
int targetHP = targetController.PlayerPawn.Value.Health < 0 ? 0 : targetController.PlayerPawn.Value.Health;
string targetName = targetController.PlayerName;
attackerController.PrintToChat($"{chatPrefix} {ChatColors.Green}To: [{damageGiven} / {hitsGiven} hits] From: [{damageTaken} / {hitsTaken} hits] - {targetName} - ({targetHP} hp){ChatColors.Default}");
targetController.PrintToChat($"{chatPrefix} {ChatColors.Green}To: [{damageTaken} / {hitsTaken} hits] From: [{damageGiven} / {hitsGiven} hits] - {attackerName} - ({attackerHP} hp){ChatColors.Default}");
}
// Mark this pair as processed to avoid duplicates.
processedPairs.Add((attackerId, targetId));
}
}
playerDamageInfo.Clear();
}
catch (Exception e)
{
Log($"[ShowDamageInfo FATAL] An error occurred: {e.Message}");
}
}
}
public class DamagePlayerInfo
{
public int DamageHP { get; set; } = 0;
public int Hits { get; set; } = 0;
}
}