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

Universal SeriLog Logging #3

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all 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
40 changes: 29 additions & 11 deletions mod/Jailbreak.Teams/Queue/QueueBehavior.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,19 +11,21 @@
using Jailbreak.Public.Generic;
using Jailbreak.Public.Mod.Teams;

using Serilog;
using Microsoft.Extensions.Logging;

namespace Jailbreak.Teams.Queue;

public class QueueBehavior : IGuardQueue, IPluginBehavior
{
private int _counter;
private IPlayerState<QueueState> _state;
private readonly ILogger<QueueBehavior> _logger;
private readonly IPlayerState<QueueState> _state;

private IRatioNotifications _notifications;

public QueueBehavior(IPlayerStateFactory factory, IRatioNotifications notifications)
public QueueBehavior(IPlayerStateFactory factory, ILogger<QueueBehavior> logger, IRatioNotifications notifications)
{
_logger = logger;
_notifications = notifications;
_counter = 0;
_state = factory.Global<QueueState>();
Expand Down Expand Up @@ -60,12 +62,13 @@
_notifications.JOIN_GUARD_QUEUE.ToAllChat().ToAllCenter();
}

Log.Information($"[Queue] {count}/{queue.Count}");
for (int i = 0; i < Math.Min(queue.Count, count); i++)
_logger.LogInformation("[Queue] Pop requested {@Count} out of {@InQueue}", count, queue.Count);

for (var i = 0; i < Math.Min(queue.Count, count); i++)
{
Log.Information($"[Queue] Popping { queue[i].PlayerName }");
_logger.LogInformation("[Queue] Popping player {@Name}", queue[i].PlayerName);

ForceGuard( queue[i] );
ForceGuard(queue[i]);
}

return true;
Expand All @@ -77,12 +80,12 @@
.Where(player => player.GetTeam() == CsTeam.CounterTerrorist)
.Shuffle(Random.Shared)
.ToList();
Log.Information($"[Queue] {count}/{players.Count}");
_logger.LogInformation("[Queue] Push requested {@Count} out of {@GuardCount}", count, players.Count);

for (int i = 0; i < Math.Min(count, players.Count); i++)
for (var i = 0; i < Math.Min(count, players.Count); i++)
{
var toSwap = players[i];
Log.Information($"[Queue] Pushing {toSwap.PlayerName}");
_logger.LogInformation("[Queue] Pushing {@Name}", toSwap.PlayerName);
var state = _state.Get(toSwap);

state.IsGuard = false;
Expand All @@ -108,6 +111,21 @@
player.ChangeTeam(CsTeam.CounterTerrorist);
}


public int GetQueuePosition(CCSPlayerController player)
{
return Queue.ToList()
.FindIndex(controller => controller.Slot == player.Slot);
}

public IEnumerable<CCSPlayerController> Queue
=> Utilities.GetPlayers()
.Select(player => (Player: player, State: _state.Get(player)))
.Where(tuple => tuple.State.InQueue) // Exclude not in queue
.Where(tuple => !tuple.State.IsGuard) // Exclude current guards
.OrderBy(tuple => tuple.State.Position) // Order by counter value when joined queue
.Select(tuple => tuple.Player);

[GameEventHandler]
public HookResult OnPlayerTeam(EventPlayerTeam ev, GameEventInfo info)
{
Expand Down Expand Up @@ -151,7 +169,7 @@
player.PrintToCenter("An error occured removing you from the queue.");
}

public int GetQueuePosition(CCSPlayerController player)

Check failure on line 172 in mod/Jailbreak.Teams/Queue/QueueBehavior.cs

View workflow job for this annotation

GitHub Actions / build

Type 'QueueBehavior' already defines a member called 'GetQueuePosition' with the same parameter types

Check failure on line 172 in mod/Jailbreak.Teams/Queue/QueueBehavior.cs

View workflow job for this annotation

GitHub Actions / build

Type 'QueueBehavior' already defines a member called 'GetQueuePosition' with the same parameter types

Check failure on line 172 in mod/Jailbreak.Teams/Queue/QueueBehavior.cs

View workflow job for this annotation

GitHub Actions / build

Type 'QueueBehavior' already defines a member called 'GetQueuePosition' with the same parameter types

Check failure on line 172 in mod/Jailbreak.Teams/Queue/QueueBehavior.cs

View workflow job for this annotation

GitHub Actions / build

Type 'QueueBehavior' already defines a member called 'GetQueuePosition' with the same parameter types
{
return Queue.ToList()
.FindIndex(controller => controller.Slot == player.Slot);
Expand All @@ -177,11 +195,11 @@
}


public IEnumerable<CCSPlayerController> Queue

Check failure on line 198 in mod/Jailbreak.Teams/Queue/QueueBehavior.cs

View workflow job for this annotation

GitHub Actions / build

The type 'QueueBehavior' already contains a definition for 'Queue'

Check failure on line 198 in mod/Jailbreak.Teams/Queue/QueueBehavior.cs

View workflow job for this annotation

GitHub Actions / build

The type 'QueueBehavior' already contains a definition for 'Queue'

Check failure on line 198 in mod/Jailbreak.Teams/Queue/QueueBehavior.cs

View workflow job for this annotation

GitHub Actions / build

The type 'QueueBehavior' already contains a definition for 'Queue'

Check failure on line 198 in mod/Jailbreak.Teams/Queue/QueueBehavior.cs

View workflow job for this annotation

GitHub Actions / build

The type 'QueueBehavior' already contains a definition for 'Queue'
=> Utilities.GetPlayers()
.Select(player => (Player: player, State: _state.Get(player)))
.Where(tuple => tuple.State.InQueue) // Exclude not in queue
.Where(tuple => !tuple.State.IsGuard) // Exclude current guards
.OrderBy(tuple => tuple.State.Position) // Order by counter value when joined queue
.Select(tuple => tuple.Player);
}
}
18 changes: 10 additions & 8 deletions mod/Jailbreak.Teams/Queue/QueueState.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,20 @@

