source("~/soccer2D/socceR/R/read_old_rcg.R") source("~/soccer2D/socceR/R/read_rcl.R") source("~/soccer2D/socceR/R/read_rcg.R") get_rcl <- function(path_rcl){ output <- path_rcl |> read_rcl() return(output) } get_name <- function(path_rcg){ team_name <- path_rcg |> stringr::str_extract("[a-zA-Z0-9_-]*.rcg") |> stringr::str_remove("[0-9]*-") |> stringr::str_remove(".rcl") l_team <- team_name |> stringr::str_extract("[a-zA-Z0-9_]*-vs-") |> stringr::str_remove("_[0-9]*-vs-") r_team <- path_rcg |> stringr::str_extract("[a-zA-Z0-9_]*.rcg") |> stringr::str_remove("_[0-9]*.rcg") name_table <- tibble::tribble( ~side, ~name, "l", l_team, "r",r_team, ) return(name_table) } replase_rcg_to_rcl <- function(path_rcg){ output <- path_rcg |> stringr::str_replace(pattern=".rcg", replacement=".rcl") } replase_rcl_to_rcg <- function(path_rcl){ output <- path_rcl |> stringr::str_replace(pattern=".rcl", replacement=".rcg") } get_player <- function(rcg,name){ output <- rcg |> dplyr::inner_join(name,by="side") |> dplyr::select( step, team = name, unum, x, y, vx, vy, body, neck, ) return(output) } get_kick <- function(rcl,ball){ output <- rcl |> get_kick_point() |> convert_ball_rcg_and_rcl(ball) output <- output %>% dplyr::mutate(before_team=lag(team)) %>% dplyr::mutate(before_unum=lag(unum)) %>% dplyr::mutate(after_team=lead(team)) %>% dplyr::mutate(after_unum=lead(unum)) %>% dplyr::mutate(b_sameteam=(before_team==team)) %>% dplyr::mutate(b_sameunum=(before_unum==unum)) %>% dplyr::mutate(a_sameteam=(after_team==team)) %>% dplyr::mutate(a_sameunum=(after_unum==unum)) output$b_sameteam[1] <- TRUE output$b_sameunum[1] <- FALSE output <- output %>% dplyr::mutate(dribble=(b_sameteam & b_sameunum)) %>% dplyr::mutate(pass=(a_sameteam & !a_sameunum)) %>% dplyr::mutate(tackle=(!b_sameteam))%>% group_by(grc = cumsum(!dribble)) %>% mutate(touch = row_number()) %>% ungroup() %>% select(-c(grc,before_team,before_unum,b_sameteam,b_sameunum,after_team,after_unum,a_sameteam,a_sameunum)) return(output) } kick_dist <- function(rcg){ ball_path <- rcg |> dplyr::group_nest(step, ball_x, ball_y) |> dplyr::mutate(move_dist_x = ball_x - dplyr::lag(ball_x), move_dist_y = ball_y - dplyr::lag(ball_y), move_dist = sqrt(move_dist_x^2 + move_dist_y^2), move_dist = dplyr::if_else(is.na(move_dist), 0, move_dist) ) |> dplyr::filter(move_dist != 0 & move_dist < 40) }