Class: GitGameShow::PlayerManager

Inherits:
Object
  • Object
show all
Defined in:
lib/git_game_show/core/player_manager.rb

Overview

Manages player connections, scores, and related operations

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializePlayerManager



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

#playersObject (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

#scoresObject (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_countObject



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_namesObject



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_scoresObject



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_scoresObject



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_playerObject



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