R/getLatestDeaths.R

#' Getting death stat
#' 
#' Get the latest deaths from an appropriate API endpoint. 
#' Uses acitvePeople for name substitution.
#' @param url URL of the appropriate API endpoint, defaults to \code{getOption("url.general.deaths.latest")}, 
#' and if that returns \code{NULL}, the Wurstmineberg API endpoint is used.
#' @param people The people dataset to be used for sorting/naming the players, defaults to \code{activePeople}
#' @keywords deaths
#' @return \code{data.frame} containing latest deaths for each player
#' @export
#' @note The API uses a different log than Minecraft stats, tl;dr things are complicated
#' @examples
#' \dontrun{
#' options(url.general.deaths.latest = "http://api.wurstmineberg.de/server/deaths/latest.json")
#' deaths <- getLatestDeaths()
#' }
getLatestDeaths <- function(url = getOption("url.general.deaths.latest"), people = activePeople){
  if (is.null(url)){
    url <- "http://api.wurstmineberg.de/server/deaths/latest.json"
  }
  if (!("activePeople" %in% ls())){
    people <- getActivePeople()
  }
  latestdeaths        <- jsonlite::fromJSON(url)
  deaths              <- plyr::ldply(latestdeaths$deaths, data.frame, .id = "player")
  deaths$timestamp    <- as.POSIXct(deaths$timestamp, tz="UTC")
  deaths$daysSince    <- as.numeric(round(difftime(Sys.time(), deaths$timestamp, units="days")))
  # Match against activePeople and sort accordingly
  deaths              <- deaths[match(people$id, deaths$player), ]
  deaths$player       <- people$name
  deaths$joinStatus   <- people$joinStatus
  # Remove NAs introduced by matching with activePoeple
  # Should be uneccessary when everyone hase been active / died since logging started
  deaths <- deaths[!is.na(deaths$timestamp), ]
  return(deaths)
}
jemus42/wurstmineR documentation built on May 19, 2019, 4:03 a.m.