private function buildMatchAndTeamsResponse(Match $match) { if (is_null($this->match_logic)) { $this->match_logic = new MatchLogic(); } $match_id = $match->getMatchId(); //-- Build Response. Get current Match and check if we have teams $current_teams = $this->match_logic->getMatchTeams($match_id); $current_match = $match->toArray(); MatchUtilities::addMoreInfoToMatch($current_match); $api_response = ["match" => $current_match]; //-- Fetch Players In teams Again from DB TODO:: Maybe we can skip this db query with what we have in memory $all_players = $this->match_logic->getAllMatchPlayers($match_id); $players_in_teams = Utilities::getPlayersInTeams($all_players); if (count($current_teams) > 0) { foreach ($current_teams as $team) { $match_team_id = intval($team['match_team_id']); $api_response["teams"][$match_team_id] = array_merge($team, ["players" => $players_in_teams[$match_team_id]]); } } else { $api_response["players"] = $all_players; } return $api_response; }
public function addUserToTeamWithLessPlayers($user_id, $match_id) { $players_in_teams = Utilities::getPlayersInTeams($this->getAllMatchPlayers($match_id)); //-- We should Have two teams now $count_of_first_team_players = count(reset($players_in_teams)); $count_of_second_team_players = count(end($players_in_teams)); if ($count_of_first_team_players >= $count_of_second_team_players) { $add_to_match_team_id = array_keys($players_in_teams)[1]; } else { $add_to_match_team_id = array_keys($players_in_teams)[0]; } Match::assignMatchTeamIdToPlayers($match_id, $add_to_match_team_id, [$user_id]); }