-
Notifications
You must be signed in to change notification settings - Fork 7
/
standings.R
106 lines (103 loc) · 2.99 KB
/
standings.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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
#' League standings
#'
#' Return the current and projected standings, win streak, total wins, losses,
#' and points scored for and against each team.
#'
#' @inheritParams ffl_api
#' @return A data frame of team standings.
#' @examples
#' league_standings(leagueId = "42654852")
#' @importFrom tibble tibble
#' @family league functions
#' @export
league_standings <- function(leagueId = ffl_id(), leagueHistory = FALSE, ...) {
dat <- ffl_api(
leagueId,
leagueHistory,
view = c("mTeam", "mStandings", "mSettings"),
...
)
if (leagueHistory) {
out <- rep(list(NA), length(dat$teams))
for (i in seq_along(dat$members)) {
out[[i]] <- parse_ranks(
teams = dat$teams[[i]],
y = dat$seasonId[i],
w = dat$scoringPeriodId[i]
)
}
} else {
out <- parse_ranks(
teams = dat$teams,
y = dat$seasonId,
w = dat$scoringPeriodId
)
}
return(out)
}
parse_ranks <- function(teams, y = NULL, w = NULL) {
tibble::tibble(
seasonId = y,
scoringPeriodId = w,
teamId = teams$id,
abbrev = factor(teams$abbrev, levels = teams$abbrev),
draftDayProjectedRank = teams$draftDayProjectedRank,
currentProjectedRank = teams$currentProjectedRank,
playoffSeed = teams$playoffSeed,
rankCalculatedFinal = teams$rankCalculatedFinal,
teams$record$overall
)
}
#' League standing simulation
#'
#' The ESPN algorithm simulates the entire season according to the projection
#' and matchup schedule to calculate the probability of a team winning their
#' division and making the playoffs.
#'
#' @inheritParams ffl_api
#' @return A data frame of simulated team standings.
#' @examples
#' league_simulation(leagueId = "42654852")
#' @importFrom tibble tibble
#' @export
league_simulation <- function(leagueId = ffl_id(), leagueHistory = FALSE, ...) {
dat <- ffl_api(
leagueId,
leagueHistory,
view = c("mTeam", "mStandings"),
...
)
if (leagueHistory) {
out <- rep(list(NA), length(dat$teams))
for (i in seq_along(dat$members)) {
out[[i]] <- parse_sim(
teams = dat$teams[[i]],
y = dat$seasonId[i],
w = dat$scoringPeriodId[i]
)
}
} else {
out <- parse_sim(
teams = dat$teams,
y = dat$seasonId,
w = dat$settings$scheduleSettings$matchupPeriodCount
)
}
return(out)
}
parse_sim <- function(teams, y = NULL, w = NULL) {
tibble::tibble(
seasonId = y,
scoringPeriodId = w,
teamId = teams$id,
abbrev = factor(teams$abbrev, levels = teams$abbrev),
draftDayProjectedRank = teams$draftDayProjectedRank,
currentProjectedRank = teams$currentProjectedRank,
simulatedRank = teams$currentSimulationResults$rank,
simulatedWins = teams$currentSimulationResults$modeRecord$wins,
simulatedLosses = teams$currentSimulationResults$modeRecord$losses,
simulatedTies = teams$currentSimulationResults$modeRecord$ties,
playoffPct = teams$currentSimulationResults$playoffPct,
divisionWinPct = teams$currentSimulationResults$divisionWinPct
)
}