parameters: (conn, has_gpu=False)
return_statement: return util.response_success({
          "type": "game",
          "width": map_width,
          "height": map_height,
          "users": players,
          "challenge": challenge["id"],
      })
docstring: Find a set of players in a challenge.
docstring_summary: Find a set of players in a challenge.
docstring_tokens: Find a set of players in a challenge .
function: def find_challenge(conn, has_gpu=False):
      """
      Find a set of players in a challenge.
      """
      # Reset any stuck challenges
      reset_challenges(conn)
  
      challenge = conn.execute(
          model.challenges.select(
              (model.challenges.c.status == model.ChallengeStatus.CREATED.value) |
              # Also pick up any stuck challenges
              (
                  (model.challenges.c.status == model.ChallengeStatus.PLAYING_GAME.value) &
                  (model.challenges.c.most_recent_game_task <
                   datetime.datetime.now() - datetime.timedelta(minutes=30))
              )
          ).order_by(
              model.challenges.c.most_recent_game_task.asc()
          )
      ).first()
  
      if not challenge:
          return None
  
      player_filter = sqlalchemy.sql.expression.true()
      if not has_gpu:
          player_filter = (model.ranked_bots_users.c.is_gpu_enabled == False)
  
      total_players = conn.execute(model.total_ranked_users).first()[0]
      thresholds = util.tier_thresholds(total_players)
      ranked_users = model.ranked_users_query()
      bots_query = sqlalchemy.sql.select([
          model.ranked_bots_users.c.user_id,
          model.ranked_bots_users.c.bot_id,
          model.ranked_bots_users.c.username,
          model.ranked_bots_users.c.rank,
          model.ranked_bots_users.c.num_submissions.label("version_number"),
      ]).select_from(
          model.ranked_bots_users.join(
              model.bots,
              (model.ranked_bots_users.c.user_id == model.bots.c.user_id) &
              (model.ranked_bots_users.c.bot_id == model.bots.c.id)
          )
      ).where(
          (model.bots.c.compile_status == model.CompileStatus.SUCCESSFUL.value) &
          sqlalchemy.sql.exists(
              model.challenge_participants.select(
                  (model.ranked_bots.c.user_id == model.challenge_participants.c.user_id) &
                  (model.challenge_participants.c.challenge_id == challenge["id"]) &
                  (model.challenge_participants.c.user_id == model.ranked_bots_users.c.user_id)
              )
          ) &
          player_filter
      )
      bots = conn.execute(bots_query).fetchall()
  
      user_bots = collections.defaultdict(list)
      for bot in bots:
          user_bots[bot["user_id"]].append(bot)
  
      if len(user_bots) < 2 or challenge["issuer"] not in user_bots:
          # We had to skip this challenge, but give it a time anyways,
          # so that we don't get stuck on it
          conn.execute(model.challenges.update().values(
              most_recent_game_task=sqlalchemy.sql.func.now(),
          ).where(model.challenges.c.id == challenge["id"]))
  
          return None
  
      selected_bots = [random.choice(user_bots[challenge["issuer"]])]
      del user_bots[challenge["issuer"]]
  
      candidate_users = list(user_bots.keys())
      if len(user_bots) >= 3:
          # If 4-player challenge, always issue a 4-player game
          random.shuffle(candidate_users)
          selected_bots.extend([random.choice(user_bots[user_id])
                                for user_id in candidate_users[:3]])
      else:
          selected_bots.append(random.choice(user_bots[random.choice(candidate_users)]))
  
      map_width, map_height = rand_map_size()
      players = [{
          "user_id": player["user_id"],
          "bot_id": player["bot_id"],
          "username": player["username"],
          "version_number": player["version_number"],
          "rank": player["rank"],
          "tier": util.tier(player["rank"], total_players),
      } for player in selected_bots]
  
      conn.execute(model.challenges.update().values(
          status=model.ChallengeStatus.PLAYING_GAME.value,
          most_recent_game_task=sqlalchemy.sql.func.now(),
      ).where(model.challenges.c.id == challenge["id"]))
  
      return util.response_success({
          "type": "game",
          "width": map_width,
          "height": map_height,
          "users": players,
          "challenge": challenge["id"],
      })
