public static function searchMatch($player_id, $player_bet_rating) { $response = []; $br_upper = $player_bet_rating + 300; $br_lower = $player_bet_rating - 300; //query for available matches within the bet range of the player $matches = DB::select(' SELECT id FROM Matches WHERE players_matched = 0 and p1_id != :player_id and p1_bet_rating >= :br_lower and p1_bet_rating <= :br_upper ', ['player_id' => $player_id, 'br_lower' => $br_lower, 'br_upper' => $br_upper]); //if no match is found call create, else add player to an existing match as player2 if (empty($matches)) { $result = Matches::createMatch($player_id, $player_bet_rating); if (!$result) { die("Error: Could not create a new match record"); } else { $response['matchFound'] = false; $response['matchID'] = $result; $response['playerRoll'] = 1; } } else { $match_id = $matches[rand(0, count($matches) - 1)]->id; $result = Matches::joinMatch($player_id, $player_bet_rating, $match_id); if (!$result) { die("Error: Could not join the match..."); } else { $response['matchFound'] = true; $response['matchID'] = $match_id; $response['playerRoll'] = 2; } } return $response; }