From 9bf08797f7df3d21fc401f33c0dc799dbd8842a6 Mon Sep 17 00:00:00 2001 From: z411 Date: Mon, 7 Nov 2022 11:56:55 -0300 Subject: [PATCH] Added equality checker for TeamBalance --- qcsrc/server/teamplay.qc | 36 +++++++++++++++++++++++++++++++----- qcsrc/server/teamplay.qh | 3 +++ 2 files changed, 34 insertions(+), 5 deletions(-) diff --git a/qcsrc/server/teamplay.qc b/qcsrc/server/teamplay.qc index d5040295a..46f9d5b1a 100644 --- a/qcsrc/server/teamplay.qc +++ b/qcsrc/server/teamplay.qc @@ -234,12 +234,13 @@ bool Player_SetTeamIndex(entity player, int index) return true; } -entity SpectatorWantsJoin(entity this) +bool IsQueueNeeded(entity ignore) { - // Check if there are spectators waiting to join - if(!teamplay) return NULL; + return (teamplay && TeamBalance_AreEqual(ignore)); +} - //FOREACH_CLIENT(IS_SPEC(it) || IS_OBSERVER(it), { +entity SpectatorWantsJoin(entity this) +{ FOREACH_CLIENT(IS_REAL_CLIENT(it), { if(it == this) continue; if(CS(it).wants_join) { @@ -273,10 +274,13 @@ bool SetPlayerTeam(entity player, int team_index, int type) if (team_index != -1) { - if (!SpectatorWantsJoin(player)) + LOG_INFOF("Is queue needed %d", IsQueueNeeded(player)); + if (IsQueueNeeded(player) && !SpectatorWantsJoin(player)) { Send_Notification(NOTIF_ALL, NULL, MSG_INFO, APP_TEAM_NUM(player.team, INFO_JOIN_WANTS_TEAM), player.netname); CS(player).wants_join = true; // TODO : Refactor + bool res = TeamBalance_AreEqual(player); + LOG_INFOF("result %d", res); } else Send_Notification(NOTIF_ALL, NULL, MSG_INFO, APP_TEAM_NUM(player.team, INFO_JOIN_PLAY_TEAM), player.netname); @@ -686,6 +690,28 @@ int TeamBalance_GetAllowedTeams(entity balance) return result; } +bool TeamBalance_AreEqual(entity ignore) +{ + entity balance = TeamBalance_CheckAllowedTeams(ignore); + TeamBalance_GetTeamCounts(balance, ignore); + + int total; + int prev_total = 0; + + for(int i = 0; i < AVAILABLE_TEAMS; i++) + { + total = TeamBalance_GetTeamFromIndex(balance, i+1).m_num_players; + if(i > 0 && total != prev_total) + { + TeamBalance_Destroy(balance); + return false; + } + prev_total = total; + } + TeamBalance_Destroy(balance); + return true; +} + bool TeamBalance_IsTeamAllowed(entity balance, int index) { if (balance == NULL) diff --git a/qcsrc/server/teamplay.qh b/qcsrc/server/teamplay.qh index c79e84bff..f50808ff4 100644 --- a/qcsrc/server/teamplay.qh +++ b/qcsrc/server/teamplay.qh @@ -113,6 +113,7 @@ enum TEAM_CHANGE_SPECTATOR = 4 ///< Player is joining spectators. //TODO: Remove? }; +bool IsQueueNeeded(entity ignore); entity SpectatorWantsJoin(entity this); /// \brief Sets the team of the player. @@ -185,6 +186,8 @@ void TeamBalance_Destroy(entity balance); /// \return Bitmask of allowed teams. int TeamBalance_GetAllowedTeams(entity balance); +bool TeamBalance_AreEqual(entity ignore); + /// \brief Returns whether the team change to the specified team is allowed. /// \param[in] balance Team balance entity. /// \param[in] index Index of the team. -- 2.39.2