Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixes to dk_prepare_schematic(), documentation updates #34

Merged
merged 8 commits into from
Sep 2, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .Rbuildignore
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,4 @@
^\.github$
^docs$
^pkgdown$
^codecov\.yml$
1 change: 1 addition & 0 deletions .github/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
*.html
36 changes: 36 additions & 0 deletions .github/workflows/R-CMD-check.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
# Workflow derived from https://github.com/r-lib/actions/tree/v2/examples
# Need help debugging build failures? Start at https://github.com/r-lib/actions#where-to-find-help
on:
push:
branches: [main, master]
pull_request:
branches: [main, master]

name: R-CMD-check.yaml

permissions: read-all

jobs:
R-CMD-check:
runs-on: ubuntu-latest
env:
GITHUB_PAT: ${{ secrets.GITHUB_TOKEN }}
R_KEEP_PKG_SOURCE: yes
DK_IV: ${{ secrets.DK_IV }}
DK_JWE: ${{ secrets.DK_JWE }}
steps:
- uses: actions/checkout@v4

- uses: r-lib/actions/setup-r@v2
with:
use-public-rspm: true

- uses: r-lib/actions/setup-r-dependencies@v2
with:
extra-packages: any::rcmdcheck
needs: check

- uses: r-lib/actions/check-r-package@v2
with:
upload-snapshots: true
build_args: 'c("--no-manual","--compact-vignettes=gs+qpdf")'
85 changes: 85 additions & 0 deletions .github/workflows/pr-commands.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
# Workflow derived from https://github.com/r-lib/actions/tree/v2/examples
# Need help debugging build failures? Start at https://github.com/r-lib/actions#where-to-find-help
on:
issue_comment:
types: [created]

name: pr-commands.yaml

permissions: read-all

jobs:
document:
if: ${{ github.event.issue.pull_request && (github.event.comment.author_association == 'MEMBER' || github.event.comment.author_association == 'OWNER') && startsWith(github.event.comment.body, '/document') }}
name: document
runs-on: ubuntu-latest
env:
GITHUB_PAT: ${{ secrets.GITHUB_TOKEN }}
permissions:
contents: write
steps:
- uses: actions/checkout@v4

- uses: r-lib/actions/pr-fetch@v2
with:
repo-token: ${{ secrets.GITHUB_TOKEN }}

- uses: r-lib/actions/setup-r@v2
with:
use-public-rspm: true

- uses: r-lib/actions/setup-r-dependencies@v2
with:
extra-packages: any::roxygen2
needs: pr-document

- name: Document
run: roxygen2::roxygenise()
shell: Rscript {0}

- name: commit
run: |
git config --local user.name "$GITHUB_ACTOR"
git config --local user.email "[email protected]"
git add man/\* NAMESPACE
git commit -m 'Document'

- uses: r-lib/actions/pr-push@v2
with:
repo-token: ${{ secrets.GITHUB_TOKEN }}

style:
if: ${{ github.event.issue.pull_request && (github.event.comment.author_association == 'MEMBER' || github.event.comment.author_association == 'OWNER') && startsWith(github.event.comment.body, '/style') }}
name: style
runs-on: ubuntu-latest
env:
GITHUB_PAT: ${{ secrets.GITHUB_TOKEN }}
permissions:
contents: write
steps:
- uses: actions/checkout@v4

- uses: r-lib/actions/pr-fetch@v2
with:
repo-token: ${{ secrets.GITHUB_TOKEN }}

- uses: r-lib/actions/setup-r@v2

- name: Install dependencies
run: install.packages("styler")
shell: Rscript {0}

- name: Style
run: styler::style_pkg()
shell: Rscript {0}

- name: commit
run: |
git config --local user.name "$GITHUB_ACTOR"
git config --local user.email "[email protected]"
git add \*.R
git commit -m 'Style'

- uses: r-lib/actions/pr-push@v2
with:
repo-token: ${{ secrets.GITHUB_TOKEN }}
63 changes: 63 additions & 0 deletions .github/workflows/test-coverage.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
# Workflow derived from https://github.com/r-lib/actions/tree/v2/examples
# Need help debugging build failures? Start at https://github.com/r-lib/actions#where-to-find-help
on:
push:
branches: [main, master]
pull_request:
branches: [main, master]

name: test-coverage.yaml

permissions: read-all

jobs:
test-coverage:
runs-on: ubuntu-latest
env:
GITHUB_PAT: ${{ secrets.GITHUB_TOKEN }}
DK_IV: ${{ secrets.DK_IV }}
DK_JWE: ${{ secrets.DK_JWE }}

steps:
- uses: actions/checkout@v4

- uses: r-lib/actions/setup-r@v2
with:
use-public-rspm: true

- uses: r-lib/actions/setup-r-dependencies@v2
with:
extra-packages: any::covr, any::xml2
needs: coverage

- name: Test coverage
run: |
cov <- covr::package_coverage(
quiet = FALSE,
clean = FALSE,
install_path = file.path(normalizePath(Sys.getenv("RUNNER_TEMP"), winslash = "/"), "package")
)
covr::to_cobertura(cov)
shell: Rscript {0}

- uses: codecov/codecov-action@v4
with:
fail_ci_if_error: ${{ github.event_name != 'pull_request' && true || false }}
file: ./cobertura.xml
plugin: noop
disable_search: true
token: ${{ secrets.CODECOV_TOKEN }}

