Skip to content

Commit

Permalink
UPDATE: TimeLimitManager fixes
Browse files Browse the repository at this point in the history
- Fixes to how time limit is calculated
  • Loading branch information
nickj609 committed Sep 16, 2024
1 parent 30f8ffa commit 8f832e0
Show file tree
Hide file tree
Showing 16 changed files with 509 additions and 275 deletions.
4 changes: 2 additions & 2 deletions GameModeManager.Shared/IGameModeApi.cs
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,8 @@ public interface IGameModeApi
public bool ScheduleWarmup(string modeName);

// Enforce time limit api handlers
public void EnforceTimeLimit(bool enabled);
public void EnforceCustomTimeLimit(bool enabled, float delay);
public void EnforceTimeLimit();
public void EnforceTimeLimit(float delay);

// Change mode api handlers
public void ChangeMode(string modeName);
Expand Down
2 changes: 2 additions & 0 deletions GameModeManager/Config.cs
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,8 @@ public class RotationSettings
{
public bool Enabled { get; set; } = true; // Enables game rotations
public bool WhenServerEmpty { get; set; } = false; // Enables rotation on server empty.
public bool EnforceCustomTimeLimit { get; set; } = false; // Enables or disables custom time limit
public int CustomTimeLimit { get; set; } = 300; // Sets custom time limit in seconds
public int Cycle { get; set; } = 0; // 0 for current mode maps, 1 for all maps, 2 for specific map groups
public List<string> MapGroups { get; set;} = new List<string>(){"mg_active", "mg_comp"}; // Map group list for cycle 2
public bool ModeRotation { get; set; } = false; // Enables game mode rotations
Expand Down
8 changes: 4 additions & 4 deletions GameModeManager/Core/GameModeApi.cs
Original file line number Diff line number Diff line change
Expand Up @@ -115,19 +115,19 @@ public bool ScheduleWarmup(string modeName)
}

// Enforce time limit api handlers
public void EnforceTimeLimit(bool enabled)
public void EnforceTimeLimit()
{
if(_plugin != null)
{
_timeLimitManager.EnforceTimeLimit(enabled);
_timeLimitManager.EnforceTimeLimit();
}
}

public void EnforceCustomTimeLimit(bool enabled, float time)
public void EnforceTimeLimit(float time)
{
if(_plugin != null)
{
_timeLimitManager.EnforceCustomTimeLimit(enabled, time);
_timeLimitManager.EnforceTimeLimit(time);
}
}

Expand Down
18 changes: 10 additions & 8 deletions GameModeManager/Core/MapManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -29,17 +29,19 @@ public void OnConfigParsed(Config config)
// Define on load behavior
public void OnLoad(Plugin plugin)
{
// Register event map transition handler to set current map
plugin.RegisterEventHandler<EventMapTransition>((@event, info) =>
{
// Register event handler
plugin.RegisterEventHandler<EventMapTransition>(EventMapTransitionHandler);
}

Map _map = _pluginState.Maps.FirstOrDefault(m => m.Name.Equals(Server.MapName, StringComparison.OrdinalIgnoreCase)) ?? new Map(Server.MapName);
_pluginState.CurrentMap = _map;
// Register event map transition handler to set current map
public HookResult EventMapTransitionHandler(EventMapTransition @event, GameEventInfo info)
{
Map _map = _pluginState.Maps.FirstOrDefault(m => m.Name.Equals(Server.MapName, StringComparison.OrdinalIgnoreCase)) ?? new Map(Server.MapName);
_pluginState.CurrentMap = _map;

return HookResult.Continue;
}, HookMode.Post);
return HookResult.Continue;
}

