Skip to content

Commit

Permalink
warden mute fix (#131)
Browse files Browse the repository at this point in the history
  • Loading branch information
jithatsonei authored Apr 30, 2024
2 parents 48d49f8 + fb62ccd commit 8c229a0
Show file tree
Hide file tree
Showing 4 changed files with 107 additions and 35 deletions.
4 changes: 3 additions & 1 deletion mod/Jailbreak.Logs/Listeners/LogEntityListeners.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
using CounterStrikeSharp.API;
using CounterStrikeSharp.API.Core;
using CounterStrikeSharp.API.Core.Attributes.Registration;

using CounterStrikeSharp.API.Modules.Memory;
using CounterStrikeSharp.API.Modules.Memory.DynamicFunctions;
using CounterStrikeSharp.API.Modules.Utils;
using Jailbreak.Formatting.Views;
using Jailbreak.Public.Behaviors;
using Jailbreak.Public.Extensions;
Expand Down
128 changes: 95 additions & 33 deletions mod/Jailbreak.Mute/MuteSystem.cs
Original file line number Diff line number Diff line change
Expand Up @@ -23,15 +23,18 @@ public class MuteSystem(IServiceProvider provider) : IPluginBehavior, IMuteServi

private IPeaceMessages messages;
private IWardenService warden;
private Queue<int> ctScheduledMutes = new();
private Queue<int> tScheduledMutes = new();


private Timer? prisonerTimer, guardTimer;

public void Start(BasePlugin parent)
{
this.parent = parent;

this.messages = provider.GetRequiredService<IPeaceMessages>();
this.warden = provider.GetRequiredService<IWardenService>();
messages = provider.GetRequiredService<IPeaceMessages>();
warden = provider.GetRequiredService<IWardenService>();

parent.RegisterListener<Listeners.OnClientVoice>(OnPlayerSpeak);
}
Expand All @@ -40,14 +43,70 @@ public void Dispose()
{
parent.RemoveListener("OnClientVoice", OnPlayerSpeak);

Check warning on line 44 in mod/Jailbreak.Mute/MuteSystem.cs

View workflow job for this annotation

GitHub Actions / build

'BasePlugin.RemoveListener(string, Delegate)' is obsolete: 'Use the generic version of this method'
}

private void TickTerroristMutes()
{
if (tScheduledMutes.Count == 0)
return;

var muteDuration = tScheduledMutes.Dequeue();

prisonerTimer = parent.AddTimer(muteDuration, () =>
{
if (tScheduledMutes.Count != 0)
{
TickTerroristMutes();
return;
}
foreach (var player in Utilities.GetPlayers().Where(player => player.IsReal() && player.Team == CsTeam.Terrorist))
{
UnMute(player);
}

prisonerTimer?.Kill();
prisonerTimer = null;
});
}

private void TickCounterTerroristMutes()
{
if (ctScheduledMutes.Count == 0)
return;

var muteDuration = ctScheduledMutes.Dequeue();

guardTimer = parent.AddTimer(muteDuration, () =>
{
if (ctScheduledMutes.Count != 0)
{
TickCounterTerroristMutes();
return;
}

foreach (var player in Utilities.GetPlayers().Where(player => player.IsReal() && player.Team == CsTeam.CounterTerrorist))
{
UnMute(player);
}


guardTimer?.Kill();
guardTimer = null;
});
}

public void PeaceMute(MuteReason reason)
{
var duration = GetPeaceDuration(reason);
var ctDuration = Math.Min(10, duration);

if (IsPeaceEnabled())
{
foreach (var player in Utilities.GetPlayers().Where(player => player.IsReal() && (player.VoiceFlags & VoiceFlags.Muted) != 0))
UnMute(player);
}
foreach (var player in Utilities.GetPlayers().Where(player => player.IsReal()))
if (!warden.IsWarden(player))
mute(player);
Mute(player);

switch (reason)
{
Expand All @@ -68,31 +127,34 @@ public void PeaceMute(MuteReason reason)
this.peaceEnd = DateTime.Now.AddSeconds(duration);
this.ctPeaceEnd = DateTime.Now.AddSeconds(ctDuration);
this.lastPeace = DateTime.Now;

guardTimer?.Kill();
prisonerTimer?.Kill();

guardTimer = parent.AddTimer(ctDuration, () =>

ctScheduledMutes.Enqueue(ctDuration);
tScheduledMutes.Enqueue(duration);

if (tScheduledMutes.Count == 1 || prisonerTimer == null) TickTerroristMutes();
if (ctScheduledMutes.Count == 1 || guardTimer == null) TickCounterTerroristMutes();
}

public void UnPeaceMute()
{
foreach (var player in Utilities.GetPlayers().Where(player => player.IsReal() && player.Team == CsTeam.Terrorist))
{
foreach (var player in Utilities.GetPlayers()
.Where(player =>
player.IsReal() && player is { Team: CsTeam.CounterTerrorist, PawnIsAlive: true }))
unmute(player);

messages.UNMUTED_GUARDS.ToAllChat();
});

prisonerTimer = parent.AddTimer(duration, () =>
UnMute(player);
}
foreach (var player in Utilities.GetPlayers().Where(player => player.IsReal() && player.Team == CsTeam.CounterTerrorist))
{
foreach (var player in Utilities.GetPlayers()
.Where(player =>
player.IsReal() && player is { Team: CsTeam.Terrorist, PawnIsAlive: true }))
unmute(player);

messages.UNMUTED_PRISONERS.ToAllChat();
});
UnMute(player);
}
prisonerTimer?.Kill();
prisonerTimer = null;
guardTimer?.Kill();
guardTimer = null;
}