- name: Show testthat output
if: always()
run: |
## --------------------------------------------------------------------
find '${{ runner.temp }}/package' -name 'testthat.Rout*' -exec cat '{}' \; || true
shell: bash

- name: Upload test results
if: failure()
uses: actions/upload-artifact@v4
with:
name: coverage-test-failures
path: ${{ runner.temp }}/package
3 changes: 1 addition & 2 deletions DESCRIPTION
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
Package: draft.kings
Type: Package
Title: Draft Kings API Wrapper and Optimization
Version: 0.5.4
Version: 0.5.5
Author: Giovanni Colitti
Maintainer: Giovanni Colitti <[email protected]>
Description: Fetch data from DraftKings using the provided API and perform lineup optimization.
Expand All @@ -13,7 +13,6 @@ Imports:
dplyr,
glue,
httr2,
jsonlite,
magrittr,
ompr,
ompr.roi,
Expand Down
8 changes: 7 additions & 1 deletion NEWS.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,12 @@
# draft.kings 0.5.5
* Fixed issue in `dk_prepare_schematic()` introduced when using player position instead of roster slot id
* Updated vignettes and README
* Removed jsonlite from imports
* Added GitHub Actions workflows for documentation and testing

# draft.kings 0.5.4
* Added pkgdown site and udpated README
* Removed `dk_get()` and `dk_multi_get()`
* Renamed functions like `dk_request_*()` to `dk_req_*()`
* Renamed functions like `dk_response_*()` to `dk_resp_*()`
* Replaced `cookies_file` argument with `iv` and `jwe` arguments to functions that require DraftKings session cookies
* Replaced `cookies_file` argument with `iv` and `jwe` arguments to functions that require DraftKings session cookies
2 changes: 1 addition & 1 deletion R/globals.R
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
utils::globalVariables(c(".", "i", "j", "draftable_id", "team_id", "player_id", "competition_id",
"position"))
"position", "roster_slot_id"))
28 changes: 12 additions & 16 deletions R/optimize.R
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ dk_prepare_schematic <- function(draft_group_id,
"salary" = c("integer", "numeric"),
"team_id" = c("integer", "numeric"),
"competition_id" = c("integer", "numeric"),
"position" = c("character"),
"roster_slot_id" = c("integer", "numeric"),
"status" = c("character")
)
)
Expand Down Expand Up @@ -211,10 +211,6 @@ dk_prepare_schematic <- function(draft_group_id,
draft_group <- draft_group %>%
dplyr::distinct(dplyr::across(!dplyr::all_of("draftable_id")), .keep_all = TRUE)

# Separate players that can perform multiple positions into different rows
draft_group <- draft_group %>%
tidyr::separate_rows("position", sep = "/")

# Add row number
draft_group <- draft_group %>%
dplyr::mutate("row_number" = dplyr::row_number())
Expand Down Expand Up @@ -523,14 +519,14 @@ dk_optimize_lineup.classic <- function(schematic, max_points = NULL, solver = "g
# Set player constraints for NBA classic
if (rules$game_type_id == 70) {

# Ensure at least 1 player in each position
# Ensure at least 1 player in each roster slot
mod <- mod %>%
ompr::add_constraint(
ompr::sum_over(
draftable_id[i],
i = draft_group$row_number[which(draft_group$position == position)]
i = draft_group$row_number[which(draft_group$roster_slot_id == roster_slot_id)]
) >= 1,
position = unique(draft_group$position)
roster_slot_id = unique(draft_group$roster_slot_id)
)

# Ensure 8 players (allows for one flex that can come from any position)
Expand All @@ -552,39 +548,39 @@ dk_optimize_lineup.classic <- function(schematic, max_points = NULL, solver = "g
ompr::add_constraint(
ompr::sum_over(
draftable_id[i],
i = draft_group$row_number[which(draft_group$position == position)]
i = draft_group$row_number[which(draft_group$roster_slot_id == roster_slot_id)]
) == 1,
position = c("QB", "DST")
roster_slot_id = c(66, 71)
)

# Ensure at least 3 WR
mod <- mod %>%
ompr::add_constraint(
ompr::sum_over(
draftable_id[i],
i = draft_group$row_number[which(draft_group$position == position)]
i = draft_group$row_number[which(draft_group$roster_slot_id == roster_slot_id)]
) >= 3,
position = "WR"
roster_slot_id = 68
)

# Ensure at least 2 RB
mod <- mod %>%
ompr::add_constraint(
ompr::sum_over(
draftable_id[i],
i = draft_group$row_number[which(draft_group$position == position)]
i = draft_group$row_number[which(draft_group$roster_slot_id == roster_slot_id)]
) >= 2,
position = "RB"
roster_slot_id = 67
)

# Ensure at least 1 TE
mod <- mod %>%
ompr::add_constraint(
ompr::sum_over(
draftable_id[i],
i = draft_group$row_number[which(draft_group$position == position)]
i = draft_group$row_number[which(draft_group$roster_slot_id == roster_slot_id)]
) >= 1,
position = "TE"
roster_slot_id = 69
)

# Ensure 9 players (allows for one flex which can be either RB/WR/TE)
Expand Down
Loading
Loading