make_data.R 9.38 KB
Newer Older
1
2
3
4
5
6
7
get_player <- function(rcg, name) {
    output <- rcg |>
        dplyr::inner_join(name, by = "side") |>
        dplyr::filter(is.na(stime)) |>
        dplyr::select(
            step,
            team = name,
8
            side,
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
            unum,
            px = x,
            py = y,
            pvx = vx,
            pvy = vy,
            body,
            neck,
        )

    return(output)
}

get_action <- function(rcl, ball, players) {
    output <- rcl |>
        dplyr::filter(command == "kick" | command == "tackle") |>
        dplyr::mutate(
            x = args |> stringr::str_extract("[0-9\\-\\.]+"),
            l1 = args |> stringr::str_remove(x),
            y = l1 |> stringr::str_extract("[0-9\\-\\.]+"),
            unum = as.numeric(unum)
        ) |>
        dplyr::select(
            step,
            team,
            unum,
            command,
            ax = x,
            ay = y,
        ) |>
        dplyr::inner_join(ball, by = "step") %>%
        dplyr::inner_join(players, by = c("step", "team", "unum")) %>%
        dplyr::mutate(after_team = lead(team)) %>%
        dplyr::mutate(a_sameteam = (after_team == team)) %>%
        dplyr::mutate(tackle_scc = (command == "tackle" & a_sameteam)) %>%
        dplyr::mutate(a_tackle_scc = lead(tackle_scc)) %>%
        dplyr::select(-c(after_team, a_sameteam))

    return(output)
}

get_tackle <- function(action) {
    output <- action %>%
        dplyr::filter(command == "tackle") %>%
        dplyr::select(-c(a_tackle_scc, ax, ay))
    return(output)
}

Takumi Amano's avatar
Takumi Amano committed
56
57


Takumi Amano's avatar
Takumi Amano committed
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
get_kick_log <- function(action) {
  output <- action %>%
    dplyr::filter(command == "kick") %>%
    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 = (a_sameteam & a_sameunum)) %>%
    dplyr::mutate(pass = (a_sameteam & !a_sameunum)) %>%
    group_by(grc = cumsum(!dribble)) %>%
    mutate(touch = row_number()) %>%
    ungroup() %>%
    select(-c(tackle_scc, grc, before_team, before_unum, b_sameteam, b_sameunum, after_team, after_unum, a_sameteam, a_sameunum))
  return(output)
}
82
83
84
85
86
87
88
89

get_kick <- function(action) {
    output <- action %>%
        dplyr::filter(command == "kick") %>%
        dplyr::mutate(after_team = lead(team)) %>%
        dplyr::mutate(after_unum = lead(unum)) %>%
        dplyr::mutate(a_sameteam = (after_team == team)) %>%
        dplyr::mutate(a_sameunum = (after_unum == unum)) %>%
90
91
92
        select(-c(command, pvx, pvy, body, neck)) %>% 
        dplyr::mutate(next_ball_x = lead(ball_x)) %>%
        dplyr::mutate(next_ball_y = lead(ball_y))
93
94
95
96
97


    return(output)
}

Takumi Amano's avatar
Takumi Amano committed
98
99
100
101
102
103
104
105
get_action_Allplayer <- function(players,action){
  action <- action  %>% 
    dplyr::select(step,action_team=team,ball_x,ball_y,pass,dribble)
  output <- players  %>% 
    dplyr::inner_join(action,by = "step")
  return(output)
}

106
107
get_pass_Allplayer <- function(players,pass){
    pass <- pass  %>% 
108
        dplyr::select(step,ax,ay,pass_team=team,ball_x,ball_y,ball_vx,ball_vy,pass_scc)
109
    output <- players  %>% 
110
111
112
113
114
115
116
117
118
        dplyr::inner_join(pass,by = "step")
    return(output)
}

get_dribble_Allplayer <- function(players,dribble){
    dribble <- dribble  %>% 
        dplyr::select(step,dribble_team=team,ball_x,ball_y,dribble_scc)
    output <- players  %>% 
        dplyr::inner_join(dribble,by = "step")
119
120
121
    return(output)
}

122
123
124
125
126
get_pass <- function(kick) {
    output <- kick %>%
        dplyr::mutate(pass_scc = (a_sameteam & !a_sameunum)) %>%
        dplyr::mutate(pass = ((a_sameteam & !a_sameunum) | (!a_tackle_scc & !a_sameteam))) %>%
        dplyr::filter(pass) %>%
127
        distinct(step,.keep_all=TRUE) %>%
128
        select(-c(a_tackle_scc,tackle_scc, pass, a_sameteam, a_sameunum, after_team, after_unum))
129
130
131
132

    return(output)
}

Takumi Amano's avatar
Takumi Amano committed
133
134
135



136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
get_dribble <- function(kick) {
    output <- kick %>%
        dplyr::mutate(dribble = ((!a_sameteam & a_tackle_scc) | (a_sameteam & a_sameunum))) %>%
        dplyr::mutate(dribble_scc = (a_sameteam & a_sameunum)) %>%
        group_by(grc = cumsum(!dribble)) %>%
        mutate(touch = row_number()) %>%
        ungroup() %>%
        dplyr::filter(dribble) %>%
        select(-c(a_tackle_scc,tackle_scc, dribble, a_sameteam, a_sameunum, after_team, after_unum, dribble, grc))

    return(output)
}

