diff --git a/NAMESPACE b/NAMESPACE index 88643054..7de4d620 100644 --- a/NAMESPACE +++ b/NAMESPACE @@ -21,6 +21,7 @@ export(league_status) export(league_teams) export(live_scoring) export(moves_summary) +export(player_acquire) export(player_info) export(player_news) export(player_outlook) diff --git a/R/acquire.R b/R/acquire.R new file mode 100644 index 00000000..5d09371b --- /dev/null +++ b/R/acquire.R @@ -0,0 +1,48 @@ +#' Roster acquisition history +#' +#' The date and method of each player's acquisition onto a fantasy roster. +#' +#' @inheritParams draft_picks +#' @return A data frame of roster players with acquisition method and date. +#' @examples +#' player_acquire(leagueId = "42654852") +#' @importFrom tibble tibble +#' @export +player_acquire <- function(leagueId = ffl_id(), leagueHistory = FALSE, ...) { + dat <- ffl_api(leagueId, leagueHistory, view = c("mRoster", "mTeam"), ...) + if (leagueHistory | length(list(...) > 0)) { + stop("Acqusition data is only available for current season and period") + } + tm <- out_team(dat$teams) + e <- dat$teams$roster$entries + out <- rep(list(NA), length(e)) + names(out) <- tm$abbrev + for (i in seq_along(e)) { + out[[i]] <- parse_acquire( + entry = e[[i]], + t = tm, + y = dat$seasonId, + w = dat$scoringPeriodId + ) + out[[i]]$onTeamId <- tm$abbrev[i] + } + return(out) +} + +parse_acquire <- function(entry, t, y, w) { + player <- entry$playerPoolEntry$player + x <- tibble::tibble( + seasonId = y, + scoringPeriodId = w, + onTeamId = entry$playerPoolEntry$onTeamId, + lineupSlotId = slot_abbrev(entry$lineupSlotId), + id = player$id, + firstName = player$firstName, + lastName = player$lastName, + proTeamId = pro_abbrev(player$proTeamId), + defaultPositionId = pos_abbrev(player$defaultPositionId), + acquisitionType = entry$acquisitionType, + acquisitionDate = ffl_date(entry$acquisitionDate) + ) + x[order(x$lineupSlotId), ] +} diff --git a/man/player_acquire.Rd b/man/player_acquire.Rd new file mode 100644 index 00000000..b82191cf --- /dev/null +++ b/man/player_acquire.Rd @@ -0,0 +1,27 @@ +% Generated by roxygen2: do not edit by hand +% Please edit documentation in R/acquire.R +\name{player_acquire} +\alias{player_acquire} +\title{Roster acquisition history} +\usage{ +player_acquire(leagueId = ffl_id(), leagueHistory = FALSE, ...) +} +\arguments{ +\item{leagueId}{Numeric league ID or ESPN fantasy page URL. Defaults to +\code{getOption("fflr.leagueId")}. Function fails if no ID is found.} + +\item{leagueHistory}{logical; Should the \code{leagueHistory} version of the API +be called? If \code{TRUE}, a list of results is returned, with one element for +each historical year of the league.} + +\item{...}{Arguments passed to \code{\link[jsonlite:fromJSON]{jsonlite::fromJSON()}} for parsing.} +} +\value{ +A data frame of roster players with acquisition method and date. +} +\description{ +The date and method of each player's acquisition onto a fantasy roster. +} +\examples{ +player_acquire(leagueId = "42654852") +}