Class: SimulatorMatch

Inherits:
ActiveRecord::Base
  • Object
show all
Defined in:
app/models/simulator_match.rb

Constant Summary collapse

STATUS_MAP =
{
  not_started:         'Partida não iniciada',
  first_period:        'Primeiro tempo',
  first_interval:      'Intervalo 1',
  second_period:       'Segundo tempo',
  second_interval:     'Intervalo 2',
  first_prorrogation:  'Prorrogação 1',
  third_interval:      'Intervalo 3',
  second_prorrogation: 'Prorrogação 2',
  penalties:           'Disputa de Pênaltis',
  finished:            'Partida encerrada',
  stopped_match:       'Partida interrompida',
  canceled:            'Partida cancelada'
}
STATUS_SEQUENCE =
{
  not_started:          0,
  first_period:         1,
  first_interval:       2,
  second_period:        3,
  second_interval:      4,
  first_prorrogation:   5,
  third_interval:       6,
  second_prorrogation:  7,
  penalties:            8,
  finished:             9,
  stopped_match:       10,
  canceled:            11
}
STATUSES =
[
  :not_started,
  :first_period,
  :first_interval,
  :second_period,
  :second_interval,
  :first_prorrogation,
  :third_interval,
  :second_prorrogation,
  :penalties,
  :finished,
  :stopped_match,
  :canceled
]

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.create_dummy(options) ⇒ Object



194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
# File 'app/models/simulator_match.rb', line 194

def create_dummy(options)
  championship = SimulatorChampionship.find_by_source_id options[:championship_source_id]
  home_team    = SimulatorTeam.find_by_source_id options[:home_team_id]
  visitor_team = SimulatorTeam.find_by_source_id options[:visitor_team_id]

  params = {
    simulator_championship_id: championship.id,
    home_team_id:              home_team.id,
    visitor_team_id:           visitor_team.id,
    timeline_name:             options[:timeline_name],
    date:                      dummy_date(options[:minutes_from_now]),
    referee:                   Faker::Name.name,
    stadium:                   "Estádio #{Faker::Name.name}",
    city:                      Faker::Address.city,
    state:                     dummy_state,
    country:                   Faker::Address.country,
    statistic:                 dummy_statistic,
    narration:                 options[:narration],
    round:                     dummy_round(championship, options[:round]),
    phase:                     dummy_phase(championship, options[:phase]),
    cup:                       '',
    group:                     '',
    game_number:               dummy_game_number(championship),
    live:                      options[:live]
  }

  match = self.create params
  match
end

.dummy_source_idObject



224
225
226
227
228
229
230
231
# File 'app/models/simulator_match.rb', line 224

def dummy_source_id
  source_id = rand(10000)
  if self.where(source_id: source_id).exists?
    dummy_source_id
  else
    source_id
  end
end

.to_foot_statsObject



186
187
188
189
190
191
192
# File 'app/models/simulator_match.rb', line 186

def to_foot_stats
  {
    "Partidas" => {
      "Partida" => all.map(&:to_foot_stats)
    }
  }
end

Instance Method Details

#home_teamObject



56
57
58
# File 'app/models/simulator_match.rb', line 56

def home_team
  home_simulator_team
end

#readable_statusObject



68
69
70
71
72
73
74
# File 'app/models/simulator_match.rb', line 68

def readable_status
  if status
    SimulatorMatch::STATUS_MAP[status.to_sym]
  else
    SimulatorMatch::STATUS_MAP[:not_started]
  end
end

#timelineObject



64
65
66
# File 'app/models/simulator_match.rb', line 64

def timeline
  @timeline ||= SimulatorTimeline.new(self, timeline_name)
end

#to_foot_statsObject



130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
# File 'app/models/simulator_match.rb', line 130

