Skip to content

Commit

Permalink
Smol improvements
Browse files Browse the repository at this point in the history
  • Loading branch information
bladuk committed Jun 8, 2022
1 parent 8dd3c5e commit 0ae2b4b
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 31 deletions.
5 changes: 4 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,9 @@ Put ScpsInfoDisplay.dll under the release tab into %appdata%\EXILED\Plugins (Win
| `%engaging%` | Count of generators that are currently engaging (format: (+count)) |
| `%zombies%` | Count of SCP-049-2 |
| `%distance%` | Distance between the player who seeng the list and SCP in meters |
| `%079level` | Level of SCP-079 |
| `%079experience` | Experience points of SCP-079 |
| `%079energy` | Aux power of SCP-079 |

Feel free to use this variables in display strings.

Expand All @@ -26,7 +29,7 @@ scpsinfodisplay:
display_strings:
Scp106: <color=#D51D1D>SCP-106 [%healthpercent%%] %distance%</color>
Scp049: '<color=#D51D1D>SCP-049 [%healthpercent%%, Zombies: %zombies%] %distance%</color>'
Scp079: <color=#D51D1D>SCP-079 [%generators%%engaging%/3]</color>
Scp079: '<color=#D51D1D>SCP-079 [%generators%%engaging%/3] Lvl: %079level% Exp: %079experience% Energy: %079energy%</color>'
Scp096: <color=#D51D1D>SCP-096 [%healthpercent%%] %distance%</color>
Scp173: <color=#D51D1D>SCP-173 [%healthpercent%%] %distance%</color>
Scp93953: <color=#D51D1D>SCP-939-53 [%healthpercent%%] %distance%</color>
Expand Down
2 changes: 1 addition & 1 deletion ScpsInfoDisplay/Config.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ public class Config : IConfig
{
{ RoleType.Scp106, "<color=#D51D1D>SCP-106 [%healthpercent%%] %distance%</color>" },
{ RoleType.Scp049, "<color=#D51D1D>SCP-049 [%healthpercent%%, Zombies: %zombies%] %distance%</color>" },
{ RoleType.Scp079, "<color=#D51D1D>SCP-079 [%generators%%engaging%/3]</color>" },
{ RoleType.Scp079, "<color=#D51D1D>SCP-079 [%generators%%engaging%/3] Lvl: %079level% Exp: %079experience% Energy: %079energy%</color>" },
{ RoleType.Scp096, "<color=#D51D1D>SCP-096 [%healthpercent%%] %distance%</color>" },
{ RoleType.Scp173, "<color=#D51D1D>SCP-173 [%healthpercent%%] %distance%</color>" },
{ RoleType.Scp93953, "<color=#D51D1D>SCP-939-53 [%healthpercent%%] %distance%</color>" },
Expand Down
55 changes: 27 additions & 28 deletions ScpsInfoDisplay/EventHandlers.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
using System.Linq;
using Exiled.API.Extensions;
using Exiled.API.Features;
using Exiled.API.Features.Roles;
using Exiled.Events.EventArgs;
using MEC;
using UnityEngine;
Expand All @@ -15,7 +16,7 @@ public class EventHandlers

public void OnWaitingForPlayers()
{
if (ScpsInfoDisplay.Singleton.Config.TextAlignment.ToLower() != "center" && ScpsInfoDisplay.Singleton.Config.TextAlignment.ToLower() != "left" && ScpsInfoDisplay.Singleton.Config.TextAlignment.ToLower() != "right")
if (!string.Equals(ScpsInfoDisplay.Singleton.Config.TextAlignment, "center", StringComparison.OrdinalIgnoreCase) && !string.Equals(ScpsInfoDisplay.Singleton.Config.TextAlignment, "left", StringComparison.OrdinalIgnoreCase) && !string.Equals(ScpsInfoDisplay.Singleton.Config.TextAlignment, "right", StringComparison.OrdinalIgnoreCase))
{
Log.Warn(ScpsInfoDisplay.Singleton.Config.TextAlignment + " is an invalid value of text_alignment config. It will be replaced with the default value (right).");
ScpsInfoDisplay.Singleton.Config.TextAlignment = "right";
Expand All @@ -42,28 +43,24 @@ public void OnWaitingForPlayers()

public void OnRoundRestart()
{
foreach (KeyValuePair<Player, CoroutineHandle> display in _allDisplays.ToList())
foreach (var display in _allDisplays.Where(display => display.Key != null && display.Value.IsRunning))
{
if (display.Key != null && display.Value.IsRunning)
Timing.KillCoroutines(display.Value);
_allDisplays.Remove(display.Key);
Timing.KillCoroutines(display.Value);
}
_allDisplays.Clear();
}

public void OnPlayerChangingRole(ChangingRoleEventArgs ev)
{
if (ev.Player != null)
if (ev.Player == null) return;
if (_allDisplays.ContainsKey(ev.Player) && !ScpsInfoDisplay.Singleton.Config.DisplayStrings.ContainsKey(ev.NewRole))
{
if (_allDisplays.ContainsKey(ev.Player) && !ScpsInfoDisplay.Singleton.Config.DisplayStrings.ContainsKey(ev.NewRole))
{
Timing.KillCoroutines(_allDisplays[ev.Player]);
_allDisplays.Remove(ev.Player);
}
else if ((!_allDisplays.ContainsKey(ev.Player) && ev.NewRole.GetTeam() == Team.SCP && ScpsInfoDisplay.Singleton.Config.DisplayStrings.ContainsKey(ev.NewRole)) || ScpsInfoDisplay.Singleton.Config.CustomRolesIntegrations.Keys.Any(key => ev.Player.SessionVariables.ContainsKey(key)))
{
_allDisplays.Add(ev.Player, Timing.RunCoroutine(_showDisplay(ev.Player)));
}
Timing.KillCoroutines(_allDisplays[ev.Player]);
_allDisplays.Remove(ev.Player);
}
else if ((!_allDisplays.ContainsKey(ev.Player) && ev.NewRole.GetTeam() == Team.SCP && ScpsInfoDisplay.Singleton.Config.DisplayStrings.ContainsKey(ev.NewRole)) || ScpsInfoDisplay.Singleton.Config.CustomRolesIntegrations.Keys.Any(key => ev.Player.SessionVariables.ContainsKey(key)))
{
_allDisplays.Add(ev.Player, Timing.RunCoroutine(_showDisplay(ev.Player)));
}
}

Expand All @@ -77,23 +74,14 @@ private IEnumerator<float> _showDisplay(Player player)
if (player != null)
{
string text = string.Empty;
foreach (KeyValuePair<string, string> integration in ScpsInfoDisplay.Singleton.Config.CustomRolesIntegrations)
foreach (var integration in ScpsInfoDisplay.Singleton.Config.CustomRolesIntegrations)
{
foreach (Player any in Player.List)
{
if (any != null && any.SessionVariables.ContainsKey(integration.Key))
{
text += $"<align={ScpsInfoDisplay.Singleton.Config.TextAlignment}>" + (player == any && ScpsInfoDisplay.Singleton.Config.MarkPlayerInList ? ScpsInfoDisplay.Singleton.Config.PlayersMarker + " " : "") + integration.Value.Replace("%arhealth%", any.ArtificialHealth > 0 ? any.ArtificialHealth.ToString() : "").Replace("%healthpercent%", Math.Round((Mathf.Clamp01(any.Health / (float)any.MaxHealth) * 100), 0).ToString()).Replace("%health%", Math.Round(any.Health, 0).ToString()).Replace("%generators%", Generator.List.Count(gen => gen.IsEngaged).ToString()).Replace("%engaging%", Generator.List.Count(gen => gen.IsActivating) > 0 ? $" (+{Generator.List.Count(gen => gen.IsActivating)})" : "").Replace("%zombies%", Player.List.Count(p => p.Role.Type == RoleType.Scp0492).ToString()).Replace("%distance%", any != player ? Math.Round(Vector3.Distance(player.Position, any.Position), 0).ToString() + "m" : "") + "</align>\n";
}
}
text = Player.List.Where(p => p != null && p.SessionVariables.ContainsKey(integration.Key)).Aggregate(text, (current, any) => current + $"<align={ScpsInfoDisplay.Singleton.Config.TextAlignment}>" + (player == any && ScpsInfoDisplay.Singleton.Config.MarkPlayerInList ? ScpsInfoDisplay.Singleton.Config.PlayersMarker + " " : "") + ProcessStringVariables(integration.Value, player, any) + "</align>\n");
}

foreach (Player scp in Player.List.Where(p => p.Role.Team == Team.SCP && ScpsInfoDisplay.Singleton.Config.DisplayStrings.ContainsKey(p.Role.Type)))
foreach (var scp in Player.List.Where(p => p != null && p.Role.Team == Team.SCP && ScpsInfoDisplay.Singleton.Config.DisplayStrings.ContainsKey(p.Role.Type)))
{
if (scp != null)
{
text += $"<align={ScpsInfoDisplay.Singleton.Config.TextAlignment}>" + (player == scp && ScpsInfoDisplay.Singleton.Config.MarkPlayerInList ? ScpsInfoDisplay.Singleton.Config.PlayersMarker + " " : "") + ScpsInfoDisplay.Singleton.Config.DisplayStrings[scp.Role.Type].Replace("%arhealth%", scp.ArtificialHealth > 0 ? scp.ArtificialHealth.ToString() : "").Replace("%healthpercent%", Math.Round((Mathf.Clamp01(scp.Health / (float)scp.MaxHealth) * 100), 0).ToString()).Replace("%health%", Math.Round(scp.Health, 0).ToString()).Replace("%generators%", Generator.List.Count(gen => gen.IsEngaged).ToString()).Replace("%engaging%", Generator.List.Count(gen => gen.IsActivating) > 0 ? $" (+{Generator.List.Count(gen => gen.IsActivating)})" : "").Replace("%zombies%", Player.List.Count(p => p.Role.Type == RoleType.Scp0492).ToString()).Replace("%distance%", scp != player ? Math.Round(Vector3.Distance(player.Position, scp.Position), 0).ToString() + "m" : "") + "</align>\n";
}
text += $"<align={ScpsInfoDisplay.Singleton.Config.TextAlignment}>" + (player == scp && ScpsInfoDisplay.Singleton.Config.MarkPlayerInList ? ScpsInfoDisplay.Singleton.Config.PlayersMarker + " " : "") + ProcessStringVariables(ScpsInfoDisplay.Singleton.Config.DisplayStrings[scp.Role.Type], player, scp) + "</align>\n";
}

if (ScpsInfoDisplay.Singleton.Config.TextPositionOffset >= 0 || (ScpsInfoDisplay.Singleton.Config.TextPositionOffset < 0 && (ScpsInfoDisplay.Singleton.Config.TextPositionOffset + text.Count(t => t == '\n')) > 0))
Expand All @@ -109,5 +97,16 @@ private IEnumerator<float> _showDisplay(Player player)
}
}
}

private string ProcessStringVariables(string raw, Player observer, Player target) => raw
.Replace("%arhealth%", target.ArtificialHealth > 0 ? target.ArtificialHealth.ToString() : "")
.Replace("%healthpercent%", Math.Floor(Mathf.Clamp01(target.Health / target.MaxHealth) * 100).ToString())
.Replace("%health%", Math.Round(target.Health, 0).ToString())
.Replace("%generators%", Generator.List.Count(gen => gen.IsEngaged).ToString())
.Replace("%engaging%", Generator.List.Count(gen => gen.IsActivating) > 0 ? $" (+{Generator.List.Count(gen => gen.IsActivating)})" : "").Replace("%zombies%", Player.List.Count(p => p.Role.Type == RoleType.Scp0492).ToString())
.Replace("%distance%", target != observer ? Math.Floor(Vector3.Distance(observer.Position, target.Position)) + "m" : "")
.Replace("%079level%", target.Role.Is(out Scp079Role _) ? (target.Role.As<Scp079Role>().Level + 1).ToString() : "")
.Replace("%079energy%", target.Role.Is(out Scp079Role _) ? Math.Floor(target.Role.As<Scp079Role>().Energy).ToString() : "")
.Replace("%079experience%", target.Role.Is(out Scp079Role _) ? Math.Floor(target.Role.As<Scp079Role>().Experience).ToString() : "");
}
}
2 changes: 1 addition & 1 deletion ScpsInfoDisplay/ScpsInfoDisplay.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ internal class ScpsInfoDisplay : Plugin<Config>
public override string Prefix => "scpsinfodisplay";
public override string Name => "ScpsInfoDisplay";
public override string Author => "bladuk.";
public override Version Version { get; } = new Version(1, 1, 2);
public override Version Version { get; } = new Version(1, 1, 3);
public override Version RequiredExiledVersion { get; } = new Version(5, 2, 1);
public static ScpsInfoDisplay Singleton = new ScpsInfoDisplay();
private EventHandlers _eventHandlers;
Expand Down

0 comments on commit 0ae2b4b

Please sign in to comment.