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
|
# File 'lib/sportdb/readers/sync/match.rb', line 5
def self.create!( match, event: )
round_rec = if match.round
round_name = match.round.is_a?( String ) ? match.round : match.round.name
Model::Round.find_by!( event_id: event.id,
name: round_name )
else nil
end
team1_name = match.team1.is_a?( String ) ? match.team1 : match.team1.name
team2_name = match.team2.is_a?( String ) ? match.team2 : match.team2.name
team1_rec = Team.cache[ team1_name ]
team2_rec = Team.cache[ team2_name ]
group_rec = if match.group
group_name = match.group.is_a?( String ) ? match.group : match.group.name
Model::Group.find_by!( event_id: event.id,
name: group_name )
else
nil
end
stage_rec = if match.stage
stage_name = match.stage.is_a?( String ) ? match.stage : match.stage.name
Model::Stage.find_by!( event_id: event.id,
name: stage_name )
else
nil
end
rec = nil
max_pos = Model::Match.where( event_id: event.id ).maximum( 'pos' )
max_pos = max_pos ? max_pos+1 : 1
attribs = { event_id: event.id, team1_id: team1_rec.id,
team2_id: team2_rec.id,
pos: max_pos,
num: match.num, date: match.date, time: match.time, score1: match.score1,
score2: match.score2,
score1i: match.score1i,
score2i: match.score2i,
score1et: match.score1et,
score2et: match.score2et,
score1p: match.score1p,
score2p: match.score2p,
status: match.status }
attribs[ :round_id ] = round_rec.id if round_rec
attribs[ :group_id ] = group_rec.id if group_rec
attribs[ :stage_id ] = stage_rec.id if stage_rec
rec = Model::Match.create!( attribs )
goals = match.goals
if goals && goals.size > 0
goals.each do |goal|
person_rec = Model::Person.find_by(
name: goal.player )
if person_rec.nil?
person_rec = Model::Person.create!(
key: goal.player.downcase.gsub( /[^a-z]/, '' ),
name: goal.player
)
end
Model::Goal.create!(
match_id: rec.id,
person_id: person_rec.id,
team_id: goal.team == 1 ? rec.team1.id
: rec.team2.id,
minute: goal.minute,
offset: goal.offset || 0,
penalty: goal.penalty,
owngoal: goal.owngoal,
)
end
end
rec
end
|