Skip to content
This repository has been archived by the owner on Feb 28, 2021. It is now read-only.

Commit

Permalink
Optimizations
Browse files Browse the repository at this point in the history
  • Loading branch information
PintTheDragon committed Jul 21, 2020
1 parent cbb0180 commit 5c8be50
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 17 deletions.
8 changes: 4 additions & 4 deletions src/Buddy.cs
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ public override void OnDisabled()
Exiled.Events.Handlers.Server.RestartingRound -= EventHandlers.OnRoundRestart;
Exiled.Events.Handlers.Server.ReloadedConfigs -= Config.OnReload;

Log.Info("Buddy v"+Version+" (by PintTheDragon) has unloaded.");
base.OnDisabled();
}

public override void OnEnabled()
Expand All @@ -43,11 +43,12 @@ public override void OnEnabled()
Exiled.Events.Handlers.Server.RestartingRound += EventHandlers.OnRoundRestart;
Exiled.Events.Handlers.Server.ReloadedConfigs += Config.OnReload;

Log.Info("Buddy v" + Version + " (by PintTheDragon) has loaded.");
base.OnEnabled();
}

public override void OnReloaded()
{
base.OnReloaded();
}

public void removePerson(string userID)
Expand Down Expand Up @@ -75,8 +76,7 @@ public string getLang(string key)

public void setLang(string key, string value)
{
Config.Messages.Remove(key);
Config.Messages.Add(key, value);
Config.Messages[key] = value;
}
}
}
24 changes: 11 additions & 13 deletions src/EventHandlers.cs
Original file line number Diff line number Diff line change
Expand Up @@ -31,15 +31,16 @@ public IEnumerator<float> sendJoinMessage(Player p)
else
{
string buddy1 = null;
buddyPlugin.buddies.TryGetValue(p.UserId, out buddy1);
if (buddy1 == null)
if (!buddyPlugin.buddies.TryGetValue(p.UserId, out buddy1) || buddy1 == null)
{
buddyPlugin.buddies.Remove(p.UserId);
buddyPlugin.removePerson(p.UserId);
}
else
{
p.SendConsoleMessage(buddyPlugin.getLang("broadcastBuddy").Replace("$buddy", Player.Get(buddy1).Nickname), "yellow");
Player player = Player.Get(buddy1);
if (player == null) yield break;
p.SendConsoleMessage(buddyPlugin.getLang("broadcastBuddy").Replace("$buddy", player.Nickname), "yellow");
}
}
}
Expand All @@ -54,15 +55,16 @@ private IEnumerator<float> sendBroadcast(Player p)
if (buddyPlugin.buddies.ContainsKey(p.UserId) && buddyPlugin.Config.sendBuddyBroadcast)
{
string buddy1 = null;
buddyPlugin.buddies.TryGetValue(p.UserId, out buddy1);
if (buddy1 == null)
if (!buddyPlugin.buddies.TryGetValue(p.UserId, out buddy1) || buddy1 == null)
{
buddyPlugin.buddies.Remove(p.UserId);
buddyPlugin.removePerson(p.UserId);
}
else
{
p.Broadcast(5, buddyPlugin.getLang("broadcastBuddy").Replace("$buddy", Player.Get(buddy1).Nickname), Broadcast.BroadcastFlags.Normal);
Player player = Player.Get(buddy1);
if (player == null) yield break;
p.Broadcast(5, buddyPlugin.getLang("broadcastBuddy").Replace("$buddy", player.Nickname), Broadcast.BroadcastFlags.Normal);
}
}
}
Expand Down Expand Up @@ -90,14 +92,14 @@ private IEnumerator<float> setRoles()
{
string id = hubs.ElementAt(i);
Player player = Player.Get(id);
if (player == null) continue;
//check if player has a buddy
if (buddyPlugin.buddies.ContainsKey(player.UserId))
{
try
{
string buddy1 = null;
buddyPlugin.buddies.TryGetValue(player.UserId, out buddy1);
if (buddy1 == null || (!onlinePlayers.Contains(id) || !onlinePlayers.Contains(buddy1)))
if (!buddyPlugin.buddies.TryGetValue(player.UserId, out buddy1) || buddy1 == null || (!onlinePlayers.Contains(id) || !onlinePlayers.Contains(buddy1)))
{
buddyPlugin.buddies.Remove(id);
if (buddy1 != null) buddyPlugin.buddies.Remove(buddy1);
Expand All @@ -108,6 +110,7 @@ private IEnumerator<float> setRoles()
}
if ((doneIDs.Contains(id) || doneIDs.Contains(buddy1))) continue;
Player buddy = Player.Get(buddy1);
if (buddy == null) continue;
//take action if they have different roles
if (player.Role != buddy.Role &&
/* massive check for scientist/guard combo */
Expand All @@ -120,7 +123,6 @@ private IEnumerator<float> setRoles()
//if force exact role is on we can just set the buddy to the other player's role
if (buddyPlugin.Config.forceExactRole)
{
buddy.Kill();
buddy.SetRole(player.Role);
doneIDs.Add(buddy1);
doneIDs.Add(id);
Expand All @@ -138,9 +140,7 @@ private IEnumerator<float> setRoles()
if (player1.UserId != id && player1.UserId != buddy1 && !buddyPlugin.buddies.ContainsKey(player1.UserId) && player1.Team == Team.SCP)
{
//set the buddy to that player's role and set the player to classd
buddy.Kill();
buddy.SetRole(player1.Role);
player1.Kill();
player1.SetRole(RoleType.ClassD);
setRole = true;
doneIDs.Add(buddy1);
Expand All @@ -153,15 +153,13 @@ private IEnumerator<float> setRoles()
{
List<RoleType> roles = new List<RoleType>(tmpArr);
roles.Remove(player.Role);
buddy.Kill();
buddy.SetRole(roles[rnd.Next(roles.Count)]);
doneIDs.Add(buddy1);
doneIDs.Add(id);
}
continue;
}
//if they are not an scp, we can just set them to the same role as their buddy
buddy.Kill();
buddy.SetRole(player.Role);
doneIDs.Add(buddy1);
doneIDs.Add(id);
Expand Down

0 comments on commit 5c8be50

Please sign in to comment.