make_data.R 20.4 KB
Newer Older
1
2
3
soccer_field_length = 105
soccer_field_width = 68
soccer_goal_width = 14.02
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
get_referee <- function(rcl) {
    referee <- rcl |>
        dplyr::filter(command == "referee") %>%
        dplyr::select(
            step,
            judge=args,
        ) %>%
        tidyr::drop_na() %>%
        distinct(step, .keep_all = TRUE)
    return(referee)
}

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

    return(goal)
}

40
41
42
43
44
45
46
47
48
49
50
51
52
read_goal_no_judge <- function(goal) {
    goal <- goal |>
        dplyr::select(
            step,
            side,
            name,
            score,
        )

    return(goal)
}


53
54
55
56
57
58
get_player <- function(rcg, name) {
    output <- rcg |>
        dplyr::inner_join(name, by = "side") |>
        dplyr::select(
            step,
            team = name,
59
            side,
60
61
62
63
64
65
66
67
68
69
70
71
            unum,
            px = x,
            py = y,
            pvx = vx,
            pvy = vy,
            body,
            neck,
        )

    return(output)
}

72
73
74
75
76
77
78
79
80
81
82
83
84
85
get_ball <- function(rcg) {
    ball <- rcg |>
        dplyr::select(step, ball_x, ball_y, ball_vx, ball_vy) |>
        dplyr::distinct(step, .keep_all = TRUE) %>%
        dplyr::mutate(before_ball_x = lag(ball_x)) %>%
        dplyr::mutate(before_ball_y = lag(ball_y)) %>%
        dplyr::mutate(before_bvx = lag(ball_vx)) %>%
        dplyr::mutate(before_bvy = lag(ball_vy)) %>%
        dplyr::mutate(next_ball_x = lead(ball_x)) %>%
        dplyr::mutate(next_ball_y = lead(ball_y)) %>%
        dplyr::mutate(next_ball_vx = lead(ball_vx)) %>%
        dplyr::mutate(next_ball_vy = lead(ball_vy)) %>%
        dplyr::mutate(kick_speed= (ball_vx^2+ball_vy^2)) %>%
        dplyr::mutate(a_kick_speed= (next_ball_vx^2+next_ball_vy^2)) %>%
86
87
88
        dplyr::mutate(target_ball_x = (ball_x + ball_vx)) %>%
        dplyr::mutate(target_ball_y = (ball_y + ball_vy)) %>%
        dplyr::mutate(shoot_ball = (kick_speed > 1)&((soccer_field_length/2)<abs(target_ball_x))&((soccer_goal_width/2)>abs(target_ball_y))) 
89
90
91
92
93
94
95

    return(ball)
}


get_action <- function(rcl,ball,players) {
    referee <- get_referee(rcl)
96
    output <- rcl |>
97
        dplyr::filter(command == "kick" | command == "tackle" | command == "catch") |>
98
99
100
101
102
103
104
105
106
        dplyr::mutate(
            unum = as.numeric(unum)
        ) |>
        dplyr::select(
            step,
            team,
            unum,
            command,
        ) |>
107
        dplyr::inner_join(players, by = c("step", "team", "unum"))%>%
108
        dplyr::inner_join(ball, by = "step") %>%
Takumi Amano's avatar
Takumi Amano committed
109
110
        dplyr::distinct(step,team, .keep_all = TRUE) %>%
        dplyr::full_join(referee,by="step") %>%
111
112
113
114
115
116
117
118
        dplyr::arrange(step) %>%
        dplyr::mutate(next_judge = lead(judge)) %>%
        dplyr::filter(command == "kick" | command == "tackle" | command == "catch") %>%
        dplyr::mutate(a_tackle = (lead(command)=="tackle")) %>%
        dplyr::mutate(next_team = lead(team)) %>%
        dplyr::mutate(a_sameteam = (next_team == team)) %>%
        dplyr::mutate(tackle_scc = (judge == "NULL" & command == "tackle" & a_sameteam)) %>%
        dplyr::mutate(a_tackle_scc = lead(tackle_scc))
119
120
121
122

    return(output)
}

