From ee1d25ffcba020d26545d9768047d0af05b464b9 Mon Sep 17 00:00:00 2001 From: storm37000 <6331913+storm37000@users.noreply.github.com> Date: Wed, 3 Jul 2019 19:21:56 -0400 Subject: [PATCH] added new queue used only for deathmatch and fixed respawning server. --- EventHandler.cs | 113 +++++++++++++++++++++++++++++++++--------------- Main.cs | 1 + 2 files changed, 79 insertions(+), 35 deletions(-) diff --git a/EventHandler.cs b/EventHandler.cs index 6d9864f..ca88f7a 100644 --- a/EventHandler.cs +++ b/EventHandler.cs @@ -3,6 +3,7 @@ using Smod2.Events; using Smod2.EventHandlers; using System.Collections.Generic; +using MEC; namespace LaterJoin { @@ -12,7 +13,6 @@ class EventHandler : IEventHandlerRoundStart, IEventHandlerRoundEnd, IEventHandl private Main plugin; private bool roundstarted = false; - private int number = 0; private List FilledTeams = new List(); private List blacklist = new List(); private List enabledSCPs = new List(); @@ -21,7 +21,10 @@ class EventHandler : IEventHandlerRoundStart, IEventHandlerRoundEnd, IEventHandl private int time = 0; private bool detonated = false; private byte fillerTeam; - private int[] spqueue; + + Queue spqueue = new Queue(); + Queue infqueue = new Queue(); + private byte[] queue; private int autoRespawnDelay = 5; @@ -47,7 +50,8 @@ public void OnRoundEnd(RoundEndEvent ev) FilledTeams.Clear(); blacklist.Clear(); enabledSCPs.Clear(); - number = 0; + spqueue.Clear(); + infqueue.Clear(); roundstarted = false; decond = false; detonated = false; @@ -92,14 +96,28 @@ public void OnWaitingForPlayers(WaitingForPlayersEvent ev) fillerTeam = (byte)Smod2.API.Team.NINETAILFOX; } - spqueue = plugin.GetConfigIntList("lj_FillerTeamQueue"); - foreach (int v in spqueue) + foreach (int v in plugin.GetConfigIntList("lj_FillerTeamQueue")) { - if (!System.Enum.IsDefined(typeof(Smod2.API.Team), v)) + if (System.Enum.IsDefined(typeof(Smod2.API.Team), v)) { - plugin.Error("your lj_FillerTeamQueue contains an invalid value! It will not be used."); - spqueue = new int[] { }; - break; + spqueue.Enqueue((byte)v); + + } + else + { + plugin.Error("your lj_FillerTeamQueue contains an invalid value of " + v + "! It will not be used."); + } + } + + foreach (int v in plugin.GetConfigIntList("lj_InfAutoRespawn_queue")) + { + if (System.Enum.IsDefined(typeof(Smod2.API.Team), v)) + { + infqueue.Enqueue((byte)v); + } + else + { + plugin.Error("your lj_InfAutoRespawn_queue contains an invalid value of " + v + "! It will not be used."); } } @@ -110,7 +128,6 @@ public void OnWaitingForPlayers(WaitingForPlayersEvent ev) public void OnRoundStart(RoundStartEvent ev) { - plugin.Debug("round start!"); roundstarted = true; foreach (Player player in ev.Server.GetPlayers()) { @@ -158,7 +175,6 @@ private byte TeamIDtoClassID(byte TeamID) case (byte)Smod2.API.Team.NINETAILFOX: if (detonated) { - plugin.Debug("Attempted to choose a facility guard but the warhead has already detonated! Spawning NTF leutenant instead."); return (byte)Smod2.API.Role.NTF_LIEUTENANT; } else @@ -184,15 +200,16 @@ private byte TeamIDtoClassID(byte TeamID) private byte getfiller() { byte chosenclass = TeamIDtoClassID(fillerTeam); - if (spqueue.Length > 0) + if (spqueue.Count > 0) { - chosenclass = TeamIDtoClassID((byte)spqueue[number]); - number = (number + 1) % spqueue.Length; + byte temp = spqueue.Dequeue(); + spqueue.Enqueue(temp); + chosenclass = TeamIDtoClassID(temp); } return chosenclass; } - private byte ChooseClass() + private string spawnPlayer(Player player) { byte attempt = 0; byte chosenclass = 255; @@ -213,32 +230,58 @@ private byte ChooseClass() chosenclass = getfiller(); } } - plugin.Debug("Choosing class finished on attempt #" + attempt); - return chosenclass; + if (chosenclass != 255) + { + plugin.Debug("Choosing class finished on attempt #" + attempt); + player.ChangeRole((Role)chosenclass); + if (enabledSCPs.Contains(chosenclass)) { enabledSCPs.Remove(chosenclass); } + return "" + (Smod2.API.Role)chosenclass; + } else + { + return "Error, could not select a spawnable class!"; + } + } - private string spawnPlayer(Player player) + private string respawnPlayer(Player player) { - if (player.OverwatchMode) { return "Error, player in overwatch mode!"; } - - byte chosenclass = ChooseClass(); + byte attempt = 0; + byte chosenclass = 255; + while (chosenclass == 255 && attempt < 10) + { + attempt++; + plugin.Debug("Choosing class... attempt #" + attempt); + if (infqueue.Count > 0 ) + { + byte temp = infqueue.Dequeue(); + infqueue.Enqueue(temp); + chosenclass = TeamIDtoClassID(temp); + } + else + { + plugin.Debug("lj_InfAutoRespawn_queue is empty! Using fillers"); + chosenclass = getfiller(); + } + } if (chosenclass != 255) { + plugin.Debug("Choosing class finished on attempt #" + attempt); player.ChangeRole((Role)chosenclass); if (enabledSCPs.Contains(chosenclass)) { enabledSCPs.Remove(chosenclass); } return "" + (Smod2.API.Role)chosenclass; - } else + } + else { return "Error, could not select a spawnable class!"; } - + } public void OnPlayerJoin(PlayerJoinEvent ev) { if (plugin.LJenabled) { - if (roundstarted && ((time >= PluginManager.Manager.Server.Round.Duration) || time == -1) && (!blacklist.Contains(ev.Player.SteamId)) && (ev.Player.TeamRole.Team == Smod2.API.Team.NONE || ev.Player.TeamRole.Team == Smod2.API.Team.SPECTATOR)) + if (roundstarted && ((time >= PluginManager.Manager.Server.Round.Duration) || time == -1) && (!ev.Player.OverwatchMode) && (!blacklist.Contains(ev.Player.SteamId)) && (ev.Player.TeamRole.Team == Smod2.API.Team.NONE || ev.Player.TeamRole.Team == Smod2.API.Team.SPECTATOR)) { plugin.Info("Player " + RemoveSpecialCharacters(ev.Player.Name) + " joined late! Setting their class to " + spawnPlayer(ev.Player)); blacklist.Add(ev.Player.SteamId); @@ -246,26 +289,26 @@ public void OnPlayerJoin(PlayerJoinEvent ev) } } + IEnumerator autoRespawn(Player player) + { + plugin.Info("Player " + RemoveSpecialCharacters(player.Name) + " died! Respawning them in " + autoRespawnDelay + " seconds!"); + yield return Timing.WaitForSeconds(autoRespawnDelay); + plugin.Info("Respawning " + RemoveSpecialCharacters(player.Name) + " as a class of " + spawnPlayer(player)); + } + public void OnPlayerDie(PlayerDeathEvent ev) { if (plugin.infAutoRespawn) { - if (ev.Player.OverwatchMode) { return; } - plugin.Info("Player " + RemoveSpecialCharacters(ev.Player.Name) + " died! Respawning them in " + autoRespawnDelay + " seconds!"); - System.Timers.Timer t = new System.Timers.Timer(); - t.Interval = autoRespawnDelay * 1000; - t.AutoReset = false; - t.Enabled = true; - t.Elapsed += delegate + if (!ev.Player.OverwatchMode && ev.Player.Name != "Server") { - plugin.Info("Respawning " + RemoveSpecialCharacters(ev.Player.Name) + " as a class of " + spawnPlayer(ev.Player)); - - }; + Timing.RunCoroutine(autoRespawn(ev.Player)); + } } } public void OnSetConfig(SetConfigEvent ev) { - if (ev.Key == "smart_class_picker" && (bool)ev.Value == true) + if (plugin.LJenabled && ev.Key == "smart_class_picker" && (bool)ev.Value == true) { plugin.Info("smart_class_picker is set to true! Disabling it as we are unable to support it."); ev.Value = false; diff --git a/Main.cs b/Main.cs index 2aa5b50..939f6d6 100644 --- a/Main.cs +++ b/Main.cs @@ -83,6 +83,7 @@ public override void Register() this.AddConfig(new Smod2.Config.ConfigSetting("lj_FillerTeamQueue", new int[] {}, Smod2.Config.SettingType.NUMERIC_LIST, true, "")); this.AddConfig(new Smod2.Config.ConfigSetting("lj_InfAutoRespawn", false, Smod2.Config.SettingType.BOOL, true, "")); this.AddConfig(new Smod2.Config.ConfigSetting("lj_InfAutoRespawn_delay", 5, Smod2.Config.SettingType.NUMERIC, true, "")); + this.AddConfig(new Smod2.Config.ConfigSetting("lj_InfAutoRespawn_queue", new int[] { }, Smod2.Config.SettingType.NUMERIC_LIST, true, "")); try {