make_data.R 2.84 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
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)
}