---
title: "Basic Simulations"
author: Tan Ho
date: "`r Sys.Date()`"
output: rmarkdown::html_vignette
vignette: >
  %\VignetteIndexEntry{Basic Simulations}
  %\VignetteEngine{knitr::rmarkdown}
  %\VignetteEncoding{UTF-8}
---

```{r, include = FALSE}
knitr::opts_chunk$set(
  collapse = TRUE,
  comment = "#>",
  fig.width = 7,
  fig.height = 5
)
options(dplyr.summarise.inform = FALSE)
```

```{r setup, message = FALSE}
library(ffsimulator)
library(ggplot2)
```

ffsimulator runs simulations on a given fantasy league based that league's rosters, scoring settings, and current redraft rankings, connecting this data to historical weekly performances for each (preseason) positional ranking.

This vignette will introduce the basic usage of ffsimulator - you may also be interested in the motivations and custom simulations vignettes!

## Usage - Simulating Seasons

ffsimulator connects directly to your league (via the [ffscrapr package](https://ffscrapr.ffverse.com)) and can be used at a high level with just a few lines of code:

```{r eval = FALSE}
foureight_conn <- mfl_connect(2021, 22627)

foureight_sim <- ff_simulate(conn = foureight_conn, n_seasons = 10, n_weeks = 14)
foureight_sim
```
```{r echo = FALSE, message = FALSE}
foureight_sim <- readRDS(system.file("cache/foureight_sim.rds", package = "ffsimulator"))
foureight_sim
```
ffsimulator includes a few automatic plots that can be run on this ff_simulation object to get a quick sense of the simulation output:
```{r}
plot(foureight_sim) # defaults to type = "wins"
plot(foureight_sim, type = "rank")
plot(foureight_sim, type = "points")
```

You can also access the various component dataframes by name, for further analysis.

The `summary_simulation` table is a team-level summary across all of the simulated seasons.
```{r}
foureight_sim$summary_simulation
```

The `summary_season` table is a season-level summary of all weeks within those seasons - any one of these seasons "could" theoretically be the outcome of the upcoming season!
```{r}
foureight_sim$summary_season
```

The `summary_week` table is a week-level summary of each team's performance, including the optimal lineup that could have been played by the team and the randomly-generated lineup efficiency factor:
```{r}
foureight_sim$summary_week
```

The `roster_scores` and `projected_scores` tables both provide individual player-week level scores, with the difference being that the `roster_scores` table is attached to franchise rosters (and thus duplicates rows if there are multiple copies of players within the league). 
```{r}
foureight_sim$roster_scores
```

Finally, some basic parameters are included, including `league_info` which is generated by `ffscrapr::ff_league()`.
```{r}
foureight_sim$league_info
```

## Usage - Simulating Weeks

As of the 1.1.0 release, ffsimulator also has functions to simulate an individual week, with that week's FP rankings!

```{r eval = FALSE}
foureight_conn <- mfl_connect(2021, 22627)

foureight_sim_week <- ff_simulate_week(conn = foureight_conn, n = 10)

foureight_sim_week
```
```{r echo = FALSE}
foureight_sim_week <- .ffs_cache("foureight_sim_week.rds")
foureight_sim_week
```

This simulation object has many of the same dataframes, including a summary_simulation, summary_week, roster_scores, projected_scores, league_info, and simulation_params. 

It also has some different automatic plots, including one for schedule luck:

```{r}
plot(foureight_sim_week,type = "luck")
```

and the usual points distribution:

```{r}
plot(foureight_sim_week, type = "points")
```
