add and fix a lot
This commit is contained in:
@@ -0,0 +1,130 @@
|
||||
using System.Collections;
|
||||
using LabApi.Features;
|
||||
using LabApi.Loader.Features.Plugins;
|
||||
using LabApi.Events.Arguments.PlayerEvents;
|
||||
using LabApi.Events.Handlers;
|
||||
using LabApi.Features.Wrappers;
|
||||
using PlayerRoles;
|
||||
using Logger = LabApi.Features.Console.Logger;
|
||||
using Version = System.Version;
|
||||
using MEC;
|
||||
using UnityEngine;
|
||||
|
||||
namespace AfkSwap;
|
||||
|
||||
public class AfkSwap : Plugin
|
||||
{
|
||||
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());
|
||||
}
|
||||
|
||||
public override void Disable()
|
||||
{
|
||||
PlayerEvents.Spawned -= OnPlayerSpawned;
|
||||
|
||||
lock (_lock)
|
||||
{
|
||||
_playerSpawnTimes.Clear();
|
||||
_playerPositions.Clear();
|
||||
_afkPlayers.Clear();
|
||||
}
|
||||
}
|
||||
|
||||
private void OnPlayerSpawned(PlayerSpawnedEventArgs ev)
|
||||
{
|
||||
var player = ev.Player;
|
||||
Timing.CallDelayed(1, () =>
|
||||
{
|
||||
lock (_lock)
|
||||
{
|
||||
_playerSpawnTimes[player] = DateTime.Now;
|
||||
_playerPositions[player] = player.Position;
|
||||
_afkPlayers[player] = DateTime.Now;
|
||||
}
|
||||
|
||||
Logger.Debug($"Player {player.DisplayName} spawned");
|
||||
});
|
||||
|
||||
}
|
||||
|
||||
private IEnumerator<float> CheckAfkPlayers()
|
||||
{
|
||||
Logger.Debug("Starting Afk Checking");
|
||||
while (true)
|
||||
{
|
||||
lock (_lock)
|
||||
{
|
||||
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)
|
||||
{
|
||||
_playerSpawnTimes.Remove(playerTime.Key);
|
||||
_playerPositions.Remove(playerTime.Key);
|
||||
continue;
|
||||
}
|
||||
if (!_playerPositions[playerTime.Key].Equals(playerTime.Key.Position))
|
||||
{
|
||||
_playerSpawnTimes.Remove(playerTime.Key);
|
||||
_playerPositions.Remove(playerTime.Key);
|
||||
continue; // Player has moved, don't swap
|
||||
}
|
||||
|
||||
_afkPlayers[playerTime.Key] = DateTime.Now;
|
||||
SwapWithSpectator(playerTime.Key);
|
||||
}
|
||||
}
|
||||
|
||||
yield return Timing.WaitForSeconds(1);
|
||||
}
|
||||
// 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();
|
||||
if (!spectators.Any())
|
||||
{
|
||||
Logger.Warn("No spectators to swap to");
|
||||
return;
|
||||
}
|
||||
var randomSpectator = spectators[UnityEngine.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);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,43 @@
|
||||
<Project Sdk="Microsoft.NET.Sdk">
|
||||
|
||||
<PropertyGroup>
|
||||
<TargetFramework>net48</TargetFramework>
|
||||
<ImplicitUsings>enable</ImplicitUsings>
|
||||
<Nullable>disable</Nullable>
|
||||
<LangVersion>10</LangVersion>
|
||||
</PropertyGroup>
|
||||
|
||||
<PropertyGroup Condition=" '$(Configuration)' == 'Debug' ">
|
||||
<Optimize>true</Optimize>
|
||||
<CheckForOverflowUnderflow>true</CheckForOverflowUnderflow>
|
||||
<DebugType>full</DebugType>
|
||||
</PropertyGroup>
|
||||
|
||||
<PropertyGroup Condition=" '$(Configuration)' == 'Release' ">
|
||||
<Optimize>true</Optimize>
|
||||
<CheckForOverflowUnderflow>false</CheckForOverflowUnderflow>
|
||||
<DebugType>none</DebugType>
|
||||
</PropertyGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<Reference Include="Assembly-CSharp">
|
||||
<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>
|
||||
</Reference>
|
||||
<Reference Include="UnityEngine.CoreModule">
|
||||
<HintPath>..\dependencies\UnityEngine.CoreModule.dll</HintPath>
|
||||
</Reference>
|
||||
</ItemGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<PackageReference Include="Northwood.LabAPI" Version="1.0.2" />
|
||||
</ItemGroup>
|
||||
</Project>
|
||||
Reference in New Issue
Block a user