Class: Hlockey::Team::Player

Inherits:
Object
  • Object
show all
Defined in:
lib/hlockey/team/player.rb

Overview

A player on a team

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name:, team:, stats:, mods: []) ⇒ Player

Returns a new instance of Player.

Parameters:

  • name (String)
  • team (Team)
  • stats (Hash<:offense, :defense, :agility => Numeric>)
  • mods (Array) (defaults to: [])


18
19
20
21
22
23
24
25
26
27
28
29
30
# File 'lib/hlockey/team/player.rb', line 18

def initialize(name:, team:, stats:, mods: [])
  @name = name
  @team = team
  @stats = stats
  @mods = mods.map do |mod|
    if mod.is_a?(Array)
      Mod::MODS.find { _1.to_s == "Hlockey::Mod::#{mod.first}" }.new(self,
                                                                     *mod[1..])
    else
      Mod::MODS.find { _1.to_s == "Hlockey::Mod::#{mod}" }.new(self)
    end
  end
end

Instance Attribute Details

#modsObject

Returns the value of attribute mods.



9
10
11
# File 'lib/hlockey/team/player.rb', line 9

def mods
  @mods
end

#nameObject (readonly) Also known as: to_s

Returns the value of attribute name.



10
11
12
# File 'lib/hlockey/team/player.rb', line 10

def name
  @name
end

#statsObject

Returns the value of attribute stats.



9
10
11
# File 'lib/hlockey/team/player.rb', line 9

def stats
  @stats
end

#teamObject

Returns the value of attribute team.



9
10
11
# File 'lib/hlockey/team/player.rb', line 9

def team
  @team
end

Instance Method Details

#mods_do(action, *args) ⇒ Object

Calls a method on every mod this player has

Parameters:

  • action (Symbol)

    method to call on each mod

  • *args (Object)

    any arguments to pass to the mods

Returns:

  • (Object)

    the first non-nil return value given from a mod, if any



54
55
56
57
58
# File 'lib/hlockey/team/player.rb', line 54

def mods_do(action, *args)
  res = nil
  @mods.each { res ||= _1.send(action, *args) }
  res
end

#stat_displayHash<String => String>

Returns #stats but better for displaying.

Returns:

  • (Hash<String => String>)

    #stats but better for displaying



45
46
47
48
# File 'lib/hlockey/team/player.rb', line 45

def stat_display =
Utils.hash_display_keys(
  @stats.transform_values { _1.round(2).to_s.ljust(4, "0") }
)

#to_h(simple: false) ⇒ Hash

Returns:

  • (Hash)


33
34
35
36
37
38
39
40
41
42
# File 'lib/hlockey/team/player.rb', line 33

def to_h(simple: false)
  res = {
    name: @name,
    stats: @stats
  }
  res[:team] = @team.to_s unless simple
  res[:mods] = @mods.map(&:to_data) unless simple && @mods.empty?

  res
end