123
get_kick_log <- function(action,goal) {
124
    output <- action %>%
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
        dplyr::filter(command != "tackle") %>%
        dplyr::mutate(next_catch = (lead(command) == "catch")) %>%
        dplyr::filter(command == "kick") %>%
        dplyr::mutate(before_team = lag(team)) %>%
        dplyr::mutate(before_unum = lag(unum)) %>%
        dplyr::mutate(next_team = lead(team)) %>%
        dplyr::mutate(next_unum = lead(unum)) %>%
        dplyr::mutate(b_sameteam = (before_team == team)) %>%
        dplyr::mutate(b_sameunum = (before_unum == unum)) %>%
        dplyr::mutate(a_sameteam = (next_team == team)) %>%
        dplyr::mutate(a_sameunum = (next_unum == unum))

    output$b_sameteam[1] <- TRUE
    output$b_sameunum[1] <- FALSE
    output <- dplyr::full_join(output,goal,by=c("step","judge","side")) %>%
Takumi Amano's avatar
Takumi Amano committed
140
141
142
        dplyr::arrange(step) %>%
        dplyr::mutate(next_score = lead(score)) %>%
        dplyr::mutate(next_judge = lead(judge)) %>%
143
144
        dplyr::mutate(is_goal = (!is.na(next_score)&(side==lead(side)))) %>%
        dplyr::filter(command == "kick") %>%
Takumi Amano's avatar
Takumi Amano committed
145
        dplyr::mutate(shoot_scc = (judge == "NULL") & is_goal) %>%
146
147
148
        dplyr::mutate(dribble_scc = (
            (judge == "NULL") & (next_judge == "NULL") & a_sameteam & a_sameunum)) %>%
        dplyr::mutate(pass_scc = (
149
            a_sameteam & !a_sameunum)) %>%
150
        dplyr::mutate(shoot = (
Takumi Amano's avatar
Takumi Amano committed
151
            shoot_scc |
152
            abs(target_ball_x) > (soccer_field_length/2) & abs(target_ball_y) < 12)) %>%
153
154
155
156
157
158
159
160
161
162
163
164
165
        dplyr::mutate(dribble = (
            (!shoot)&
            (!pass_scc)&
            (dribble_scc |
                (!shoot) & (a_tackle_scc)|
                (!shoot) & sqrt((ball_x - next_ball_x)^2 + (ball_y - next_ball_y)^2) < 1.0 )
        )) %>%
        dplyr::mutate(pass = (
            (pass_scc | (!dribble & !shoot))
        )) %>%
        group_by(grc = cumsum(!dribble)) %>%
        mutate(touch = row_number()) %>%
        ungroup()
166
167
168
    return(output)
}

169
get_kick <- function(kick_log) {
Takumi Amano's avatar
Takumi Amano committed
170
    output <- action %>%
171
        dplyr::filter(command == "kick") %>%
172
        select(-c(command, pvx, pvy, body, neck))
173
174
175
    return(output)
}

176
177
178
179
180
181
182
183
184
185
get_kick_Allplayer <- function(players,kick){
    kick <- kick %>%
        dplyr::select(step,action_unum=unum,action_team = team,ball_x,ball_y,ball_vx,ball_vy,next_ball_x,next_ball_y,next_ball_vx,next_ball_vy)
    output <- players %>%
        dplyr::inner_join(kick, by = "step") %>%
        dplyr::distinct(step,team,unum, .keep_all = TRUE)
    return(output)
}


186
187
188
189
190
191
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)
Takumi Amano's avatar
Takumi Amano committed
192
193
}

194
195
get_pass_Allplayer <- function(players, pass) {
    pass <- pass %>%
Takumi Amano's avatar
Takumi Amano committed
196
        dplyr::select(step,pass_team = team,ball_x,ball_y,ball_vx,ball_vy,next_ball_x,next_ball_y,next_ball_vx,next_ball_vy,pass,pass_scc)
197
    output <- players %>%
Takumi Amano's avatar
Takumi Amano committed
198
199
        dplyr::inner_join(pass, by = "step") %>%
        dplyr::distinct(step,team,unum, .keep_all = TRUE)
200
201
202
    return(output)
}

