Class: KAG::Gather::Plugin

Inherits:
Object
  • Object
show all
Includes:
Cinch::Commands, Cinch::Plugin, Common
Defined in:
lib/kag/gather/plugin.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Common

#_h, #debug, #is_admin, #is_banned?, #reply, #send_channels_msg

Methods included from Cinch::Commands

included

Constructor Details

#initialize(*args) ⇒ Plugin

Returns a new instance of Plugin.



19
20
21
22
23
24
# File 'lib/kag/gather/plugin.rb', line 19

def initialize(*args)
  super
  @queue = KAG::Gather::Queue.new
  @matches = {}
  _load_servers
end

Instance Attribute Details

#queueObject

Returns the value of attribute queue.



17
18
19
# File 'lib/kag/gather/plugin.rb', line 17

def queue
  @queue
end

#serversObject

Returns the value of attribute servers.



17
18
19
# File 'lib/kag/gather/plugin.rb', line 17

def servers
  @servers
end

Instance Method Details

#_load_serversObject



26
27
28
# File 'lib/kag/gather/plugin.rb', line 26

def _load_servers
  @servers = KAG::Server::Instance.fetch_all
end

#add(m) ⇒ Object



79
80
81
82
83
84
85
# File 'lib/kag/gather/plugin.rb', line 79

def add(m)
  unless is_banned?(m.user)
    KAG::User::User.add_stat(m.user,:adds)
    KAG::Stats::Main.add_stat(:adds)
    add_user_to_queue(m,m.user)
  end
end

#add_admin(m, nicks) ⇒ Object



271
272
273
274
275
276
277
278
279
280
281
282
283
# File 'lib/kag/gather/plugin.rb', line 271

def add_admin(m, nicks)
  if is_admin(m.user)
    nicks = nicks.split(",")
    nicks.each do |nick|
      u = User(nick)
      if u
        add_user_to_queue(m,u)
      else
        reply m,"Could not find user #{nick}"
      end
    end
  end
end

#add_silent(m, nicks) ⇒ Object



288
289
290
291
292
293
294
295
296
297
298
299
300
301
# File 'lib/kag/gather/plugin.rb', line 288

def add_silent(m, nicks)
  if is_admin(m.user)
    nicks = nicks.split(",")
    puts nicks.inspect
    nicks.each do |nick|
      u = User(nick)
      if u
        add_user_to_queue(m,u,false)
      else
        reply m,"Could not find user #{nick}"
      end
    end
  end
end

#add_user_to_queue(m, user, send_msg = true) ⇒ Object



140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
# File 'lib/kag/gather/plugin.rb', line 140

def add_user_to_queue(m,user,send_msg = true)
  if @queue.has_player?(user)
    reply m,"#{user.authname} is already in the queue!"
    false
  elsif get_match_in(user)
    reply m,"#{user.authname} is already in a match!"
    false
  else
    if @queue.add(user)
      send_channels_msg "Added #{user.authname} to queue (#{KAG::Gather::Match.type_as_string}) [#{@queue.length}]" if send_msg
      check_for_new_match
      true
    else
      false
    end
  end
end

#check_for_new_matchObject



181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
# File 'lib/kag/gather/plugin.rb', line 181

def check_for_new_match
  if @queue.length >= KAG::Config.instance[:match_size]
    server = get_unused_server
    unless server
      send_channels_msg "Could not find any available servers!"
      debug "FAILED TO FIND UNUSED SERVER"
      return false
    end

    players = @queue.players

    # reset queue first to prevent 11-player load
    @queue.reset

    match = KAG::Gather::Match.new(SymbolTable.new({
      :server => server,
      :players => players,
      :bot => self.bot
    }))
    match.start # prepare match data
    messages = match.notify_teams_of_match_start # gather texts for private messages
    send_channels_msg(match.text_for_match_start,false) # send channel-wide first
    messages.each do |user,msg|
      user.send(msg)
      sleep(2) # prevent excess flood stuff
    end
    @matches[server[:key]] = match
  end
end

#clear(m) ⇒ Object



225
226
227
228
229
230
# File 'lib/kag/gather/plugin.rb', line 225

def clear(m)
  if is_admin(m.user)
    send_channels_msg "Match queue cleared."
    @queue.reset
  end
end

#end(m) ⇒ Object



124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
# File 'lib/kag/gather/plugin.rb', line 124

def end(m)
  unless is_banned?(m.user)
    match = get_match_in(m.user)
    if match
      match.add_end_vote
      if match.voted_to_end?
        match.cease
        @matches.delete(match.server[:key])
        send_channels_msg("Match at #{match.server[:key]} finished!")
      else
        reply m,"End vote started, #{match.get_needed_end_votes_left} more votes to end match at #{match.server[:key]}"
      end
    end
  end
end

#get_match_in(user) ⇒ Object



171
172
173
174
175
176
177
178
179
# File 'lib/kag/gather/plugin.rb', line 171

def get_match_in(user)
  m = false
  @matches.each do |k,match|
    if match.has_player?(user)
      m = match
    end
  end
  m
end

#get_unused_serverObject



211
212
213
214
215
216
217
218
# File 'lib/kag/gather/plugin.rb', line 211

def get_unused_server
  server = false
  @servers.shuffle!
  @servers.each do |k,s|
    server = s unless s.in_use?
  end
  server
end

#kick_from_match(m, nick) ⇒ Object



358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
# File 'lib/kag/gather/plugin.rb', line 358

