Class: GitGameShow::PlayerManager
- Inherits:
-
Object
- Object
- GitGameShow::PlayerManager
- Defined in:
- lib/git_game_show/core/player_manager.rb
Overview
Manages player connections, scores, and related operations
Instance Attribute Summary collapse
-
#players ⇒ Object
readonly
Returns the value of attribute players.
-
#scores ⇒ Object
readonly
Returns the value of attribute scores.
Instance Method Summary collapse
- #add_player(name, ws) ⇒ Object
- #find_player_by_ws(ws) ⇒ Object
- #get_ws(name) ⇒ Object
-
#initialize ⇒ PlayerManager
constructor
A new instance of PlayerManager.
- #player_count ⇒ Object
- #player_exists?(name) ⇒ Boolean
- #player_names ⇒ Object
- #remove_player(name) ⇒ Object
- #reset_scores ⇒ Object
- #sorted_scores ⇒ Object
- #top_player ⇒ Object
- #update_score(name, points) ⇒ Object
Constructor Details
#initialize ⇒ PlayerManager
6 7 8 9 |
# File 'lib/git_game_show/core/player_manager.rb', line 6 def initialize @players = {} # WebSocket connections by player name @scores = {} # Player scores end |
Instance Attribute Details
#players ⇒ Object (readonly)
Returns the value of attribute players.
4 5 6 |
# File 'lib/git_game_show/core/player_manager.rb', line 4 def players @players end |
#scores ⇒ Object (readonly)
Returns the value of attribute scores.
4 5 6 |
# File 'lib/git_game_show/core/player_manager.rb', line 4 def scores @scores end |
Instance Method Details
#add_player(name, ws) ⇒ Object
11 12 13 14 15 16 |
# File 'lib/git_game_show/core/player_manager.rb', line 11 def add_player(name, ws) return false if @players.key?(name) @players[name] = ws @scores[name] = 0 true end |
#find_player_by_ws(ws) ⇒ Object
24 25 26 |
# File 'lib/git_game_show/core/player_manager.rb', line 24 def find_player_by_ws(ws) @players.key(ws) end |
#get_ws(name) ⇒ Object
32 33 34 |
# File 'lib/git_game_show/core/player_manager.rb', line 32 def get_ws(name) @players[name] end |
#player_count ⇒ Object
36 37 38 |
# File 'lib/git_game_show/core/player_manager.rb', line 36 def player_count @players.size end |
#player_exists?(name) ⇒ Boolean
28 29 30 |
# File 'lib/git_game_show/core/player_manager.rb', line 28 def player_exists?(name) @players.key?(name) end |
#player_names ⇒ Object
40 41 42 |
# File 'lib/git_game_show/core/player_manager.rb', line 40 def player_names @players.keys end |
#remove_player(name) ⇒ Object
18 19 20 21 22 |
# File 'lib/git_game_show/core/player_manager.rb', line 18 def remove_player(name) return false unless @players.key?(name) @players.delete(name) true end |
#reset_scores ⇒ Object
48 49 50 |
# File 'lib/git_game_show/core/player_manager.rb', line 48 def reset_scores @players.keys.each { |name| @scores[name] = 0 } end |
#sorted_scores ⇒ Object
52 53 54 |
# File 'lib/git_game_show/core/player_manager.rb', line 52 def sorted_scores @scores.sort_by { |_, score| -score }.to_h end |
#top_player ⇒ Object
56 57 58 59 |
# File 'lib/git_game_show/core/player_manager.rb', line 56 def top_player sorted = sorted_scores sorted.empty? ? nil : sorted.first end |
#update_score(name, points) ⇒ Object
44 45 46 |
# File 'lib/git_game_show/core/player_manager.rb', line 44 def update_score(name, points) @scores[name] = (@scores[name] || 0) + points end |