Class: SciolyFF::Helper

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

Overview

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.



55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
# File 'lib/sciolyff.rb', line 55

def initialize(rep)
  @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.



52
53
54
# File 'lib/sciolyff.rb', line 52

def events_by_name
  @events_by_name
end

#penalties_by_teamObject (readonly)

Returns the value of attribute penalties_by_team.



53
54
55
# File 'lib/sciolyff.rb', line 53

def penalties_by_team
  @penalties_by_team
end

#placings_by_eventObject (readonly)

Returns the value of attribute placings_by_event.



53
54
55
# File 'lib/sciolyff.rb', line 53

def placings_by_event
  @placings_by_event
end

#placings_by_teamObject (readonly)

Returns the value of attribute placings_by_team.



53
54
55
# File 'lib/sciolyff.rb', line 53

def placings_by_team
  @placings_by_team
end

#repObject (readonly)

Returns the value of attribute rep.



52
53
54
# File 'lib/sciolyff.rb', line 52

def rep
  @rep
end

#teams_by_numberObject (readonly)

Returns the value of attribute teams_by_number.



52
53
54
# File 'lib/sciolyff.rb', line 52

def teams_by_number
  @teams_by_number
end

#tournamentObject (readonly)

Returns the value of attribute tournament.



52
53
54
# File 'lib/sciolyff.rb', line 52

def tournament
  @tournament
end

Instance Method Details

#event_points(team_number, event_name) ⇒ Object



75
76
77
78
79
80
81
82
83
84
85
# File 'lib/sciolyff.rb', line 75

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



111
112
113
114
115
116
117
118
# File 'lib/sciolyff.rb', line 111

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



71
72
73
# File 'lib/sciolyff.rb', line 71

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

#sort_teams_by_rankObject



99
100
101
102
103
104
105
106
107
108
109
# File 'lib/sciolyff.rb', line 99

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



87
88
89
90
91
# File 'lib/sciolyff.rb', line 87

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



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

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