Class: Pug
- Inherits:
-
Object
- Object
- Pug
- Defined in:
- lib/pug.rb
Constant Summary collapse
- DEFAULT_TEAMSIZE =
4- MIN_NO_OF_TEAMS =
2
Class Method Summary collapse
Instance Method Summary collapse
- #active? ⇒ Boolean
- #actual_teams ⇒ Object
- #add_maps(maps) ⇒ Object
- #empty? ⇒ Boolean
- #end_pug ⇒ Object
- #equal_number_of_players_on_each_team? ⇒ Boolean
- #full? ⇒ Boolean
- #game_map ⇒ Object
- #game_map=(map) ⇒ Object
-
#initialize(channel_id) ⇒ Pug
constructor
A new instance of Pug.
- #join(player_id) ⇒ Object
- #join_team(team_no:, player_id:) ⇒ Object
- #joined?(player_id) ⇒ Boolean
- #joined_player_count ⇒ Object
- #joined_players ⇒ Object
- #last_result_time ⇒ Object
- #leave(player_id) ⇒ Object
- #maps ⇒ Object
- #maxplayers ⇒ Object
- #notify_roles ⇒ Object
- #notify_roles=(roles) ⇒ Object
- #player_slots ⇒ Object
- #remove_maps(maps) ⇒ Object
- #slots_left ⇒ Object
- #team(number) ⇒ Object
- #team_player_count(team_no) ⇒ Object
- #teams ⇒ Object
- #teamsize ⇒ Object
- #teamsize=(teamsize) ⇒ Object
- #unteam_all_players ⇒ Object
- #update_last_result_time ⇒ Object
- #vote(player_id:, map:) ⇒ Object
- #vote_count(map) ⇒ Object
Constructor Details
#initialize(channel_id) ⇒ Pug
Returns a new instance of Pug.
9 10 11 |
# File 'lib/pug.rb', line 9 def initialize(channel_id) @channel_id = channel_id end |
Class Method Details
.for(channel_id) ⇒ Object
5 6 7 |
# File 'lib/pug.rb', line 5 def self.for(channel_id) new(channel_id) end |
Instance Method Details
#active? ⇒ Boolean
102 103 104 |
# File 'lib/pug.rb', line 102 def active? redis.get(pug_key) end |
#actual_teams ⇒ Object
132 133 134 |
# File 'lib/pug.rb', line 132 def actual_teams teams.tap { |team| team.delete(0) } end |
#add_maps(maps) ⇒ Object
30 31 32 |
# File 'lib/pug.rb', line 30 def add_maps(maps) redis.sadd(maps_key, maps) end |
#empty? ⇒ Boolean
62 63 64 |
# File 'lib/pug.rb', line 62 def empty? joined_player_count.zero? end |
#end_pug ⇒ Object
110 111 112 113 114 |
# File 'lib/pug.rb', line 110 def end_pug redis.keys([pug_key, "*"].join).each do |key| redis.del(key) end end |
#equal_number_of_players_on_each_team? ⇒ Boolean
150 151 152 153 154 155 156 |
# File 'lib/pug.rb', line 150 def equal_number_of_players_on_each_team? team_player_counts = actual_teams.map do |_name, players| players.size end team_player_counts.uniq.size == 1 end |
#full? ⇒ Boolean
58 59 60 |
# File 'lib/pug.rb', line 58 def full? joined_player_count >= maxplayers end |
#game_map ⇒ Object
86 87 88 |
# File 'lib/pug.rb', line 86 def game_map redis.get([pug_key, 'map'].join(':')) end |
#game_map=(map) ⇒ Object
82 83 84 |
# File 'lib/pug.rb', line 82 def game_map=(map) redis.set([pug_key, 'map'].join(':'), map) end |
#join(player_id) ⇒ Object
13 14 15 16 |
# File 'lib/pug.rb', line 13 def join(player_id) redis.setnx(pug_key, Time.now.to_i) redis.sadd(team_key(0), player_id) end |
#join_team(team_no:, player_id:) ⇒ Object
18 19 20 21 22 |
# File 'lib/pug.rb', line 18 def join_team(team_no:, player_id:) redis.setnx(pug_key, Time.now.to_i) leave_teams(player_id) redis.sadd(team_key(team_no), player_id) end |
#joined?(player_id) ⇒ Boolean
116 117 118 |
# File 'lib/pug.rb', line 116 def joined?(player_id) joined_players.include?(player_id) end |
#joined_player_count ⇒ Object
66 67 68 |
# File 'lib/pug.rb', line 66 def joined_player_count joined_players.count end |
#joined_players ⇒ Object
24 25 26 27 28 |
# File 'lib/pug.rb', line 24 def joined_players teams_keys.inject([]) do |players, team| players + redis.smembers(team).map(&:to_i) end end |
#last_result_time ⇒ Object
146 147 148 |
# File 'lib/pug.rb', line 146 def last_result_time redis.get(last_result_time_key).to_i end |
#leave(player_id) ⇒ Object
106 107 108 |
# File 'lib/pug.rb', line 106 def leave(player_id) leave_teams(player_id) end |
#maps ⇒ Object
38 39 40 |
# File 'lib/pug.rb', line 38 def maps redis.smembers(maps_key) end |
#maxplayers ⇒ Object
120 121 122 |
# File 'lib/pug.rb', line 120 def maxplayers teamsize * no_of_teams end |
#notify_roles ⇒ Object
94 95 96 |
# File 'lib/pug.rb', line 94 def notify_roles redis.get(notify_roles_key) || '@here' end |
#notify_roles=(roles) ⇒ Object
90 91 92 |
# File 'lib/pug.rb', line 90 def notify_roles=(roles) redis.set(notify_roles_key, roles) end |
#player_slots ⇒ Object
74 75 76 |
# File 'lib/pug.rb', line 74 def player_slots "#{joined_player_count}/#{maxplayers}" end |
#remove_maps(maps) ⇒ Object
34 35 36 |
# File 'lib/pug.rb', line 34 def remove_maps(maps) redis.srem(maps_key, maps) end |
#slots_left ⇒ Object
78 79 80 |
# File 'lib/pug.rb', line 78 def slots_left maxplayers - joined_player_count end |
#team(number) ⇒ Object
50 51 52 |
# File 'lib/pug.rb', line 50 def team(number) redis.smembers(team_key(number)).map(&:to_i) end |
#team_player_count(team_no) ⇒ Object
70 71 72 |
# File 'lib/pug.rb', line 70 def team_player_count(team_no) redis.scard(team_key(team_no)).to_i end |
#teams ⇒ Object
124 125 126 127 128 129 130 |
# File 'lib/pug.rb', line 124 def teams all_teams = teams_keys.inject({}) do |teams, team| teams.merge({ team.split(':').last.to_i => redis.smembers(team).map(&:to_i) }) end all_teams.sort.to_h end |
#teamsize ⇒ Object
98 99 100 |
# File 'lib/pug.rb', line 98 def teamsize (redis.get(teamsize_key) || DEFAULT_TEAMSIZE).to_i end |
#teamsize=(teamsize) ⇒ Object
54 55 56 |
# File 'lib/pug.rb', line 54 def teamsize=(teamsize) redis.set(teamsize_key, teamsize) end |
#unteam_all_players ⇒ Object
136 137 138 139 140 |
# File 'lib/pug.rb', line 136 def unteam_all_players joined_players.each do |player_id| join_team(team_no: 0, player_id: player_id) end end |
#update_last_result_time ⇒ Object
142 143 144 |
# File 'lib/pug.rb', line 142 def update_last_result_time redis.set(last_result_time_key, Time.now.to_i) end |
#vote(player_id:, map:) ⇒ Object
42 43 44 |
# File 'lib/pug.rb', line 42 def vote(player_id:, map:) redis.sadd(votes_key(map), player_id) end |
#vote_count(map) ⇒ Object
46 47 48 |
# File 'lib/pug.rb', line 46 def vote_count(map) redis.scard(votes_key(map)).to_i end |