R/utils.R

Defines functions get_player_by_id frames_to_ms ms_to_frames

Documented in frames_to_ms get_player_by_id ms_to_frames

# Utility functions

#' Convert a duration in milliseconds to a frame count
#' @param ms milliseconds
#' @param fps frame rate
#' @param floor whether to return the answer as maximum number of frames in the duration (T) or exact answer including partial frames (F)
#' @return frames in \code{ms}
#' @export
ms_to_frames <- function(ms, fps = 30, floor = T) {
  f <- fps * ms / 1000
  if (floor)
    floor(f)
  else
    f
}

#' Find the millisecond at which frames begin
#' @param frameNumbers frames to get the beginnings of
#' @param fps frame rate
#' @return millisecond at which each frame in frames begins
#' @export
frames_to_ms <- function(frameNumbers, fps = 30) {
  frameNumbers / fps * 1000
}


#' Return the player whose id matches id
#' @param id id of the player to fetch
#' @param players list of players
#' @return player or NA
get_player_by_id <- function(id, players) {
  for (player in players) {
    if (player$id == id)
      return(player)
  }
  return(NA)
}
mjaquiery/silly-smile-sim documentation built on Nov. 21, 2020, 9:15 a.m.