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
- #add_maps(maps) ⇒ Object
- #destroy_teams ⇒ Object
- #empty? ⇒ Boolean
- #end_pug ⇒ Object
- #equal_number_of_players_on_each_team? ⇒ Boolean
- #full? ⇒ Boolean
- #game_map ⇒ Object
- #game_map=(map) ⇒ Object
- #has_exactly_maxplayers? ⇒ Boolean
-
#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
- #last_result_time ⇒ Object
- #leave(player_id) ⇒ Object
- #maps ⇒ Object
- #maxplayers ⇒ Object
- #notify_roles ⇒ Object
- #notify_roles=(roles) ⇒ Object
- #player_slots ⇒ Object
- #players ⇒ Object
- #queued_players ⇒ Object
- #remove_maps(maps) ⇒ Object
- #slots_left ⇒ Object
- #team(number) ⇒ Object
- #team_player_count(team_no) ⇒ Object
- #teamed_player_count ⇒ Object
- #teamed_players ⇒ Object
- #teams ⇒ Object
- #teamsize ⇒ Object
- #teamsize=(teamsize) ⇒ Object
- #total_player_count ⇒ Object
- #unteam(player_id) ⇒ Object
- #up_now_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
121 122 123 |
# File 'lib/pug.rb', line 121 def active? redis.get(pug_key) end |
#add_maps(maps) ⇒ Object
41 42 43 |
# File 'lib/pug.rb', line 41 def add_maps(maps) redis.sadd(maps_key, maps) end |
#destroy_teams ⇒ Object
29 30 31 32 33 |
# File 'lib/pug.rb', line 29 def destroy_teams teamed_players.each do |player_id| unteam(player_id) end end |
#empty? ⇒ Boolean
81 82 83 |
# File 'lib/pug.rb', line 81 def empty? total_player_count.zero? end |
#end_pug ⇒ Object
130 131 132 133 134 |
# File 'lib/pug.rb', line 130 def end_pug redis.keys([pug_key, "*"].join).each do |key| redis.del(key) end end |
#equal_number_of_players_on_each_team? ⇒ Boolean
164 165 166 167 168 169 170 |
# File 'lib/pug.rb', line 164 def equal_number_of_players_on_each_team? team_player_counts = teams.map do |_name, players| players.size end team_player_counts.uniq.size == 1 end |
#full? ⇒ Boolean
73 74 75 |
# File 'lib/pug.rb', line 73 def full? total_player_count >= maxplayers end |
#game_map ⇒ Object
105 106 107 |
# File 'lib/pug.rb', line 105 def game_map redis.get([pug_key, 'map'].join(':')) end |
#game_map=(map) ⇒ Object
101 102 103 |
# File 'lib/pug.rb', line 101 def game_map=(map) redis.set([pug_key, 'map'].join(':'), map) end |
#has_exactly_maxplayers? ⇒ Boolean
77 78 79 |
# File 'lib/pug.rb', line 77 def has_exactly_maxplayers? total_player_count == maxplayers end |
#join(player_id) ⇒ Object
13 14 15 16 17 |
# File 'lib/pug.rb', line 13 def join(player_id) = Time.now.to_i redis.setnx(pug_key, ) redis.zadd(queue_key, , player_id, nx: true) end |
#join_team(team_no:, player_id:) ⇒ Object
19 20 21 22 23 |
# File 'lib/pug.rb', line 19 def join_team(team_no:, player_id:) join(player_id) unteam(player_id) redis.sadd(team_key(team_no), player_id) end |
#joined?(player_id) ⇒ Boolean
136 137 138 |
# File 'lib/pug.rb', line 136 def joined?(player_id) redis.zrank(queue_key, player_id) end |
#last_result_time ⇒ Object
160 161 162 |
# File 'lib/pug.rb', line 160 def last_result_time redis.get(last_result_time_key).to_i end |
#leave(player_id) ⇒ Object
125 126 127 128 |
# File 'lib/pug.rb', line 125 def leave(player_id) leave_queue(player_id) unteam(player_id) end |
#maps ⇒ Object
49 50 51 |
# File 'lib/pug.rb', line 49 def maps redis.smembers(maps_key) end |
#maxplayers ⇒ Object
140 141 142 |
# File 'lib/pug.rb', line 140 def maxplayers teamsize * no_of_teams end |
#notify_roles ⇒ Object
113 114 115 |
# File 'lib/pug.rb', line 113 def notify_roles redis.get(notify_roles_key) || '@here' end |
#notify_roles=(roles) ⇒ Object
109 110 111 |
# File 'lib/pug.rb', line 109 def notify_roles=(roles) redis.set(notify_roles_key, roles) end |
#player_slots ⇒ Object
93 94 95 |
# File 'lib/pug.rb', line 93 def player_slots "#{total_player_count}/#{maxplayers}" end |
#players ⇒ Object
178 179 180 |
# File 'lib/pug.rb', line 178 def players redis.zrange(queue_key, 0, -1).map(&:to_i) end |
#queued_players ⇒ Object
144 145 146 |
# File 'lib/pug.rb', line 144 def queued_players players - teamed_players end |
#remove_maps(maps) ⇒ Object
45 46 47 |
# File 'lib/pug.rb', line 45 def remove_maps(maps) redis.srem(maps_key, maps) end |
#slots_left ⇒ Object
97 98 99 |
# File 'lib/pug.rb', line 97 def slots_left maxplayers - total_player_count end |
#team(number) ⇒ Object
61 62 63 |
# File 'lib/pug.rb', line 61 def team(number) redis.smembers(team_key(number)).map(&:to_i) end |
#team_player_count(team_no) ⇒ Object
89 90 91 |
# File 'lib/pug.rb', line 89 def team_player_count(team_no) redis.scard(team_key(team_no)).to_i end |
#teamed_player_count ⇒ Object
85 86 87 |
# File 'lib/pug.rb', line 85 def teamed_player_count teamed_players.count end |
#teamed_players ⇒ Object
35 36 37 38 39 |
# File 'lib/pug.rb', line 35 def teamed_players teams_keys.inject([]) do |players, team| players + redis.smembers(team).map(&:to_i) end end |
#teams ⇒ Object
148 149 150 151 152 153 154 |
# File 'lib/pug.rb', line 148 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
117 118 119 |
# File 'lib/pug.rb', line 117 def teamsize (redis.get(teamsize_key) || DEFAULT_TEAMSIZE).to_i end |
#teamsize=(teamsize) ⇒ Object
65 66 67 |
# File 'lib/pug.rb', line 65 def teamsize=(teamsize) redis.set(teamsize_key, teamsize) end |
#total_player_count ⇒ Object
69 70 71 |
# File 'lib/pug.rb', line 69 def total_player_count players.count end |
#unteam(player_id) ⇒ Object
172 173 174 175 176 |
# File 'lib/pug.rb', line 172 def unteam(player_id) teams_keys.each do |team| redis.srem(team, player_id) end end |
#up_now_players ⇒ Object
25 26 27 |
# File 'lib/pug.rb', line 25 def up_now_players players[0, maxplayers] end |
#update_last_result_time ⇒ Object
156 157 158 |
# File 'lib/pug.rb', line 156 def update_last_result_time redis.set(last_result_time_key, Time.now.to_i) end |
#vote(player_id:, map:) ⇒ Object
53 54 55 |
# File 'lib/pug.rb', line 53 def vote(player_id:, map:) redis.sadd(votes_key(map), player_id) end |
#vote_count(map) ⇒ Object
57 58 59 |
# File 'lib/pug.rb', line 57 def vote_count(map) redis.scard(votes_key(map)).to_i end |