-
Notifications
You must be signed in to change notification settings - Fork 7
/
acquire.R
51 lines (50 loc) · 1.51 KB
/
acquire.R
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
#' Roster acquisition history
#'
#' The date and method of each player's acquisition onto a fantasy roster.
#'
#' @inheritParams ffl_api
#' @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")
}
if (is_predraft(dat)) {
return(data.frame())
}
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,
teamId = entry$playerPoolEntry$onTeamId,
lineupSlot = slot_abbrev(entry$lineupSlotId),
playerId = player$id,
firstName = player$firstName,
lastName = player$lastName,
proTeam = pro_abbrev(player$proTeamId),
position = pos_abbrev(player$defaultPositionId),
acquisitionType = entry$acquisitionType,
acquisitionDate = ffl_date(entry$acquisitionDate)
)
x[order(x$lineupSlot), ]
}