This commit is contained in:
2025-06-05 01:12:55 +02:00
parent 17c654d889
commit 96596e9c08
39 changed files with 1157 additions and 1350 deletions
+33 -29
View File
@@ -1,36 +1,36 @@
using System.Collections;
using LabApi.Features;
using LabApi.Loader.Features.Plugins;
using LabApi.Events.Arguments.PlayerEvents;
using LabApi.Events.Arguments.PlayerEvents;
using LabApi.Events.Handlers;
using LabApi.Features;
using LabApi.Features.Wrappers;
using PlayerRoles;
using Logger = LabApi.Features.Console.Logger;
using Version = System.Version;
using LabApi.Loader.Features.Plugins;
using MEC;
using PlayerRoles;
using UnityEngine;
using Logger = LabApi.Features.Console.Logger;
using Random = UnityEngine.Random;
using Version = System.Version;
namespace AfkSwap;
public class AfkSwap : Plugin
{
private const float AfkTimeLimit = 60; // 1 minute in seconds
private readonly Dictionary<Player, DateTime> _afkPlayers = new();
private readonly object _lock = new();
private readonly Dictionary<Player, Vector3> _playerPositions = new();
private readonly Dictionary<Player, DateTime> _playerSpawnTimes = new();
public override string Name => "AfkSwap";
public override string Author => "Code002Lover";
public override Version Version { get; } = new(1, 0, 0);
public override string Description => "Swaps AFK players with spectators after one minute.";
public override Version RequiredApiVersion { get; } = new(LabApiProperties.CompiledVersion);
private readonly Dictionary<Player, DateTime> _playerSpawnTimes = new();
private readonly Dictionary<Player, Vector3> _playerPositions = new();
private readonly Dictionary<Player, DateTime> _afkPlayers = new();
private const float AfkTimeLimit = 60; // 1 minute in seconds
private readonly object _lock = new();
public override void Enable()
{
PlayerEvents.Spawned += OnPlayerSpawned;
Timing.RunCoroutine(CheckAfkPlayers());
}
@@ -45,7 +45,7 @@ public class AfkSwap : Plugin
_afkPlayers.Clear();
}
}
private void OnPlayerSpawned(PlayerSpawnedEventArgs ev)
{
var player = ev.Player;
@@ -60,9 +60,8 @@ public class AfkSwap : Plugin
Logger.Debug($"Player {player.DisplayName} spawned");
});
}
private IEnumerator<float> CheckAfkPlayers()
{
Logger.Debug("Starting Afk Checking");
@@ -70,14 +69,17 @@ public class AfkSwap : Plugin
{
lock (_lock)
{
foreach (var playerTime in _playerSpawnTimes.ToList().Where(playerTime => (DateTime.Now - playerTime.Value).TotalSeconds >= AfkTimeLimit))
foreach (var playerTime in _playerSpawnTimes.ToList().Where(playerTime =>
(DateTime.Now - playerTime.Value).TotalSeconds >= AfkTimeLimit))
{
if (playerTime.Key.Role is RoleTypeId.Spectator or RoleTypeId.Destroyed or RoleTypeId.Overwatch or RoleTypeId.Tutorial)
if (playerTime.Key.Role is RoleTypeId.Spectator or RoleTypeId.Destroyed or RoleTypeId.Overwatch
or RoleTypeId.Tutorial)
{
_playerSpawnTimes.Remove(playerTime.Key);
_playerPositions.Remove(playerTime.Key);
continue;
}
if (!_playerPositions[playerTime.Key].Equals(playerTime.Key.Position))
{
_playerSpawnTimes.Remove(playerTime.Key);
@@ -94,35 +96,37 @@ public class AfkSwap : Plugin
}
// ReSharper disable once IteratorNeverReturns
}
private void SwapWithSpectator(Player afkPlayer)
{
var spectators = Player.ReadyList.Where(p => p.Role == RoleTypeId.Spectator && (DateTime.Now - _afkPlayers[p]).TotalSeconds > 10).ToList();
var spectators = Player.ReadyList
.Where(p => p.Role == RoleTypeId.Spectator && (DateTime.Now - _afkPlayers[p]).TotalSeconds > 10).ToList();
if (!spectators.Any())
{
Logger.Warn("No spectators to swap to");
return;
}
var randomSpectator = spectators[UnityEngine.Random.Range(0, spectators.Count)];
var randomSpectator = spectators[Random.Range(0, spectators.Count)];
Logger.Debug($"Swapping {afkPlayer.DisplayName} with {randomSpectator.DisplayName}");
// Store the AFK player's position and role
var afkPosition = afkPlayer.Position;
var afkRole = afkPlayer.Role;
// Give the spectator the AFK player's role and position
randomSpectator.Role = afkRole;
randomSpectator.Position = afkPosition;
// Make the AFK player a spectator
afkPlayer.Role = RoleTypeId.Spectator;
// Remove the AFK player from tracking
_playerSpawnTimes.Remove(afkPlayer);
_playerPositions.Remove(afkPlayer);
_playerSpawnTimes[randomSpectator] = DateTime.Now;
_playerPositions[randomSpectator] = randomSpectator.Position;
// Broadcast the swap
afkPlayer.SendBroadcast($"You were swapped with {randomSpectator.DisplayName} due to inactivity.", 10);
randomSpectator.SendBroadcast($"You were swapped with {afkPlayer.DisplayName} due to them being AFK.", 5);
+2 -8
View File
@@ -24,13 +24,7 @@
<HintPath>..\dependencies\Assembly-CSharp.dll</HintPath>
</Reference>
<Reference Include="Assembly-CSharp-firstpass">
<HintPath>..\dependencies\Assembly-CSharp-firstpass.dll</HintPath>
</Reference>
<Reference Include="Mirror">
<HintPath>..\dependencies\Mirror.dll</HintPath>
</Reference>
<Reference Include="Pooling">
<HintPath>..\dependencies\Pooling.dll</HintPath>
<HintPath>..\dependencies\Assembly-CSharp-firstpass.dll</HintPath>
</Reference>
<Reference Include="UnityEngine.CoreModule">
<HintPath>..\dependencies\UnityEngine.CoreModule.dll</HintPath>
@@ -38,6 +32,6 @@
</ItemGroup>
<ItemGroup>
<PackageReference Include="Northwood.LabAPI" Version="1.0.2" />
<PackageReference Include="Northwood.LabAPI" Version="1.0.2"/>
</ItemGroup>
</Project>