public class QueueState
{

public QueueState()
{
}

/// <summary>
/// The counter value when the player entered the queue
/// Lower = join CT sooner
/// The counter value when the player entered the queue
/// Lower = join CT sooner
/// </summary>
public int Position { get; set; }

/// <summary>
/// True when this player is currently in the queue
/// </summary>
public bool InQueue { get; set; }

/// <summary>
/// This player is allowed to be on the CT team.
/// If this is false, they will be swapped back to prisoner.
/// </summary>
public bool IsGuard { get; set; }
}
}
66 changes: 34 additions & 32 deletions mod/Jailbreak.Teams/Ratio/RatioBehavior.cs
Original file line number Diff line number Diff line change
@@ -1,48 +1,42 @@
using CounterStrikeSharp.API;
using CounterStrikeSharp.API.Core;
using CounterStrikeSharp.API.Core.Attributes.Registration;
using CounterStrikeSharp.API.Modules.Cvars;
using CounterStrikeSharp.API.Modules.Utils;

using Jailbreak.Public.Behaviors;
using Jailbreak.Public.Extensions;
using Jailbreak.Public.Mod.Teams;

using Microsoft.Extensions.Logging;

namespace Jailbreak.Teams.Ratio;

public class RatioBehavior : IPluginBehavior
{
private IGuardQueue _guardQueue;

public enum RatioActionType
{
Add,
Remove,
None,
}

public struct RatioAction
{
public RatioActionType Type { get; init; }
public int Count { get; init; }

public RatioAction(int count = 0, RatioActionType type = RatioActionType.None)
{
Count = count;
Type = type;
}
None
}

private RatioConfig _config;
private readonly RatioConfig _config;
private readonly IGuardQueue _guardQueue;
private readonly ILogger<RatioBehavior> _logger;

public RatioBehavior(RatioConfig config, IGuardQueue guardQueue)
public RatioBehavior(RatioConfig config, IGuardQueue guardQueue, ILogger<RatioBehavior> logger)
{
_config = config;
_guardQueue = guardQueue;
_logger = logger;
}

public void Dispose()
{
}

/// <summary>
/// Evaluate whether the provided CT/T ratio needs
/// Evaluate whether the provided CT/T ratio needs
/// </summary>
/// <param name="ct"></param>
/// <param name="t"></param>
Expand All @@ -52,34 +46,34 @@ public RatioAction Evaluate(int ct, int t)
// No divide by zero errors today...
// Make value 0.01 so the decision will always be to add Ts
// 1 / 0.01 = 100, etc...
var normalized_ct = (ct == 0 ? 0.01 : (double)ct);
double ts_per_ct = t / (double)normalized_ct;
int target = (int)( (ct + t) / _config.Target ) + 1;
var normalized_ct = ct == 0 ? 0.01 : ct;
var ts_per_ct = t / normalized_ct;
var target = (int)((ct + t) / _config.Target) + 1;

Server.PrintToConsole($"[Ratio] Evaluating ratio of {ct}ct to {t}t: {ts_per_ct}t/ct ratio, {target} target.");
_logger.LogTrace("[Ratio] Evaluating ratio of {@Ct}ct to {@T}t: {@TsPerCt}t/ct ratio, {@Target} target.", ct, t, ts_per_ct, target);

if (_config.Maximum <= ts_per_ct)
{
// There are too many Ts per CT!
// Get more guards on the team
Server.PrintToConsole($"[Ratio] Decision: Not enough CTs: {_config.Maximum} <= {ts_per_ct}");
return new(target - ct, RatioActionType.Add);
_logger.LogTrace("[Ratio] Decision: Not enough CTs: {@Maximum} <= {@TsPerCt}", _config.Maximum, ts_per_ct);
return new RatioAction(target - ct, RatioActionType.Add);
}

if (ts_per_ct <= _config.Minimum)
{
// There are too many guards per T!
Server.PrintToConsole($"[Ratio] Decision: Too many CTs: {ts_per_ct} <= {_config.Minimum}");
return new(ct - target, RatioActionType.Remove);
_logger.LogTrace("[Ratio] Decision: Too many CTs: {@TsPerCt} <= {@Minimum}", ts_per_ct, _config.Minimum);
return new RatioAction(ct - target, RatioActionType.Remove);
}

Server.PrintToConsole($"[Ratio] Decision: Goldilocks: {_config.Maximum} (max) <= {ts_per_ct} (t/ct) <= {_config.Minimum} (min)");
_logger.LogTrace("[Ratio] Decision: Goldilocks: {@Maximum} (max) <= {@TsPerCt} (t/ct) <= {@Minimum} (min)", _config.Maximum, ts_per_ct, _config.Minimum);
// Take no action
return new();
return new RatioAction();
}

