#' Summarizing playerSessions by X
#'
#' Summarize \code{playerSessions} for easier display. Either by person and day,
#' day only (without player subsets), weekday, or month.
#' @param playerSessions The sessions dataset summarized by person.
#' @param people The dataset containing the people information, defaults to using \code{activePeople}
#' @param sumBy What to summarize by? Either \code{person} (default),
#' \code{day}, \code{weekday}, or \code{month}
#' @keywords deaths
#' @return \code{data.frame} containing latest deaths for each player
#' @export
#' @import plyr
#' @note The API uses a different log than Minecraft stats, tl;dr things are complicated
#' @seealso \link[wurstmineR]{getPlayerSessions}, \link[wurstmineR]{getSessions}
#' @examples
#' \dontrun{
#' sessions <- getSessions()
#' playerSessions <- getPlayerSessions(sessions, splitByDay = TRUE)
#' playedPerMonth <- getPlayedPerX(playerSessions, sumBy = "month")
#' }
getPlayedPerX <- function(playerSessions = NULL, people = NULL, sumBy = "person"){
if (!("activePeople" %in% ls())){
people <- getActivePeople(size = "full")
}
playedPerPerson <- plyr::ddply(playerSessions, .(date, person, wday, month), summarize, timePlayed = sum(playedMinutes))
playedPerPerson <- plyr::arrange(playedPerPerson, date, person)
playedPerPerson$person <- factor(playedPerPerson$person, levels = people$id, labels = people$name, ordered = T)
if (sumBy == "person"){
return(playedPerPerson)
} else if (sumBy == "day"){
oldTZ <- Sys.getenv("TZ")
Sys.setenv(TZ = "UTC")
playedPerDay <- plyr::ddply(playerSessions, .(date, wday), plyr::summarize, timePlayed = sum(playedMinutes))
# Fix for missing days (ugly, sry.)
temp <- data.frame(date = seq.Date(as.Date(playedPerDay$date[1]), as.Date(playedPerDay$date[nrow(playedPerDay)]), by="day"))
temp$wday <- factor(wday(temp$date, T, F), levels = levels(playedPerDay$wday))
temp$timePlayed <- 0
temp$date <- as.POSIXct(temp$date, tz = "UTC")
temp$timePlayed[temp$date %in% playedPerDay$date] <- playedPerDay$timePlayed
playedPerDay <- temp
Sys.setenv(TZ = oldTZ)
return(playedPerDay)
} else if (sumBy == "weekday"){
playedPerWeekday <- plyr::ddply(playedPerPerson, .(wday, person), plyr::summarize, timePlayed = sum(timePlayed))
return(playedPerWeekday)
} else if (sumBy == "month"){
playedPerMonth <- plyr::ddply(playedPerPerson, .(month, person), plyr::summarize, timePlayed = sum(timePlayed))
return(playedPerMonth)
} else if (sumBy == "year"){
playedPerYear <- plyr::ddply(playerSessions, .(year, person), plyr::summarize, playedMinutes = sum(playedMinutes))
playedPerYear$person <- factor(playedPerYear$person, levels = people$id, labels = people$name, ordered = T)
return(playedPerYear)
}
}
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.