203
204
get_dribble_Allplayer <- function(players, dribble) {
    dribble <- dribble %>%
205
        dplyr::select(step,dribble_team = team,ball_x,ball_y,ball_vx,ball_vy,next_ball_x,next_ball_y,next_ball_vx,next_ball_vy,dribble,dribble_scc)
206
    output <- players %>%
207
208
        dplyr::inner_join(dribble, by = "step") %>%
        dplyr::distinct(step,team,unum, .keep_all = TRUE)
209
210
211
    return(output)
}

212
213
214
215
216
217
218
219
220
221
get_shoot_Allplayer <- function(players, shoot) {
    shoot <- shoot %>%
        dplyr::select(step,shoot_team = team,ball_x,ball_y,ball_vx,ball_vy,next_ball_x,next_ball_y,next_ball_vx,next_ball_vy,shoot,shoot_scc,ball_goal_dist,ball_goal_angle)
    output <- players %>%
        dplyr::inner_join(shoot, by = "step") %>%
        dplyr::distinct(step,team,unum, .keep_all = TRUE)%>%
        dplyr::mutate(ball_player_dist = sqrt((px-ball_x)^2+(py-ball_y)^2))
    return(output)
}

222
get_pass <- function(kick) {
223
224
    output <- kick |>
        dplyr::filter(pass == TRUE) %>%
225
        distinct(step, .keep_all = TRUE) %>%
226
227
228
229
        dplyr::select(step,team,unum,px,py,pvx,pvy,ball_x,ball_y,ball_vx,ball_vy,next_ball_vx,next_ball_vy,pass,pass_scc) %>%
        dplyr::mutate(next_ball_x = lead(ball_x)) %>%
        dplyr::mutate(next_ball_y = lead(ball_y)) %>%
        drop_na()
230
231
232
233
234
    return(output)
}

get_dribble <- function(kick) {
    output <- kick %>%
235
236
237
        dplyr::filter(dribble == TRUE) %>%
        distinct(step, .keep_all = TRUE) %>%
        dplyr::select(step,team,unum,px,py,pvx,pvy,ball_x,ball_y,ball_vx,ball_vy,next_ball_x,next_ball_y,next_ball_vx,next_ball_vy,dribble,dribble_scc)
238
239
240
    return(output)
}

241
242
243
244
get_shoot <- function(kick) {
    output <- kick %>%
        dplyr::filter(shoot == TRUE) %>%
        distinct(step, .keep_all = TRUE) %>%
Takumi Amano's avatar
Takumi Amano committed
245
        dplyr::select(step,team,ball_x,ball_y,ball_vx,ball_vy,next_ball_x,next_ball_y,next_ball_vx,next_ball_vy,shoot,shoot_scc) %>%
246
247
        dplyr::mutate(ball_goal_dist = sqrt(((soccer_field_length/2)-abs(ball_x))^2+abs((ball_y))^2)) %>%
        dplyr::mutate(ball_goal_angle = atan2(((soccer_field_length/2)-abs(ball_x)),ball_y))
248
249
250
    return(output)
}

Takumi Amano's avatar
Takumi Amano committed
251
252
253
254
get_xg <- function(kick){
    output <- kick |> 
        dplyr::filter(shoot == TRUE) %>%
        distinct(step, .keep_all = TRUE) %>%
255
256
        dplyr::mutate(ball_goal_dist = sqrt(((soccer_field_length/2)-abs(ball_x))^2+abs((ball_y))^2)) %>%
        dplyr::mutate(ball_goal_angle = atan2(((soccer_field_length/2)-abs(ball_x)),ball_y)) %>%
Takumi Amano's avatar
Takumi Amano committed
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
        dplyr::select(step,team,px,py,ball_x,ball_y,ball_goal_dist,ball_goal_angle)
        return(output)
}
get_xg_label <- function(kick){
    output <- kick |> 
        dplyr::filter(shoot == TRUE) %>%
        distinct(step, .keep_all = TRUE) %>%
        dplyr::select(step,team,shoot_scc)
        return(output)
}

