Commit 49c9f784 authored by Keisuke ANDO's avatar Keisuke ANDO 😌
Browse files

Merge branch 'feature/readability' into 'develop'

Summarize functions to read log

See merge request !5
parents 1b31b399 39740e4a
#' 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")
#' rcl <- read_log("data/20220304142841-HELIOS_base_1-vs-ThunderLeague_1.rcl")
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") |>
select(time,stime,players, ball.x, ball.y, ball.vx, ball.vy) |>
unnest(players) |>
select(time:capacity, ball.x:ball.vy) |>
rename(step = time) |>
rename_with(stringr::str_replace, pattern = "\\.", replacement = "_")
return(rcg)
}
#' @rdname read_log
#' @export
read_rcl <- function(path) {
rcl <- path |>
read_lines() |>
as_tibble() |>
mutate(
step = value |> stringr::str_extract("\\d+") |> as.numeric(),
agent = value |> stringr::str_extract("\\w+_([0-9]{1,2}|Coach)(?!\\))"),
team = agent |> stringr::str_remove("_([0-9]{1,2}|Coach)"),
unum = agent |> stringr::str_extract("([0-9]{1,2}|Coach)$"),
commands = value |>
stringr::str_extract("\\(.+\\)$") |>
purrr::map(~ .x |>
stringr::str_split("\\(|\\)", simplify = TRUE) |>
stringr::str_trim() |>
purrr::discard(~ .x == "")),
) |>
unnest(commands) |>
mutate(
commands = commands |> stringr::str_split("\\ ", n = 2),
command = commands |> purrr::map_chr(1),
args = commands |> purrr::map(~ .x[-1]),
) |>
select(
step,
team,
unum,
command,
args
)
return(rcl)
}
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment