]> git.rm.cloudns.org Git - xonotic/xonotic-data.pk3dir.git/commitdiff
Added equality checker for TeamBalance
authorz411 <z411@omaera.org>
Mon, 7 Nov 2022 14:56:55 +0000 (11:56 -0300)
committerz411 <z411@omaera.org>
Mon, 7 Nov 2022 14:56:55 +0000 (11:56 -0300)
qcsrc/server/teamplay.qc
qcsrc/server/teamplay.qh

index d5040295abd16c640859cbca21bc672d297fd5fa..46f9d5b1accbba49d99420a1f1b6a88f4f47311d 100644 (file)
@@ -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)
index c79e84bffaa6ad5beff45c71f2a3923ccdf960e3..f50808ff436501815190e8bd8eb3d14f87fac2fb 100644 (file)
@@ -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.