def kick_from_match(m,nick)
  if is_admin(m.user)
    user = User(nick.to_s)
    user.refresh
    if user
      match = get_match_in(user)
      if match
        match.remove_player(user)
        m.reply "#{user.nick} has been kicked from the match"
      else
        m.reply "#{user.nick} is not in a match!"
      end
    else
      reply m,"User #{nick} not found"
    end
  end
end

#list(m) ⇒ Object



107
108
109
110
111
# File 'lib/kag/gather/plugin.rb', line 107

def list(m)
  unless is_banned?(m.user)
    m.user.send "Queue (#{KAG::Gather::Match.type_as_string}) [#{@queue.length}] #{@queue.list}"
  end
end

#next_map(m) ⇒ Object



332
333
334
335
336
337
338
339
# File 'lib/kag/gather/plugin.rb', line 332

def next_map(m)
  if is_admin(m.user)
    match = get_match_in(m.user)
    if match and match.server
      match.server.next_map
    end
  end
end

#next_map_specify(m, server) ⇒ Object



345
346
347
348
349
350
351
352
353
# File 'lib/kag/gather/plugin.rb', line 345

def next_map_specify(m,server)
  if is_admin(m.user)
    if @servers[server]
      @servers[server].next_map
    else
      m.reply "No server found with key #{server}"
    end
  end
end

#on_leaving(m, user) ⇒ Object



35
36
37
38
39
40
41
42
43
44
45
# File 'lib/kag/gather/plugin.rb', line 35

def on_leaving(m,user)
  match = get_match_in(user)
  if match
    sub = match.remove_player(user)
    if sub
      m.channel.msg sub[:msg]
    end
  elsif @queue.has_player?(user)
    remove_user_from_queue(user)
  end
end

#on_nick(m) ⇒ Object



48
49
# File 'lib/kag/gather/plugin.rb', line 48

def on_nick(m)
end

#rem(m) ⇒ Object



89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
# File 'lib/kag/gather/plugin.rb', line 89

def rem(m)
  unless is_banned?(m.user)
    match = get_match_in(m.user)
    if match
      match.remove_player(m.user)
      send_channels_msg "#{m.user.authname} has left the match at #{match.server[:key]}! You can sub in by typing !sub"
    elsif @queue.has_player?(m.user)
      KAG::User::User.add_stat(m.user,:rems)
      KAG::Stats::Main.add_stat(:rems)
      unless remove_user_from_queue(m.user)
        debug "#{m.user.authname} is not in the queue."
      end
    end
  end
end

#rem_admin(m, nicks) ⇒ Object



236
237
238
239
240
241
242
243
244
245
246
247
248
# File 'lib/kag/gather/plugin.rb', line 236

def rem_admin(m, nicks)
  if is_admin(m.user)
    nicks = nicks.split(",")
    nicks.each do |nick|
      u = User(nick)
      if u
        remove_user_from_queue(u)
      else
        reply m,"Could not find user #{nick}"
      end
    end
  end
end

#rem_silent(m, nicks) ⇒ Object



253
254
255
256
257
258
259
260
261
262
263
264
265
# File 'lib/kag/gather/plugin.rb', line 253

def rem_silent(m, nicks)
  if is_admin(m.user)
    nicks = nicks.split(",")
    nicks.each do |nick|
      u = User(nick)
      if u
        remove_user_from_queue(u,false)
      else
        reply m,"Could not find user #{nick}"
      end
    end
  end
end

#remove_user_from_queue(user, send_msg = true) ⇒ Object



158
159
160
161
162
163
164
165
166
167
168
169
# File 'lib/kag/gather/plugin.rb', line 158

def remove_user_from_queue(user,send_msg = true)
  if @queue.has_player?(user)
    if @queue.remove(user)
      send_channels_msg "Removed #{user.authname} from queue (#{KAG::Gather::Match.type_as_string}) [#{@queue.length}]" if send_msg
      true
    else
      false
    end
  else
    false
  end
end

#restart_map(m) ⇒ Object



306
307
308
309
310
311
312
313
# File 'lib/kag/gather/plugin.rb', line 306

def restart_map(m)
  if is_admin(m.user)
    match = get_match_in(m.user)
    if match and match.server
      match.server.restart_map
    end
  end
end

#restart_map_specify(m, server) ⇒ Object



319
320
321
322
323
324
325
326
327
# File 'lib/kag/gather/plugin.rb', line 319

def restart_map_specify(m,server)
  if is_admin(m.user)
    if @servers[server.to_sym]
      @servers[server.to_sym].restart_map
    else
      m.reply "No server found with key #{server.to_s}"
    end
  end
end

#status(m) ⇒ Object



115
116
117
118
119
# File 'lib/kag/gather/plugin.rb', line 115

def status(m)
  unless is_banned?(m.user)
    reply m,"Matches in progress: #{@matches.length.to_s}"
  end
end

#sub(m) ⇒ Object



62
63
64
65
66
67
68
69
70
71
72
73
74
75
# File 'lib/kag/gather/plugin.rb', line 62

def sub(m)
  unless is_banned?(m.user)
    @matches.each do |k,match|
      if match.needs_sub?
        placement = match.sub_in(m.user)
        if placement
          placement[:channel_msg] = placement[:channel_msg].gsub("[[+user]]",m.user.authname)
          reply m,placement[:channel_msg]
          m.user.send placement[:private_msg]
        end
      end
    end
  end
end