Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

css_peace / first warden mute #69

Closed
wants to merge 16 commits into from
Closed
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion mod/Jailbreak.Warden/Commands/WardenCommandsBehavior.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,13 @@
using CounterStrikeSharp.API.Core;
using CounterStrikeSharp.API.Core.Attributes.Registration;
using CounterStrikeSharp.API.Modules.Commands;
using CounterStrikeSharp.API.Modules.Memory;
using CounterStrikeSharp.API.Modules.Utils;
using Jailbreak.Formatting.Extensions;
using Jailbreak.Formatting.Views;
using Jailbreak.Public.Behaviors;
using Jailbreak.Public.Extensions;
using Jailbreak.Public.Mod.Warden;
using System.Runtime.CompilerServices;

namespace Jailbreak.Warden.Commands;

Expand Down
42 changes: 42 additions & 0 deletions mod/Jailbreak.Warden/Commands/WardenPeaceCommandsBehavior.cs
Copy link
Contributor

@MSWS MSWS Mar 4, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you add a cooldown to the command?
Also, please add a message broadcasted to players that the warden issued peace.

Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
using CounterStrikeSharp.API.Core.Attributes.Registration;
using CounterStrikeSharp.API.Core;
using CounterStrikeSharp.API.Modules.Commands;
using Jailbreak.Public.Behaviors;
using Jailbreak.Public.Mod.Warden;
using Jailbreak.Public.Generic;

namespace Jailbreak.Warden.Commands;

public class WardenPeaceCommandsBehavior : IPluginBehavior
{

private readonly IWardenPeaceService _peaceService;
private readonly ICoroutines _coroutines;

private static readonly float _muteTime = 10.0f;

public WardenPeaceCommandsBehavior(IWardenPeaceService peaceService, ICoroutines coroutines)
{
_peaceService = peaceService;
_coroutines = coroutines;
}

[ConsoleCommand("css_peace", "Gives everybody some peace of mind by muting Prisoners/Guards for x seconds (warden is exempt from this).")]
[CommandHelper(0, "", CommandUsage.CLIENT_ONLY)]
public void Command_Peace(CCSPlayerController? invoker, CommandInfo command)
{

if (invoker == null) return;

CCSPlayerController? warden = _peaceService.GetWarden();

if (warden == null) return;

// we only want the warden to be able to run this!
if (!invoker.Equals(warden)) return;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not a fan of wrapping around already written logic for checking if activator is warden. Please use https://github.com/edgegamers/Jailbreak/blob/main/public/Jailbreak.Public/Mod/Warden/IWardenService.cs#L15

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Also done


_peaceService.PeaceMute(_muteTime, true);

}

}
46 changes: 39 additions & 7 deletions mod/Jailbreak.Warden/Global/WardenBehavior.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,28 +7,32 @@
using Jailbreak.Formatting.Views;
using Jailbreak.Public.Behaviors;
using Jailbreak.Public.Extensions;
using Jailbreak.Public.Mod.Logs;
using Jailbreak.Public.Mod.Plugin;
using Jailbreak.Public.Mod.Warden;
using Microsoft.Extensions.Logging;

namespace Jailbreak.Warden.Global;

public class WardenBehavior : IPluginBehavior, IWardenService
{
private ILogger<WardenBehavior> _logger;
private IRichLogService _logs;

private IWardenNotifications _notifications;
private readonly ILogger<WardenBehavior> _logger;
private readonly IRichLogService _logs;
private readonly IWardenNotifications _notifications;
private readonly IEventsService _eventsService;

private bool _firstWarden = false;
private bool _currentWardenIsFirst = false;
private bool _hasWarden;
private CCSPlayerController? _warden;

public WardenBehavior(ILogger<WardenBehavior> logger, IWardenNotifications notifications, IRichLogService logs)
public WardenBehavior(ILogger<WardenBehavior> logger, IWardenNotifications notifications, IRichLogService logs, IEventsService eventsService)
{
_logger = logger;
_notifications = notifications;
_logs = logs;
}
_eventsService = eventsService;

}

/// <summary>
/// Get the current warden, if there is one.
Expand Down Expand Up @@ -66,7 +70,23 @@ public bool TrySetWarden(CCSPlayerController controller)
.ToAllCenter();

