Skip to content

Commit

Permalink
added new queue used only for deathmatch and fixed respawning server.
Browse files Browse the repository at this point in the history
  • Loading branch information
storm37000 committed Jul 3, 2019
1 parent 51aaf19 commit ee1d25f
Show file tree
Hide file tree
Showing 2 changed files with 79 additions and 35 deletions.
113 changes: 78 additions & 35 deletions EventHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
using Smod2.Events;
using Smod2.EventHandlers;
using System.Collections.Generic;
using MEC;

namespace LaterJoin
{
Expand All @@ -12,7 +13,6 @@ class EventHandler : IEventHandlerRoundStart, IEventHandlerRoundEnd, IEventHandl

private Main plugin;
private bool roundstarted = false;
private int number = 0;
private List<byte> FilledTeams = new List<byte>();
private List<string> blacklist = new List<string>();
private List<byte> enabledSCPs = new List<byte>();
Expand All @@ -21,7 +21,10 @@ class EventHandler : IEventHandlerRoundStart, IEventHandlerRoundEnd, IEventHandl
private int time = 0;
private bool detonated = false;
private byte fillerTeam;
private int[] spqueue;

Queue<byte> spqueue = new Queue<byte>();
Queue<byte> infqueue = new Queue<byte>();

private byte[] queue;
private int autoRespawnDelay = 5;

Expand All @@ -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;
Expand Down Expand Up @@ -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.");
}
}

Expand All @@ -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())
{
Expand Down Expand Up @@ -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
Expand All @@ -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;
Expand All @@ -213,59 +230,85 @@ 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);
}
}
}

IEnumerator<float> 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;
Expand Down
1 change: 1 addition & 0 deletions Main.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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
{
Expand Down

0 comments on commit ee1d25f

Please sign in to comment.