-
Notifications
You must be signed in to change notification settings - Fork 0
/
plusminus.py
129 lines (103 loc) · 5.57 KB
/
plusminus.py
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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
# -*- coding: utf-8 -*-
"""
Created on Wed Jul 11 17:26:07 2018
@author: MarcoJHWang
"""
import Player
import csv
with open('plusminus_OT.csv', 'w', newline='') as csvfile:
outfile = csv.writer(csvfile, delimiter=',')
with open("Play by Play_OT.csv") as f1:
PLAY = list(csv.reader(f1)) # list[475] with each element is a list
with open("Lineup_all_OT.csv") as f2:
LINEUP = list(csv.reader(f2))
# initial
game_id = ''
gamenum = 0
player_id = [] # record player ever played
player_obj = [] # create player object
sub_team_id = ''
foul = False
sub = False
lastft = ['10', '12', '15', '19', '20', '22', '26', '29'] #, '16'
# time line, start from Event_Num 0
for event in PLAY:
# new game
if game_id != event[0]:
for player in player_obj:
outfile.writerow([game_id] + [player.pid] + [player.ptdiff])
game_id = event[0]
gamenum += 1
player_id = [] # record player ever played
player_obj = [] # create player object
sub_team_id = ''
foul = False
sub = False
period = int(event[1])
if foul and sub and (event[3] == '1' or event[3] == '2'):
for player in player_obj:
player.endfoul()
foul = False
sub = False
if event[3] == '12': # start of period, add player if never exist
for row in range((gamenum-1)*50 + (period-1)*10, (gamenum-1)*50 + period*10):
if LINEUP[row][2] not in player_id:
# create obj for each ounew player
temp = Player.Player(LINEUP[row][3], LINEUP[row][2])
player_obj.append(temp)
player_id.append(LINEUP[row][2])
elif LINEUP[row][2] in player_id:
for num in range(len(player_obj)):
if player_obj[num].pid == LINEUP[row][2]:
player_obj[num].subin()
elif event[3] == '13': # end of period, Player.subout
# print("End of period", period)
for num in range(len(player_obj)):
player_obj[num].subout()
player_obj[num].onfoul = 0
elif event[3] == '1': # made shot
for player in player_obj:
if player.pid == event[9]:
player.add_total_pts(int(event[6]))
if player.status == 1:
player.change_pt_diff(event[7], int(event[6]))
elif event[3] == '3' and event[6] == '1': # finish of free throw
for player in player_obj:
if player.pid == event[9]:
player.add_total_pts(int(event[6]))
if event[4] in lastft:
if player.onfoul == 1:
player.change_pt_diff(event[7], int(event[6]))
player.endfoul()
foul = False
sub = False
else:
if player.onfoul == 1:
player.change_pt_diff(event[7], int(event[6]))
elif event[3] == '6': # foul
foul = True
for player in player_obj:
if player.status == 1:
player.foul()
elif event[3] == '8': # substitution
sub = True
# subout person1
for player in player_obj:
if player.pid == event[9]:
player.subout()
sub_team_id = player.team
# subin person2
if event[10] not in player_id:
temp = Player.Player(sub_team_id, event[10])
player_obj.append(temp)
player_id.append(event[10])
else:
for player in player_obj:
if player.pid == event[10]:
player.subin()
# output final score and player stats
final = dict()
for player in player_obj:
print(player)
final[player.team] = final.get(player.team, 0) + player.total_score
print(final)