_logs.Append( _logs.Player(_warden), "is now the warden.");

// makes sure the second person (and people thereafter) who claim warden are not labelled as "first warden"
if (_currentWardenIsFirst && _firstWarden)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Confusing variable / if check here.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

removed entirely :P idk why it's there!

{
_currentWardenIsFirst = false;
return true;
}

if (!_firstWarden)
{
_firstWarden = true;
_currentWardenIsFirst = true;
_eventsService.FireEvent("first_warden_event");
}

return true;

}

public bool TryRemoveWarden()
Expand All @@ -89,6 +109,16 @@ public bool TryRemoveWarden()
return true;
}

public bool HasBeenFirstWarden()
{
return _firstWarden;
}

public bool CurrentWardenIsFirst()
{
return _currentWardenIsFirst;
}

[GameEventHandler]
public HookResult OnDeath(EventPlayerDeath ev, GameEventInfo info)
{
Expand Down Expand Up @@ -127,6 +157,8 @@ private void ProcessWardenDeath()
public HookResult OnRoundEnd(EventRoundEnd ev, GameEventInfo info)
{
this.TryRemoveWarden();
_firstWarden = false;
_currentWardenIsFirst = false;

return HookResult.Continue;
}
Expand Down
80 changes: 80 additions & 0 deletions mod/Jailbreak.Warden/Global/WardenPeaceBehaviour.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
using CounterStrikeSharp.API;
using CounterStrikeSharp.API.Core;
using Jailbreak.Public.Behaviors;
using Jailbreak.Public.Generic;
using Jailbreak.Public.Mod.Plugin;
using Jailbreak.Public.Mod.Warden;

namespace Jailbreak.Warden.Global;

public class WardenPeaceBehaviour : IPluginBehavior, IWardenPeaceService
{

private readonly IWardenService _wardenService;
private readonly ICoroutines _coroutines;
private readonly IEventsService _eventsService;

private readonly float _muteTime = 10.0f;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done


public WardenPeaceBehaviour(IWardenService wardenService, ICoroutines coroutines, IEventsService eventsService)
{
_wardenService = wardenService;
_coroutines = coroutines;
_eventsService = eventsService;

Func<bool> firstWardenPeaceMuteCallback = () =>
{
PeaceMute(_muteTime, true);
return true;
};

_eventsService.RegisterEventListener("first_warden_event", firstWardenPeaceMuteCallback);

}

public CCSPlayerController? GetWarden()
{
return _wardenService.Warden;
}

public void PeaceMute(float time, bool exemptWarden = false)
{

List<CCSPlayerController> alreadyMuted = new List<CCSPlayerController>();
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why keep track of already muted players?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good point 😆

List<CCSPlayerController> prevUnmutedPlayers = new List<CCSPlayerController>();

foreach (CCSPlayerController player in Utilities.GetPlayers())
{

if (player.Equals(_wardenService.Warden) && exemptWarden)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same issue here, use the interface instead of lower level logic.

continue;

if (player.VoiceFlags == VoiceFlags.Muted)
{
player.PrintToChat("bro you already muted");
alreadyMuted.Add(player);
}
else
{
player.VoiceFlags |= VoiceFlags.Muted;
prevUnmutedPlayers.Add(player);
player.PrintToChat("we muted you");
}

}

// then unmute the people who weren't already muted after _muteTime seconds
_coroutines.Round(() =>
{

foreach (CCSPlayerController player in prevUnmutedPlayers)
{
player.VoiceFlags &= ~VoiceFlags.Muted;
player.PrintToChat("we've unmuted you");
}

}, time);
}


}
2 changes: 2 additions & 0 deletions mod/Jailbreak.Warden/WardenServiceExtension.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,11 @@ public static void AddJailbreakWarden(this IServiceCollection serviceCollection)
{
serviceCollection.AddPluginBehavior<IWardenService, WardenBehavior>();
serviceCollection.AddPluginBehavior<IWardenSelectionService, WardenSelectionBehavior>();
serviceCollection.AddPluginBehavior<IWardenPeaceService, WardenPeaceBehaviour>();

serviceCollection.AddPluginBehavior<WardenCommandsBehavior>();
serviceCollection.AddPluginBehavior<WardenMarkerBehavior>();
serviceCollection.AddPluginBehavior<WardenPaintBehavior>();
serviceCollection.AddPluginBehavior<WardenPeaceCommandsBehavior>();
}
}
6 changes: 3 additions & 3 deletions public/Jailbreak.Public/Jailbreak.Public.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,12 @@
</PropertyGroup>

