Browse Source

Add get_game

tags/0.1
jemus42 2 years ago
parent
commit
f0e76c85af
11 changed files with 156 additions and 16 deletions
  1. +6
    -2
      DESCRIPTION
  2. +3
    -1
      NAMESPACE
  3. +1
    -0
      NEWS.md
  4. +0
    -0
      R/get.R
  5. +36
    -0
      R/get_game.R
  6. +9
    -0
      R/zzz.R
  7. +75
    -0
      experiments.R
  8. +24
    -0
      man/get_game.Rd
  9. +0
    -12
      man/hello.Rd
  10. +1
    -0
      speedrunr.Rproj
  11. +1
    -1
      tests/testthat.R

+ 6
- 2
DESCRIPTION View File

@@ -1,7 +1,8 @@
Package: speedrunr
Version: 0.0.0.9000
Title: Access Data From 'speedrun.com'
Description: What the package does (one paragraph).
Description: This package provides wrappers for the 'speedrun.com' API to make it easier to identify
the ids of games and categories which are required to access run data.
Authors@R: person('Lukas', 'Burk', email = 'lukas@quantenbrot.de', role = c('aut', 'cre'))
License: MIT + file LICENSE
Encoding: UTF-8
@@ -11,6 +12,9 @@ Imports:
httr,
purrr,
hms,
lubridate
lubridate,
tibble
Suggests:
testthat
Roxygen: list(markdown = TRUE)
RoxygenNote: 6.0.1

+ 3
- 1
NAMESPACE View File

@@ -1 +1,3 @@
exportPattern("^[[:alpha:]]+")
# Generated by roxygen2: do not edit by hand

export(get_game)

+ 1
- 0
NEWS.md View File

@@ -1,3 +1,4 @@
# speedrunr 0.0.0.9000

* Added a `NEWS.md` file to track changes to the package.
* Added `get_game`. Search for a game and retrieve its `id` which is required for the next step.

+ 0
- 0
R/get.R View File


+ 36
- 0
R/get_game.R View File

@@ -0,0 +1,36 @@
#' Search for Games by Name
#'
#' @param name A search query, `character`.
#' @param ... Other named parameters passed to the API.
#'
#' @return A [tibble::tibble] with search results
#' @export
#' @examples
#' \dontrun{
#' get_game("Ocarina of Time")
#' }
get_game <- function(name = "sm64", ...) {

url <- httr::modify_url(url = paste0(getOption("speedruncom_base"), "games"),
query = list(name = name, ...))
res <- httr::GET(url)
httr::warn_for_status(res)
res <- httr::content(res)
data <- res$data

games <- purrr::map_df(data, function(x) {
tibble::tibble(
id = x$id,
name_international = x$names$international,
name_twitch = x$names$twitch,
name_abbr = x$abbreviation,
weblink = x$weblink,
released = lubridate::ymd(x$`release-date`),
released_year = x$released,
romhack = x$romhack,
created = lubridate::ymd_hms(x$created)
)
})

games[order(games$created), ]
}

+ 9
- 0
R/zzz.R View File

@@ -0,0 +1,9 @@
# Set API base URL on startup
.onLoad <- function(...) {
options(speedruncom_base = "https://www.speedrun.com/api/v1/")
}

# Clean up option on unload
.onDetach <- function(...) {
options(speedruncom_base = NULL)
}

+ 75
- 0
experiments.R View File

@@ -0,0 +1,75 @@
# experiments
library(jsonlite)
library(tibble)
library(lubridate)
library(hms)
library(ggplot2)
library(dplyr)
library(purrr)
theme_set(tadaatoolbox::theme_tadaa())

runs <- fromJSON("https://www.speedrun.com/api/v1/runs?status=verified&game=j1l9qz1g&category=q255jw2o&max=200")


runs_hundo <- tibble(
date = ymd_hms(runs$data$submitted),
time = hms::hms(seconds = runs$data$times$primary_t),
runner = map_chr(runs$data$players, 2),
zfg = if_else(runner == "e8e5v680", TRUE, FALSE)
) %>%
filter(time < hms::hms(hours = 12)) %>%
arrange(date)