/// <summary>
/// When a round starts, balance out the teams
/// When a round starts, balance out the teams
/// </summary>
[GameEventHandler(HookMode.Pre)]
public HookResult OnRoundStart(EventRoundStart ev, GameEventInfo info)
Expand All @@ -102,8 +96,16 @@ public HookResult OnRoundStart(EventRoundStart ev, GameEventInfo info)
return HookResult.Continue;
}

public void Dispose()
public struct RatioAction
{
public RatioActionType Type { get; init; }

public int Count { get; init; }

public RatioAction(int count = 0, RatioActionType type = RatioActionType.None)
{
Count = count;
Type = type;
}
}
}
}
14 changes: 7 additions & 7 deletions mod/Jailbreak.Teams/Ratio/RatioConfig.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,20 +3,20 @@
public class RatioConfig
{
/// <summary>
/// The minimum amount of Ts per every CT.
/// When this is passed, CTs will be added until Target is reached.
/// The minimum amount of Ts per every CT.
/// When this is passed, CTs will be added until Target is reached.
/// </summary>
public double Minimum { get; set; } = 2.5;

/// <summary>
/// The maximum amount of Ts per every CT.
/// When this is passed, CTs will be removed until Target is reached.
/// The maximum amount of Ts per every CT.
/// When this is passed, CTs will be removed until Target is reached.
/// </summary>
public double Maximum { get; set; } = 4;

/// <summary>
/// When the ratio is autobalanced, the amount of guards
/// should be total_players / target.
/// When the ratio is autobalanced, the amount of guards
/// should be total_players / target.
/// </summary>
public double Target { get; set; } = 4;
}
}
2 changes: 1 addition & 1 deletion mod/Jailbreak.Teams/TeamsServiceExtension.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,4 +16,4 @@ public static void AddJailbreakTeams(this IServiceCollection collection)
collection.AddPluginBehavior<IGuardQueue, QueueBehavior>();
collection.AddPluginBehavior<RatioBehavior>();
}
}
}
8 changes: 4 additions & 4 deletions mod/Jailbreak.Warden/Commands/WardenCommandsBehavior.cs
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,10 @@ public WardenCommandsBehavior(IWardenSelectionService queue, IWardenService ward
_notifications = notifications;
}

public void Dispose()
{
}

public HookResult HandleWarden(CCSPlayerController sender)
{
var isCt = sender.GetTeam() == CsTeam.CounterTerrorist;
Expand Down Expand Up @@ -92,8 +96,4 @@ public HookResult OnPlayerChat(EventPlayerChat chat, GameEventInfo info)

return HookResult.Continue;
}

public void Dispose()
{
}
}
Loading
Loading