Skip to content
This repository has been archived by the owner on Oct 9, 2022. It is now read-only.

Commit

Permalink
Npcs, GetRoles() Optimization, Test Command
Browse files Browse the repository at this point in the history
Added a testing command so I can test the plugin on my own, made GetRoles() not be called 7 times per check, no longer checks player objects without a userid
  • Loading branch information
Build committed Feb 20, 2021
1 parent 747242f commit aa32051
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 9 deletions.
29 changes: 29 additions & 0 deletions EndConditions/Commands/TestConditions.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
namespace EndConditions.Commands
{
using CommandSystem;
using Exiled.API.Enums;
using Exiled.Events.EventArgs;
using Exiled.Permissions.Extensions;
using System;

[CommandHandler(typeof(RemoteAdminCommandHandler))]
public class TestConditions : ICommand
{
public bool Execute(ArraySegment<string> arguments, ICommandSender sender, out string response)
{
if (!sender.CheckPermission("ec.test"))
{
response = "Insufficient permission. Required: ec.test";
return false;
}

EndConditions.EventHandlers.OnCheckRoundEnd(new EndingRoundEventArgs(LeadingTeam.Draw, default, false));
response = "Fired event.";
return true;
}

public string Command { get; } = "testconditions";
public string[] Aliases { get; } = Array.Empty<string>();
public string Description { get; } = "Fires the EndingRound method once. Used for debugging.";
}
}
14 changes: 7 additions & 7 deletions EndConditions/EndConditions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,23 +16,23 @@ public class EndConditions : Plugin<Config>
{
private static readonly string ConfigsDirectory = Path.Combine(Paths.Configs, "EndConditions");
private static readonly string FileDirectory = Path.Combine(ConfigsDirectory, "config.yml");
private EventHandlers _eventHandlers;
internal static EventHandlers EventHandlers;

public override void OnEnabled()
{
_eventHandlers = new EventHandlers(Config);
ServerHandlers.RoundStarted += _eventHandlers.OnRoundStart;
ServerHandlers.EndingRound += _eventHandlers.OnCheckRoundEnd;
EventHandlers = new EventHandlers(Config);
ServerHandlers.RoundStarted += EventHandlers.OnRoundStart;
ServerHandlers.EndingRound += EventHandlers.OnCheckRoundEnd;
LoadConditions();
base.OnEnabled();
}

public override void OnDisabled()
{
EventHandlers.Conditions.Clear();
ServerHandlers.RoundStarted -= _eventHandlers.OnRoundStart;
ServerHandlers.EndingRound -= _eventHandlers.OnCheckRoundEnd;
_eventHandlers = null;
ServerHandlers.RoundStarted -= EventHandlers.OnRoundStart;
ServerHandlers.EndingRound -= EventHandlers.OnCheckRoundEnd;
EventHandlers = null;
}

public override string Author => "Build";
Expand Down
7 changes: 5 additions & 2 deletions EndConditions/EventHandlers.cs
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ public void OnCheckRoundEnd(EndingRoundEventArgs ev)

if (Warhead.IsDetonated && _config.EndOnDetonation)
{
Log.Debug("Ending the round via warhead detonation.", _config.AllowDebug);
EndGame(ev, _config.DetonationWinner);
return;
}
Expand All @@ -50,8 +51,10 @@ public void OnCheckRoundEnd(EndingRoundEventArgs ev)
_escAdditions["-science"] = RoundSummary.escaped_scientists == 0;
_escAdditions["+science"] = RoundSummary.escaped_scientists != 0;

IEnumerable<string> roles = GetRoles();

// Pull all the lists from the core dictionary and check em
foreach (var condition in Conditions.Where(condition => !GetRoles().Except(condition.RoleConditions).Any()))
foreach (var condition in Conditions.Where(condition => !roles.Except(condition.RoleConditions).Any()))
{
try
{
Expand Down Expand Up @@ -82,7 +85,7 @@ private IEnumerable<string> GetRoles()
List<string> list = new List<string>();
foreach (Player ply in Player.List)
{
if (ply.Role == RoleType.Spectator || API.BlacklistedPlayers.Contains(ply))
if (string.IsNullOrEmpty(ply.UserId) || ply.Role == RoleType.Spectator || API.BlacklistedPlayers.Contains(ply))
continue;

if (API.ModifiedRoles.ContainsKey(ply))
Expand Down

0 comments on commit aa32051

Please sign in to comment.