-
Notifications
You must be signed in to change notification settings - Fork 7
/
BombsiteAnnouncer.cs
244 lines (214 loc) · 8.76 KB
/
BombsiteAnnouncer.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
using System.Text.Json.Serialization;
using CounterStrikeSharp.API;
using CounterStrikeSharp.API.Core;
using CounterStrikeSharp.API.Core.Attributes.Registration;
using CounterStrikeSharp.API.Modules.Admin;
using CounterStrikeSharp.API.Modules.Entities;
using CounterStrikeSharp.API.Modules.Utils;
using Microsoft.Extensions.Logging;
namespace BombsiteAnnouncer;
public class Config : BasePluginConfig
{
[JsonPropertyName("show-announcer-delay")]
public float ShowAnnouncerDelay { get; set; } = 0.1f;
[JsonPropertyName("announcer-visible-for-time")]
public float AnnouncerVisibleForTime { get; set; } = 10.0f;
[JsonPropertyName("remove-bomb-planted-message")]
public bool RemoveDefaultMsg { get; set; } = true;
[JsonPropertyName("bombsite-A-img")]
public string BombsiteAimg { get; set; } = "https://raw.githubusercontent.com/audiomaster99/CS2BombsiteAnnouncer/main/img/Site-A.png";
[JsonPropertyName("bombsite-B-img")]
public string BombsiteBimg { get; set; } = "https://raw.githubusercontent.com/audiomaster99/CS2BombsiteAnnouncer/main/img/Site-B.png";
[JsonPropertyName("show-site-info-text")]
public bool SiteText { get; set; } = true;
[JsonPropertyName("show-site-info-image")]
public bool SiteImage { get; set; } = true;
[JsonPropertyName("show-player-counter")]
public bool PlayerCounter { get; set; } = true;
[JsonPropertyName("show-announcer-flags")]
public List<string> ShowAnnouncerFlags { get; set; } = new List<string>
{
"@css/vip",
"#css/admin",
"leave empty array []; to show announcer to everyone"
};
[JsonPropertyName("ConfigVersion")]
public override int Version { get; set; } = 3;
}
public partial class BombsiteAnnouncer : BasePlugin, IPluginConfig<Config>
{
public override string ModuleName => "BombsiteAnnouncer";
public override string ModuleAuthor => "itsAudio";
public override string ModuleDescription => "Simple bombsite announcer";
public override string ModuleVersion => "V. 0.0.7";
public required Config Config { get; set; }
public bool bombsiteAnnouncer;
public bool isRetakesEnabled;
public string? bombsite;
public string? message;
public string? color;
public string? siteTextString;
public string? siteImageString;
public string? playerCounterString;
public string? siteImage;
public string? breakLine;
public int ctNum;
public int ttNum;
public override void Load(bool hotReload)
{
Logger.LogInformation("BombsiteAnnouncer Plugin has started!");
AddTimer(0.1f, () => { IsRetakesPluginInstalled(); });
RegisterListener<Listeners.OnTick>(() =>
{
if (bombsiteAnnouncer == true)
{
Utilities.GetPlayers().Where(player => IsValid(player) && IsConnected(player) && PlayerHasPermissions(player)).ToList().ForEach(p => OnTick(p));
}
});
}
public void OnConfigParsed(Config config)
{
Config = config;
}
private void OnTick(CCSPlayerController player)
{
ctNum = GetCurrentNumPlayers(CsTeam.CounterTerrorist);
ttNum = GetCurrentNumPlayers(CsTeam.Terrorist);
if (player.Team == CsTeam.CounterTerrorist)
{
color = "green";
message = Localizer["phrases.retake"];
}
else
{
color = "red";
message = Localizer["phrases.defend"];
}
HandleCustomizedMessage();
player.PrintToCenterHtml(siteTextString + siteImageString + playerCounterString);
}
public void ShowAnnouncer()
{
AddTimer(Config.ShowAnnouncerDelay, () =>
{
bombsiteAnnouncer = true;
AddTimer(Config.AnnouncerVisibleForTime, () => { bombsiteAnnouncer = false; });
});
}
public void HandleCustomizedMessage()
{
siteTextString = Config.SiteText ? $"<font class='fontSize-l' color='{color}'>{message} <font color='white'>{Localizer["phrases.site"]}</font> <font color='{color}'>{bombsite}</font>{breakLine}" : "";
siteImageString = Config.SiteImage ? $"<img src='{siteImage}'> {breakLine}" : "";
playerCounterString = Config.PlayerCounter ? $"<font class='fontSize-m' color='white'>{ttNum}</font> <font class='fontSize-m'color='red'>{Localizer["phrases.terrorist"]} </font><font class='fontSize-m' color='white'> {Localizer["phrases.versus"]}</font> <font class='fontSize-m' color='white'> {ctNum} </font><font class='fontSize-m' color='blue'>{Localizer["phrases.cterrorist"]}</font>" : "";
//fix bad looking message if some lines are not displayed
//caused by line-break
if (!Config.SiteText && !Config.PlayerCounter && Config.SiteImage) { breakLine = ""; }
else if (Config.SiteText && !Config.PlayerCounter && !Config.SiteImage) { breakLine = ""; }
else { breakLine = "<br>"; }
}
public void GetSiteImage()
{
siteImage = bombsite == "B" ? Config.BombsiteBimg : bombsite == "A" ? Config.BombsiteAimg : "";
if (siteImage == "")
{
Logger.LogWarning($"Unknown bombsite value: {bombsite}");
}
}
//---- P L U G I N - H O O O K S ----
[GameEventHandler(HookMode.Pre)]
public HookResult OnBombPlanted(EventBombPlanted @event, GameEventInfo info)
{
CCSPlayerController? player = @event.Userid;
CBombTarget site = new CBombTarget(NativeAPI.GetEntityFromIndex(@event.Site));
if (isRetakesEnabled == true)
{
bombsite = (@event.Site == 1) ? "B" : "A";
}
else bombsite = site.IsBombSiteB ? "B" : "A";
GetSiteImage();
ShowAnnouncer();
Logger.LogInformation($"Bomb Planted on [{bombsite}]");
// remove bomb planted message
if (Config.RemoveDefaultMsg && @event != null)
{
return HookResult.Handled;
}
return HookResult.Continue;
}
[GameEventHandler]
public HookResult OnBombDefused(EventBombDefused @event, GameEventInfo info)
{
bombsiteAnnouncer = false;
return HookResult.Continue;
}
[GameEventHandler]
public HookResult OnBombDetonate(EventBombExploded @event, GameEventInfo info)
{
bombsiteAnnouncer = false;
return HookResult.Continue;
}
[GameEventHandler]
public HookResult OnRoundEnd(EventRoundEnd @event, GameEventInfo info)
{
bombsiteAnnouncer = false;
return HookResult.Continue;
}
[GameEventHandler]
public HookResult OnRoundStart(EventRoundStart @event, GameEventInfo info)
{
bombsiteAnnouncer = false;
return HookResult.Continue;
}
//---- P L U G I N - H E L P E R S ----
static bool IsValid(CCSPlayerController? player)
{
return player?.IsValid == true && player.PlayerPawn?.IsValid == true && !player.IsBot && !player.IsHLTV;
}
static bool IsConnected(CCSPlayerController? player)
{
return player?.Connected == PlayerConnectedState.PlayerConnected;
}
static bool IsAlive(CCSPlayerController player)
{
return player.PawnIsAlive;
}
public static int GetCurrentNumPlayers(CsTeam? csTeam = null)
{
return Utilities.GetPlayers().Count(player => IsAlive(player) && IsConnected(player) && (csTeam == null || player.Team == csTeam));
}
public void IsRetakesPluginInstalled()
{
string? path = Directory.GetParent(ModuleDirectory)?.FullName;
if (Directory.Exists(path + "/RetakesPlugin"))
{
Logger.LogInformation("RETAKES MODE ENABLED");
isRetakesEnabled = true;
}
else isRetakesEnabled = false;
}
public bool PlayerHasPermissions(CCSPlayerController player)
{
if (Config.ShowAnnouncerFlags.Count == 0)
return true;
bool hasPermission = false;
foreach (string checkPermission in Config.ShowAnnouncerFlags)
{
switch (checkPermission[0])
{
case '@':
if (AdminManager.PlayerHasPermissions(player, checkPermission))
hasPermission = true;
break;
case '#':
if (AdminManager.PlayerInGroup(player, checkPermission))
hasPermission = true;
break;
default:
if (AdminManager.PlayerHasCommandOverride(player, checkPermission))
hasPermission = true;
break;
}
}
return hasPermission;
}
}