diff --git a/Content.Server/AutoVote/AutoVoteSystem.cs b/Content.Server/AutoVote/AutoVoteSystem.cs new file mode 100644 index 00000000000..7fb053b2f82 --- /dev/null +++ b/Content.Server/AutoVote/AutoVoteSystem.cs @@ -0,0 +1,54 @@ +using Robust.Shared.Configuration; +using Content.Server.Voting.Managers; +using Content.Shared.GameTicking; +using Content.Shared.Voting; +using Content.Shared.CCVar; +using Robust.Server.Player; +using Content.Server.GameTicking; + +namespace Content.Server.AutoVote; + +public sealed class AutoVoteSystem : EntitySystem +{ + [Dependency] private readonly IConfigurationManager _cfg = default!; + [Dependency] public readonly IVoteManager _voteManager = default!; + [Dependency] public readonly IPlayerManager _playerManager = default!; + + public bool _shouldVoteNextJoin = false; + + public override void Initialize() + { + base.Initialize(); + + SubscribeLocalEvent(OnReturnedToLobby); + SubscribeLocalEvent(OnPlayerJoinedLobby); + } + + public void OnReturnedToLobby(RoundRestartCleanupEvent ev) => CallAutovote(); + + public void OnPlayerJoinedLobby(PlayerJoinedLobbyEvent ev) + { + if (!_shouldVoteNextJoin) + return; + + CallAutovote(); + _shouldVoteNextJoin = false; + } + + private void CallAutovote() + { + if (!_cfg.GetCVar(CCVars.AutoVoteEnabled)) + return; + + if (_playerManager.PlayerCount == 0) + { + _shouldVoteNextJoin = true; + return; + } + + if (_cfg.GetCVar(CCVars.MapAutoVoteEnabled)) + _voteManager.CreateStandardVote(null, StandardVoteType.Map); + if (_cfg.GetCVar(CCVars.PresetAutoVoteEnabled)) + _voteManager.CreateStandardVote(null, StandardVoteType.Preset); + } +} diff --git a/Content.Shared/CCVar/CCVars.cs b/Content.Shared/CCVar/CCVars.cs index ccef09000f3..60dceea897e 100644 --- a/Content.Shared/CCVar/CCVars.cs +++ b/Content.Shared/CCVar/CCVars.cs @@ -2640,5 +2640,23 @@ public static readonly CVarDef /// public static readonly CVarDef DebugPow3rDisableParallel = CVarDef.Create("debug.pow3r_disable_parallel", true, CVar.SERVERONLY); + + /* + * AUTOVOTE SYSTEM + */ + + /// Enables the automatic voting system. + public static readonly CVarDef AutoVoteEnabled = + CVarDef.Create("vote.autovote_enabled", false, CVar.SERVERONLY); + + /// Automatically starts a map vote when returning to the lobby. + /// Requires auto voting to be enabled. + public static readonly CVarDef MapAutoVoteEnabled = + CVarDef.Create("vote.map_autovote_enabled", true, CVar.SERVERONLY); + + /// Automatically starts a gamemode vote when returning to the lobby. + /// Requires auto voting to be enabled. + public static readonly CVarDef PresetAutoVoteEnabled = + CVarDef.Create("vote.preset_autovote_enabled", true, CVar.SERVERONLY); } }