Class: SimulatorTimeline

Inherits:
Object
  • Object
show all
Defined in:
app/models/simulator_timeline.rb,
app/models/simulator_timeline/event.rb,
app/models/simulator_timeline/lorem.rb,
app/models/simulator_timeline/minute.rb,
app/models/simulator_timeline/period.rb,
app/models/simulator_timeline/states.rb,
app/models/simulator_timeline/card_event.rb,
app/models/simulator_timeline/goal_event.rb,
app/models/simulator_timeline/player_event.rb,
app/models/simulator_timeline/event_handler.rb,
app/models/simulator_timeline/lorem_messages.rb,
app/models/simulator_timeline/narration_event.rb

Overview

TODO: Handle penalties

Defined Under Namespace

Modules: LoremMessages Classes: CardEvent, Event, EventHandler, GoalEvent, Lorem, Minute, NarrationEvent, Period, PlayerEvent, States

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(match, timeline_name) ⇒ SimulatorTimeline

Returns a new instance of SimulatorTimeline.



9
10
11
12
13
# File 'app/models/simulator_timeline.rb', line 9

def initialize(match, timeline_name)
  @match         = match
  @timeline_path = File.expand_path File.join(FootStatsSimulator.timelines_dir, "#{timeline_name}.rb")
  load_timeline
end

Instance Attribute Details

#current_home_playersObject

Returns the value of attribute current_home_players.



7
8
9
# File 'app/models/simulator_timeline.rb', line 7

def current_home_players
  @current_home_players
end

#current_minuteObject

Returns the value of attribute current_minute.



6
7
8
# File 'app/models/simulator_timeline.rb', line 6

def current_minute
  @current_minute
end

#current_periodObject

Returns the value of attribute current_period.



6
7
8
# File 'app/models/simulator_timeline.rb', line 6

def current_period
  @current_period
end

#current_visitor_playersObject

Returns the value of attribute current_visitor_players.



7
8
9
# File 'app/models/simulator_timeline.rb', line 7

def current_visitor_players
  @current_visitor_players
end

#home_cardsObject

Returns the value of attribute home_cards.



7
8
9
# File 'app/models/simulator_timeline.rb', line 7

def home_cards
  @home_cards
end

#home_goalsObject

Returns the value of attribute home_goals.



7
8
9
# File 'app/models/simulator_timeline.rb', line 7

def home_goals
  @home_goals
end

#matchObject (readonly)

Returns the value of attribute match.



4
5
6
# File 'app/models/simulator_timeline.rb', line 4

def match
  @match
end

#narrationObject

Returns the value of attribute narration.



7
8
9
# File 'app/models/simulator_timeline.rb', line 7

def narration
  @narration
end

#statesObject

Returns the value of attribute states.



6
7
8
# File 'app/models/simulator_timeline.rb', line 6

def states
  @states
end

#visitor_cardsObject

Returns the value of attribute visitor_cards.



7
8
9
# File 'app/models/simulator_timeline.rb', line 7

def visitor_cards
  @visitor_cards
end

#visitor_goalsObject

Returns the value of attribute visitor_goals.



7
8
9
# File 'app/models/simulator_timeline.rb', line 7

def visitor_goals
  @visitor_goals
end

Instance Method Details

#all_current_playersObject



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

def all_current_players
  current_home_players + current_visitor_players
end

#all_playersObject



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

def all_players
  home_players + visitor_players
end

#collection_of(n, collection) ⇒ Object



23
24
25
26
27
28
29
30
# File 'app/models/simulator_timeline.rb', line 23

def collection_of(n, collection)
  collecteds = []
  while collecteds.size < n
    collected = sample(collection)
    collecteds << collected unless collecteds.include?(collected)
  end
  collecteds
end

#current_period_timeObject



84
85
86
# File 'app/models/simulator_timeline.rb', line 84

def current_period_time
  SimulatorMatch::STATUS_SEQUENCE[current_period.to_sym] || current_period.to_i
end

#home_playersObject



48
49
50
# File 'app/models/simulator_timeline.rb', line 48

def home_players
  @home_players ||= @match.home_simulator_team.simulator_players.order(:source_id)
end

#home_scoreObject



32
33
34
# File 'app/models/simulator_timeline.rb', line 32

def home_score
  home_goals.count{ |g| g.type == 'Favor' } + visitor_goals.count{ |g| g.type != 'Favor' }
end

#home_teamObject



40
41
42
# File 'app/models/simulator_timeline.rb', line 40

def home_team
  @match.home_team
end

#period(period) ⇒ Object



76
77
78
79
80
81
82
# File 'app/models/simulator_timeline.rb', line 76

def period(period)
  if block_given?
    yield Period.new(self, period)
  else
    Period.new(self, period_time)
  end
end

#sample(collection) ⇒ Object



19
20
21
# File 'app/models/simulator_timeline.rb', line 19

def sample(collection)
  collection[timeline_rand(collection.size)]
end

#timeline_rand(limit = nil) ⇒ Object



15
16
17
# File 'app/models/simulator_timeline.rb', line 15

def timeline_rand(limit = nil)
  @random.rand limit
end

#transitions(&block) ⇒ Object



72
73
74
# File 'app/models/simulator_timeline.rb', line 72

def transitions(&block)
  States.new(self, &block)
end

#update_matchObject



64
65
66
67
68
69
70
# File 'app/models/simulator_timeline.rb', line 64

def update_match
  @match.update_attributes status:                    current_period,
                           home_score:                home_score,
                           home_penalties_score:      nil,
                           visitor_score:             visitor_score,
                           visitor_penalties_score:   nil
end

#visitor_playersObject



52
53
54
# File 'app/models/simulator_timeline.rb', line 52

def visitor_players
  @visitor_players ||= @match.visitor_simulator_team.simulator_players.order(:source_id)
end

#visitor_scoreObject



36
37
38
# File 'app/models/simulator_timeline.rb', line 36

def visitor_score
  visitor_goals.count{ |g| g.type == 'Favor' } + home_goals.count{ |g| g.type != 'Favor' }
end

#visitor_teamObject



44
45
46
# File 'app/models/simulator_timeline.rb', line 44

def visitor_team
  @match.visitor_team
end