read_log.R 1.78 KB
Newer Older
Keisuke ANDO's avatar
Keisuke ANDO committed
1
2
3
4
5
6
7
#' Read log file
#' 
#' @param path log file path
#' @return Parsed tibble of log file in \code{path}
#' @export
#' @examples 
#' rcg <- read_log("data/20220405162804-HELIOS_base_3-vs-enemy_2.rcg")
8
#' rcl <- read_log("data/20220405162804-HELIOS_base_3-vs-enemy_2.rcl")
Keisuke ANDO's avatar
Keisuke ANDO committed
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
read_log <- function(path) {
  ext <- path |> path() |> path_ext()
  
  if (ext == "rcg") {
    return(read_rcg(path))
  }
  if (ext == "rcl") {
    return(read_rcl(path))
  }
  
  stop("Not supported format: ", path)
}

#' @rdname read_log
#' @export 
read_rcg <- function(path) {
  rcg <- path |>
    read_file() |>
    parse_json(simplifyVector = TRUE, flatten = TRUE) |>
    as_tibble() |>
    filter(type == "show") |>
30
    select(time, stime, players, ball.x, ball.y, ball.vx, ball.vy) |>
Keisuke ANDO's avatar
Keisuke ANDO committed
31
32
33
    unnest(players) |>
    select(time:capacity, ball.x:ball.vy) |>
    rename(step = time) |>
34
    rename_with(str_replace, pattern = "\\.", replacement = "_")
Keisuke ANDO's avatar
Keisuke ANDO committed
35
36
37
38
39
40
41
42
43
44
45
  
  return(rcg)
}

#' @rdname read_log
#' @export
read_rcl <- function(path) {
  rcl <- path |>
    read_lines() |>
    as_tibble() |>
    mutate(
46
47
48
49
      step     = value |> str_extract("\\d+") |> as.numeric(),
      agent    = value |> str_extract("\\w+_([0-9]{1,2}|Coach)(?!\\))"),
      team     = agent |> str_remove("_([0-9]{1,2}|Coach)"),
      unum     = agent |> str_extract("([0-9]{1,2}|Coach)$"),
Keisuke ANDO's avatar
Keisuke ANDO committed
50
      commands = value |>
51
52
53
54
55
        str_extract("\\(.+\\)$") |>
        map(~ .x |>
              str_split("\\(|\\)", simplify = TRUE) |>
              str_trim() |>
              discard(~ .x == "")),
Keisuke ANDO's avatar
Keisuke ANDO committed
56
57
58
    ) |>
    unnest(commands) |>
    mutate(
59
60
61
      commands = commands |> str_split("\\ ", n = 2),
      command  = commands |> map_chr(1),
      args     = commands |> map(~ .x[-1]),
Keisuke ANDO's avatar
Keisuke ANDO committed
62
63
64
65
66
67
68
69
70
71
72
    ) |>
    select(
      step,
      team,
      unum,
      command,
      args
    )
  
  return(rcl)
}