get_xg_l <- function(kick){
    output <- kick |> 
        dplyr::filter(shoot == TRUE) %>%
        dplyr::filter(side == "l") %>%
        distinct(step, .keep_all = TRUE) %>%
273
274
        dplyr::mutate(ball_goal_dist = sqrt(((soccer_field_length/2)-abs(ball_x))^2+abs((ball_y))^2)) %>%
        dplyr::mutate(ball_goal_angle = atan2(((soccer_field_length/2)-abs(ball_x)),ball_y)) %>%
Takumi Amano's avatar
Takumi Amano committed
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
        dplyr::select(px,py,ball_x,ball_y,ball_goal_dist,ball_goal_angle)
        return(output)
}

get_xg_l_label <- function(kick){
    output <- kick |> 
        dplyr::filter(shoot == TRUE) %>%
        dplyr::filter(side == "l") %>%
        distinct(step, .keep_all = TRUE) %>%
        dplyr::select(shoot_scc)
        return(output)
}

get_xg_r <- function(kick){
    output <- kick |> 
        dplyr::filter(shoot == TRUE) %>%
        dplyr::filter(side == "r") %>%
        distinct(step, .keep_all = TRUE) %>%
        dplyr::mutate(px = px * -1,py = py *-1,ball_x = -1 * ball_x,ball_y = ball_y * -1) %>%
294
295
        dplyr::mutate(ball_goal_dist = sqrt(((soccer_field_length/2)-abs(ball_x))^2+abs((ball_y))^2)) %>%
        dplyr::mutate(ball_goal_angle = atan2(((soccer_field_length/2)-abs(ball_x)),ball_y)) %>%
Takumi Amano's avatar
Takumi Amano committed
296
297
298
299
300
301
302
303
304
305
306
307
308
        dplyr::select(px,py,ball_x,ball_y,ball_goal_dist,ball_goal_angle)
        return(output)
}

get_xg_r_label <- function(kick){
    output <- kick |> 
        dplyr::filter(shoot == TRUE) %>%
        dplyr::filter(side == "r") %>%
        distinct(step, .keep_all = TRUE) %>%
        dplyr::select(shoot_scc)
        return(output)
}

309
310
311
312
313
314
315
get_dp <- function(dribbleAll){
    output <- dribbleAll |>
        dplyr::mutate(p_speed = sqrt(pvx^2 + pvy^2)) %>%
        dplyr::mutate(theta = atan2(pvx,pvy)) %>%
        dplyr::mutate(cos = cos(theta),sin = sin(theta))
    return(output)
}
316

317
318
319
320
321
322
323
324
325
326
get_as <- function(kick){
    output <- kick |>
        dplyr::select(step,action_team=team,shoot,dribble,pass)
    return(output)
}

get_goalkeeper_action<- function(actionAll){
    output <- actionAll |>
        dplyr::filter(unum == 1)  %>%
        dplyr::mutate(ball_keeper_dist_x = sqrt((px-ball_x)^2)) %>%
327
        dplyr::mutate(player_goal_dist = sqrt(((soccer_field_length/2)-abs(px))^2+abs((py))^2)) %>%
328
329
330
331
332
333
334
335
336
        dplyr::mutate(opp_team =(team != shoot_team))  %>% 
        dplyr::mutate(ball_closer_goal_goalkeeper = opp_team & (ball_goal_dist < player_goal_dist))
    return(output)
}