function_tokens: def find_challenge ( conn , has_gpu = False ) : # Reset any stuck challenges reset_challenges ( conn ) challenge = conn . execute ( model . challenges . select ( ( model . challenges . c . status == model . ChallengeStatus . CREATED . value ) | # Also pick up any stuck challenges ( ( model . challenges . c . status == model . ChallengeStatus . PLAYING_GAME . value ) & ( model . challenges . c . most_recent_game_task < datetime . datetime . now ( ) - datetime . timedelta ( minutes = 30 ) ) ) ) . order_by ( model . challenges . c . most_recent_game_task . asc ( ) ) ) . first ( ) if not challenge : return None player_filter = sqlalchemy . sql . expression . true ( ) if not has_gpu : player_filter = ( model . ranked_bots_users . c . is_gpu_enabled == False ) total_players = conn . execute ( model . total_ranked_users ) . first ( ) [ 0 ] thresholds = util . tier_thresholds ( total_players ) ranked_users = model . ranked_users_query ( ) bots_query = sqlalchemy . sql . select ( [ model . ranked_bots_users . c . user_id , model . ranked_bots_users . c . bot_id , model . ranked_bots_users . c . username , model . ranked_bots_users . c . rank , model . ranked_bots_users . c . num_submissions . label ( "version_number" ) , ] ) . select_from ( model . ranked_bots_users . join ( model . bots , ( model . ranked_bots_users . c . user_id == model . bots . c . user_id ) & ( model . ranked_bots_users . c . bot_id == model . bots . c . id ) ) ) . where ( ( model . bots . c . compile_status == model . CompileStatus . SUCCESSFUL . value ) & sqlalchemy . sql . exists ( model . challenge_participants . select ( ( model . ranked_bots . c . user_id == model . challenge_participants . c . user_id ) & ( model . challenge_participants . c . challenge_id == challenge [ "id" ] ) & ( model . challenge_participants . c . user_id == model . ranked_bots_users . c . user_id ) ) ) & player_filter ) bots = conn . execute ( bots_query ) . fetchall ( ) user_bots = collections . defaultdict ( list ) for bot in bots : user_bots [ bot [ "user_id" ] ] . append ( bot ) if len ( user_bots ) < 2 or challenge [ "issuer" ] not in user_bots : # We had to skip this challenge, but give it a time anyways, # so that we don't get stuck on it conn . execute ( model . challenges . update ( ) . values ( most_recent_game_task = sqlalchemy . sql . func . now ( ) , ) . where ( model . challenges . c . id == challenge [ "id" ] ) ) return None selected_bots = [ random . choice ( user_bots [ challenge [ "issuer" ] ] ) ] del user_bots [ challenge [ "issuer" ] ] candidate_users = list ( user_bots . keys ( ) ) if len ( user_bots ) >= 3 : # If 4-player challenge, always issue a 4-player game random . shuffle ( candidate_users ) selected_bots . extend ( [ random . choice ( user_bots [ user_id ] ) for user_id in candidate_users [ : 3 ] ] ) else : selected_bots . append ( random . choice ( user_bots [ random . choice ( candidate_users ) ] ) ) map_width , map_height = rand_map_size ( ) players = [ { "user_id" : player [ "user_id" ] , "bot_id" : player [ "bot_id" ] , "username" : player [ "username" ] , "version_number" : player [ "version_number" ] , "rank" : player [ "rank" ] , "tier" : util . tier ( player [ "rank" ] , total_players ) , } for player in selected_bots ] conn . execute ( model . challenges . update ( ) . values ( status = model . ChallengeStatus . PLAYING_GAME . value , most_recent_game_task = sqlalchemy . sql . func . now ( ) , ) . where ( model . challenges . c . id == challenge [ "id" ] ) ) return util . response_success ( { "type" : "game" , "width" : map_width , "height" : map_height , "users" : players , "challenge" : challenge [ "id" ] , } )