read_log.R 2.87 KB
Newer Older
Keisuke ANDO's avatar
Keisuke ANDO committed
1
2
3
4
#' Read log file
#' 
#' @param path log file path
#' @return Parsed tibble of log file in \code{path}
5
6
7
8
9
10
11
12
#' @importFrom fs path path_ext
#' @importFrom readr read_file read_lines
#' @importFrom jsonlite parse_json
#' @importFrom dplyr as_tibble filter select select_if rename_all mutate
#' @importFrom tidyr unnest
#' @importFrom purrr map map_chr discard
#' @importFrom stringr str_replace str_extract str_remove str_split str_trim
#' @importFrom rlang .data
Keisuke ANDO's avatar
Keisuke ANDO committed
13
14
#' @export
#' @examples 
15
#' \dontrun{
Keisuke ANDO's avatar
Keisuke ANDO committed
16
#' rcg <- read_log("data/20220405162804-HELIOS_base_3-vs-enemy_2.rcg")
17
#' rcl <- read_log("data/20220405162804-HELIOS_base_3-vs-enemy_2.rcl")
18
#' }
Keisuke ANDO's avatar
Keisuke ANDO committed
19
20
21
22
23
24
25
26
27
28
29
30
31
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)
}

32
33
34
35
36
37
38
39
40
filter_rcg <- function(rcg, type) {
  filtered <- rcg |>
    filter(.data$type == (!!type)) |>
    select_if(~ !all(is.na(.x)) && !all(is.null(unlist(.x)))) |>
    select(!type)
  
  return(filtered)
}

Keisuke ANDO's avatar
Keisuke ANDO committed
41
#' @rdname read_log
42
#' @export
Keisuke ANDO's avatar
Keisuke ANDO committed
43
read_rcg <- function(path) {
44
  parsed <- path |>
Keisuke ANDO's avatar
Keisuke ANDO committed
45
46
    read_file() |>
    parse_json(simplifyVector = TRUE, flatten = TRUE) |>
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
    as_tibble()
  
  rcg <- list(
    header = parsed |>
      filter_rcg("header") |>
      as.list(),
    
    server_param = parsed |>
      filter_rcg("server_param") |>
      rename_all(str_remove, "params.") |>
      as.list(),
    
    player_param = parsed |>
      filter_rcg("player_param") |>
      rename_all(str_remove, "params.") |>
      as.list(),
    
    player_type = parsed |>
      filter_rcg("player_type") |>
      rename_all(str_remove, "params."),
    
    team = parsed |>
      filter_rcg("team") |>
      unnest(.data$teams),
    
    show = parsed |>
      filter_rcg("show") |>
      unnest(.data$players) |>
      rename_all(str_replace, "[.]", "_"),
    
    msg = parsed |>
      filter_rcg("msg")
  )
Keisuke ANDO's avatar
Keisuke ANDO committed
80
81
82
83
84
85
86
87
88
89
90
  
  return(rcg)
}

#' @rdname read_log
#' @export
read_rcl <- function(path) {
  rcl <- path |>
    read_lines() |>
    as_tibble() |>
    mutate(
91
92
93
94
95
      time     = .data$value |> str_extract("\\d+") |> as.numeric(),
      agent    = .data$value |> str_extract("\\w+_([0-9]{1,2}|Coach)(?!\\))"),
      team     = .data$agent |> str_remove("_([0-9]{1,2}|Coach)"),
      unum     = .data$agent |> str_extract("([0-9]{1,2}|Coach)$"),
      commands = .data$value |>
96
        str_extract("\\(.+\\)$") |>
97
98
99
100
101
        map(
          ~ .x |>
            str_split("\\(|\\)", simplify = TRUE) |>
            str_trim() |>
            discard(~ .x == "")),
Keisuke ANDO's avatar
Keisuke ANDO committed
102
    ) |>
103
    unnest(.data$commands) |>
Keisuke ANDO's avatar
Keisuke ANDO committed
104
    mutate(
105
106
107
      splited = .data$commands |> str_split("\\ ", n = 2),
      command = .data$splited |> map_chr(1),
      args    = .data$splited |> map(~ .x[-1]),
Keisuke ANDO's avatar
Keisuke ANDO committed
108
109
    ) |>
    select(
110
111
112
113
114
      .data$time,
      .data$team,
      .data$unum,
      .data$command,
      .data$args
Keisuke ANDO's avatar
Keisuke ANDO committed
115
116
117
118
    )
  
  return(rcl)
}