refactor
This commit is contained in:
@@ -1,5 +1,4 @@
|
||||
using HintServiceMeow.Core.Enum;
|
||||
using HintServiceMeow.Core.Models.HintContent;
|
||||
using HintServiceMeow.Core.Models.Hints;
|
||||
using HintServiceMeow.Core.Utilities;
|
||||
using LabApi.Events.Arguments.PlayerEvents;
|
||||
@@ -9,195 +8,187 @@ using LabApi.Features.Console;
|
||||
using LabApi.Features.Wrappers;
|
||||
using LabApi.Loader.Features.Plugins;
|
||||
using PlayerRoles;
|
||||
using PlayerRoles.Spectating;
|
||||
using Timer = System.Timers.Timer;
|
||||
|
||||
namespace VisibleSpectators
|
||||
namespace VisibleSpectators;
|
||||
|
||||
public class Plugin : Plugin<SpectatorConfig>
|
||||
{
|
||||
public class Plugin : Plugin<SpectatorConfig>
|
||||
private static Plugin _singleton;
|
||||
|
||||
private static readonly Dictionary<string, string> GetColorMap = new()
|
||||
{
|
||||
public override string Name => "VisibleSpectators";
|
||||
public override string Author => "Code002Lover";
|
||||
public override Version Version { get; } = new(1, 0, 0);
|
||||
public override string Description => "See your spectators";
|
||||
public override Version RequiredApiVersion { get; } = new (LabApiProperties.CompiledVersion);
|
||||
{ "DEFAULT", "FFFFFF" },
|
||||
{ "PUMPKIN", "EE7600" },
|
||||
{ "ARMY_GREEN", "4B5320" },
|
||||
{ "MINT", "98FB98" },
|
||||
{ "NICKEL", "727472" },
|
||||
{ "CARMINE", "960018" },
|
||||
{ "EMERALD", "50C878" },
|
||||
{ "GREEN", "228B22" },
|
||||
{ "LIME", "BFFF00" },
|
||||
{ "POLICE_BLUE", "002DB3" },
|
||||
{ "ORANGE", "FF9966" },
|
||||
{ "SILVER_BLUE", "666699" },
|
||||
{ "BLUE_GREEN", "4DFFB8" },
|
||||
{ "MAGENTA", "FF0090" },
|
||||
{ "YELLOW", "FAFF86" },
|
||||
{ "TOMATO", "FF6448" },
|
||||
{ "DEEP_PINK", "FF1493" },
|
||||
{ "AQUA", "00FFFF" },
|
||||
{ "CYAN", "00B7EB" },
|
||||
{ "CRIMSON", "DC143C" },
|
||||
{ "LIGHT_GREEN", "32CD32" },
|
||||
{ "SILVER", "A0A0A0" },
|
||||
{ "BROWN", "944710" },
|
||||
{ "RED", "C50000" },
|
||||
{ "PINK", "FF96DE" },
|
||||
{ "LIGHT_RED", "FD8272" },
|
||||
{ "PURPLE", "8137CE" },
|
||||
{ "BLUE", "005EBC" },
|
||||
{ "TEAL", "008080" },
|
||||
{ "GOLD", "EFC01A" }
|
||||
};
|
||||
|
||||
public int YCoordinate { get; set; } = 100;
|
||||
private readonly Dictionary<Player, Hint> _spectatorHints = new();
|
||||
private Timer _timer;
|
||||
public override string Name => "VisibleSpectators";
|
||||
public override string Author => "Code002Lover";
|
||||
public override Version Version { get; } = new(1, 0, 0);
|
||||
public override string Description => "See your spectators";
|
||||
public override Version RequiredApiVersion { get; } = new(LabApiProperties.CompiledVersion);
|
||||
|
||||
private static Plugin _singleton;
|
||||
private Timer _timer;
|
||||
private readonly Dictionary<Player,Hint> _spectatorHints = new();
|
||||
public int YCoordinate { get; set; } = 100;
|
||||
|
||||
public override void Enable()
|
||||
public override void Enable()
|
||||
{
|
||||
Logger.Debug("starting...");
|
||||
_singleton = this;
|
||||
|
||||
PlayerEvents.ChangedSpectator += OnSpectate;
|
||||
PlayerEvents.Joined += OnJoin;
|
||||
|
||||
_timer = new Timer(1000);
|
||||
_timer.Elapsed += (_, _) => UpdateSpectators();
|
||||
_timer.Start();
|
||||
}
|
||||
|
||||
public override void Disable()
|
||||
{
|
||||
Logger.Debug("unloading...");
|
||||
|
||||
_timer.Stop();
|
||||
_timer.Dispose();
|
||||
_timer = null;
|
||||
|
||||
PlayerEvents.Joined -= OnJoin;
|
||||
PlayerEvents.ChangedSpectator -= OnSpectate;
|
||||
|
||||
_singleton = null;
|
||||
}
|
||||
|
||||
private void UpdateSpectators()
|
||||
{
|
||||
foreach (var player in GetPlayers()) UpdateSpectators(player);
|
||||
}
|
||||
|
||||
private void AddPlayerHint(Player player)
|
||||
{
|
||||
var hint = new Hint
|
||||
{
|
||||
Logger.Debug("starting...");
|
||||
_singleton = this;
|
||||
|
||||
PlayerEvents.ChangedSpectator += OnSpectate;
|
||||
PlayerEvents.Joined += OnJoin;
|
||||
|
||||
_timer = new Timer(1000);
|
||||
_timer.Elapsed += (_, _) => UpdateSpectators();
|
||||
_timer.Start();
|
||||
}
|
||||
|
||||
public override void Disable()
|
||||
{
|
||||
Logger.Debug("unloading...");
|
||||
|
||||
_timer.Stop();
|
||||
_timer.Dispose();
|
||||
_timer = null;
|
||||
|
||||
PlayerEvents.Joined -= OnJoin;
|
||||
PlayerEvents.ChangedSpectator -= OnSpectate;
|
||||
|
||||
_singleton = null;
|
||||
}
|
||||
|
||||
private void UpdateSpectators()
|
||||
{
|
||||
foreach (var player in GetPlayers())
|
||||
{
|
||||
UpdateSpectators(player);
|
||||
}
|
||||
}
|
||||
|
||||
private void AddPlayerHint(Player player)
|
||||
{
|
||||
var hint = new Hint
|
||||
{
|
||||
Text = $"{Config!.HeaderMessage}\n{Config!.NoSpectatorsMessage}",
|
||||
Alignment = HintAlignment.Right,
|
||||
YCoordinate = YCoordinate,
|
||||
Hide = true
|
||||
};
|
||||
|
||||
var playerDisplay = PlayerDisplay.Get(player);
|
||||
playerDisplay.AddHint(hint);
|
||||
|
||||
_spectatorHints[player] = hint;
|
||||
}
|
||||
|
||||
private static readonly Dictionary<string, string> GetColorMap = new()
|
||||
{
|
||||
{ "DEFAULT", "FFFFFF" },
|
||||
{ "PUMPKIN", "EE7600" },
|
||||
{ "ARMY_GREEN", "4B5320" },
|
||||
{ "MINT", "98FB98" },
|
||||
{ "NICKEL", "727472" },
|
||||
{ "CARMINE", "960018" },
|
||||
{ "EMERALD", "50C878" },
|
||||
{ "GREEN", "228B22" },
|
||||
{ "LIME", "BFFF00" },
|
||||
{ "POLICE_BLUE", "002DB3" },
|
||||
{ "ORANGE", "FF9966" },
|
||||
{ "SILVER_BLUE", "666699" },
|
||||
{ "BLUE_GREEN", "4DFFB8" },
|
||||
{ "MAGENTA", "FF0090" },
|
||||
{ "YELLOW", "FAFF86" },
|
||||
{ "TOMATO", "FF6448" },
|
||||
{ "DEEP_PINK", "FF1493" },
|
||||
{ "AQUA", "00FFFF" },
|
||||
{ "CYAN", "00B7EB" },
|
||||
{ "CRIMSON", "DC143C" },
|
||||
{ "LIGHT_GREEN", "32CD32" },
|
||||
{ "SILVER", "A0A0A0" },
|
||||
{ "BROWN", "944710" },
|
||||
{ "RED", "C50000" },
|
||||
{ "PINK", "FF96DE" },
|
||||
{ "LIGHT_RED", "FD8272" },
|
||||
{ "PURPLE", "8137CE" },
|
||||
{ "BLUE", "005EBC" },
|
||||
{ "TEAL", "008080" },
|
||||
{ "GOLD", "EFC01A" }
|
||||
Text = $"{Config!.HeaderMessage}\n{Config!.NoSpectatorsMessage}",
|
||||
Alignment = HintAlignment.Right,
|
||||
YCoordinate = YCoordinate,
|
||||
Hide = true
|
||||
};
|
||||
|
||||
private static string PlayerToDisplay(Player player)
|
||||
{
|
||||
if (player == null) return "";
|
||||
if (!player.IsReady) return "";
|
||||
|
||||
// Default color if GroupColor is null or not found in the map
|
||||
const string defaultColor = "FFFFFF";
|
||||
|
||||
try
|
||||
{
|
||||
var groupColor = player.GroupColor;
|
||||
if (string.IsNullOrEmpty(groupColor))
|
||||
return $"<color=#{defaultColor}FF>{player.DisplayName}</color>";
|
||||
|
||||
return GetColorMap.TryGetValue(groupColor.ToUpper(), out var color) ? $"<color=#{color}FF>{player.DisplayName}</color>" : $"<color=#{defaultColor}FF>{player.DisplayName}</color>";
|
||||
}
|
||||
catch
|
||||
{
|
||||
return $"<color=#{defaultColor}FF>{player.DisplayName}</color>";
|
||||
}
|
||||
}
|
||||
var playerDisplay = PlayerDisplay.Get(player);
|
||||
playerDisplay.AddHint(hint);
|
||||
|
||||
private static bool IsNotOverwatch(Player player)
|
||||
{
|
||||
return player != null && player.Role != RoleTypeId.Overwatch;
|
||||
}
|
||||
|
||||
private void UpdateSpectators(Player player)
|
||||
{
|
||||
// Safety check - if player doesn't have a hint, create one
|
||||
if (!_spectatorHints.ContainsKey(player))
|
||||
{
|
||||
AddPlayerHint(player);
|
||||
}
|
||||
|
||||
var spectators = Config!.NoSpectatorsMessage;
|
||||
|
||||
try
|
||||
{
|
||||
spectators = string.Join("\n",player.CurrentSpectators.Where(IsNotOverwatch).Select(PlayerToDisplay));
|
||||
if (player.Role == RoleTypeId.Spectator)
|
||||
spectators = player.CurrentlySpectating == null
|
||||
? Config!.NoSpectatorsMessage
|
||||
: string.Join("\n",
|
||||
player.CurrentlySpectating?.CurrentSpectators.Where(IsNotOverwatch)
|
||||
.Select(PlayerToDisplay) ?? Array.Empty<string>());
|
||||
} catch (Exception e)
|
||||
{
|
||||
Logger.Error(e);
|
||||
}
|
||||
|
||||
if (spectators.Length < 2)
|
||||
{
|
||||
spectators = Config!.NoSpectatorsMessage;
|
||||
}
|
||||
|
||||
|
||||
_spectatorHints[player].Text = $"{Config!.HeaderMessage}\n{spectators}";
|
||||
|
||||
_spectatorHints[player].Hide = player.Role is RoleTypeId.Destroyed or RoleTypeId.None;
|
||||
|
||||
_spectatorHints[player].YCoordinate = YCoordinate + player.CurrentSpectators.Count * 10;
|
||||
}
|
||||
|
||||
private static Player[] GetPlayers()
|
||||
{
|
||||
return Player.ReadyList.Where(IsNotOverwatch).ToArray();
|
||||
}
|
||||
|
||||
private static void OnSpectate(PlayerChangedSpectatorEventArgs ev)
|
||||
{
|
||||
_singleton.UpdateSpectators(ev.OldTarget);
|
||||
_singleton.UpdateSpectators(ev.NewTarget);
|
||||
_singleton.UpdateSpectators(ev.Player);
|
||||
}
|
||||
|
||||
private void OnJoin(PlayerJoinedEventArgs ev)
|
||||
{
|
||||
AddPlayerHint(ev.Player);
|
||||
}
|
||||
_spectatorHints[player] = hint;
|
||||
}
|
||||
|
||||
public class SpectatorConfig
|
||||
private static string PlayerToDisplay(Player player)
|
||||
{
|
||||
public string HeaderMessage { get; set; } = "Spectators:";
|
||||
public string NoSpectatorsMessage { get; set; } = "No spectators";
|
||||
if (player == null) return "";
|
||||
if (!player.IsReady) return "";
|
||||
|
||||
// Default color if GroupColor is null or not found in the map
|
||||
const string defaultColor = "FFFFFF";
|
||||
|
||||
try
|
||||
{
|
||||
var groupColor = player.GroupColor;
|
||||
if (string.IsNullOrEmpty(groupColor))
|
||||
return $"<color=#{defaultColor}FF>{player.DisplayName}</color>";
|
||||
|
||||
return GetColorMap.TryGetValue(groupColor.ToUpper(), out var color)
|
||||
? $"<color=#{color}FF>{player.DisplayName}</color>"
|
||||
: $"<color=#{defaultColor}FF>{player.DisplayName}</color>";
|
||||
}
|
||||
catch
|
||||
{
|
||||
return $"<color=#{defaultColor}FF>{player.DisplayName}</color>";
|
||||
}
|
||||
}
|
||||
|
||||
private static bool IsNotOverwatch(Player player)
|
||||
{
|
||||
return player != null && player.Role != RoleTypeId.Overwatch;
|
||||
}
|
||||
|
||||
private void UpdateSpectators(Player player)
|
||||
{
|
||||
// Safety check - if player doesn't have a hint, create one
|
||||
if (!_spectatorHints.ContainsKey(player)) AddPlayerHint(player);
|
||||
|
||||
var spectators = Config!.NoSpectatorsMessage;
|
||||
|
||||
try
|
||||
{
|
||||
spectators = string.Join("\n", player.CurrentSpectators.Where(IsNotOverwatch).Select(PlayerToDisplay));
|
||||
if (player.Role == RoleTypeId.Spectator)
|
||||
spectators = player.CurrentlySpectating == null
|
||||
? Config!.NoSpectatorsMessage
|
||||
: string.Join("\n",
|
||||
player.CurrentlySpectating?.CurrentSpectators.Where(IsNotOverwatch)
|
||||
.Select(PlayerToDisplay) ?? Array.Empty<string>());
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
Logger.Error(e);
|
||||
}
|
||||
|
||||
if (spectators.Length < 2) spectators = Config!.NoSpectatorsMessage;
|
||||
|
||||
|
||||
_spectatorHints[player].Text = $"{Config!.HeaderMessage}\n{spectators}";
|
||||
|
||||
_spectatorHints[player].Hide = player.Role is RoleTypeId.Destroyed or RoleTypeId.None;
|
||||
|
||||
_spectatorHints[player].YCoordinate = YCoordinate + player.CurrentSpectators.Count * 10;
|
||||
}
|
||||
|
||||
private static Player[] GetPlayers()
|
||||
{
|
||||
return Player.ReadyList.Where(IsNotOverwatch).ToArray();
|
||||
}
|
||||
|
||||
private static void OnSpectate(PlayerChangedSpectatorEventArgs ev)
|
||||
{
|
||||
_singleton.UpdateSpectators(ev.OldTarget);
|
||||
_singleton.UpdateSpectators(ev.NewTarget);
|
||||
_singleton.UpdateSpectators(ev.Player);
|
||||
}
|
||||
|
||||
private void OnJoin(PlayerJoinedEventArgs ev)
|
||||
{
|
||||
AddPlayerHint(ev.Player);
|
||||
}
|
||||
}
|
||||
|
||||
public class SpectatorConfig
|
||||
{
|
||||
public string HeaderMessage { get; set; } = "Spectators:";
|
||||
public string NoSpectatorsMessage { get; set; } = "No spectators";
|
||||
}
|
||||
@@ -20,14 +20,11 @@
|
||||
</PropertyGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<Reference Include="0Harmony">
|
||||
<HintPath>..\dependencies\0Harmony.dll</HintPath>
|
||||
</Reference>
|
||||
<Reference Include="Assembly-CSharp">
|
||||
<HintPath>..\..\.local\share\Steam\steamapps\common\SCP Secret Laboratory Dedicated Server\SCPSL_Data\Managed\Assembly-CSharp.dll</HintPath>
|
||||
</Reference>
|
||||
<Reference Include="HintServiceMeow">
|
||||
<HintPath>..\dependencies\HintServiceMeow-LabAPI.dll</HintPath>
|
||||
<HintPath>..\dependencies\HintServiceMeow-LabAPI.dll</HintPath>
|
||||
</Reference>
|
||||
<Reference Include="Mirror">
|
||||
<HintPath>..\..\.local\share\Steam\steamapps\common\SCP Secret Laboratory Dedicated Server\SCPSL_Data\Managed\Mirror.dll</HintPath>
|
||||
@@ -38,6 +35,6 @@
|
||||
</ItemGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<PackageReference Include="Northwood.LabAPI" Version="1.0.2" />
|
||||
<PackageReference Include="Northwood.LabAPI" Version="1.0.2"/>
|
||||
</ItemGroup>
|
||||
</Project>
|
||||
|
||||
Reference in New Issue
Block a user