rankPlayer: Get current player's rank among group of past players

View source: R/rankPlayer.R

rankPlayerR Documentation

Get current player's rank among group of past players

Description

Get current player's rank among group of past players

Usage

rankPlayer(
  score,
  playerId = NULL,
  rankFile,
  score_col = "score",
  id_col = "pin",
  rank_col = "rank"
)

Arguments

score

The player's current score.

playerId

The player's ID number. MUST be unique for each player! Use pinGen.

rankFile

A file path to a new or existing .rds or .csv file of previous players' scores. Must contain 3 columns described by the remaining arguments.

score_col

The name of the column in rankFile where the past players' scores are stored. Defaults to "score".

id_col

The name of the column in rankFile where the past players' unique ID numbers are stored. Defaults to "pin".

rank_col

The name of the column in rankFile where the past players' ranks are stored. Defaults to "rank".

Value

A list with 2 values: (1) rank = the current players rank (integer) and (2) out_of = the total number of players so far.

Examples

# Erase the out file, "sample_rank.rds" after you run this example,
# ...so you won't get the error of multiple "players" with the same ID

if (interactive()) {
  library(shiny)
  library(shinyalert)
  library(shinyjs)
  ui <- fluidPage(
    useShinyalert(),
    actionButton("ok", "Click to add to your score!"),
    textOutput("score_txt"),
    actionButton("rank", "Show your rank when you're ready.")
  )

  server <- function(input, output, session) {
    rv <- reactiveValues(score = 0, pin = 1)
    observeEvent(input$ok, {
      rv$score <- rv$score + sample(c(1:9), 1)
      output$score_txt <- renderText({
        paste0("Your current score: ",rv$score)
        })
      enable("rank")
    })

    observeEvent(input$rank, {
      disable("rank")
      rv$pin <- rv$pin+1
      rank <- rankPlayer(score = rv$score,
                         playerId = rv$pin,
                         rankFile = "sample_rank.rds")
      text <- paste0("You rank #", rank$rank,
                     " out of ", rank$out_of, " player(s).")
      shinyalert(title = "Congratulations!",
                 type = "success",
                 text = text)
    })
  }
  shinyApp(ui = ui, server = server)
}

abbey-thomas/speechcollectr documentation built on Nov. 19, 2024, 7:09 p.m.