Class: Hlockey::Team

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

Overview

A team in the league Created by League when data is loaded

Defined Under Namespace

Classes: Player, Stadium

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name:, color:, emoji:, motto:, stadium:, roster:, shadows:, evolutions: 0) ⇒ Team

Returns a new instance of Team.

Parameters:

  • name (String)
  • color (String)
  • emoji (String)
  • motto (String)
  • stadium (Hash<:full_name, :hlockey_type, :nickname, :description => String>)
  • roster (Hash<:lwing, :center, :rwing, :ldef, :goalie, :rdef => Hash>)
  • shadows (Array<Hash>)


21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
# File 'lib/hlockey/team.rb', line 21

def initialize(name:, color:, emoji:, motto:, stadium:, roster:, shadows:,
               evolutions: 0)
  @name = name
  @color = color
  @emoji = emoji
  @motto = motto
  @evolutions = evolutions

  @stadium = Stadium.new(team: self, **stadium)

  @roster = roster.transform_values { Player.new(**_1, team: self) }
  @shadows = shadows.map { Player.new(**_1, team: self) }

  @status = :in_contention
  @wins = @losses = 0
end

Instance Attribute Details

#colorObject (readonly)

Returns the value of attribute color.



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

def color
  @color
end

#emojiObject (readonly)

Returns the value of attribute emoji.



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

def emoji
  @emoji
end

#evolutionsObject (readonly)

Returns the value of attribute evolutions.



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

def evolutions
  @evolutions
end

#lossesObject

Returns the value of attribute losses.



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

def losses
  @losses
end

#mottoObject (readonly)

Returns the value of attribute motto.



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

def motto
  @motto
end

#nameObject (readonly) Also known as: to_s

Returns the value of attribute name.



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

def name
  @name
end

#rosterObject

Returns the value of attribute roster.



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

def roster
  @roster
end

#shadowsObject

Returns the value of attribute shadows.



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

def shadows
  @shadows
end

#stadiumObject (readonly)

Returns the value of attribute stadium.



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

def stadium
  @stadium
end

#statusObject

Returns the value of attribute status.



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

def status
  @status
end

#winsObject

Returns the value of attribute wins.



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

def wins
  @wins
end

Class Method Details

.pos_name(pos) ⇒ Object

Takes symbol from Team roster & converts it to full position name



79
80
81
82
83
84
85
86
87
88
89
90
91
# File 'lib/hlockey/team.rb', line 79

def self.pos_name(pos) =
case pos
when :lwing
  "Left wing"
when :rwing
  "Right wing"
when :ldef
  "Left defender"
when :rdef
  "Right defender"
else
  pos.capitalize.to_s
end

Instance Method Details

#playersArray<Player>

Returns:



69
# File 'lib/hlockey/team.rb', line 69

def players = @roster.values + @shadows

#positive_record?Boolean

Returns:

  • (Boolean)


66
# File 'lib/hlockey/team.rb', line 66

def positive_record? = @wins > @losses

#roster_displayHash<String => Player>

Returns #roster, but with keys better for displaying.

Returns:

  • (Hash<String => Player>)

    #roster, but with keys better for displaying



76
# File 'lib/hlockey/team.rb', line 76

def roster_display = @roster.transform_keys(&self.class.method(:pos_name))

#to_h(simple: false) ⇒ Hash

Returns:

  • (Hash)


39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
# File 'lib/hlockey/team.rb', line 39

def to_h(simple: false)
  res = {
    name: @name,
    color: @color,
    emoji: @emoji,
    motto: @motto,
    evolutions: @evolutions,
    stadium: @stadium.to_h(simple:),
    roster: @roster.transform_values { _1.to_h(simple:) },
    shadows: @shadows.map { _1.to_h(simple:) }
  }

  res.delete(:evolutions) if simple && res[:evolutions].zero?

  unless simple
    res[:status] = @status
    res[:wins] = @wins
    res[:losses] = @losses
  end

  res
end

#w_lString

Returns wins/losses in string representation.

Returns:

  • (String)

    wins/losses in string representation



63
# File 'lib/hlockey/team.rb', line 63

def w_l = "#{@wins}-#{@losses}"

#worst_player_posSymbol

Returns the position of the worst player on the roster.

Returns:

  • (Symbol)

    the position of the worst player on the roster



72
73
# File 'lib/hlockey/team.rb', line 72

def worst_player_pos =
@roster.transform_values { |v| v.stats.values.sum }.min_by { |_, v| v }.first