Skip to content

Commit

Permalink
Merge pull request #17 from zwolof/indefinite-scenario
Browse files Browse the repository at this point in the history
Indefinite/forced scenario replay command
  • Loading branch information
ipsvn committed Jun 27, 2024
2 parents 71fa47d + e8fd81f commit e255171
Show file tree
Hide file tree
Showing 2 changed files with 66 additions and 0 deletions.
45 changes: 45 additions & 0 deletions ExecutesPlugin.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
using CounterStrikeSharp.API.Core.Attributes.Registration;
using CounterStrikeSharp.API.Modules.Admin;
using CounterStrikeSharp.API.Modules.Commands;
using CounterStrikeSharp.API.Modules.Entities.Constants;
using CounterStrikeSharp.API.Modules.Memory.DynamicFunctions;
using CounterStrikeSharp.API.Modules.Timers;
using CounterStrikeSharp.API.Modules.Utils;
Expand Down Expand Up @@ -263,6 +264,50 @@ public void OnCommandScramble(CCSPlayerController? player, CommandInfo commandIn
{
_gameManager.ScrambleNextRound(player);
}

[ConsoleCommand("css_forcescenario", "Force a scenario to be replayed continously.")]
[CommandHelper(whoCanExecute: CommandUsage.CLIENT_AND_SERVER)]
[RequiresPermissions("@css/admin")]
public void OnCommandForceScenario(CCSPlayerController? player, CommandInfo commandInfo)
{
var mapConfig = _gameManager._mapConfig;
if (mapConfig == null)
{
commandInfo.ReplyToCommand("[Executes] No map config is loaded.");
return;
}

var scenarioName = commandInfo.GetArg(1).ToLower();
if (string.IsNullOrEmpty(scenarioName))
{
if (_gameManager.IsForcingScenario)
{
_gameManager.SetForcedScenario(null);
commandInfo.ReplyToCommand($"[Executes] Stopped force scenario.");
Helpers.TerminateRound(RoundEndReason.RoundDraw);
}
else
{
commandInfo.ReplyToCommand("[Executes] You must provide a scenario name to start force scenario.");
}

return;
}

var scenario = mapConfig.Scenarios
.FirstOrDefault(scenario => scenario.Name?.ToLower()?.Contains(scenarioName) ?? false);

if (scenario == null)
{
commandInfo.ReplyToCommand($"[Executes] Could not find a scenario with the name {scenarioName}.");
return;
}

_gameManager.SetForcedScenario(scenario);
commandInfo.ReplyToCommand($"[Executes] Started force scenario of {scenario.Name}");
Helpers.TerminateRound(RoundEndReason.RoundDraw);

}

[GameEventHandler]
public HookResult OnRoundEnd(EventRoundEnd @event, GameEventInfo info)
Expand Down
21 changes: 21 additions & 0 deletions Managers/GameManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,11 @@ namespace ExecutesPlugin.Managers;
public sealed class GameManager : BaseManager
{
public MapConfig? _mapConfig;


private Scenario? _currentScenario;
public bool IsForcingScenario;

private readonly QueueManager _queueManager;
private Dictionary<int, int> _playerRoundScores = new();
private readonly int _consecutiveRoundWinsToScramble;
Expand Down Expand Up @@ -77,6 +81,12 @@ public bool LoadSpawns(string moduleDirectory, string map)
return null;
}

if (IsForcingScenario)
{
Console.WriteLine("[Executes] Reusing scenario due to forcing enabled");
return _currentScenario;
}

Console.WriteLine($"There are {_mapConfig.Scenarios.Count} scenarios loaded.");

var validScenarios = _mapConfig.Scenarios;
Expand Down Expand Up @@ -166,6 +176,17 @@ public void ParseMapConfigIdReferences(MapConfig mapConfig)
}
}

public void SetForcedScenario(Scenario? scenario)
{
if (scenario == null)
{
IsForcingScenario = false;
return;
}
_currentScenario = scenario;
IsForcingScenario = true;
}

public Scenario GetCurrentScenario()
{
// TODO: Implement this properly
Expand Down

0 comments on commit e255171

Please sign in to comment.