private int GetPeaceDuration(MuteReason reason)
{
var prisoners = Utilities.GetPlayers()
Expand All @@ -110,14 +172,14 @@ private int GetPeaceDuration(MuteReason reason)
};
}

private void mute(CCSPlayerController player)
private void Mute(CCSPlayerController player)
{
if (bypassMute(player))
if (BypassMute(player))
return;
player.VoiceFlags |= VoiceFlags.Muted;
}

private void unmute(CCSPlayerController player)
private void UnMute(CCSPlayerController player)
{
player.VoiceFlags &= ~VoiceFlags.Muted;
}
Expand All @@ -141,26 +203,26 @@ private void OnPlayerSpeak(int playerSlot)
if (warden.IsWarden(player))
{
// Always let the warden speak
unmute(player);
UnMute(player);
return;
}

if (!player.PawnIsAlive && !bypassMute(player))
if (!player.PawnIsAlive && !BypassMute(player))
{
// Normal players can't speak when dead
messages.DEAD_REMINDER.ToPlayerCenter(player);
mute(player);
Mute(player);
return;
}

if (isMuted(player))
if (IsMuted(player))
{
// Remind any muted players they're muted
messages.MUTE_REMINDER.ToPlayerCenter(player);
return;
}

if (bypassMute(player))
if (BypassMute(player))
{
// Warn admins if they're not muted
if (IsPeaceEnabled())
Expand All @@ -175,15 +237,15 @@ private void OnPlayerSpeak(int playerSlot)
}
}

private bool isMuted(CCSPlayerController player)
private bool IsMuted(CCSPlayerController player)
{
if (!player.IsReal())
return false;
return (player.VoiceFlags & VoiceFlags.Muted) != 0;
}

private bool bypassMute(CCSPlayerController player)
private bool BypassMute(CCSPlayerController player)
{
return player.IsReal() && AdminManager.PlayerHasPermissions(player, "@css/chat");
}
}
}
8 changes: 7 additions & 1 deletion mod/Jailbreak.Warden/Global/WardenBehavior.cs
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,8 @@ public bool TrySetWarden(CCSPlayerController controller)
if (!controller.PawnIsAlive)
return false;

mute.UnPeaceMute();

_hasWarden = true;
_warden = controller;

Expand Down Expand Up @@ -96,6 +98,8 @@ public bool TryRemoveWarden()
{
if (!_hasWarden)
return false;

mute.UnPeaceMute();

_hasWarden = false;

Expand All @@ -119,6 +123,7 @@ public HookResult OnDeath(EventPlayerDeath ev, GameEventInfo info)
if(!((IWardenService)this).IsWarden(ev.Userid))
return HookResult.Continue;

mute.UnPeaceMute();
ProcessWardenDeath();
return HookResult.Continue;
}
Expand All @@ -130,6 +135,7 @@ public HookResult OnChangeTeam(EventPlayerTeam @event, GameEventInfo info)
if (!((IWardenService)this).IsWarden(player))
return HookResult.Continue;

mute.UnPeaceMute();
ProcessWardenDeath();
return HookResult.Continue;
}
Expand Down Expand Up @@ -207,7 +213,7 @@ private bool IgnoreColor(CCSPlayerController player)
public HookResult OnRoundEnd(EventRoundEnd ev, GameEventInfo info)
{
this.TryRemoveWarden();

mute.UnPeaceMute();
return HookResult.Continue;
}

Expand Down
2 changes: 2 additions & 0 deletions public/Jailbreak.Public/Mod/Mute/IMuteService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ public interface IMuteService
{
void PeaceMute(MuteReason reason);

void UnPeaceMute();

bool IsPeaceEnabled();

DateTime GetLastPeace();
Expand Down

0 comments on commit 8c229a0

Please sign in to comment.