R/getItemStats.R

#' Generate per-item stats
#' 
#' Gets a \code{data.frame} of item stats with item stat IDs, their associated 
#' action, and the total values and leading players of each stat.
#' @keywords items
#' @param items The items dataset
#' @param itemData The dataset of descriptive item strings, defaults to
#'   \link[wurstmineR]{strings.items}
#' @return \code{data.frame} of per-item stats
#' @export
#' @note Requires \code{items} to be cleaned up via
#'   \link[wurstmineR]{mergeItemStats}, because legacy item IDs are not recognized
#' @examples
#' \dontrun{
#' items <- stats2df(fromJSON("http://api.wurstmineberg.de/server/playerstats/item.json"))
#' items <- mergeItemStats(items, strings.items)
#' itemStats <- getItemStats(items, strings.items)
#' }
getItemStats <- function(items = NULL, itemData = NULL){
  if (is.null(items)){
    stop("No item dataset defined")
  }
  if (is.null(itemData)){
    itemData <- wurstmineR::strings.items
  }
  items.only               <- items[names(items) != "player"]
  itemStats                <- data.frame(stat = names(items.only), stringsAsFactors = F)
  itemStats$action         <- character(nrow(itemStats))
  itemStats$action[grep("mineBlock",  itemStats$stat)] <- "mined"
  itemStats$action[grep("craftItem.", itemStats$stat)] <- "crafted"
  itemStats$action[grep("useItem.",   itemStats$stat)] <- "used"
  itemStats$action[grep("breakItem.", itemStats$stat)] <- "broken"
  itemStats$item           <- extractItemNames(itemStats$stat, itemData)
  itemStats$total          <- as.numeric(plyr::colwise(sum)(items.only))
  itemStats$playerMax      <- as.numeric(plyr::colwise(max)(items.only))
  itemStats$leadingPlayers <- getLeadingPlayers(itemStats, items)
  return(itemStats)
}
jemus42/wurstmineR documentation built on May 19, 2019, 4:03 a.m.