def to_foot_stats
  update_by_timeline
  {
    "@Id"    => source_id.to_s,
    "Equipe" => [
      {
        "@Id"             => home_team.source_id.to_s,
        "@Nome"           => home_team.full_name,
        "@Placar"         => timeline.home_score.to_s,
        "@PlacarPenaltis" => "",
        "@Tipo"           => "Mandante"
      },
      {
        "@Id"             => visitor_team.source_id.to_s,
        "@Nome"           => visitor_team.full_name,
        "@Placar"         => timeline.visitor_score.to_s,
        "@PlacarPenaltis" => "",
        "@Tipo"           => "Visitante"
      }
    ],
    "Data"           => date.strftime("%-m/%e/%Y %I:%M:%S %p"),
    "Status"         => readable_status,
    "Arbitro"        => referee,
    "Estadio"        => stadium,
    "Cidade"         => city,
    "Estado"         => state,
    "Pais"           => country,
    "TemEstatistica" => ((statistic) ? 'Sim' : 'Não'),
    "TemNarracao"    => ((narration) ? 'Sim' : 'Não'),
    "Rodada"         => round.to_s,
    "Fase"           => phase,
    "Taca"           => cup,
    "Grupo"          => group,
    "NumeroJogo"     => game_number.to_s,
    "AoVivo"         => ((live) ? 'Sim' : 'Não')
  }
end

#to_foot_stats_liveObject



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
# File 'app/models/simulator_match.rb', line 76

def to_foot_stats_live
  update_by_timeline
  {
    "@IdPartida"      => source_id.to_s,
    "@Placar"         => [home_score, visitor_score].join('-'),
    "@PlacarPenaltis" => [home_penalties_score, visitor_penalties_score].join('-'),
    "@Data"           => date.strftime("%-m/%e/%Y %I:%M:%S %p"),
    "@Periodo"        => readable_status,
    "@Arbitro"        => referee,
    "@Estadio"        => stadium,
    "@Cidade"         => city,
    "@Pais"           => country,
    "@TemNarracao"    => ((narration) ? 'Sim' : 'Não'),
    "@Rodada"         => round.to_s,
    "@Fase"           => phase,
    "@Taca"           => cup,
    "@Grupo"          => group,
    "Mandante" => {
      "@Id"       => home_team.source_id,
      "@Nome"     => home_team.full_name,
      "@Tecnico"  => "Técnico do #{home_team.full_name}",
      "Gols"      => SimulatorTimeline::GoalEvent.to_foot_stats(timeline.home_goals),
      "Cartoes"   => SimulatorTimeline::CardEvent.to_foot_stats(timeline.home_cards),
      "Escalacao" => SimulatorTimeline::PlayerEvent.to_foot_stats(timeline.home_players),
    },
    "Visitante" => {
      "@Id"       => visitor_team.source_id,
      "@Nome"     => visitor_team.full_name,
      "@Tecnico"  => "Técnico do #{visitor_team.full_name}",
      "Gols"      => SimulatorTimeline::GoalEvent.to_foot_stats(timeline.visitor_goals),
      "Cartoes"   => SimulatorTimeline::CardEvent.to_foot_stats(timeline.visitor_cards),
      "Escalacao" => SimulatorTimeline::PlayerEvent.to_foot_stats(timeline.visitor_players),
    }
  }
end

#to_foot_stats_narrationObject



112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
# File 'app/models/simulator_match.rb', line 112

def to_foot_stats_narration
  update_by_timeline
  {
    "Campeonato" => {
      "@Id"        => simulator_championship.source_id.to_s,
      "@Nome"      => simulator_championship.name,
      "@Temporada" => Time.now.year.to_s,
      "Partida" => {
        "@Id"     => source_id.to_s,
        "@Rodada" => round.to_s,
        "@Placar" => [home_score, visitor_score].join('-'),
        "@TemDisputaPenaltis" => "Nao"
      },
      "Narracoes" => SimulatorTimeline::NarrationEvent.to_foot_stats(timeline.narration)
    }
  }
end

#visitor_teamObject



60
61
62
# File 'app/models/simulator_match.rb', line 60

def visitor_team
  visitor_simulator_team
end