Class: FtcEvent::Match

Inherits:
Object
  • Object
show all
Defined in:
lib/ftc_event/match.rb

Direct Known Subclasses

Elimination, Qualification

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(event, match) ⇒ Match

Returns a new instance of Match.



8
9
10
11
# File 'lib/ftc_event/match.rb', line 8

def initialize(event, match)
  @event = event
  @match = match
end

Instance Attribute Details

#eventObject (readonly)

Returns the value of attribute event.



5
6
7
# File 'lib/ftc_event/match.rb', line 5

def event
  @event
end

#matchObject (readonly)

Returns the value of attribute match.



6
7
8
# File 'lib/ftc_event/match.rb', line 6

def match
  @match
end

Instance Method Details

#dataObject



26
27
28
# File 'lib/ftc_event/match.rb', line 26

def data
  one_row_from_table('Data', match)
end

#each_team(_alliance) ⇒ Object



58
59
60
# File 'lib/ftc_event/match.rb', line 58

def each_team(_alliance)
  raise 'Abstract method not implemented'
end

#game_specificObject



34
35
36
# File 'lib/ftc_event/match.rb', line 34

def game_specific
  one_row_from_table('GameSpecific', match)
end

#infoObject



22
23
24
# File 'lib/ftc_event/match.rb', line 22

def info
  one_row_from_table('', match)
end

#long_alliance_description(_color) ⇒ Object



66
67
68
# File 'lib/ftc_event/match.rb', line 66

def long_alliance_description(_color)
  raise 'Abstract method not implemented'
end

#long_alliances_descriptionObject



77
78
79
80
81
82
# File 'lib/ftc_event/match.rb', line 77

def long_alliances_description
  '%s vs. %s' % [
    long_alliance_description('red'),
    long_alliance_description('blue'),
  ]
end

#long_descriptionObject



88
89
90
# File 'lib/ftc_event/match.rb', line 88

def long_description
  "#{long_name}: #{long_alliances_description}"
end

#long_nameObject



46
47
48
# File 'lib/ftc_event/match.rb', line 46

def long_name
  raise 'Abstract method not implemented'
end

#one_row_from_table(table, match) ⇒ Object



17
18
19
20
# File 'lib/ftc_event/match.rb', line 17

def one_row_from_table(table, match)
  result = event.db.query("SELECT * FROM #{table_prefix}#{table} WHERE match = ?", [match])
  result&.first
end

#other_alliance(alliance) ⇒ Object



92
93
94
95
96
# File 'lib/ftc_event/match.rb', line 92

def other_alliance(alliance)
  return unless FtcEvent::ALLIANCES.include?(alliance)

  FtcEvent::ALLIANCES.reject { |x| x == alliance }.first
end

#penalties_by(alliance) ⇒ Object



98
99
100
# File 'lib/ftc_event/match.rb', line 98

def penalties_by(alliance)
  results["#{alliance}PenaltyCommitted"]
end

#points_for(alliance) ⇒ Object



102
103
104
# File 'lib/ftc_event/match.rb', line 102

def points_for(alliance)
  results["#{alliance}Score"]
end

#positionsObject



50
51
52
# File 'lib/ftc_event/match.rb', line 50

def positions
  raise 'Abstract method not implemented'
end

#postedObject



157
158
159
# File 'lib/ftc_event/match.rb', line 157

def posted
  Time.at(data['postedTime'].to_f / 1000.0)
end

#resultObject



145
146
147
# File 'lib/ftc_event/match.rb', line 145

def result
  "Red #{result_for('red')}, Blue #{result_for('blue')}"
end

#result_for(alliance) ⇒ Object



134
135
136
137
138
139
140
141
142
143
# File 'lib/ftc_event/match.rb', line 134

def result_for(alliance)
  return unless ALLIANCES.include?(alliance)

  from_penalties = penalties_by(other_alliance(alliance))
  '%i points [%s penalties] (%s)' % [
    score_for(alliance),
    from_penalties.zero? ? 'no' : "#{from_penalties} from",
    win_for(alliance),
  ]
end

#resultsObject



30
31
32
# File 'lib/ftc_event/match.rb', line 30

def results
  one_row_from_table('Results', match)
end

#scheduledObject



153
154
155
# File 'lib/ftc_event/match.rb', line 153

def scheduled
  Time.at(data['scheduleStart'].to_f / 1000.0)
end

#score_for(alliance) ⇒ Object



106
107
108
# File 'lib/ftc_event/match.rb', line 106

def score_for(alliance)
  points_for(alliance) + penalties_by(other_alliance(alliance))
end

#short_alliance_description(_color) ⇒ Object



62
63
64
# File 'lib/ftc_event/match.rb', line 62

def short_alliance_description(_color)
  raise 'Abstract method not implemented'
end

#short_alliances_descriptionObject



70
71
72
73
74
75
# File 'lib/ftc_event/match.rb', line 70

def short_alliances_description
  '%s vs. %s' % [
    short_alliance_description('red'),
    short_alliance_description('blue'),
  ]
end

#short_descriptionObject



84
85
86
# File 'lib/ftc_event/match.rb', line 84

def short_description
  "#{short_name}: #{short_alliances_description}"
end

#short_identifierObject



42
43
44
# File 'lib/ftc_event/match.rb', line 42

def short_identifier
  raise 'Abstract method not implemented'
end

#short_nameObject



38
39
40
# File 'lib/ftc_event/match.rb', line 38

def short_name
  raise 'Abstract method not implemented'
end

#startedObject



149
150
151
# File 'lib/ftc_event/match.rb', line 149

def started
  Time.at(data['start'].to_f / 1000.0)
end

#table_prefixObject



13
14
15
# File 'lib/ftc_event/match.rb', line 13

def table_prefix
  raise 'Abstract method not implemented'
end

#teamsObject



54
55
56
# File 'lib/ftc_event/match.rb', line 54

def teams
  raise 'Abstract method not implemented'
end

#win_for(alliance) ⇒ Object



123
124
125
126
127
128
129
130
131
132
# File 'lib/ftc_event/match.rb', line 123

def win_for(alliance)
  case winner
  when 'tie'
    'tie'
  when alliance
    'win'
  when other_alliance(alliance)
    'loss'
  end
end

#winnerObject



110
111
112
113
114
115
116
117
118
119
120
121
# File 'lib/ftc_event/match.rb', line 110

def winner
  red  = score_for('red')
  blue = score_for('blue')

  if red > blue
    'red'
  elsif blue > red
    'blue'
  else
    'tie'
  end
end