Class: GitGameShow::MiniGameLoader
- Inherits:
-
Object
- Object
- GitGameShow::MiniGameLoader
- Defined in:
- lib/git_game_show/core/mini_game_loader.rb
Overview
Handles loading and selecting mini-games
Instance Attribute Summary collapse
-
#mini_games ⇒ Object
readonly
Returns the value of attribute mini_games.
Instance Method Summary collapse
-
#initialize ⇒ MiniGameLoader
constructor
A new instance of MiniGameLoader.
- #select_next_mini_game ⇒ Object
Constructor Details
#initialize ⇒ MiniGameLoader
Returns a new instance of MiniGameLoader.
6 7 8 9 10 |
# File 'lib/git_game_show/core/mini_game_loader.rb', line 6 def initialize @mini_games = load_mini_games @used_mini_games = [] @available_mini_games = [] end |
Instance Attribute Details
#mini_games ⇒ Object (readonly)
Returns the value of attribute mini_games.
4 5 6 |
# File 'lib/git_game_show/core/mini_game_loader.rb', line 4 def mini_games @mini_games end |
Instance Method Details
#select_next_mini_game ⇒ Object
12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 |
# File 'lib/git_game_show/core/mini_game_loader.rb', line 12 def select_next_mini_game # Special case for when only one mini-game type is enabled if @mini_games.size == 1 return @mini_games.first.new end # If we have no more available mini-games, reset the cycle if @available_mini_games.empty? # Handle the case where we might have only one game left after excluding the last used if @mini_games.size <= 2 @available_mini_games = @mini_games.dup else # Repopulate with all mini-games except the last one used (if possible) @available_mini_games = @mini_games.reject { |game| game == @used_mini_games.last } end end # Select a random game from the available ones selected_game = @available_mini_games.sample return @mini_games.first.new if selected_game.nil? # Fallback for safety # Remove the selected game from available and add to used @available_mini_games.delete(selected_game) @used_mini_games << selected_game # Return a new instance of the selected game class selected_game.new end |