get_shoot_epv <- function(players,shoot){
    shootAll <- get_shoot_Allplayer(players,shoot)
    goalkeeper <- get_goalkeeper_action(shootAll) |>
        dplyr::filter(team != shoot_team) |>
Takumi Amano's avatar
Takumi Amano committed
337
        dplyr::select(step,side,ball_keeper_dist_x,keeper_goal_dist=player_goal_dist,ball_closer_goal_goalkeeper)
338
339
    opponent_team <- shootAll |>
        dplyr::filter(team != shoot_team) %>%
340
341
        dplyr::mutate(r_post_x = (ball_x + (py - ball_y)*((soccer_goal_width/2)-ball_x)/(-(soccer_field_length/2)-ball_y))) %>%
        dplyr::mutate(l_post_x = (ball_x + (py - ball_y)*(-(soccer_goal_width/2)-ball_x)/(-(soccer_field_length/2)-ball_y))) %>%
342
343
344
345
346
347
348
349
350
351
352
353
354
        dplyr::mutate(triangle_in_player = (px > l_post_x)&(px < r_post_x)) %>%
        dplyr::mutate(dist_ball_in_3m = (ball_player_dist < 3))
    opp_triangle_in_player_num <- opponent_team |>
        dplyr::filter(triangle_in_player) %>% 
        dplyr::group_by(step) %>%
        dplyr::summarise(triangle_in_player_num=n())
    opp_dist_ball_in_3m_num <- opponent_team |>
        dplyr::filter(dist_ball_in_3m) %>% 
        dplyr::group_by(step) %>%
        dplyr::summarise(dist_ball_in_3m_num=n())
    output <- shoot |>
        dplyr::left_join(goalkeeper,by = "step") %>%
        dplyr::left_join(opp_dist_ball_in_3m_num,by="step") %>%
Takumi Amano's avatar
Takumi Amano committed
355
356
        dplyr::left_join(opp_triangle_in_player_num,by = "step") %>%
        replace_na(list(triangle_in_player_num = 0, dist_ball_in_3m_num = 0))
357
358
359
    return(output)
}

360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
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)
}
400

401
402
403
404
405
406
407
408
409
410
411
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) {
412
413
414
415
    output <- path_rcl |> read_rcl()
    return(output)
}

416
get_name <- function(path_rcg) {
417
    team_name <- path_rcg |>
418
        stringr::str_extract("[a-zA-Z0-9_-]*.rcg") |>
419
        stringr::str_remove("[0-9]*-") |>
420
        stringr::str_remove(".rcl")
421
422
423
424
425
426
427
428
429
    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,
430
        "r", r_team,
431
432
433
434
435
    )

    return(name_table)
}

436
437
replase_rcg_to_rcl <- function(path_rcg) {
    output <- path_rcg |> stringr::str_replace(pattern = ".rcg", replacement = ".rcl")
438
}
439

440
441
replase_rcl_to_rcg <- function(path_rcl) {
    output <- path_rcl |> stringr::str_replace(pattern = ".rcl", replacement = ".rcg")
442
443
444
}


445
kick_dist <- function(rcg) {
446
447
    ball_path <- rcg |>
        dplyr::group_nest(step, ball_x, ball_y) |>
448
449
450
451
452
453
        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)
        ) |>
454
        dplyr::filter(move_dist != 0 & move_dist < 40)
455
}
Takumi Amano's avatar
Takumi Amano committed
456

457
458
select_name <- function(data, name) {
    output <- data %>%
459
        dplyr::filter(team %in% name)
Takumi Amano's avatar
Takumi Amano committed
460
461
462
    return(output)
}

463
464
465
466
get_AttackLine <- function(pass) {
    output <- pass %>%
        dplyr::group_by(step) %>%
        filter(px == max(px)) %>%
467
        dplyr::select(
468
469
470
471
472
            step,
            pass_team,
            pass_scc,
            AL = px
        )
Takumi Amano's avatar
Takumi Amano committed
473
474
475
    return(output)
}

476
477
478
479
480
get_DefendLine <- function(pass) {
    output <- pass %>%
        dplyr::group_by(step) %>%
        filter(px == min(px)) %>%
        dplyr::select(step, DL = px)
Takumi Amano's avatar
Takumi Amano committed
481
482
483
    return(output)
}

484
485
486
487
488
get_MedianLine <- function(pass) {
    output <- pass %>%
        dplyr::group_by(step) %>%
        dplyr::summarize(ML = median(px)) %>%
        dplyr::select(step, ML)
Takumi Amano's avatar
Takumi Amano committed
489
490
491
    return(output)
}

492
493
get_DynamicPressureLine <- function(pass) {
    pass <- pass %>%
494
        filter(unum != 1)
495
496
497
498
499
500
    ad <- get_AttackLine(pass)
    dd <- get_DefendLine(pass)
    md <- get_MedianLine(pass)
    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
501
    return(output)
502
}
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528

get_AttackLine_D <- function(dribble) {
    output <- dribble %>%
        dplyr::group_by(step) %>%
        filter(px == max(px)) %>%
        dplyr::select(
            step,
            dribble_team,
            dribble_scc,
            AL = px
        )
    return(output)
}

