R/get_complete_details.R

Defines functions get_complete_details

Documented in get_complete_details

#' Get the return of get_details for a whole game
#'
#' This function automatically scans the initial return of
#' `get_window()` for a given gameId for relevant timestamps,
#' which are utilized in another query to function, giving
#' another timestamp. This continues until no new data can be
#' generated by the query, indicating that a game is over.
#'
#' @param gameId string. The ID of the game for which data should be searched.
#' @param hl string. Locale or language code using ISO 639-1 and ISO 3166-1 alpha-2.
#'
#' @return A list of a game data.frame and associated meta data.
#' @export
get_complete_details <- function(gameId,
                               hl = "en-US") {

  details <- get_details(gameId, hl = hl)

  start_window <- details

  curr_time <- utils::tail(start_window$timestamp, 1) %>%
    lubridate::ymd_hms() %>%
    lubridate::round_date(unit="10s")
  last_time <- curr_time

  complete_data <- start_window
  game_still_going <- TRUE

  start_time <- Sys.time()
  while(game_still_going) {

    curr_win <- get_details(gameId, startingTime = curr_time)

    curr_time <- max(curr_win$timestamp) %>%
      lubridate::ymd_hms() %>%
      lubridate::round_date(unit="10s")

    if(curr_time == last_time) {
      curr_time <- curr_time + 10
    }

    last_time <- curr_time

    if(max(curr_win$timestamp) <= max(complete_data$timestamp)) {
      game_still_going <- FALSE
      break
    }
    game_still_going <- !any(curr_win$gamestate == "finished")

    complete_data <- dplyr::bind_rows(complete_data, curr_win)
  }

  difft <-round(difftime(Sys.time(), start_time, units = "mins"), 4)
  print(paste0("Game done, duration: ", difft, "mins"))
  return(
    complete_data
  )
}
flsck/rlolesports documentation built on April 4, 2021, 6:59 p.m.