Class: Hlockey::Game

Inherits:
Object
  • Object
show all
Includes:
Actions
Defined in:
lib/hlockey/game.rb,
lib/hlockey/game/fight.rb,
lib/hlockey/game/weather.rb,
lib/hlockey/game/weather/stars.rb,
lib/hlockey/game/weather/waves.rb,
lib/hlockey/game/weather/sunset.rb,
lib/hlockey/game/weather/chicken.rb,
lib/hlockey/game/weather/incline.rb,
lib/hlockey/game/weather/audacity.rb,
lib/hlockey/game/weather/weatherable.rb

Overview

A single game of Hlockey

Defined Under Namespace

Modules: Weather Classes: Fight

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(home, away, rng_seed, weather = nil) ⇒ Game

Returns a new instance of Game.

Parameters:

  • home (Team)
  • away (Team)
  • rng_seed (Integer)

    number to seed the game’s RNG with

  • weather (Class<Weatherable>) (defaults to: nil)


49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
# File 'lib/hlockey/game.rb', line 49

def initialize(home, away, rng_seed, weather = nil)
  @home = home
  @away = away
  @prng = Utils::Rng.new(rng_seed)

  @stadium = @home.stadium

  if weather.nil?
    weather = Weather::WEATHERS.sample(random: prng)
    @rng_seed = @prng.state
  end

  @weather = weather.new(self)
  @score = { home: 0, away: 0 }
  @in_progress = true
  @actions = 0
  @period = 1
  @stream = [Message.new(:game_start)]
  @faceoff = true
  @faceoff_won = false
  @team_with_puck = nil
  @puckless_team = nil
  @puck_holder_pos = nil
  @shooting_chance = 0
  @fight = nil
  @fight_chance = 0
  @pre_tmp_change_stats = {}
  @partying_teams = [@home, @away].select { _1.status == :partying }
  @do_final_cleanup = false

  set_mods_game
  @weather.on_game_start
end

Instance Attribute Details

#awayTeam (readonly)

Returns the teams in the game.

Returns:

  • (Team)

    the teams in the game



15
16
17
# File 'lib/hlockey/game.rb', line 15

def away
  @away
end

#fightFight? (readonly)

Returns the current fight, if there is any.

Returns:

  • (Fight, nil)

    the current fight, if there is any



40
41
42
# File 'lib/hlockey/game.rb', line 40

def fight
  @fight
end

#homeTeam (readonly)

Returns the teams in the game.

Returns:

  • (Team)

    the teams in the game



15
16
17
# File 'lib/hlockey/game.rb', line 15

def home
  @home
end

#in_progressBoolean (readonly) Also known as: in_progress?

Returns if the game is still in progress.

Returns:

  • (Boolean)

    if the game is still in progress



33
34
35
# File 'lib/hlockey/game.rb', line 33

def in_progress
  @in_progress
end

#periodInteger (readonly)

Returns what period the game is in.

Returns:

  • (Integer)

    what period the game is in



37
38
39
# File 'lib/hlockey/game.rb', line 37

def period
  @period
end

#pre_tmp_change_statsHash<Player => Hash<Symbol => Number>> (readonly)

Returns stats of players before changes.

Returns:

  • (Hash<Player => Hash<Symbol => Number>>)

    stats of players before changes



43
44
45
# File 'lib/hlockey/game.rb', line 43

def pre_tmp_change_stats
  @pre_tmp_change_stats
end

#prngUtils::Rng (readonly)

Returns the RNG the game is getting numbers from.

Returns:

  • (Utils::Rng)

    the RNG the game is getting numbers from



18
19
20
# File 'lib/hlockey/game.rb', line 18

def prng
  @prng
end

#scoreHash<Symbol => Integer> (readonly)

Returns the score of each team.

Returns:

  • (Hash<Symbol => Integer>)

    the score of each team



21
22
23
# File 'lib/hlockey/game.rb', line 21

def score
  @score
end

#stadiumTeam::Stadium (readonly)

Returns the stadium this game is located at.

Returns:



24
25
26
# File 'lib/hlockey/game.rb', line 24

def stadium
  @stadium
end

#streamArray<Message> (readonly)

Returns messages about game events.

Returns:

  • (Array<Message>)

    messages about game events



30
31
32
# File 'lib/hlockey/game.rb', line 30

def stream
  @stream
end

#weatherWeather::Weatherable (readonly)

Returns the weather effecting the game.

Returns:



