Skip to content

Commit

Permalink
Merge pull request #47 from B3none/update-queue-updating
Browse files Browse the repository at this point in the history
Updated QueueManager logic for VIP players
  • Loading branch information
B3none authored Jan 23, 2024
2 parents 517d2ff + 72c5425 commit 79b4b64
Show file tree
Hide file tree
Showing 5 changed files with 75 additions and 54 deletions.
4 changes: 0 additions & 4 deletions Modules/Configs/MapConfig.cs
Original file line number Diff line number Diff line change
Expand Up @@ -78,8 +78,6 @@ public bool AddSpawn(Spawn spawn)
_mapConfigData.Spawns.Add(spawn);

Save();

// TODO: Figure out why the spawns can't be added on the fly.
Load();

return true;
Expand All @@ -97,8 +95,6 @@ public bool RemoveSpawn(Spawn spawn)
_mapConfigData.Spawns.Remove(spawn);

Save();

// TODO: Figure out why the spawns can't be added on the fly.
Load();

return true;
Expand Down
10 changes: 2 additions & 8 deletions Modules/Managers/GameManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -205,17 +205,11 @@ private void SetTeams(List<CCSPlayerController>? terrorists, List<CCSPlayerContr
{
if (terrorists.Contains(player))
{
if (player.Team != CsTeam.Terrorist)
{
player.SwitchTeam(CsTeam.Terrorist);
}
player.SwitchTeam(CsTeam.Terrorist);
}
else if (counterTerrorists.Contains(player))
{
if (player.Team != CsTeam.CounterTerrorist)
{
player.SwitchTeam(CsTeam.CounterTerrorist);
}
player.SwitchTeam(CsTeam.CounterTerrorist);
}
}
}
Expand Down
93 changes: 60 additions & 33 deletions Modules/Managers/QueueManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ public HookResult PlayerJoinedTeam(CCSPlayerController player, CsTeam fromTeam,
{
Console.WriteLine($"{RetakesPlugin.LogPrefix}[{player.PlayerName}] Switching to spectator.");
RemovePlayerFromQueues(player);
Helpers.CheckRoundDone();
return HookResult.Continue;
}

Expand All @@ -72,7 +73,7 @@ public HookResult PlayerJoinedTeam(CCSPlayerController player, CsTeam fromTeam,
Console.WriteLine($"{RetakesPlugin.LogPrefix}[{player.PlayerName}] player is not in round list for {toTeam}, switching to spectator.");
ActivePlayers.Remove(player);
QueuePlayers.Add(player);

if (player.PawnIsAlive)
{
player.CommitSuicide(false, true);
Expand Down Expand Up @@ -100,7 +101,6 @@ public HookResult PlayerJoinedTeam(CCSPlayerController player, CsTeam fromTeam,
Console.WriteLine($"{RetakesPlugin.LogPrefix}[{player.PlayerName}] Not found, adding to QueuePlayers.");
player.PrintToChat($"{RetakesPlugin.MessagePrefix}{_translator["queue.joined"]}");
QueuePlayers.Add(player);
return HookResult.Handled;
}
else
{
Expand Down Expand Up @@ -136,66 +136,93 @@ private void RemoveDisconnectedPlayers()

private void HandleQueuePriority()
{
var vipsInQueue = QueuePlayers.Where(player => AdminManager.PlayerHasPermissions(player, "@css/vip")).ToList()
.Count;
Console.WriteLine($"{RetakesPlugin.LogPrefix}handling queue priority.");
if (ActivePlayers.Count != _maxRetakesPlayers)
{
Console.WriteLine($"{RetakesPlugin.LogPrefix}ActivePlayers.Count != _maxRetakesPlayers, returning.");
return;
}

var vipQueuePlayers = QueuePlayers.Where(player => AdminManager.PlayerHasPermissions(player, "@css/vip")).ToList();

if (vipsInQueue > 0)
if (vipQueuePlayers.Count <= 0)
{
Helpers.Shuffle(
ActivePlayers
.Where(player => !AdminManager.PlayerHasPermissions(player, "@css/vip"))
.ToList()
)
.ForEach(player =>
{
if (vipsInQueue <= 0)
{
return;
}

ActivePlayers.Remove(player);
QueuePlayers.Add(player);
vipsInQueue--;
});
Console.WriteLine($"{RetakesPlugin.LogPrefix}No VIP players found in queue, returning.");
return;
}

// loop through vipQueuePlayers and add them to ActivePlayers
foreach (var vipQueuePlayer in vipQueuePlayers)
{
// If the player is no longer valid, skip them
if (!Helpers.IsValidPlayer(vipQueuePlayer))
{
continue;
}

// TODO: We shouldn't really shuffle here, implement a last in first out queue instead.
var nonVipActivePlayers = Helpers.Shuffle(
ActivePlayers
.Where(player => !AdminManager.PlayerHasPermissions(player, "@css/vip"))
.ToList()
);

if (nonVipActivePlayers.Count == 0)
{
Console.WriteLine($"{RetakesPlugin.LogPrefix}No non-VIP players found in ActivePlayers, returning.");
break;
}

var nonVipActivePlayer = nonVipActivePlayers.First();

// Switching them to spectator will automatically remove them from the queue
nonVipActivePlayer.ChangeTeam(CsTeam.Spectator);
ActivePlayers.Remove(nonVipActivePlayer);
QueuePlayers.Add(nonVipActivePlayer);
nonVipActivePlayer.PrintToChat($"{RetakesPlugin.MessagePrefix}{_translator["queue.replaced_by_vip", vipQueuePlayer.PlayerName]}");

// Add the new VIP player to ActivePlayers and remove them from QueuePlayers
ActivePlayers.Add(vipQueuePlayer);
QueuePlayers.Remove(vipQueuePlayer);
vipQueuePlayer.ChangeTeam(CsTeam.CounterTerrorist);
vipQueuePlayer.PrintToChat($"{RetakesPlugin.MessagePrefix}{_translator["queue.vip_took_place", nonVipActivePlayer.PlayerName]}");
}
}

public void Update()
{
RemoveDisconnectedPlayers();
HandleQueuePriority();


Console.WriteLine($"{RetakesPlugin.LogPrefix}{_maxRetakesPlayers} max players, {ActivePlayers.Count} active players, {QueuePlayers.Count} players in queue.");
Console.WriteLine($"{RetakesPlugin.LogPrefix}players to add: {_maxRetakesPlayers - ActivePlayers.Count}");
var playersToAdd = _maxRetakesPlayers - ActivePlayers.Count;

if (playersToAdd > 0 && QueuePlayers.Count > 0)
{
Console.WriteLine($"{RetakesPlugin.LogPrefix} inside if - this means the game has players to add and players in the queue.");
// Take players from QueuePlayers and add them to ActivePlayers
// Ordered by players with @retakes/queue group first since they
// Ordered by players with @css/vip group first since they
// have queue priority.
var playersToAddList = QueuePlayers
.OrderBy(player => AdminManager.PlayerHasPermissions(player, "@css/vip"))
.OrderBy(player => AdminManager.PlayerHasPermissions(player, "@css/vip") ? 1 : 0)
.Take(playersToAdd)
.ToList();

QueuePlayers.RemoveWhere(playersToAddList.Contains);

// loop players to add, and set their team to CT
foreach (var player in playersToAddList)
{
// If the player is no longer valid, skip them
if (!Helpers.IsValidPlayer(player))
{
continue;
}

ActivePlayers.Add(player);

if (player.Team != CsTeam.CounterTerrorist)
{
player.SwitchTeam(CsTeam.CounterTerrorist);
}
player.ChangeTeam(CsTeam.CounterTerrorist);
}
}

HandleQueuePriority();

if (ActivePlayers.Count == _maxRetakesPlayers && QueuePlayers.Count > 0)
{
Expand Down
20 changes: 11 additions & 9 deletions RetakesPlugin.cs
Original file line number Diff line number Diff line change
Expand Up @@ -373,14 +373,7 @@ public HookResult OnPlayerConnectFull(EventPlayerConnectFull @event, GameEventIn

player.TeamNum = (int)CsTeam.Spectator;
player.ForceTeamTime = 3600.0f;

// TODO: Remove this once we know it's working.
// Console.WriteLine($"{LogPrefix}OnPlayerConnectFull event fired. {Utilities.GetPlayers().ToList().Count} players connected.");
// if (Utilities.GetPlayers().Where(Helpers.IsPlayerConnected).ToList().Count <= 2)
// {
// Console.WriteLine($"{LogPrefix}First or second player connected, resetting game.");
// Helpers.RestartGame();
// }
player.ExecuteClientCommand("teammenu");

return HookResult.Continue;
}
Expand Down Expand Up @@ -438,6 +431,15 @@ public HookResult OnRoundPreStart(EventRoundPrestart @event, GameEventInfo info)
[GameEventHandler]
public HookResult OnRoundStart(EventRoundStart @event, GameEventInfo info)
{
// TODO: FIGURE OUT WHY THE FUCK I NEED TO DO THIS
var weirdAliveSpectators = Utilities.GetPlayers().Where(x => x is { TeamNum: < (int)CsTeam.Terrorist, PawnIsAlive: true });
foreach (var weirdAliveSpectator in weirdAliveSpectators)
{
// I **think** it's caused by auto team balance being on, so turn it off
Server.ExecuteCommand("mp_autoteambalance 0");
weirdAliveSpectator.CommitSuicide(false, true);
}

// If we are in warmup, skip.
if (Helpers.GetGameRules().WarmupPeriod)
{
Expand Down Expand Up @@ -553,7 +555,7 @@ public HookResult OnPlayerSpawn(EventPlayerSpawn @event, GameEventInfo info)

var player = @event.Userid;

if (!Helpers.IsValidPlayer(player) || !Helpers.IsPlayerConnected(player) || Helpers.GetGameRules().WarmupPeriod)
if (!Helpers.IsValidPlayer(player) || !Helpers.IsPlayerConnected(player))
{
return HookResult.Continue;
}
Expand Down
2 changes: 2 additions & 0 deletions lang/en.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
{
"queue.joined": "You have been placed in the waiting queue.",
"queue.waiting": "The game is currently full with [GREEN]{0}[NORMAL] players. You are in the waiting queue.",
"queue.replaced_by_vip": "You have been replaced by VIP player [GREEN]{0}[NORMAL].",
"queue.vip_took_place": "You have taken [GREEN]{0}[NORMAL]'s place in the game.",

"bombsite.announcement": "Retake [GREEN]{0}[NORMAL]: [LIGHT_RED]{1} Ts[NORMAL] vs [PURPLE]{2} CTs",

Expand Down

0 comments on commit 79b4b64

Please sign in to comment.