<ItemGroup>
<PackageReference Include="CounterStrikeSharp.API" Version="1.0.142"/>
<PackageReference Include="System.Text.Json" Version="8.0.0"/>
<PackageReference Include="CounterStrikeSharp.API" Version="1.0.142" />
<PackageReference Include="System.Text.Json" Version="8.0.0" />
</ItemGroup>

<ItemGroup>
<Folder Include="Plugin\"/>
<Folder Include="Plugin\" />
</ItemGroup>

</Project>
30 changes: 30 additions & 0 deletions public/Jailbreak.Public/Mod/Plugin/EventsService.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
using System.Diagnostics.Tracing;

namespace Jailbreak.Public.Mod.Plugin;

public class EventsService : IEventsService
{
// key: event name, value: callback
private readonly Dictionary<string, Func<bool>> _eventListeners;

public EventsService()
{
_eventListeners = new Dictionary<string, Func<bool>>();
}

public void RegisterEventListener(string eventName, Func<bool> eventListener)
{
_eventListeners.Add(eventName, eventListener);
}

public void FireEvent(string eventName)
{
foreach (var listener in _eventListeners)
{
if (listener.Key.Equals(eventName))
{
listener.Value.Invoke();
}
}
}
}
16 changes: 16 additions & 0 deletions public/Jailbreak.Public/Mod/Plugin/IEventsService.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@

namespace Jailbreak.Public.Mod.Plugin;

// Ideally very simple event driven callback service...
// Allows parts of the plugin to tell each other it's done things.
// this must be registered before any other service...
public interface IEventsService
{

// usage: CreateEventListener( bool (eventName) => { callback here } )
// returns true if successful
void RegisterEventListener(string eventName, Func<bool> eventListener);

void FireEvent(string eventName);

}
15 changes: 15 additions & 0 deletions public/Jailbreak.Public/Mod/Warden/IWardenPeaceService.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@

using CounterStrikeSharp.API.Core;

namespace Jailbreak.Public.Mod.Warden;

public interface IWardenPeaceService
{

public CCSPlayerController? GetWarden();

// todo document saying that by default all admins SHOULD bypass this mute
// not implemented bypass yet
public void PeaceMute(float time, bool exemptWarden = false);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Warden should always be exempt.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done


}
5 changes: 5 additions & 0 deletions public/Jailbreak.Public/Mod/Warden/IWardenService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,4 +22,9 @@ public bool IsWarden(CCSPlayerController? player)
public bool TrySetWarden(CCSPlayerController warden);

public bool TryRemoveWarden();

public bool HasBeenFirstWarden();

public bool CurrentWardenIsFirst();

}
5 changes: 1 addition & 4 deletions src/Jailbreak/Jailbreak.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -74,10 +74,7 @@
</ItemGroup>

<Target Name="ZipOutputPath" AfterTargets="Publish">
<ZipDirectory
Overwrite="true"
SourceDirectory="$(PublishDir)\"
DestinationFile="$(PublishBaseDirectory)\Jailbreak.zip" />
<ZipDirectory Overwrite="true" SourceDirectory="$(PublishDir)\" DestinationFile="$(PublishBaseDirectory)\Jailbreak.zip" />
</Target>

</Project>
2 changes: 2 additions & 0 deletions src/Jailbreak/JailbreakServiceCollection.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
using Jailbreak.Teams;
using Jailbreak.Warden;
using Microsoft.Extensions.DependencyInjection;
using Jailbreak.Public.Mod.Plugin;

namespace Jailbreak;

Expand All @@ -29,6 +30,7 @@ public void ConfigureServices(IServiceCollection serviceCollection)
{
// Do we want to make this scoped?
// Not sure how this will behave with multiple rounds and whatnot.
serviceCollection.AddSingleton<IEventsService, EventsService>();
serviceCollection.AddTransient<IConfigService, ConfigService>();

serviceCollection.AddJailbreakGeneric();
Expand Down
Loading