Class: SciolyFF::Helper

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

Overview

Deprecated: Please use SciolyFF::Interpreter instead

Wrapper class around a SciolyFF Ruby object representation with utility methods to help in displaying results

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(rep) ⇒ Helper

Returns a new instance of Helper.



59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
# File 'lib/sciolyff.rb', line 59

def initialize(rep)
  warn 'Class `SciolyFF::Helper` is deprecated. '\
       'Please use `SciolyFF::Interpreter` instead.'

  @rep = rep
  @exhibition_teams_count = rep[:Teams].count { |t| t[:exhibition] }
  @exempt_placings_count = rep[:Placings].count { |p| p[:exempt] }
  @team_points_cache = {}

  @tournament = rep[:Tournament]
  @events_by_name = index_array(rep[:Events], [:name])
  @teams_by_number = index_array(rep[:Teams], [:number])
  @placings_by_event = index_array(rep[:Placings], %i[event team])
  @placings_by_team = index_array(rep[:Placings], %i[team event])
  return unless rep[:Penalties]

  @penalties_by_team = index_array(rep[:Penalties], [:team])
end

Instance Attribute Details

#events_by_nameObject (readonly)

Returns the value of attribute events_by_name.



56
57
58
# File 'lib/sciolyff.rb', line 56

def events_by_name
  @events_by_name
end

#penalties_by_teamObject (readonly)

Returns the value of attribute penalties_by_team.



57
58
59
# File 'lib/sciolyff.rb', line 57

def penalties_by_team
  @penalties_by_team
end

#placings_by_eventObject (readonly)

Returns the value of attribute placings_by_event.



57
58
59
# File 'lib/sciolyff.rb', line 57

def placings_by_event
  @placings_by_event
end

#placings_by_teamObject (readonly)

Returns the value of attribute placings_by_team.



57
58
59
# File 'lib/sciolyff.rb', line 57

def placings_by_team
  @placings_by_team
end

#repObject (readonly)

Returns the value of attribute rep.



56
57
58
# File 'lib/sciolyff.rb', line 56

def rep
  @rep
end

#teams_by_numberObject (readonly)

Returns the value of attribute teams_by_number.



56
57
58
# File 'lib/sciolyff.rb', line 56

def teams_by_number
  @teams_by_number
end

#tournamentObject (readonly)

Returns the value of attribute tournament.



56
57
58
# File 'lib/sciolyff.rb', line 56

def tournament
  @tournament
end

Instance Method Details

#event_points(team_number, event_name) ⇒ Object



82
83
84
85
86
87
88
89
90
91
92
# File 'lib/sciolyff.rb', line 82

def event_points(team_number, event_name)
  placing = @placings_by_event[event_name][team_number]
  number_of_teams = number_of_competing_teams(event_name)

  if placing[:disqualified] then number_of_teams + 2
  elsif placing[:participated] == false then number_of_teams + 1
  elsif placing[:unknown] then number_of_teams - 1
  elsif placing[:place].nil? then number_of_teams
  else calculate_event_points(placing)
  end
end

#medal_counts(team_number) ⇒ Object



118
119
120
121
122
123
124
125
# File 'lib/sciolyff.rb', line 118

def medal_counts(team_number)
  (1..(nonexhibition_teams.count + 2)).map do |m|
    @events_by_name
      .values
      .reject { |e| e[:trial] || e[:trialed] }
      .count { |e| event_points(team_number, e[:name]) == m }
  end
end

#nonexhibition_teamsObject



78
79
80
# File 'lib/sciolyff.rb', line 78

def nonexhibition_teams
  @teams_by_number.reject { |_, t| t[:exhibition] }
end

#sort_teams_by_rankObject



106
107
108
109
110
111
112
113
114
115
116
# File 'lib/sciolyff.rb', line 106

def sort_teams_by_rank
  @teams_by_number
    .values
    .sort do |a, b|
    next  1 if  a[:exhibition] && !b[:exhibition]
    next -1 if !a[:exhibition] &&  b[:exhibition]

    cmp = team_points(a[:number]) - team_points(b[:number])
    cmp.zero? ? break_tie(a[:number], b[:number]) : cmp
  end
end

#team_points(team_number) ⇒ Object



94
95
96
97
98
# File 'lib/sciolyff.rb', line 94

def team_points(team_number)
  return @team_points_cache[team_number] if @team_points_cache[team_number]

  @team_points_cache[team_number] = calculate_team_points(team_number)
end

#team_points_from_penalties(team_number) ⇒ Object



100
101
102
103
104
# File 'lib/sciolyff.rb', line 100

def team_points_from_penalties(team_number)
  if @penalties_by_team.nil? || @penalties_by_team[team_number].nil? then 0
  else @penalties_by_team[team_number][:points]
  end
end