read_referee <- function(path_rcl) {
    referee <- path_rcl |>
        readr::read_lines() |>
        tibble::as_tibble() |>
        dplyr::mutate(
            step = value |> stringr::str_extract("\\d+"),
            command = value |>
                stringr::str_extract("referee [a-zA-Z0-9_]*") |>
                stringr::str_remove("referee "),
            step = as.numeric(step),
        ) |>
        dplyr::select(
            step,
            command,
        ) %>%
        tidyr::drop_na()


    return(referee)
}

read_goal <- function(referee, name) {
    goal <- referee |>
        dplyr::filter(stringr::str_detect(command, "goal_[rl]_[0-9]+")) |>
        dplyr::mutate(
            side = command |>
                stringr::str_remove("goal_") |>
                stringr::str_extract("[rl]"),
            score = command |>
                stringr::str_remove("goal_[rl]_") |>
                stringr::str_extract("[0-9]+"),
        ) |>
        dplyr::inner_join(name, by = "side") |>
        dplyr::select(
            step,
            side,
            name,
            score,
        ) %>%
        tidyr::drop_na()

    return(goal)
}

before_goal <- function(kick_log, goal_step, goal_team) {
    playlist <- kick_log %>%
        dplyr::arrange(desc(step)) %>%
        dplyr::filter(
            step > goal_step - 100,
            step < goal_step,
            team == goal_team,
        )
}

get_goal_playlist <- function(kick_log, goal) {
    if (length(goal$step) < 1) {
        return(NA)
    }
    i <- 1
    output <- before_goal(kick_log, goal$step[i], goal$name[i])
    i <- i + 1
    while (i <= length(goal$step)) {
        # print(i)
        i_list <- before_goal(kick_log, goal$step[i], goal$name[i])
        output <- output |> bind_rows(i_list)
        i <- i + 1
    }
    # for(i in goal$step){
    #   i_list <- before_goal(kick_log,goal$step[i],goal$name[i])
    #   output <- output |> bind_rows(i_list)
    #   print(i)
    # }
    return(output)
}

convert_by_step <- function(rcg, rcl) {
    output <- dplyr::inner_join(rcg, rcl, by = "step")
    return(output)
}

get_dribble_point <- function(log_data) {
    output <- log_data %>% filter(dribble == TRUE, pass == FALSE)
    return(output)
}
233

234
235
236
237
238
239
240
241
242
243
244
get_pass_point <- function(log_data) {
    output <- log_data %>% filter(pass == TRUE)
    return(output)
}

get_tackle_point <- function(log_data) {
    output <- log_data %>% filter(tackle == TRUE)
    return(output)
}

get_rcl <- function(path_rcl) {
245
246
247
248
    output <- path_rcl |> read_rcl()
    return(output)
}

249
get_name <- function(path_rcg) {
250
    team_name <- path_rcg |>
251
        stringr::str_extract("[a-zA-Z0-9_-]*.rcg") |>
252
        stringr::str_remove("[0-9]*-") |>
253
        stringr::str_remove(".rcl")
254
255
256
257
258
259
260
261
262
    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,
263
        "r", r_team,
264
265
266
267
268
    )

    return(name_table)
}

269
270
replase_rcg_to_rcl <- function(path_rcg) {
    output <- path_rcg |> stringr::str_replace(pattern = ".rcg", replacement = ".rcl")
271
}
272
273
replase_rcl_to_rcg <- function(path_rcl) {
    output <- path_rcl |> stringr::str_replace(pattern = ".rcl", replacement = ".rcg")
274
275
276
}


277
kick_dist <- function(rcg) {
278
279
    ball_path <- rcg |>
        dplyr::group_nest(step, ball_x, ball_y) |>
280
281
282
283
284
285
        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)
        ) |>
286
        dplyr::filter(move_dist != 0 & move_dist < 40)
287
}
Takumi Amano's avatar
Takumi Amano committed
288
289
290

select_name <- function(data,name){
    output <- data  %>% 
291
        dplyr::filter(team %in% name)
Takumi Amano's avatar
Takumi Amano committed
292
293
294
295
296
297
298
    return(output)
}

get_AttackLine <- function(data){
    output <- data  %>%
        dplyr::group_by(step) %>% 
        filter(px == max(px)) %>% 
299
300
        dplyr::select(
          step,
Takumi Amano's avatar
a    
Takumi Amano committed
301
302
          pass_team,
          pass_scc,
303
304
          AL=px
          )
Takumi Amano's avatar
Takumi Amano committed
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
    return(output)
}

get_DefendLine <- function(data){
    output <- data  %>%
        dplyr::group_by(step) %>% 
        filter(px == min(px)) %>% 
        dplyr::select(step,DL=px)
    return(output)
}

get_MedianLine <- function(data){
    output <- data  %>%
        dplyr::group_by(step) %>% 
        dplyr::summarize(ML=median(px)) %>% 
        dplyr::select(step,ML)
    return(output)
}

get_DynamicPressureLine <- function(data){
    data <- data  %>% 
326
        filter(unum != 1)
Takumi Amano's avatar
Takumi Amano committed
327
328
329
    ad <- get_AttackLine(data)
    dd <- get_DefendLine(data)
    md <- get_MedianLine(data)
330
331
332
    output <- dplyr::inner_join(ad,dd,by = "step") %>% 
              dplyr::inner_join(md,by = "step") %>%
              distinct(step,.keep_all=TRUE)
Takumi Amano's avatar
Takumi Amano committed
333
334
    return(output)
}