Привет! Мне нужно сделать систему игр с тем условие, что в одну и туже игру могут играть неограниченное кол-во игроков. Мне кажется, что текущее решение не очень хорошее. Сделай рефакторинг


public void OnGamesCommand(CCSPlayerController? player, CommandInfo commandInfo)
{
if (player.Team != CsTeam.Terrorist)
{
player.PrintToChat(“This command is only for inmates”);
return;
}

var players = Utilities.GetPlayers();

var inmates = players.Where(player => player.Team == CsTeam.Terrorist).ToList();

if (inmates.Count < 2)
{
player.PrintToChat(“Inmates count must be more or equal 2”);
return;
}

var menu = new ChatMenu(“Выберите игру”);
foreach (var game in _lrGames)
{
menu.AddMenuOption(game.Name, (controller, option) => { GameSelected(player, game, players); });
}

MenuManager.OpenChatMenu(player, menu);
}

private void GameSelected(CCSPlayerController inmate, LRGame lrGame, List<CCSPlayerController> players)
{
var menu = new ChatMenu(“Выберите кт”);

Server.PrintToConsole(“SelectMenu”);
var guardians = players.Where(player => player.Team == CsTeam.CounterTerrorist).ToList();
foreach (var guardian in guardians)
{
menu.AddMenuOption(guardian.PlayerName, (controller, option) =>
{
var newGame = (LRGame)Activator.CreateInstance(lrGame.GetType(), this);
newGame.Guardian = guardian;
newGame.Inmate = inmate;

RegisterAllAttributes(newGame);
newGame.Execute();
});
}

MenuManager.OpenChatMenu(inmate, menu);
}

Остальные классы:

public class LRGame : JailBreakObject
{
public virtual string Name { get; }
public bool IsEnd { get; private set; }

public CCSPlayerController Guardian { get; set; }
public CCSPlayerController Inmate { get; set; }


private Listeners.OnClientDisconnect _onClientDisconnect;
public LRGame(JailBreak jailBreak) : base(jailBreak)
{
jailBreak.RegisterEventHandler<EventPlayerDeath>(((@event, info) =>
{
var player = @event.Userid;

if (player == Guardian || player == Inmate)
{
End();
}

return HookResult.Continue;
}));
}

[GameEventHandler]
public HookResult EventRoundEnd(EventRoundEnd @event, GameEventInfo info)
{
End();
return HookResult.Continue;
}

[GameEventHandler]
public HookResult EventRoundStart(EventRoundStart @event, GameEventInfo info)
{
End();
return HookResult.Continue;
}

public void Execute()
{
_onClientDisconnect = OnClientDisconnect;
JailBreak.RegisterListener(_onClientDisconnect);

Start();
}

private void OnClientDisconnect(int slot)
{
if (slot == Inmate.Slot || slot == Guardian.Slot)
{
End();
}
}

protected virtual void Start()
{
}

protected void End()
{
JailBreak.RemoveListener(“OnClientDisconnect”, _onClientDisconnect);
IsEnd = true;

}
}

public class RouletteGame : LRGame
{
public override string Name => “Рулетка”;

public CBasePlayerWeapon[] _weapons = new CBasePlayerWeapon[2];

public RouletteGame(JailBreak jailBreak) : base(jailBreak)
{
}

[GameEventHandler]
public HookResult EventBulletImpact(EventBulletImpact @event, GameEventInfo info)
{
if (IsEnd)
return HookResult.Continue;


var player = @event.Userid;

if (player == Inmate)
{
Server.PrintToChatAll(“MORE22”);
Guardian.SetAmmo(1, 0);
}
else if (player == Guardian)
{
Server.PrintToChatAll(“MORE123”);
Inmate.SetAmmo(1, 0);
}

return HookResult.Continue;
}

protected override void Start()
{
Inmate.RemoveWeapons(false);
Guardian.RemoveWeapons(false);

var magicNumber = Random.Shared.Next(0, 2);

_weapons[0] = new CCSWeaponBaseGun(Inmate.GiveNamedItem(CsItem.DesertEagle));
_weapons[0].SetAmmo(magicNumber == 0 ? 1 : 0, 0);
_weapons[1] = new CCSWeaponBaseGun(Guardian.GiveNamedItem(CsItem.DesertEagle));
_weapons[1].SetAmmo(magicNumber == 0 ? 0 : 1, 0);
}
}