knitr::opts_chunk$set(echo = TRUE)

Packages

# devtools::install_github("tidyverse/dplyr")
pacman::p_load(tidyverse, pokerena, crayon)


devtools::document()
# devtools::load_all()
# devtools::install()




pair_ranks
# usethis::use_testthat()
# usethis::use_test("02-splitpot")
testthat::test_package("pokerena")
req <- httr::GET(url = "http://0.0.0.0:8000/registered")
d <- jsonlite::fromJSON(rawToChar(req$content))
d
rstudioapi::jobRunScript("jobs/job_server_api.R", workingDir = ".")

players <- rbind(
  dplyr::tibble(name = "fish1", fun = "pokerena::player_fish"),
  dplyr::tibble(name = "fish2", fun = "pokerena::player_random")
)
req <- httr::POST(url = "http://0.0.0.0:8000/register", body = players, encode = "json")

config <- tibble(
  credit = 100, # inital stack size, if NULL assumes individual credits
  max_round = 10, # how many rounds at max should be played
  bb = 2, # inital big blind
  bb_round = 10, # increase blinds after x hands
  bb_slope = "linear" # how to increase blinds
)

req <- httr::POST(url = "http://0.0.0.0:8000/start", body = config, encode = "json")

jsonlite::fromJSON(rawToChar(req$content))

Players

devtools::load_all()
players <- rbind(
  tibble(name = "AA", fun = list(player_call)),
  tibble(name = "BB", fun = list(player_fold))
  #tibble(name = "potman", fun = list(player_api))
  #tibble(name = "me", fun = list(player_random))
) %>% mutate(credit = c(100, 100), bb = 2)

p_game <- game$new(players, delay = 0)
p_game$run()
# p_game$admin$history
# p_game$session
#sp_game$events %>% View
p_game$result
p_game$events %>% glimpse

Break

player_app <- function(state_, ...){
  params <- list(...)
  inp <- params$input
  if(is.null(inp)) stop("wait for user input")
  return(inp)
}

players <- rbind(
  tibble(name = "random", fun = list(player_random)),
  tibble(name = "random2", fun = list(player_random)),
  tibble(name = "me", fun = list(player_api))
) %>% mutate(credit = 100, bb = 2)

p_game <- game$new(players, delay = 0)
p_game$run()

p_game$events


p_game$set_input <- dplyr::tibble(chips = 10, action = "raise")
p_game$step()
# p_game$get_state("me")
# p_game$session

p_game$events

Step

-players <- rbind(
  tibble(name = "potman", fun = list(player_api)),
  tibble(name = "me", fun = list(player_random)),
  tibble(name = "me2", fun = list(player_random))
) %>% mutate(credit = 100, bb = 2)

p_game <- game$new(players, delay = 0)
p_game$run(verbose = T)
p_game$step(verbose = T, step = 1)
p_game$events %>% select(name, action)
p_game$session
players <- rbind(
  tibble(name = "caller", fun = list(player_call)),
  tibble(name = "random", fun = list(player_random)),
  tibble(name = "potman", fun = list(player_random)),
  tibble(name = "folder", fun = list(player_fold))
)

config <- list(
  credit = 100,
  max_round = 20, 
  bb = 2, 
  bb_rounds = 10, 
  bb_slope = "linear"
)

tourn <- tournament$new(players, config)
tourn$run()
tourn$games
tourn$games %>% 
  transmute(id = 1:n(), result) %>% 
  unnest("result") %>% 
  group_by(name) %>% 
  mutate(net = cumsum(net)) %>% 
  ungroup() %>% 
  ggplot(aes(id, net, colour = name)) + 
  geom_line()

tourn$games %>% 
  transmute(id = 1:n(), result) %>% 
  unnest("result") %>% 
  group_by(name) %>% 
  ungroup() %>% 
  ggplot(aes(id, credit, colour = name)) + 
  geom_line()

OLD

# tournament <- R6::R6Class("tournament",
# 
#   public = list(
#     
#     players = NULL,
#     config = NULL, 
#     
#     BB = NULL, 
#     N_ROUND = 1, 
#     
#     games = NULL,
#     result = NULL,
#     
#     initialize = function(players, config){
#       self$players <- players
#       self$config <- config
#       if(!is.null(config$bb)){
#         self$players$bb <- self$BB <- config$bb
#       } else {
#         bb <- self$players[["bb"]][1]
#         if(is.null(bb)) bb <- 2
#         self$BB <- bb
#       }
#       if(!is.null(config$credit)) self$players$credit <- config$credit
#       if(is.null(config$max_rounds)) self$config$max_rounds <- 50
#     }, 
#   
#     
#     run = function(){
#       
#       while(self$N_ROUND <= self$config$max_round){
#         
#         p <- game$new(self$players, delay = 0)
#         p$step()
#         self$games <- rbind(self$games, p$export())
#         
#         players <- p$players %>% 
#           dplyr::filter(credit > 0)
#         
#         if(nrow(players) == 1) self$N_ROUND <- self$config$max_round + 1
#         
#         players$bb <- self$BB
#         self$players <- rbind(players[-1, ], players[1, ])
#           
#         self$N_ROUND <- self$N_ROUND + 1
#       }
#     }
#   )                                
# )


systats/pokerena documentation built on Sept. 17, 2020, 2:35 p.m.