// Define reusable method to update map list
public void UpdateRTVMapList()
{
Expand Down
82 changes: 49 additions & 33 deletions GameModeManager/Core/RotationManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ namespace GameModeManager.Core
public class RotationManager : IPluginDependency<Plugin, Config>
{
// Define dependencies
private Plugin? _plugin;
private ServerManager _serverManager;
private Config _config = new Config();
private TimeLimitManager _timeLimitManager;
Expand All @@ -33,15 +32,10 @@ public void OnConfigParsed(Config config)
// Define on load behavior
public void OnLoad(Plugin plugin)
{
// Load plugin
_plugin = plugin;

// Define event game end handler
_plugin.RegisterEventHandler<EventCsWinPanelMatch>((@event, info) =>
{
_serverManager.TriggerRotation();
return HookResult.Continue;
}, HookMode.Post);
// Register event handlers
plugin.RegisterEventHandler<EventCsWinPanelMatch>(EventCsWinPanelMatchHandler);
plugin.RegisterEventHandler<EventPlayerDisconnect>(EventPlayerDisconnectHandler);
plugin.RegisterEventHandler<EventPlayerConnectFull>(EventPlayerConnectFullHandler);

// Create mode schedules
if (_config.Rotation.ModeSchedules)
Expand All @@ -67,43 +61,65 @@ public void OnLoad(Plugin plugin)
_serverManager.TriggerScheduleChange(entry);

// Update delay for the next occurrence (tomorrow)
delay = targetTime.AddDays(1) - DateTime.Now;
delay = targetTime.AddDays(1) - DateTime.Now;

}, CounterStrikeSharp.API.Modules.Timers.TimerFlags.REPEAT);
}
}
}

// Define event game end handler
public HookResult EventCsWinPanelMatchHandler(EventCsWinPanelMatch @event, GameEventInfo info)
{
_serverManager.TriggerRotation();
return HookResult.Continue;
}

// Define event player connect full handler
public HookResult EventPlayerConnectFullHandler(EventPlayerConnectFull @event, GameEventInfo info)
{
// Check if server empty
if(!Extensions.IsServerEmpty())
{
_timeLimitManager.RemoveTimeLimit();
}
return HookResult.Continue;
}

// Define event player disconnect handler
public HookResult EventPlayerDisconnectHandler(EventPlayerDisconnect @event, GameEventInfo info)
{
// Check if rotation on server empty is enabled
if(_config.Rotation.WhenServerEmpty)
{
_plugin.RegisterEventHandler<EventPlayerConnect>((@event, info) =>
{
if(Extensions.ValidPlayerCount(false) == 1)
// Check if server empty
if(Extensions.IsServerEmpty())
{
// Check if server is hibernating
if(!Extensions.IsHibernationEnabled())
{
if (!_timeLimitManager.UnlimitedTime)
{
_timeLimitManager.EnforceTimeLimit(false);
}
// Disable hibernation
Server.ExecuteCommand("sv_hibernate_when_empty false");
}
return HookResult.Continue;
}, HookMode.Post);

_plugin.RegisterEventHandler<EventPlayerDisconnect>((@event, info) =>
{
if(Extensions.ValidPlayerCount(false) == 0)
// Check if custom time limit set
if(_config.Rotation.EnforceCustomTimeLimit)
{
if(!Extensions.IsHibernationEnabled())
{
Server.ExecuteCommand("sv_hibernate_when_empty false");
}

if(!_timeLimitManager.UnlimitedTime)
// Enforce custom time limit
_timeLimitManager.EnforceTimeLimit(_config.Rotation.CustomTimeLimit);
}
else
{
// Check if map has an unlimited time set
if(_timeLimitManager.TimeRemaining != 0)
{
_timeLimitManager.EnforceTimeLimit(true);
// Enforce time limit
_timeLimitManager.EnforceTimeLimit();
}
}
return HookResult.Continue;
}, HookMode.Post);
}
}
}
return HookResult.Continue;
}
}
}
95 changes: 68 additions & 27 deletions GameModeManager/Core/TimeLimitManager.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
// Included libraries
using CounterStrikeSharp.API;
using GameModeManager.Contracts;
using CounterStrikeSharp.API.Core;
using GameModeManager.CrossCutting;
using CounterStrikeSharp.API.Modules.Cvars;

