To extend the GetBy and Find methods, we can add additional methods that allow searching and filtering based on different criteria. Here are some commonly used criteria that can be implemented for players in a game context:
- GetByControllerId: Allows extraction of players by their controller's ID.
- GetByScore: Filters players based on their scores.
- FindByPlayerName: Finds a player by their name.
Below is an updated version of your class with these additional methods:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using CounterStrikeSharp.API.Modules.Entities;
using CounterStrikeSharp.API.Modules.Utils;
namespace CounterStrikeSharp.API.Core.Players
{
public abstract class PluginPlayerList<T> : IPlayersEventsHandler where T : PluginPlayerInfo
{
public event Action<T>? ClientConnected;
public event Action<T>? ClientAuthorized;
public event Action<T>? ClientDisconnected;
private readonly T?[] _players = new T?[66];
public IEnumerable<T> Players => _players.Where(p => p != null)!;
public IEnumerable<T> AuthorizedPlayers => Players.Where(p => p.SteamId != null);
[EditorBrowsable(EditorBrowsableState.Never)]
public void Initialize(IEnumerable<BasicPlayerInfo> players)
{
if (players == null) throw new ArgumentNullException(nameof(players));
foreach (var info in players)
{
HandleClientConnection(info);
}
}
private void HandleClientConnection(BasicPlayerInfo info)
{
if (info == null) throw new ArgumentNullException(nameof(info));
OnClientConnected(info);
if (info.SteamId != null)
OnClientAuthorized(info);
}
[EditorBrowsable(EditorBrowsableState.Never)]
public void OnClientConnected(BasicPlayerInfo info)
{
if (info == null) throw new ArgumentNullException(nameof(info));
if (info.Slot < 0 || info.Slot >= _players.Length) throw new ArgumentOutOfRangeException(nameof(info.Slot));
var player = CreatePlayer(info);
_players[info.Slot] = player;
ClientConnected?.Invoke(player);
}
[EditorBrowsable(EditorBrowsableState.Never)]
public void OnClientAuthorized(BasicPlayerInfo info)
{
if (info == null) throw new ArgumentNullException(nameof(info));
var player = _players[info.Slot];
if (player != null && player.SteamId != null)
{
ClientAuthorized?.Invoke(player);
}
}
[EditorBrowsable(EditorBrowsableState.Never)]
public void OnClientDisconnected(BasicPlayerInfo info)
{
if (info == null) throw new ArgumentNullException(nameof(info));
var player = _players[info.Slot];
_players[info.Slot] = null; // Ensure player slot is cleared upon disconnection
ClientDisconnected?.Invoke(player!);
}
protected abstract T CreatePlayer(BasicPlayerInfo info);
public IEnumerable<T> GetByTeam(CsTeam team)
{
return Players.Where(p => p.Controller.Team == team);
}
public T? FindBySteamId(SteamID steamId)
{
return Players.FirstOrDefault(p => p.SteamId == steamId);
}
public IEnumerable<T> GetByControllerId(Guid controllerId)
{
return Players.Where(p => p.Controller.Id == controllerId);
}
public IEnumerable<T> GetByScore(int score)
{
// Assuming `Score` is a property in `PluginPlayerInfo`
return Players.Where(p => p.Score == score);
}
public T? FindByPlayerName(string playerName)
{
if (string.IsNullOrWhiteSpace(playerName)) throw new ArgumentException("Player name cannot be null or whitespace.", nameof(playerName));
return Players.FirstOrDefault(p => string.Equals(p.PlayerName, playerName, StringComparison.OrdinalIgnoreCase));
}
public T? this[int i]
{
get
{
if (i < 0 || i >= _players.Length) throw new ArgumentOutOfRangeException(nameof(i));
return _players[i];
}
}
public T? this[CCSPlayerController controller]
{
get
{
if (controller == null) throw new ArgumentNullException(nameof(controller));
return this[controller.Slot];
}
}
}
}
New Methods Overview:
- GetByControllerId: Returns players matching the specified controller ID.
- GetByScore: Filters players by their score, assuming
Score is correctly defined in PluginPlayerInfo.
- FindByPlayerName: Searches for a player by name. It utilizes case-insensitive string comparison for robustness.
Ensure that any new properties or methods used (like Controller.Id, Score, or PlayerName) are correctly defined in the base PluginPlayerInfo class or its subclasses. These extensions provide more comprehensive player search and filtering capabilities to your PluginPlayerList and cater to a variety of query needs.