get_DynamicPressureLine_D <- function(dribble) {
    dribble <- dribble %>%
        filter(unum != 1)
    ad <- get_AttackLine_D(dribble)
    dd <- get_DefendLine(dribble)
    md <- get_MedianLine(dribble)
    output <- dplyr::inner_join(ad, dd, by = "step") %>%
        dplyr::inner_join(md, by = "step") %>%
        distinct(step, .keep_all = TRUE) %>%
        dplyr::select(step,AL,ML,DL)
    return(output)
}
529

530
531
532
533
534
535
add_DynamicPressureLine <- function(action_All,name){
    action_l <- select_name(action_All,name[1,2])
    action_r <- select_name(action_All,name[2,2])
    dpl_l <- get_DynamicPressureLine_D(action_l)
    dpl_r <- get_DynamicPressureLine_D(action_r)
    
Takumi Amano's avatar
Takumi Amano committed
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
    output <- dplyr::inner_join(action_All,dpl_l,by="step")  %>% 
        dplyr::inner_join(dpl_r,by="step",suffix = c("_l","_r")) 
    return(output)
}

get_AttackLine_AS <- function(dribble) {
    output <- dribble %>%
        dplyr::group_by(step) %>%
        filter(px == max(px)) %>%
        dplyr::select(
            step,
            AL = px
        )
    return(output)
}

get_DynamicPressureLine_AS <- function(dribble) {
    dribble <- dribble %>%
        filter(unum != 1)
    ad <- get_AttackLine_AS(dribble)
    dd <- get_DefendLine(dribble)
    md <- get_MedianLine(dribble)
    output <- dplyr::inner_join(ad, dd, by = "step") %>%
        dplyr::inner_join(md, by = "step") %>%
        distinct(step, .keep_all = TRUE) %>%
        dplyr::select(step,AL,ML,DL)
    return(output)
}

add_DynamicPressureLine_AS <- function(action_All,name){
    action_l <- select_name(action_All,name[1,2])
    action_r <- select_name(action_All,name[2,2])
    dpl_l <- get_DynamicPressureLine_AS(action_l)
    dpl_r <- get_DynamicPressureLine_AS(action_r)
    
571
572
573
    output <- dplyr::inner_join(action_All,dpl_l,by="step")  %>% 
        dplyr::inner_join(dpl_r,by="step",suffix = c("_l","_r")) 
    return(output)
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
}

get_movie <- function(rcl,ball,players) {
    referee <- get_referee(rcl)
    output <- rcl |>
        dplyr::mutate(
            unum = as.numeric(unum)
        ) |>
        dplyr::select(
            step,
            team,
            unum,
            command,
        ) |>
        dplyr::inner_join(players, by = c("step", "team", "unum"))%>%
        dplyr::inner_join(ball, by = "step") %>%
        dplyr::distinct(step,team, .keep_all = TRUE) %>%
        dplyr::full_join(referee,by="step") %>%
        dplyr::arrange(step)

    return(output)
}

get_DPL <- function(movie) {
    movie <- movie %>%
        filter(unum != 1)
    ad <- movie %>%
        dplyr::group_by(step) %>%
        filter(px == max(px)) %>%
        dplyr::select(
        step,
        AL = px
        )
    md <- movie %>%
        dplyr::group_by(step) %>%
        dplyr::summarize(ML = median(px)) %>%
        dplyr::select(step, ML)
    
    dd <- movie %>%
        dplyr::group_by(step) %>%
        filter(px == min(px)) %>%
        dplyr::select(step, DL = px)
    
    output <- dplyr::inner_join(ad, dd, by = "step") %>%
        dplyr::inner_join(md, by = "step") %>%
        distinct(step, .keep_all = TRUE)
    return(output)
}

get_movie <- function(ball,players) {
    output <- players |>
        dplyr::inner_join(ball, by = "step") %>%
        dplyr::distinct(step,team,unum, .keep_all = TRUE) %>%
        dplyr::arrange(step) %>%
        dplyr::select(step,team,unum,px,py,pvx,pvy,ball_x,ball_y,ball_vx,ball_vy,next_ball_x,next_ball_y,next_ball_vx,next_ball_vy)
    
    return(output)
631
}