hlockey 3 → 4

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -6,6 +6,9 @@ module Hlockey
6
6
  class Weather
7
7
  def initialize(game)
8
8
  @game = game
9
+ @home = @game.home
10
+ @away = @game.away
11
+ @prng = @game.prng
9
12
  end
10
13
 
11
14
  def to_s
@@ -24,33 +27,80 @@ module Hlockey
24
27
 
25
28
  private
26
29
 
27
- def worst_player_rand_replace(message, apprx_times_per_game = 1, store_old_in = nil)
28
- return unless @game.prng.rand(Game::NON_OT_ACTIONS / apprx_times_per_game).zero?
29
-
30
- team = @game.prng.rand(2).zero? ? @game.home : @game.away
31
- pos = team.worst_player_pos
30
+ # Swaps roster & shadows player, used for Chicken & Waves weather
31
+ # @param team_sym [:home, :away]
32
+ # @param pos [Symbol] position of player swapped out
33
+ # @param shadows_idx [Integer] index of shadows player swapped in
34
+ # @return [Array<Player>] the players that were swapped
35
+ def swap(team_sym, pos, shadows_idx)
36
+ team = @game.send(team_sym)
32
37
 
33
38
  prev_player = team.roster[pos]
34
- next_player = Player.new(@game.prng)
35
-
39
+ next_player = team.shadows[shadows_idx]
36
40
  team.roster[pos] = next_player
37
- store_old_in[team][pos] ||= prev_player unless store_old_in.nil?
41
+ team.shadows[shadows_idx] = prev_player
42
+
43
+ unless @game.fight.nil?
44
+ player_array = @game.fight.players[team_sym]
45
+ player_idx = player_array.index(prev_player)
46
+
47
+ player_array[player_idx] = next_player unless player_idx.nil?
48
+ end
49
+
50
+ [prev_player, next_player]
51
+ end
38
52
 
39
- @game.stream << Message.send(message, prev_player, next_player)
53
+ # @return [:home, :away]
54
+ def rand_team
55
+ @prng.rand(2).zero? ? :home : :away
56
+ end
57
+ end
58
+
59
+ class Audacity < Weather
60
+ def on_action
61
+ @game.shoot(audacity: true) if @prng.rand(Game::NON_OT_ACTIONS / 10).zero?
40
62
  end
41
63
  end
42
64
 
43
65
  class Chicken < Weather
44
66
  def on_game_start
45
- @chickened_out = { @game.home => {}, @game.away => {} }
67
+ @chickened_out = { home: {}, away: {} }
46
68
  end
47
69
 
48
70
  def on_action
49
- worst_player_rand_replace(:ChickenedOut, 5, @chickened_out)
71
+ return unless @prng.rand(Game::NON_OT_ACTIONS / 5).zero?
72
+
73
+ team = rand_team
74
+ pos, shadows_idx = get_swap(team)
75
+ if pos.nil? || shadows_idx.nil?
76
+ team = team == :home ? :away : :home
77
+ pos, shadows_idx = get_swap(team)
78
+ return if pos.nil? || shadows_idx.nil?
79
+ end
80
+
81
+ @chickened_out[team][pos] = shadows_idx
82
+
83
+ @game.stream << Message.ChickenedOut(*swap(team, pos, shadows_idx))
50
84
  end
51
85
 
52
86
  def on_game_end
53
- @chickened_out.each { |team, players| team.roster.merge!(players) }
87
+ @chickened_out.each do |team, swaps|
88
+ swaps.each { |pos, shadows_idx| swap(team, pos, shadows_idx) }
89
+ end
90
+ end
91
+
92
+ private
93
+
94
+ # @param team_sym [:home, :away]
95
+ # @return [Array<Symbol, Integer>]
96
+ def get_swap(team_sym)
97
+ team = @game.send(team_sym)
98
+
99
+ [
100
+ (team.roster.keys - @chickened_out[team_sym].keys).sample(random: @prng),
101
+ (team.shadows.each_index.to_a - @chickened_out[team_sym].values)
102
+ .sample(random: @prng)
103
+ ]
54
104
  end
55
105
  end
56
106
 
@@ -62,14 +112,14 @@ module Hlockey
62
112
  def on_goal(scoring_team)
63
113
  return unless scoring_team == @favored_team
64
114
 
65
- @game.score[scoring_team] += @game.prng.rand(0.1..0.5)
115
+ @game.score[scoring_team] += @prng.rand(0.1..0.5)
66
116
  make_incline
67
117
  end
68
118
 
69
119
  private
70
120
 
71
121
  def make_incline
72
- new_favored_team = @game.prng.rand(2).zero? ? :home : :away
122
+ new_favored_team = rand_team
73
123
  return unless new_favored_team != @favored_team
74
124
 
75
125
  @favored_team = new_favored_team
@@ -79,11 +129,15 @@ module Hlockey
79
129
 
80
130
  class Stars < Weather
81
131
  def on_period_end
82
- return unless @game.score[:home] != @game.score[:away]
132
+ team = %i[home away].find { |sym| @game.send(sym).to_s == "Sleepers" }
133
+ if team.nil?
134
+ return if @game.score[:home] == @game.score[:away]
83
135
 
84
- losing_team = @game.score.min_by { |_k, v| v }.first
85
- @game.score[losing_team] += 0.5
86
- @game.stream << Message.StarsPity(@game.send(losing_team))
136
+ team = @game.score.min_by { |_, v| v }.first
137
+ end
138
+
139
+ @game.score[team] += 0.5
140
+ @game.stream << Message.StarsAlign(@game.send(team))
87
141
  end
88
142
  end
89
143
 
@@ -100,15 +154,15 @@ module Hlockey
100
154
 
101
155
  class Waves < Weather
102
156
  def on_action
103
- worst_player_rand_replace(:WavesWashedAway)
157
+ return unless @prng.rand(Game::NON_OT_ACTIONS).zero?
158
+
159
+ team_sym = rand_team
160
+ team = @game.send(team_sym)
161
+ @game.stream << Message.WavesWashedAway(*swap(team_sym,
162
+ team.worst_player_pos,
163
+ @prng.rand(team.shadows.length)))
104
164
  end
105
165
  end
106
166
 
107
- Weather::WEIGHTS = {
108
- Chicken => 4,
109
- Inclines => 4,
110
- Stars => 4,
111
- Sunset => 4,
112
- Waves => 3
113
- }.freeze
167
+ Weather::WEATHERS = [Audacity, Chicken, Inclines, Stars, Sunset, Waves].freeze
114
168
  end
metadata CHANGED
@@ -1,15 +1,29 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: hlockey
3
3
  version: !ruby/object:Gem::Version
4
- version: '3'
4
+ version: '4'
5
5
  platform: ruby
6
6
  authors:
7
7
  - Lavender Perry
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2023-06-02 00:00:00.000000000 Z
12
- dependencies: []
11
+ date: 2023-07-04 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: psych
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '5'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '5'
13
27
  description: Hlockey library.
14
28
  email: endie2@protonmail.com
15
29
  executables: []
@@ -21,6 +35,7 @@ files:
21
35
  - data/information.yaml
22
36
  - data/league.yaml
23
37
  - data/links.yaml
38
+ - data/previous_election_results.yaml
24
39
  - lib/hlockey.rb
25
40
  - lib/hlockey/constants.rb
26
41
  - lib/hlockey/data.rb
@@ -57,5 +72,5 @@ requirements: []
57
72
  rubygems_version: 3.4.10
58
73
  signing_key:
59
74
  specification_version: 4
60
- summary: Hlockey season 3.
75
+ summary: Hlockey season 4.
61
76
  test_files: []