-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathHitEventDisplay.cs
More file actions
192 lines (154 loc) · 6.83 KB
/
Copy pathHitEventDisplay.cs
File metadata and controls
192 lines (154 loc) · 6.83 KB
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
using CounterStrikeSharp.API.Core;
using System.Text;
using System.Text.RegularExpressions;
namespace EntBossHP
{
internal partial class HitEventDisplay
{
private const int MaxSplits = 20;
private const int HudTextCapacity = 192;
[GeneratedRegex(@"_\d{3,}$")]
private static partial Regex BossNameSuffixRegex();
private static readonly string[] FilledBars = BuildBars('█', MaxSplits + 1);
private static readonly string[] EmptyBars = BuildBars('░', MaxSplits + 1);
private static string[] BuildBars(char ch, int length)
{
var arr = new string[length];
for (var i = 0; i < length; i++)
{
arr[i] = new string(ch, i);
}
return arr;
}
private readonly EntBossHP _plugin;
public HitEventDisplay(EntBossHP plugin)
{
_plugin = plugin;
}
private static string SanitizeBossName(string entityName)
{
if (string.IsNullOrEmpty(entityName)) return string.Empty;
return BossNameSuffixRegex().Replace(entityName, "_");
}
private string GetBossDisplayName(string entityName)
{
return ResolveDisplayName(_plugin.BossConfigs, entityName);
}
internal static string ResolveDisplayName(BossConfig? config, string entityName)
{
var sanitizedName = SanitizeBossName(entityName);
if (config == null) return sanitizedName;
var best = FindBestDisplayEntry(
sanitizedName,
GetDisplayEntries(config.BreakableList, config.MathCounterList));
return best.DisplayName ?? sanitizedName;
}
private static IEnumerable<(string ConfiguredName, string? DisplayName)> GetDisplayEntries(
IEnumerable<BreakableConfig>? breakables,
IEnumerable<MathCounterConfig>? mathCounters)
{
if (breakables != null)
{
foreach (var breakable in breakables)
{
if (breakable != null) yield return (breakable.Breakable, breakable.Name);
}
}
if (mathCounters != null)
{
foreach (var mathCounter in mathCounters)
{
if (mathCounter != null) yield return (mathCounter.MathCounter, mathCounter.Name);
}
}
}
private static (string ConfiguredName, string? DisplayName) FindBestDisplayEntry(
string sanitizedName,
IEnumerable<(string ConfiguredName, string? DisplayName)> entries)
{
(string ConfiguredName, string? DisplayName) bestPrefix = default;
var bestPrefixLength = -1;
foreach (var entry in entries)
{
if (string.IsNullOrEmpty(entry.ConfiguredName) || string.IsNullOrEmpty(sanitizedName)) continue;
if (sanitizedName.Equals(entry.ConfiguredName, StringComparison.Ordinal))
{
return entry;
}
if (!MatchesDisplayName(entry.ConfiguredName, sanitizedName)) continue;
if (entry.ConfiguredName.Length <= bestPrefixLength) continue;
bestPrefix = entry;
bestPrefixLength = entry.ConfiguredName.Length;
}
return bestPrefix;
}
public void ShowHitEvent(CCSPlayerController controller, BossData boss, int health)
{
if (controller == null || !controller.IsValid || boss == null) return;
if (!boss.Enabled) return;
var displayHealth = Math.Max(0, health + boss.HpOffset);
if (boss.MaxHealth <= 0 || health > boss.MaxHealth)
{
boss.MaxHealth = health;
}
var maxHealth = boss.MaxHealth > 0 ? boss.MaxHealth : health;
var displayMaxHealth = maxHealth + boss.HpOffset;
if (displayMaxHealth <= 0) displayMaxHealth = 1;
int p;
if (displayHealth <= 0)
{
p = 0;
}
else
{
p = (displayHealth * 100 + displayMaxHealth - 1) / displayMaxHealth;
if (p > 100) p = 100;
}
var healthBarCount = p <= 0 ? 0 : Math.Min(p / (100 / MaxSplits) + 1, MaxSplits);
var filledSegment = FilledBars[healthBarCount];
var emptySegment = EmptyBars[MaxSplits - healthBarCount];
var builder = new StringBuilder(HudTextCapacity);
var displayName = GetBossDisplayName(boss.BossName);
var segmentCount = GetSegmentCount(boss);
builder.Append("<font class='fontSize-lg' color='#FFFFFF'>").Append(displayName).Append("</font> : ");
builder.Append("<font color='#31700'>").Append(displayHealth).Append("</font>");
if (segmentCount >= 1)
{
builder.Append(" <font color='#FFFF00'>(").Append(segmentCount).Append(")</font>");
}
builder.Append("<br>");
if (healthBarCount > 0)
{
builder.Append("<font class='fontSize-lg' color='#00FF00'>").Append(filledSegment).Append("</font>");
}
builder.Append("<font class='fontSize-lg' color='#363636'>").Append(emptySegment).Append("</font>");
controller.PrintToCenterHtml(builder.ToString(), 3);
}
public void ShowBossDefeated(CCSPlayerController controller, BossData boss)
{
if (controller == null || !controller.IsValid || boss == null) return;
var displayName = GetBossDisplayName(boss.BossName);
var builder = new StringBuilder(HudTextCapacity);
builder.Append("<font class='fontSize-lg' color='#888888'>").Append(displayName).Append("</font> : ");
builder.Append("<font color='#888888'>0</font>");
builder.Append("<br>");
builder.Append("<font class='fontSize-lg' color='#363636'>").Append(EmptyBars[MaxSplits]).Append("</font>");
controller.PrintToCenterHtml(builder.ToString(), 3);
}
private static int GetSegmentCount(BossData boss)
{
return boss switch
{
MathCounterBoss m when m.IsSegmented && m.HealthSegments >= 0 => m.HealthSegments,
BreakableBoss b when b.IsSegmented && b.HealthSegments >= 0 => b.HealthSegments,
_ => -1,
};
}
private static bool MatchesDisplayName(string configuredName, string sanitizedName)
{
return !string.IsNullOrEmpty(configuredName)
&& !string.IsNullOrEmpty(sanitizedName)
&& sanitizedName.StartsWith(configuredName, StringComparison.Ordinal);
}
}
}