runs_hundo$record <- FALSE

for (run in seq_len(nrow(runs_hundo))) {
if (runs_hundo$time[run] == min(runs_hundo$time[seq_len(run)])) {
runs_hundo$record[run] <- TRUE
} else {
runs_hundo$record[run] <- FALSE
}
}

runs_hundo %>%
arrange(date) %>%
#filter(record) %>%
filter(time < hms::hms(hours = 5)) %>%
ggplot(., aes(date, time, fill = zfg, color = zfg)) +
#geom_smooth(method = lm, se = FALSE, fullrange = TRUE, color = "red", formula = y ~ splines::ns(x, 2)) +
geom_smooth(aes(color = zfg), method = lm, se = FALSE, fullrange = TRUE) +
geom_point(size = 2, shape = 21) +
scale_x_datetime(date_breaks = "2 months", date_labels = "%b '%y",
limits = c(ymd_hms("20161001_100000"), ymd_hms("20181001_100000"))) +
scale_y_time(breaks = seq(2 * 60^2, 20 * 60^2, 1/6 * 60^2),
minor_breaks = seq(2 * 60^2, 20 * 60^2, 60),
expand = c(.2, 0)) +
scale_color_brewer(palette = "Set1", labels = c("Other Runners", "zfg"), name = NULL) +
scale_fill_brewer(palette = "Set1", labels = c("Other Runners", "zfg"), name = NULL) +
labs(title = "Ocarina of Time: 100% Speedrun Record History",
subtitle = "All data from speedrun.com",
x = "Date Submitted", y = "Time",
caption = "@jemus42") +
theme(legend.position = "top")




records <- runs_hundo %>%
arrange(date) %>%
filter(record) %>%
mutate(diff = as.hms(abs(time - lag(time))),
prev = lag(time))

records %>%
mutate(diff = if_else(is.na(diff), hms(seconds = 0), diff),
cumsum = cumsum(as.numeric(seconds(diff)))) %>%
ggplot(aes(x = date, y = hms(seconds = cumsum))) +
geom_point(size = 2) +
scale_x_datetime(date_breaks = "2 months", date_labels = "%b '%y") +
scale_y_time(breaks = seq(0, 60^2, 5 * 60),
minor_breaks = seq(0, 60^2, 1 * 60)) +
labs(title = "Ocarina of Time: 100% Record Progression",
subtitle = "Cumulative Time Save Since 2017",
x = "Date of Run", y = "Time Save (H:M:S)",
caption = "Data from speedrun.com\n@jemus42")


+ 24
- 0
man/get_game.Rd View File

@@ -0,0 +1,24 @@
% Generated by roxygen2: do not edit by hand
% Please edit documentation in R/get_game.R
\name{get_game}
\alias{get_game}
\title{Search for Games by Name}
\usage{
get_game(name = "sm64", ...)
}
\arguments{
\item{name}{A search query, \code{character}.}

\item{...}{Other named parameters passed to the API.}
}
\value{
A \link[tibble:tibble]{tibble::tibble} with search results
}
\description{
Search for Games by Name
}
\examples{
\dontrun{
get_game("Ocarina of Time")
}
}

+ 0
- 12
man/hello.Rd View File

@@ -1,12 +0,0 @@
\name{hello}
\alias{hello}
\title{Hello, World!}
\usage{
hello()
}
\description{
Prints 'Hello, world!'.
}
\examples{
hello()
}

+ 1
- 0
speedrunr.Rproj View File

@@ -17,3 +17,4 @@ StripTrailingWhitespace: Yes
BuildType: Package
PackageUseDevtools: Yes
PackageInstallArgs: --no-multiarch --with-keep.source
PackageRoxygenize: rd,collate,namespace,vignette

+ 1
- 1
tests/testthat.R View File

@@ -1,4 +1,4 @@
library(testthat)
library(speedrunr)

test_check("speedrunr")
# test_check("speedrunr")

Loading…
Cancel
Save