Skip to content

Commit

Permalink
Full cleanup + reformat (automated)
Browse files Browse the repository at this point in the history
  • Loading branch information
Mooshua committed Jan 4, 2024
1 parent 0fdef8b commit f0fda6a
Show file tree
Hide file tree
Showing 39 changed files with 276 additions and 338 deletions.
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>
50 changes: 22 additions & 28 deletions mod/Jailbreak.Teams/Queue/QueueBehavior.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,17 +9,14 @@
using Jailbreak.Public.Mod.Teams;

using Microsoft.Extensions.Logging;
using Microsoft.VisualBasic.CompilerServices;

using Serilog;

namespace Jailbreak.Teams.Queue;

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

public QueueBehavior(IPlayerStateFactory factory, ILogger<QueueBehavior> logger)
{
Expand Down Expand Up @@ -63,11 +60,11 @@ public bool TryPop(int count)

_logger.LogInformation("[Queue] Pop requested {@Count} out of {@InQueue}", count, queue.Count);

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

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

return true;
Expand All @@ -83,7 +80,7 @@ public bool TryPush(int count)
.ToList();
_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];
_logger.LogInformation("[Queue] Pushing {@Name}", toSwap.PlayerName);
Expand All @@ -109,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 @@ -124,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);
}
}
17 changes: 6 additions & 11 deletions mod/Jailbreak.Teams/Queue/QueueState.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,25 +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
/// 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.
/// 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; }
}
}
58 changes: 27 additions & 31 deletions mod/Jailbreak.Teams/Ratio/RatioBehavior.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
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;
Expand All @@ -10,35 +9,20 @@

using Microsoft.Extensions.Logging;

using Serilog;

namespace Jailbreak.Teams.Ratio;

public class RatioBehavior : IPluginBehavior
{
private IGuardQueue _guardQueue;
private ILogger<RatioBehavior> _logger;

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, ILogger<RatioBehavior> logger)
{
Expand All @@ -47,8 +31,12 @@ public RatioBehavior(RatioConfig config, IGuardQueue guardQueue, ILogger<RatioBe
_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 @@ -58,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;

_logger.LogTrace("[Ratio] Evaluating ratio of {@Ct}ct to {@T}t: {@TsPerCt}t/ct ratio, {@Target} target.", ct ,t ,ts_per_ct, 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
_logger.LogTrace("[Ratio] Decision: Not enough CTs: {@Maximum} <= {@TsPerCt}", _config.Maximum, ts_per_ct);
return new(target - ct, RatioActionType.Add);
return new RatioAction(target - ct, RatioActionType.Add);
}

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

_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 @@ -108,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 @@ namespace Jailbreak.Warden.Commands;

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 Down Expand Up @@ -87,8 +91,4 @@ public HookResult OnPlayerChat(EventPlayerChat chat, GameEventInfo info)

return HookResult.Continue;
}

public void Dispose()
{
}
}
Loading

0 comments on commit f0fda6a

Please sign in to comment.