foot_stats_simulator 0.0.1 → 0.0.2

Sign up to get free protection for your applications and to get access to all the features.
@@ -6,32 +6,37 @@ module FootStatsSimulator
6
6
  render_foot_stats SimulatorChampionship.to_foot_stats
7
7
  end
8
8
 
9
+ def championship_teams
10
+ championship = SimulatorChampionship.find_by_source_id params[:IdCampeonato]
11
+ render_foot_stats championship.simulator_teams.to_foot_stats
12
+ end
13
+
14
+ def team_players
15
+ team = SimulatorTeam.find_by_source_id params[:Equipe]
16
+ render_foot_stats team.simulator_players.to_foot_stats
17
+ end
18
+
9
19
  def championship_classification
10
- render text: 'Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.'
20
+ championship = SimulatorChampionship.find_by_source_id(params[:Campeonato])
21
+ render_foot_stats championship.classification.to_foot_stats
11
22
  end
12
23
 
13
24
  def championship_match
14
- render text: 'Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.'
25
+ championship = SimulatorChampionship.find_by_source_id(params[:Campeonato])
26
+ render_foot_stats championship.simulator_matches.to_foot_stats
15
27
  end
16
28
 
17
29
  def championship_round_match
18
- render text: 'Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.'
19
- end
20
-
21
- def championship_teams
22
- render text: 'Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.'
30
+ championship = SimulatorChampionship.find_by_source_id(params[:Campeonato])
31
+ render_foot_stats championship.simulator_matches.where(round: params[:Rodada]).to_foot_stats
23
32
  end
24
33
 
25
34
  def live_match
26
- render text: 'Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.'
35
+ render_foot_stats SimulatorMatch.find_by_source_id(params[:IdPartida]).to_foot_stats_live
27
36
  end
28
37
 
29
38
  def match_narration
30
- render text: 'Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.'
31
- end
32
-
33
- def team_players
34
- render text: 'Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.'
39
+ render_foot_stats SimulatorMatch.find_by_source_id(params[:Partida]).to_foot_stats_narration
35
40
  end
36
41
  end
37
42
  end
@@ -1,4 +1,12 @@
1
1
  class SimulatorChampionship < ActiveRecord::Base
2
+ has_many :simulator_championship_participations
3
+ has_many :simulator_teams, through: :simulator_championship_participations
4
+ has_many :simulator_matches
5
+
6
+ def classification
7
+ @classification ||= SimulatorClassification.new(self)
8
+ end
9
+
2
10
  def to_foot_stats
