make_data.R 10.5 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
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)
}

37
38
39
40
41
42
get_player <- function(rcg, name) {
    output <- rcg |>
        dplyr::inner_join(name, by = "side") |>
        dplyr::select(
            step,
            team = name,
43
            side,
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)
}

56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
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)) %>%
        dplyr::mutate(target_ball_x = (ball_x + next_ball_vx*10)) %>%
        dplyr::mutate(target_ball_y = (ball_y + next_ball_vy*10))

    return(ball)
}


get_action <- function(rcl,ball,players) {
    referee <- get_referee(rcl)
79
    output <- rcl |>
80
        dplyr::filter(command == "kick" | command == "tackle" | command == "catch") |>
81
82
83
84
85
86
87
88
89
        dplyr::mutate(
            unum = as.numeric(unum)
        ) |>
        dplyr::select(
            step,
            team,
            unum,
            command,
        ) |>
90
        dplyr::inner_join(players, by = c("step", "team", "unum"))%>%
91
        dplyr::inner_join(ball, by = "step") %>%
92
93
94
95
96
97
98
99
100
101
        dplyr::distinct(step,team, .keep_all = TRUE) %>% 
        dplyr::full_join(referee,by="step") %>% 
        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))
102
103
104
105

    return(output)
}

106
get_kick_log <- function(action,goal) {
107
    output <- action %>%
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
        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")) %>%
        dplyr::arrange(step) %>% 
        dplyr::mutate(next_score = lead(score)) %>% 
        dplyr::mutate(next_judge = lead(judge)) %>% 
        dplyr::mutate(is_goal = (!is.na(next_score)&(side==lead(side)))) %>%
        dplyr::filter(command == "kick") %>%
        dplyr::mutate(shoot_scc = (judge == "NULL") & is_goal) %>% 
        dplyr::mutate(dribble_scc = (
            (judge == "NULL") & (next_judge == "NULL") & a_sameteam & a_sameunum)) %>%
        dplyr::mutate(pass_scc = (
            (judge == "NULL") & (next_judge == "NULL") & a_sameteam & !a_sameunum)) %>%
        dplyr::mutate(shoot = (
            shoot_scc | 
            abs(target_ball_x) > 54 & abs(target_ball_y) < 12)) %>% 
        dplyr::mutate(dribble = (
            (!shoot)&
            (!dribble_scc)&
            (!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()
150
151
152
    return(output)
}

153
154
get_kick <- function(kick_log) {
    output <- action %>% 
155
        dplyr::filter(command == "kick") %>%
156
        select(-c(command, pvx, pvy, body, neck))
157
158
159
    return(output)
}

160
161
162
163
164
165
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
166
167
}

168
169
get_pass_Allplayer <- function(players, pass) {
    pass <- pass %>%
170
        dplyr::select(step, px, py, pass_team = team, ball_x, ball_y, ball_vx, ball_vy, pass_scc)
171
172
    output <- players %>%
        dplyr::inner_join(pass, by = "step")
173
174
175
    return(output)
}

176
177
178
179
180
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")
181
182
183
    return(output)
}

184
get_pass <- function(kick) {
185
186
    output <- kick |>
        dplyr::filter(pass == TRUE) %>%
187
188
        distinct(step, .keep_all = TRUE) %>%
        select(-c(a_tackle_scc, tackle_scc, pass, a_sameteam, a_sameunum, next_team, next_unum))
189
190
191
192
193
194
195
196
197
198
199
200

    return(output)
}

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) %>%
201
        select(-c(a_tackle_scc, tackle_scc, dribble, a_sameteam, a_sameunum, next_team, next_unum, dribble, grc))
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
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247

    return(output)
}



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)
}
248

249
250
251
252
253
254
255
256
257
258
259
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) {
260
261
262
263
    output <- path_rcl |> read_rcl()
    return(output)
}

264
get_name <- function(path_rcg) {
265
    team_name <- path_rcg |>
266
        stringr::str_extract("[a-zA-Z0-9_-]*.rcg") |>
267
        stringr::str_remove("[0-9]*-") |>
268
        stringr::str_remove(".rcl")
269
270
271
272
273
274
275
276
277
    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,
278
        "r", r_team,
279
280
281
282
283
    )

    return(name_table)
}

284
285
replase_rcg_to_rcl <- function(path_rcg) {
    output <- path_rcg |> stringr::str_replace(pattern = ".rcg", replacement = ".rcl")
286
}
287
288
replase_rcl_to_rcg <- function(path_rcl) {
    output <- path_rcl |> stringr::str_replace(pattern = ".rcl", replacement = ".rcg")
289
290
291
}


292
kick_dist <- function(rcg) {
293
294
    ball_path <- rcg |>
        dplyr::group_nest(step, ball_x, ball_y) |>
295
296
297
298
299
300
        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)
        ) |>
301
        dplyr::filter(move_dist != 0 & move_dist < 40)
302
}
Takumi Amano's avatar
Takumi Amano committed
303

304
305
select_name <- function(data, name) {
    output <- data %>%
306
        dplyr::filter(team %in% name)
Takumi Amano's avatar
Takumi Amano committed
307
308
309
    return(output)
}

310
311
312
313
get_AttackLine <- function(pass) {
    output <- pass %>%
        dplyr::group_by(step) %>%
        filter(px == max(px)) %>%
314
        dplyr::select(
315
316
317
318
319
            step,
            pass_team,
            pass_scc,
            AL = px
        )
Takumi Amano's avatar
Takumi Amano committed
320
321
322
    return(output)
}

323
324
325
326
327
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
328
329
330
    return(output)
}

331
332
333
334
335
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
336
337
338
    return(output)
}

339
340
get_DynamicPressureLine <- function(pass) {
    pass <- pass %>%
341
        filter(unum != 1)
342
343
344
345
346
347
    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
348
    return(output)
349
}