Class: ActiveGenie::Ranker::Entities::Players

Inherits:
Object
  • Object
show all
Defined in:
lib/active_genie/entities/players.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(players) ⇒ Players

Returns a new instance of Players.



9
10
11
12
13
14
15
# File 'lib/active_genie/entities/players.rb', line 9

def initialize(players)
  @players = if players.is_a?(Players)
               players.players
             else
               build(players)
             end
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missingObject



70
71
72
# File 'lib/active_genie/entities/players.rb', line 70

def method_missing(...)
  @players.send(...)
end

Instance Attribute Details

#playersObject (readonly)

Returns the value of attribute players.



17
18
19
# File 'lib/active_genie/entities/players.rb', line 17

def players
  @players
end

Instance Method Details

#all_scoresObject



30
31
32
# File 'lib/active_genie/entities/players.rb', line 30

def all_scores
  eligible.map(&:score).compact
end

#calc_higher_tierObject

Since eligible is sorted in descending order (best player first), lower_tier represents the worst performing players at the end of the array.



42
43
44
# File 'lib/active_genie/entities/players.rb', line 42

def calc_higher_tier
  eligible[(tier_size * -2)...(tier_size * -1)]
end

#calc_lower_tierObject



46
47
48
# File 'lib/active_genie/entities/players.rb', line 46

def calc_lower_tier
  eligible[(tier_size * -1)..]
end

#coefficient_of_variationObject



19
20
21
22
23
24
25
26
27
28
# File 'lib/active_genie/entities/players.rb', line 19

def coefficient_of_variation
  mean = score_mean

  return 0 if mean.zero?

  variance = all_scores.map { |num| (num - mean)**2 }.sum / all_scores.size
  standard_deviation = Math.sqrt(variance)

  (standard_deviation / mean) * 100
end

#eligibleObject



50
51
52
# File 'lib/active_genie/entities/players.rb', line 50

def eligible
  sorted.reject(&:eliminated)
end

#eligible_sizeObject



54
55
56
# File 'lib/active_genie/entities/players.rb', line 54

def eligible_size
  @players.reject(&:eliminated).size
end

#elo_eligible?Boolean

Returns:

  • (Boolean)


58
59
60
# File 'lib/active_genie/entities/players.rb', line 58

def elo_eligible?
  eligible.size > 15
end

#respond_to_missing?(method_name, include_private = false) ⇒ Boolean

Returns:

  • (Boolean)


74
75
76
# File 'lib/active_genie/entities/players.rb', line 74

def respond_to_missing?(method_name, include_private = false)
  @players.respond_to?(method_name, include_private)
end

#score_meanObject



34
35
36
37
38
# File 'lib/active_genie/entities/players.rb', line 34

def score_mean
  return 0 if all_scores.empty?

  all_scores.sum.to_f / all_scores.size
end

#sortedObject



62
63
64
# File 'lib/active_genie/entities/players.rb', line 62

def sorted
  @players.sort_by { |p| -p.sort_value }
end

#to_json(*_args) ⇒ Object



66
67
68
# File 'lib/active_genie/entities/players.rb', line 66

def to_json(*_args)
  @players.map(&:to_h).to_json
end