Class: Pug

Inherits:
Object
  • Object
show all
Defined in:
lib/pug.rb

Constant Summary collapse

DEFAULT_TEAMSIZE =
4
MIN_NO_OF_TEAMS =
2

Class Method Summary collapse

Instance Method Summary collapse

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

Returns:

  • (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_teamsObject



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

Returns:

  • (Boolean)


81
82
83
# File 'lib/pug.rb', line 81

def empty?
  total_player_count.zero?
end

#end_pugObject



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

Returns:

  • (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

Returns:

  • (Boolean)


73
74
75
# File 'lib/pug.rb', line 73

def full?
  total_player_count >= maxplayers
end

#game_mapObject



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

Returns:

  • (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)
  timestamp = Time.now.to_i
  redis.setnx(pug_key, timestamp)
  redis.zadd(queue_key, timestamp, 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

Returns:

  • (Boolean)


136
137
138
# File 'lib/pug.rb', line 136

def joined?(player_id)
  redis.zrank(queue_key, player_id)
end

#last_result_timeObject



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

#mapsObject



49
50
51
# File 'lib/pug.rb', line 49

def maps
  redis.smembers(maps_key)
end

#maxplayersObject



140
141
142
# File 'lib/pug.rb', line 140

def maxplayers
  teamsize * no_of_teams
end

#notify_rolesObject



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_slotsObject



93
94
95
# File 'lib/pug.rb', line 93

def player_slots
  "#{total_player_count}/#{maxplayers}"
end

#playersObject



178
179
180
# File 'lib/pug.rb', line 178

def players
  redis.zrange(queue_key, 0, -1).map(&:to_i)
end

#queued_playersObject



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_leftObject



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_countObject



85
86
87
# File 'lib/pug.rb', line 85

def teamed_player_count
  teamed_players.count
end

#teamed_playersObject



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

#teamsObject



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

#teamsizeObject



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_countObject



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_playersObject



25
26
27
# File 'lib/pug.rb', line 25

def up_now_players
  players[0, maxplayers]
end

#update_last_result_timeObject



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