Expand All @@ -13,11 +14,10 @@ public class TimeLimitManager : IPluginDependency<Plugin, Config>
// Define dependencies
private Plugin? _plugin;
private ConVar? _timeLimit;
private ConVar? _warmupTime;
private GameRules _gameRules;
private PluginState _pluginState;
private ServerManager _serverManager;
public decimal TimeLimitValue => (decimal)(_timeLimit?.GetPrimitiveValue<float>() ?? 0F) * 60M;
public decimal WarmupTimeValue => (decimal)(_warmupTime?.GetPrimitiveValue<float>() ?? 0F) * 60M;
public bool UnlimitedTime => TimeLimitValue <= 0;

// Calculate time played
Expand Down Expand Up @@ -45,17 +45,25 @@ public decimal TimeRemaining
}

// Define class instance
public TimeLimitManager(GameRules gameRules, ServerManager serverManager)
public TimeLimitManager(GameRules gameRules, ServerManager serverManager, PluginState pluginState)
{
_gameRules = gameRules;
_pluginState = pluginState;
_serverManager = serverManager;

}

// Define on load behavior
public void OnLoad(Plugin plugin)
{
// Load plugin instance
_plugin = plugin;

// Load convars
LoadCvar();

// Register event handler
plugin.RegisterEventHandler<EventRoundAnnounceMatchStart>(EventRoundAnnounceMatchStartHandler);
}

// Define on map start behavior
Expand All @@ -68,49 +76,82 @@ public void OnMapStart(string map)
public void LoadCvar()
{
_timeLimit = ConVar.Find("mp_timelimit");
_warmupTime = ConVar.Find("mp_warmuptime");
}

// Define method to enforce time limit
public void EnforceTimeLimit(bool enabled)
// Define method to remove timelimit

public void RemoveTimeLimit()
{
if(enabled && _plugin != null)
if(_plugin != null)
{
// Clear previous timers
// Clear timers
_plugin.Timers.Clear();

// Enforce time limit
_plugin.AddTimer((float)TimeRemaining, () =>
{
_serverManager.TriggerRotation();
});
}
else if(!enabled && _plugin != null)
// Set plugin state
_pluginState.TimeLimitEnabled = false;
_pluginState.TimeLimitEnabled = false;
_pluginState.TimeLimitScheduled = false;
}
}

// Define methods to enforce time limit
public void EnforceTimeLimit()
{
if(_plugin != null)
{
// Clear previous timers
_plugin.Timers.Clear();
if(TimeRemaining != 0 && TimePlayed != 0)
{
// Create timer
_plugin.AddTimer((float)TimeRemaining, () =>
{
_serverManager.TriggerRotation();
_pluginState.TimeLimitEnabled = false;
});

// Set plugin state
_pluginState.TimeLimitEnabled = true;
_pluginState.TimeLimitScheduled = false;
}
}
}

// Define method to enforce custom time limit
public void EnforceCustomTimeLimit(bool enabled, float timeLimit)
public void EnforceTimeLimit(float timeLimit)
{
if(enabled && _plugin != null)
if(_plugin != null)
{
// Clear previous timers
_plugin.Timers.Clear();

// Enforce time limit
// Create timer
_plugin.AddTimer(timeLimit, () =>
{
_serverManager.TriggerRotation();
_pluginState.TimeLimitEnabled = false;
});

// Set plugin state
_pluginState.TimeLimitCustom = false;
_pluginState.TimeLimitEnabled = true;
_pluginState.TimeLimitScheduled = false;
}
}

// Define on match start behavior
public HookResult EventRoundAnnounceMatchStartHandler(EventRoundAnnounceMatchStart @event, GameEventInfo info)
{
if (_pluginState.TimeLimitScheduled)
{
if(_pluginState.TimeLimitCustom)
{
EnforceTimeLimit(_pluginState.TimeLimit);
}
else
{
EnforceTimeLimit();
}
}
else if(!enabled && _plugin != null)
else
{
// Clear previous timers
_plugin.Timers.Clear();
RemoveTimeLimit();
}
return HookResult.Continue;
}
}
}
Loading

0 comments on commit 8f832e0

Please sign in to comment.