27
28
29
# File 'lib/hlockey/game.rb', line 27

def weather
  @weather
end

Instance Method Details

#checkObject

Makes a player check in the game, regardless of if it would normally occur



147
148
149
150
151
152
153
154
155
156
157
158
159
# File 'lib/hlockey/game.rb', line 147

def check
  defender_pos = defensive_pos
  defender = @puckless_team.roster[defender_pos]

  @stream << Message.new(:game_hit, defender:, puck_holder:)
  if !puck_holder.mods_do(:on_got_hit, defender) &&
     try_take_puck(defender_pos, stat: :defense)
    @stream << Message.new(:game_possession_change,
                           player: defender, new_puck_team: defender.team)
  end

  @fight_chance += 0.1
end

#passObject

Makes a player pass in the game, regardless of if it would normally occur



125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
# File 'lib/hlockey/game.rb', line 125

def pass
  sender = puck_holder
  receiver_pos = pass_reciever_pos
  interceptor_pos = defensive_pos

  @stream << Message.new(:game_pass,
                         sender:,
                         receiver: @team_with_puck.roster[receiver_pos])

  if !@faceoff && try_take_puck(interceptor_pos, dis: 10 - @shooting_chance)
    @stream << Message.new(:game_pass_intercept,
                           sender:,
                           receiver: @puckless_team.roster[receiver_pos],
                           interceptor: @team_with_puck.roster[interceptor_pos])
    return
  end

  @puck_holder_pos = receiver_pos
  @shooting_chance += 1
end

#shoot(audacity: false) ⇒ Object

Makes a player shoot in the game, regardless of if it would normally occur

Parameters:

  • audacity (Boolean) (defaults to: false)

    if the function was called by Audacity weather



163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
# File 'lib/hlockey/game.rb', line 163

def shoot(audacity: false)
  return if @puck_holder_pos.nil?

  shooter = puck_holder

  @stream << Message.new(audacity ? :game_shot_audacious : :game_shot, shooter:)

  blocking_pos = @prng.rand(2).zero? ? :ldef : :rdef
  return if try_block_shot(blocking_pos) || try_block_shot(:goalie)

  # Goal scored

  scoring_team = @team_with_puck == @home ? :home : :away
  @score[scoring_team] += 1
  weather.on_goal(scoring_team)

  @stream << Message.new(:game_shot_scored, shooter:)

  start_fight if @prng.rand(3 + @fight_chance) > 3

  start_faceoff
  @actions = ACTIONS_PER_PERIOD - 1 if @period > NON_OT_PERIODS # Sudden death OT
end

#start_fight(starting_player = nil) ⇒ Object

Starts a fight in the game

Parameters:

  • starting_player (Player, nil) (defaults to: nil)

    the player that started the fight (optional)



189
190
191
192
193
194
# File 'lib/hlockey/game.rb', line 189

def start_fight(starting_player = nil)
  @fight = Fight.new(self, starting_player)
  @stream << Message.new(:fight_start,
                         home_player: @fight.players[:home].first,
                         away_player: @fight.players[:away].first)
end

#to_h(simple: false, stream_start_idx: 0, show_events: true, do_color: false) ⇒ Hash

Returns:

  • (Hash)


84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
# File 'lib/hlockey/game.rb', line 84

def to_h(simple: false, stream_start_idx: 0, show_events: true, do_color: false)
  res = {
    home: @home.name,
    away: @away.name,
    stadium: @stadium.to_s,
    weather: @weather.to_s,
    score: @score,
    in_progress: @in_progress,
    period: @period
  }
  unless simple
    if show_events
      res[:events] = @stream[stream_start_idx..]&.map(&:event)
    else
      res[:stream] = @stream[stream_start_idx..]&.map { _1.to_s(do_color:) }
    end

    res[:team_with_puck] = @team_with_puck&.name
    res[:shooting_chance] = @shooting_chance
  end

  res
end

#to_s(do_color: true) ⇒ String

Returns:

  • (String)


109
110
# File 'lib/hlockey/game.rb', line 109

def to_s(do_color: true) =
"#{Message.color(@home, do_color:)} vs #{Message.color(@away, do_color:)}"

#updateObject

Update the game state by one action



113
114
115
116
117
118
119
120
121
122
# File 'lib/hlockey/game.rb', line 113

def update
  if @do_final_cleanup
    @in_progress = false
    set_mods_game(unset: true)
    return
  end

  do_action
  handle_parties
end