115 lines
3.6 KiB
C#
115 lines
3.6 KiB
C#
using HintServiceMeow.Core.Enum;
|
|
using HintServiceMeow.Core.Models.Hints;
|
|
using HintServiceMeow.Core.Utilities;
|
|
using LabApi.Events.Arguments.PlayerEvents;
|
|
using LabApi.Events.Handlers;
|
|
using LabApi.Features;
|
|
using LabApi.Features.Console;
|
|
using LabApi.Features.Wrappers;
|
|
using LabApi.Loader.Features.Plugins;
|
|
using PlayerRoles;
|
|
using Timer = System.Timers.Timer;
|
|
|
|
namespace VisibleSpectators
|
|
{
|
|
public class Plugin : Plugin<SpectatorConfig>
|
|
{
|
|
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 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 UpdateSpectators(Player player)
|
|
{
|
|
var spectators = string.Join("\n",player.CurrentSpectators.Select(x => x.DisplayName));
|
|
if (player.Role == RoleTypeId.Spectator)
|
|
{
|
|
spectators = string.Join("\n",player.CurrentlySpectating?.CurrentSpectators.Select(x => x.DisplayName) ?? Array.Empty<string>());
|
|
}
|
|
|
|
if (spectators.Length < 2)
|
|
{
|
|
spectators = Config!.NoSpectatorsMessage;
|
|
}
|
|
|
|
|
|
_spectatorHints[player].Text = $"{Config!.HeaderMessage}\n{spectators}\n{DateTime.UtcNow:HH:mm:ss}";
|
|
|
|
_spectatorHints[player].Hide = player.Role is RoleTypeId.Overwatch or RoleTypeId.Destroyed or RoleTypeId.None;
|
|
}
|
|
|
|
private static Player[] GetPlayers()
|
|
{
|
|
return Player.Dictionary.Values.Where(x=>!x.IsHost).ToArray();
|
|
}
|
|
|
|
private static void OnSpectate(PlayerChangedSpectatorEventArgs ev)
|
|
{
|
|
_singleton.UpdateSpectators(ev.OldTarget);
|
|
_singleton.UpdateSpectators(ev.NewTarget);
|
|
}
|
|
|
|
private void OnJoin(PlayerJoinedEventArgs ev)
|
|
{
|
|
var hint = new Hint
|
|
{
|
|
Text = $"{Config!.HeaderMessage}\n{Config!.NoSpectatorsMessage}",
|
|
Alignment = HintAlignment.Right,
|
|
YCoordinate = 100,
|
|
Hide = true
|
|
};
|
|
|
|
var playerDisplay = PlayerDisplay.Get(ev.Player);
|
|
playerDisplay.AddHint(hint);
|
|
|
|
_spectatorHints[ev.Player] = hint;
|
|
}
|
|
}
|
|
|
|
public class SpectatorConfig
|
|
{
|
|
public string HeaderMessage => "Spectators:";
|
|
public string NoSpectatorsMessage => "No spectators";
|
|
}
|
|
} |