using System.Numerics;
using CounterStrikeSharp.API;
using CounterStrikeSharp.API.Core;
using CounterStrikeSharp.API.Modules.Commands;
using PubgMod.Players;
using Vector = CounterStrikeSharp.API.Modules.Utils.Vector;

namespace PubgMod.Main.Game.Arenas;

public class ArenasManager
{
    private readonly List<PubgPlayer> _arenaPlayersQueue = new();
    private readonly List<Arena> _arenas = new();
    private readonly PlayersManager _playersManager;

    public ArenasManager(PubgPlugin plugin, PlayersManager playersManager)
    {
        _playersManager = playersManager;
        plugin.AddCommand("css_1v1", "start 1v1", OnCommand);

        plugin.RegisterEventHandler<EventRoundStart>(OnRoundStart);
        plugin.RegisterEventHandler<EventPlayerDeath>(OnPlayerDeath, HookMode.Pre);
        plugin.RegisterListener<Listeners.OnMapStart>(OnMapStart);

        playersManager.OnPlayerDisconnect += OnPlayerDisconnect;
    }

    private HookResult OnPlayerDeath(EventPlayerDeath @event, GameEventInfo info)
    {
        var victim = @event.Userid;
        if (victim == null)
            return HookResult.Continue;

        var victimPlayer = _playersManager[victim];
        var arena = victimPlayer?.Arena;
        if (arena == null)
            return HookResult.Continue;

        var opponent = arena.GetOpponent(victimPlayer!)!;
        opponent.PrintToChat($"You win {victim.PlayerName}");
        victimPlayer!.PrintToChat($"You lost to {opponent.Controller.PlayerName}");

        arena.EndGame();
        
        info.DontBroadcast = true;
        return HookResult.Changed;
    }

    private void OnPlayerDisconnect(PubgPlayer player)
    {
        _arenaPlayersQueue.RemoveAll(p => p == player);
    }

    private HookResult OnRoundStart(EventRoundStart @event, GameEventInfo info)
    {
        foreach (var player in _playersManager.LoadedPlayers)
        {
            player!.Arena = null;
        }

        _arenaPlayersQueue.Clear();
        return HookResult.Continue;
    }

    private void OnMapStart(string map)
    {
        const string arenaPrefix = "ar_spawn";

        _arenas.Clear();

        var arenaPoints = new Dictionary<int, Vector[]>();
        foreach (var entityInstance in Utilities.GetAllEntities())
        {
            var entity = entityInstance.Entity;
            if (entity == null)
                continue;

            var name = entity.Name;
            if (name.StartsWith(arenaPrefix))
            {
                var position = entityInstance.As<CBaseEntity>().AbsOrigin!;
                var positionCopy = new Vector(position.X, position.Y, position.Z);
                var vals = name[arenaPrefix.Length..].Split("_");
                var arenaIndex = int.Parse(vals[0]);

                if (arenaPoints.TryGetValue(arenaIndex, out var points))
                    points[1] = positionCopy;
                else
                    arenaPoints[arenaIndex] = [positionCopy, default];
            }
        }

        foreach (var points in arenaPoints.Values)
        {
            _arenas.Add(new Arena(points[0], points[1]));
        }
    }

    private void OnCommand(CCSPlayerController? controller, CommandInfo info)
    {
        var player = _playersManager[controller!]!;
        if (_arenaPlayersQueue.Any(p => p == player))
        {
            player.PrintToChat("You're already in the queue");
            return;
        }

        AddToQueue(player);
    }

    private void AddToQueue(PubgPlayer player)
    {
        _arenaPlayersQueue.Add(player);
    }

    private void TryStartGame()
    {
    }
}



using CounterStrikeSharp.API.Modules.Utils;
using PubgMod.Players;

namespace PubgMod.Main.Game.Arenas;

public class Arena
{
    public bool Occupied => Player1 != null || Player2 != null;

    public PubgPlayer? Player1 { get; private set; }
    public PubgPlayer? Player2 { get; private set; }

    private readonly ArenasManager _manager;
    private readonly Vector _pos1;
    private readonly Vector _pos2;

    public Arena(ArenasManager manager, Vector pos1, Vector pos2)
    {
        _manager = manager;
        _pos1 = pos1;
        _pos2 = pos2;
    }

    public void StartGame(PubgPlayer player1, PubgPlayer player2)
    {
        Player1 = player1;
        Player2 = player2;

        player1.Controller.Teleport(_pos1);
        player2.Controller.Teleport(_pos2);
    }

    public PubgPlayer? GetOpponent(PubgPlayer player)
    {
        if (!Occupied)
            return null!;

        if (Player1 == player)
            return Player2;

        if (Player2 == player)
            return Player1;

        return null;
    }

    public void EndGame()
    {
        Player1 = null;
        Player2 = null;
    }
}


когда игра на арене кончается, мне нужно проверять если у игроков вклюен autoqueue, то заново кидать их в очередь на след.кадре, вопрос, какой класс должен это делать, ArenaManager или Arena