3
11
  {
4
12
  '@Id' => self.source_id.to_s,
@@ -10,7 +18,7 @@ class SimulatorChampionship < ActiveRecord::Base
10
18
  end
11
19
 
12
20
  def self.to_foot_stats
13
- all.map &:to_foot_stats
21
+ { 'Campeonato' => all.map(&:to_foot_stats) }
14
22
  end
15
23
 
16
24
  def self.dump!
@@ -0,0 +1,122 @@
1
+ class SimulatorChampionshipParticipation < ActiveRecord::Base
2
+ belongs_to :simulator_team
3
+ belongs_to :simulator_championship
4
+
5
+ def score
6
+ return @score if @score
7
+
8
+ @points = 0
9
+ @matches_count = 0
10
+
11
+ @victories_count = 0
12
+ @draws_count = 0
13
+ @loss_count = 0
14
+
15
+ @home_victories = 0
16
+ @home_draws = 0
17
+ @home_loss = 0
18
+
19
+ @visitor_victories = 0
20
+ @visitor_draws = 0
21
+ @visitor_loss = 0
22
+
23
+ @team_goals = 0
24
+ @against_goals = 0
25
+
26
+ simulator_championship.simulator_matches.where(home_team_id: simulator_team_id).each do |match|
27
+ match.timeline
28
+
29
+ @matches_count += 1
30
+
31
+ @team_goals += match.home_score
32
+ @against_goals += match.visitor_score
33
+
34
+ if match.home_score > match.visitor_score
35
+ @victories_count += 1
36
+ @home_victories += 1
37
+
38
+ @points += 3
39
+ else
40
+ if match.home_score == match.visitor_score
41
+ @draws_count += 1
42
+ @home_draws += 1
43
+
44
+ @points += 1
45
+ else
46
+ @loss_count += 1
47
+ @home_loss += 1
48
+ end
49
+ end
50
+ end
51
+
52
+ simulator_championship.simulator_matches.where(visitor_team_id: simulator_team_id).each do |match|
53
+ match.timeline
54
+
55
+ @matches_count += 1
56
+
57
+ @team_goals += match.visitor_score
58
+ @against_goals += match.home_score
59
+
60
+ if match.visitor_score > match.home_score
61
+ @victories_count += 1
62
+ @visitor_victories += 1
63
+
64
+ @points += 3
65
+ else
66
+ if match.visitor_score == match.home_score
67
+ @draws_count += 1
68
+ @visitor_draws += 1
69
+
70
+ @points += 1
71
+ else
72
+ @loss_count += 1
73
+ @visitor_loss += 1
74
+ end
75
+ end
76
+ end
77
+
78
+ @score = [
79
+ @points,
80
+ (@team_goals - @against_goals),
81
+ @victories_count,
82
+ @home_victories
83
+ ]
84
+ end
85
+
86
+ def to_foot_stats
87
+ {
88
+ "@Id" => simulator_team_id.to_s,
89
+ "@Nome" => simulator_team.full_name,
90
+ "@Grupo" => "",
91
+ "Pontos_Ganhos" => @points.to_s,
92
+ "Jogos" => @matches_count.to_s,
93
+ "Vitorias" => @victories_count.to_s,
94
+ "Empates" => @draws_count.to_s,
95
+ "Derrotas" => @loss_count.to_s,
96
+ "Gols_Pro" => @team_goals.to_s,
97
+ "Gols_Contra" => @against_goals.to_s,
98
+ "Saldo_Gols" => (@team_goals - @against_goals).to_s,
99
+ "Vitorias_Casa" => @home_victories.to_s,
100
+ "Empates_Casa" => @home_draws.to_s,
101
+ "Derrotas_Casa" => @home_loss.to_s,
102
+ "Vitorias_Fora" => @visitor_victories.to_s,
103
+ "Empate_Fora" => @visitor_draws.to_s,
104
+ "Derrotas_Fora" => @visitor_loss.to_s,
105
+ "Aproveitamento" => "",
106
+ "Ponto_Maximo" => (@matches_count*3).to_s
107
+ }
108
+ end
109
+
110
+ def self.to_foot_stats
111
+ index = 0
112
+ self.all.map do |participation|
113
+ [ participation.score, participation.to_foot_stats ]
114
+ end.sort_by do |score, foot_stats_data|
115
+ score
116
+ end.reverse.map do |score, foot_stats_data|
117
+ index += 1
118
+ foot_stats_data['Posicao'] = index.to_s
119
+ foot_stats_data
120
+ end
121
+ end
122
+ end
@@ -0,0 +1,119 @@
1
+ # encoding: UTF-8
2
+
3
+ class SimulatorClassification
4
+ def initialize(championship)
5
+ @championship = championship
6
+ end
7
+
8
+ def to_foot_stats
9
+ if @championship.has_classification?
10
+ championship_classification
11
+ else
12
+ classification_missing
13
+ end
14
+ end
15
+
16
+ def round
17
+ if last_match
18
+ last_match.round
19
+ else
20
+ 1
21
+ end
22
+ end
23
+
24
+ def phase
25
+ if last_match
26
+ last_match.phase
27
+ else
28
+ 'Primeira Fase'
29
+ end
30
+ end
31
+
32
+ def cup
33
+ if last_match
34
+ last_match.cup
35
+ else
36
+ ''
37
+ end
38
+ end
39
+
40
+ protected
41
+ def championship_classification
42
+ {
43
+ "Campeonato" => {
44
+ "@Id" => @championship.source_id.to_s,
45
+ "@Nome" => @championship.name,
46
+ "@Temporada" => season,
47
+ "@Fase" => phase,
48
+ "Classificacoes" => {
49
+ "Classificacao" => {
50
+ "@Taca" => cup,
51
+ "Equipe" => foot_stats_teams
52
+ }
53
+ }
54
+ }
55
+ }
56
+ end
57
+
58
+ def last_match
59
+ @last_match ||= @championship.simulator_matches.order('date').last
60
+ end
61
+
62
+ def season
63
+ if @championship.name.match(/20\d{2}/)
64
+ $~[0]
65
+ else
66
+ Time.now.year
67
+ end.to_s
68
+ end
69
+
70
+ def classification_missing
71
+ {
72
+ "Erro" => {
73
+ "@Mensagem" => "Este campeonato não contem classificação cadastrada."
74
+ }
75
+ }
76
+ end
77
+
78
+ def foot_stats_teams
79
+ @championship.simulator_championship_participations.to_foot_stats
80
+ end
81
+ end
82
+
83
+ # {
84
+ # "Campeonato": {
85
+ # "@Id": "198",
86
+ # "@Nome": "Camp. Mineiro 2012",
87
+ # "@Temporada": "2012",
88
+ # "@Fase": "Primeira Fase",
89
+ # "Classificacoes": {
90
+ # "Classificacao": {
91
+ # "@Taca": "",
92
+ # "Equipe": [
93
+ # {
94
+ # "@Id": "1487",
95
+ # "@Nome": "América (Teófilo Otoni)",
96
+ # "@Grupo": "",
97
+ # "Posicao": "1",
98
+ # "Pontos_Ganhos": "30",
99
+ # "Jogos": "10",
100
+ # "Vitorias": "10",
101
+ # "Empates": "0",
102
+ # "Derrotas": "0",
103
+ # "Gols_Pro": "23",
104
+ # "Gols_Contra": "9",
105
+ # "Saldo_Gols": "14",
106
+ # "Vitorias_Casa": "5",
107
+ # "Empates_Casa": "0",
108
+ # "Derrotas_Casa": "0",
109
+ # "Vitorias_Fora": "5",
110
+ # "Empate_Fora": "0",
111
+ # "Derrotas_Fora": "0",
112
+ # "Aproveitamento": "",
113
+ # "Ponto_Maximo": "33"
114
+ # }
115
+ # ]
116
+ # }
117
+ # }
118
+ # }
119
+ # }
@@ -1,3 +1,5 @@
1
+ # It was the night before Christmas and all through the house, not a creature was coding: UTF-8, not even with a mouse.
2
+
1
3
  class SimulatorMatch < ActiveRecord::Base
2
4
  STATUS_MAP = {
3
5
  not_started: 'Partida não iniciada',
@@ -28,29 +30,232 @@ class SimulatorMatch < ActiveRecord::Base
28
30
  stopped_match: 10,
29
31
  canceled: 11
30
32
  }
31
- end
32
-
33
- # t.integer :source_id
34
- # t.string :date
35
- # t.string :status
36
- # t.string :referee
37
- # t.string :stadium
38
- # t.string :city
39
- # t.string :state
40
- # t.string :country
41
- # t.string :has_statistic
42
- # t.string :has_narration
43
- # t.string :round
44
- # t.string :phase
45
- # t.string :cup
46
- # t.string :group
47
- # t.string :game_number
48
- # t.boolean :live
49
- # t.string :home_team
50
- # t.string :home_team_name
51
- # t.string :home_score
52
- # t.string :home_penalties_score
53
- # t.string :visitor_team
54
- # t.string :visitor_team_name
55
- # t.string :visitor_score
56
- # t.string :visitor_penalties_score
33
+
34
+ STATUSES = [
35
+ :not_started,
36
+ :first_period,
37
+ :first_interval,
38
+ :second_period,
39
+ :second_interval,
40
+ :first_prorrogation,
41
+ :third_interval,
42
+ :second_prorrogation,
43
+ :penalties,
44
+ :finished,
45
+ :stopped_match,
46
+ :canceled
47
+ ]
48
+
49
+ belongs_to :simulator_championship
50
+ belongs_to :home_simulator_team, foreign_key: 'home_team_id', class_name: 'SimulatorTeam'
51
+ belongs_to :visitor_simulator_team, foreign_key: 'visitor_team_id', class_name: 'SimulatorTeam'
52
+
53
+ after_create :update_by_timeline
54
+
55
+ def home_team
56
+ home_simulator_team
57
+ end
58
+
59
+ def visitor_team
60
+ visitor_simulator_team
61
+ end
62
+
63
+ def timeline
64
+ @timeline ||= SimulatorTimeline.new(self, timeline_name)
65
+ end
66
+
67
+ def readable_status
68
+ if status
69
+ SimulatorMatch::STATUS_SEQUENCE[status.to_sym]
70
+ else
71
+ 0
72
+ end
73
+ end
74
+
75
+ def to_foot_stats_live
76
+ update_by_timeline
77
+ {
78
+ "@IdPartida" => source_id.to_s,
79
+ "@Placar" => [home_score, visitor_score].join('-'),
80
+ "@PlacarPenaltis" => [home_penalties_score, visitor_penalties_score].join('-'),
81
+ "@Data" => date.strftime("%-m/%e/%Y %I:%M:%S %p"),
82
+ "@Periodo" => readable_status,
83
+ "@Arbitro" => referee,
84
+ "@Estadio" => stadium,
85
+ "@Cidade" => city,
86
+ "@Pais" => country,
87
+ "@TemNarracao" => ((narration) ? 'Sim' : 'Não'),
88
+ "@Rodada" => round.to_s,
89
+ "@Fase" => phase,
90
+ "@Taca" => cup,
91
+ "@Grupo" => group,
92
+ "Mandante" => {
93
+ "@Id" => home_team.source_id,
94
+ "@Nome" => home_team.full_name,
95
+ "@Tecnico" => "Técnico do #{home_team.full_name}",
96
+ "Gols" => SimulatorTimeline::GoalEvent.to_foot_stats(timeline.home_goals),
97
+ "Cartoes" => SimulatorTimeline::CardEvent.to_foot_stats(timeline.home_cards),
98
+ "Escalacao" => SimulatorTimeline::PlayerEvent.to_foot_stats(timeline.home_players),
99
+ },
100
+ "Visitante" => {
101
+ "@Id" => visitor_team.source_id,
102
+ "@Nome" => visitor_team.full_name,
103
+ "@Tecnico" => "Técnico do #{visitor_team.full_name}",
104
+ "Gols" => SimulatorTimeline::GoalEvent.to_foot_stats(timeline.visitor_goals),
105
+ "Cartoes" => SimulatorTimeline::CardEvent.to_foot_stats(timeline.visitor_cards),
106
+ "Escalacao" => SimulatorTimeline::PlayerEvent.to_foot_stats(timeline.visitor_players),
107
+ }
108
+ }
109
+ end
110
+
111
+ def to_foot_stats_narration
112
+ {
113
+ "Campeonato" => {
114
+ "@Id" => simulator_championship.source_id.to_s,
115
+ "@Nome" => simulator_championship.name,
116
+ "@Temporada" => Time.now.year.to_s,
117
+ "Partida" => {
118
+ "@Id" => source_id.to_s,
119
+ "@Rodada" => round.to_s,
120
+ "@Placar" => [home_score, visitor_score].join('-'),
121
+ "@TemDisputaPenaltis" => "Nao"
122
+ },
123
+ "Narracoes" => SimulatorTimeline::NarrationEvent.to_foot_stats(timeline.narration)
124
+ }
125
+ }
126
+ end
127
+
128
+ def to_foot_stats
129
+ update_by_timeline
130
+ {
131
+ "@Id" => source_id.to_s,
132
+ "Equipe" => [
133
+ {
134
+ "@Id" => home_team.source_id.to_s,
135
+ "@Nome" => home_team.full_name,
136
+ "@Placar" => " ",
137
+ "@PlacarPenaltis" => "",
138
+ "@Tipo" => "Mandante"
139
+ },
140
+ {
141
+ "@Id" => visitor_team.source_id.to_s,
142
+ "@Nome" => visitor_team.full_name,
143
+ "@Placar" => " ",
144
+ "@PlacarPenaltis" => "",
145
+ "@Tipo" => "Visitante"
146
+ }
147
+ ],
148
+ "Data" => date.strftime("%-m/%e/%Y %I:%M:%S %p"),
149
+ "Status" => readable_status,
150
+ "Arbitro" => referee,
151
+ "Estadio" => stadium,
152
+ "Cidade" => city,
153
+ "Estado" => state,
154
+ "Pais" => country,
155
+ "TemEstatistica" => ((statistic) ? 'Sim' : 'Não'),
156
+ "TemNarracao" => ((narration) ? 'Sim' : 'Não'),
157
+ "Rodada" => round.to_s,
158
+ "Fase" => phase,
159
+ "Taca" => cup,
160
+ "Grupo" => group,
161
+ "NumeroJogo" => game_number.to_s,
162
+ "AoVivo" => ((live) ? 'Sim' : 'Não')
163
+ }
164
+ end
165
+
166
+ protected
167
+ def update_by_timeline
168
+ return if readable_status >= SimulatorMatch::STATUS_SEQUENCE[:finished]
169
+ timeline.update_match
170
+ end
171
+
172
+ class << self
173
+ def to_foot_stats
174
+ {
175
+ "Partidas" => {
176
+ "Partida" => all.map(&:to_foot_stats)
177
+ }
178
+ }
179
+ end
180
+
181
+ def create_dummy(options)
182
+ championship = SimulatorChampionship.find_by_source_id options[:championship_source_id]
183
+ home_team = SimulatorTeam.find_by_source_id options[:home_team_id]
184
+ visitor_team = SimulatorTeam.find_by_source_id options[:visitor_team_id]
185
+
186
+ params = {
187
+ simulator_championship_id: championship.id,
188
+ home_team_id: home_team.id,
189
+ visitor_team_id: visitor_team.id,
190
+ timeline_name: options[:timeline_name],
191
+ source_id: dummy_source_id,
192
+ date: dummy_date(options[:minutes_from_now]),
193
+ referee: Faker::Name.name,
194
+ stadium: "Estádio #{Faker::Name.name}",
195
+ city: Faker::Address.city,
196
+ state: dummy_state,
197
+ country: Faker::Address.country,
198
+ statistic: dummy_statistic,
199
+ narration: options[:narration],
200
+ round: dummy_round(championship, options[:round]),
201
+ phase: dummy_phase(championship, options[:phase]),
202
+ cup: '',
203
+ group: '',
204
+ game_number: dummy_game_number(championship),
205
+ live: options[:live],
206
+ home_team_name: home_team.full_name,
207
+ home_score: 0,
208
+ home_penalties_score: nil,
209
+ visitor_team_name: visitor_team.full_name,
210
+ visitor_score: 0,
211
+ visitor_penalties_score: nil,
212
+ timeline_random_seed: Random.new_seed
213
+ }
214
+
215
+ match = self.create params
216
+ match
217
+ end
218
+
219
+ protected
220
+ def dummy_source_id
221
+ source_id = rand(10000)
222
+ if self.where(source_id: source_id).exists?
223
+ dummy_source_id
224
+ else
225
+ source_id
226
+ end
227
+ end
228
+
229
+ def dummy_date(minutes_from_now)
230
+ minutes_from_now.minutes.from_now
231
+ end
232
+
233
+ def dummy_state
234
+ ['SP', 'RJ', 'MG', 'EX'].sample
235
+ end
236
+
237
+ def dummy_statistic
238
+ [true, false].sample
239
+ end
240
+
241
+ def dummy_round(championship, round)
242
+ if round
243
+ round
244
+ else
245
+ championship.classification.round
246
+ end
247
+ end
248
+
249
+ def dummy_phase(championship, phase)
250
+ if phase
251
+ phase
252
+ else
253
+ championship.classification.phase
254
+ end
255
+ end
256
+
257
+ def dummy_game_number(championship)
258
+ championship.simulator_matches.count + 1
259
+ end
260
+ end
261
+ end