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

Make logging consistent + full reformat #1

Merged
merged 2 commits into from
Jan 6, 2024
Merged
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
2 changes: 1 addition & 1 deletion mod/Jailbreak.Teams/Jailbreak.Teams.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
</PropertyGroup>

<ItemGroup>
<ProjectReference Include="..\..\public\Jailbreak.Public\Jailbreak.Public.csproj" />
<ProjectReference Include="..\..\public\Jailbreak.Public\Jailbreak.Public.csproj"/>
</ItemGroup>

</Project>
64 changes: 31 additions & 33 deletions mod/Jailbreak.Teams/Queue/QueueBehavior.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,19 +8,19 @@
using Jailbreak.Public.Generic;
using Jailbreak.Public.Mod.Teams;

using Microsoft.VisualBasic.CompilerServices;

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;

public QueueBehavior(IPlayerStateFactory factory)
public QueueBehavior(IPlayerStateFactory factory, ILogger<QueueBehavior> logger)
{
_logger = logger;
_counter = 0;
_state = factory.Global<QueueState>();
}
Expand Down Expand Up @@ -55,15 +55,16 @@ public bool TryPop(int count)
{
Server.PrintToChatAll("[Jail] Not enough guards are in the queue!");
Server.PrintToChatAll("[Jail] Type !guard in chat to join the queue");
ServerExtensions.PrintToCenterAll("Type !guard to become a guard!");
ServerExtensions.PrintToCenterAll("Not enough players in guard queue!\nType !guard to become a guard.");
}

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 +78,12 @@ public bool TryPush(int count)
.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 @@ -105,6 +106,21 @@ public void ForceGuard(CCSPlayerController player)
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 All @@ -120,27 +136,9 @@ public HookResult OnPlayerTeam(EventPlayerTeam ev, GameEventInfo info)
}

if (player.GetTeam() == CsTeam.Terrorist && state.IsGuard)
{
if (this.TryExitQueue(player))
if (TryExitQueue(player))
player.PrintToCenter("You were removed from the guard queue for switching to T.\nUse !guard to rejoin the queue!");
}

return HookResult.Continue;
}



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);
}
}
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>();
}
}
}
12 changes: 6 additions & 6 deletions mod/Jailbreak.Warden/Commands/WardenCommandsBehavior.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,15 +11,19 @@

public class WardenCommandsBehavior : IPluginBehavior
{
private IWardenSelectionService _queue;
private IWardenService _warden;
private readonly IWardenSelectionService _queue;
private readonly IWardenService _warden;

public WardenCommandsBehavior(IWardenSelectionService queue, IWardenService warden)
{
_queue = queue;
_warden = warden;
}

public void Dispose()
{
}

public HookResult HandleWarden(CCSPlayerController sender)
{
var isCt = sender.GetTeam() == CsTeam.CounterTerrorist;
Expand All @@ -44,7 +48,7 @@

// Respond to all other requests
if (_warden.HasWarden)
sender.PrintToChat($"[Warden] The current warden is {_warden.Warden.PlayerName}");

Check warning on line 51 in mod/Jailbreak.Warden/Commands/WardenCommandsBehavior.cs

View workflow job for this annotation

GitHub Actions / build

Dereference of a possibly null reference.

Check warning on line 51 in mod/Jailbreak.Warden/Commands/WardenCommandsBehavior.cs

View workflow job for this annotation

GitHub Actions / build

Dereference of a possibly null reference.

Check warning on line 51 in mod/Jailbreak.Warden/Commands/WardenCommandsBehavior.cs

View workflow job for this annotation

GitHub Actions / build

Dereference of a possibly null reference.
else
sender.PrintToChat("[Warden] There is currently no warden!");

Expand Down Expand Up @@ -87,8 +91,4 @@

return HookResult.Continue;
}

public void Dispose()
{
}
}
Loading