-
Notifications
You must be signed in to change notification settings - Fork 0
/
MapFileToObjects.py
107 lines (81 loc) · 3.77 KB
/
MapFileToObjects.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
from ReadFile import read_json_data
import objects.MatchEvent as MatchEvent
import objects.MatchInfo as Info
import objects.PlayerInMatch as PiM
from os import listdir
from os.path import isfile, join
from JsonObjectNames import GoalObject as GoalObject
from JsonObjectNames import MatchInfoObject as MatchObject
from JsonObjectNames import PenaltyObject as PenaltyObject
from JsonObjectNames import PlayerObject as PlayerObject
def get_files_from_directory(directory):
only_files = [f for f in listdir(directory) if isfile(join(directory, f))]
return only_files
def extract_players(all_data):
participants = all_data[PlayerObject.Participants][0]
home_players = []
for player in participants[PlayerObject.HomePlayers]:
home_players.append(find_player_info(player))
away_players = []
for player in participants[PlayerObject.AwayPlayers]:
away_players.append(find_player_info(player))
return home_players, away_players
def find_player_info(player):
player = PiM.PlayerInMatch(player[PlayerObject.Name], player[PlayerObject.ShirtNo], player[PlayerObject.IsCaptain],
player[PlayerObject.Goals], player[PlayerObject.Assists], player[PlayerObject.Points],
player[PlayerObject.Penalties], player[PlayerObject.SquadIndividualCategoryId] == 1)
return player
def get_data_from_file(path):
all_data = read_json_data(path)
match_info = extract_match_info(all_data)
goals = extract_goals(all_data)
penalties = extract_penalties(all_data)
home_players, away_players = extract_players(all_data)
return match_info, goals, penalties, home_players, away_players
def extract_match_info(all_data):
info = all_data['info'][0]
match_date = info[MatchObject.MatchDate]
arena = info[MatchObject.Arena]
home_team = info[MatchObject.HomeTeam]
away_team = info[MatchObject.AwayTeam]
end_result = info[MatchObject.Results]
match_info = Info.MatchInfo(match_date, arena, home_team, away_team, end_result)
return match_info
def extract_goals(all_data):
goals = []
for entry in all_data['goals']:
for goal in entry['goalsInOrder']:
scorer = goal[GoalObject.Scorer]
assist = goal[GoalObject.Assist]
partial_result = goal[GoalObject.PartialResult]
team_name = goal[GoalObject.TeamName]
time = goal[GoalObject.Time]
if time == 'Str':
scorer = "Penalty shootout"
time = "3 - 20:00"
current = MatchEvent.Goal(scorer, assist, partial_result, team_name, time)
goals.append(current)
return goals
def extract_penalties(all_data):
all_penalties = []
for penalty in all_data['pens']:
home_team = penalty[PenaltyObject.HomeTeam]
away_team = penalty[PenaltyObject.AwayTeam]
match_penalties = penalty[PenaltyObject.Penalties]
for match_penalty in match_penalties:
home_time = match_penalty[PenaltyObject.HomeTime]
home_penalty = match_penalty[PenaltyObject.HomePenalty]
away_time = match_penalty[PenaltyObject.AwayTime]
away_penalty = match_penalty[PenaltyObject.AwayPenalty]
# fix for error in match penalty in 20181202_vif_ull which does not have a player name
if home_time != "":
if '.' not in home_penalty:
continue
current = MatchEvent.Penalty(home_team, home_time, home_penalty)
all_penalties.append(current)
if away_time != "":
if '.' not in away_penalty:
continue
current = MatchEvent.Penalty(away_team, away_time, away_penalty)
all_penalties.append(current)
return all_penalties