qwtf_discord_bot 5.4.4 → 5.4.9

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 81a6cd600bd16f113cbfd1a44b05a5d362428c5f31e3bc3a1310fc243cbb14d7
4
- data.tar.gz: 0da538524b5ad76d74ce7eaf380331769759e540ffc80719d784abea9d0789eb
3
+ metadata.gz: 3978acdc270b907c20e237c57690192b753aa96be251edcda59f80f60fa188a5
4
+ data.tar.gz: fbda19b74c0f215cbd2789743db1e303352dae5488fe4633e683c7982b7295f2
5
5
  SHA512:
6
- metadata.gz: 2c349a08aaeb38a0dee3448fe0f57307dd20d17b71ff6dbae4e6a1da2d4c6198c13ed04ca560386013fcc89cafa7d584a5f5388b965594d6e8879c71d86efdc4
7
- data.tar.gz: 73e8b9482f07568f16fc3c1100103aa2679e84f4cf656d163bb0f5dc7d51259b8d422498b7dfcef2c62fca5f093b8a6981814dae956da0d5bd49eae044e076b1
6
+ metadata.gz: 0b343f7bbcf1435df653f9cc6a810db2a09472d2238134a8acc39446c1e54a828a5c6d1e5fbef318271bd0019d70304febe39aab107ceefe8ceada2ae7318a99
7
+ data.tar.gz: e627fca364cd5f51fa3514c4c8c66d9bf80486f6114778bf82086165706503d808d89fc2b717ea4e2bcfc70d8fe7de5376432f68436a836d49e92b833611add4
data/README.md CHANGED
@@ -90,13 +90,14 @@ This responds to discord messages:
90
90
  - `!join`
91
91
  - `!leave`
92
92
  - `!teamsize <no_of_players>`
93
- - `!kick <@player>`
93
+ - `!kick <@player> [@player2]`
94
94
  - `!team <team_no>`
95
95
  - `!unteam`
96
96
  - `!addmap <map_name>`
97
97
  - `!removemap <map_name>`
98
98
  - `!maps`
99
- - `!map <map_name>`
99
+ - `!map [map_name]`
100
+ - `!choose [n]`
100
101
  - `!win <team_no>`
101
102
  - `!draw`
102
103
  - `!notify <@role>`
data/VERSION CHANGED
@@ -1 +1 @@
1
- 5.4.4
1
+ 5.4.9
@@ -7,7 +7,7 @@ class QwtfDiscordBotPug # :nodoc:
7
7
  include QwtfDiscordBot
8
8
 
9
9
  MSG_SNIPPET_DELIMITER = ' · '
10
- TEAM_NAMES = { 0 => "No team", 1 => "Blue", 2 => "Red" }
10
+ TEAM_NAMES = { 1 => "Blue", 2 => "Red" }
11
11
 
12
12
  def run
13
13
  bot = Discordrb::Commands::CommandBot.new(
@@ -26,86 +26,114 @@ class QwtfDiscordBotPug # :nodoc:
26
26
  )
27
27
 
28
28
  bot.command :help do |event, *args|
29
- "Pug commands: `!status`, `!join`, `!team <team_no>`, `!unteam`, `!leave`, `!kick <@player>`, `!win <team_no>`, `!draw`, `!end`, `!teamsize <no_of_players>`, `!addmap <map_name>`, `!removemap <map_name>`, `!maps`, `!map <map_name>`, `!notify <@role>`"
29
+ "Pug commands: `!status`, `!join`, `!team <team_no> [@player1] [@player2]`, `!unteam`, `!leave`, `!kick <@player>`, `!win <team_no>`, `!draw`, `!end`, `!teamsize <no_of_players>`, `!addmap <map_name>`, `!removemap <map_name>`, `!maps`, `!map [map_name]`, `!choose [n]`, `!notify <@role>`"
30
30
  end
31
31
 
32
32
  bot.command :join do |event, *args|
33
33
  setup_pug(event) do |e, pug|
34
- return send_embedded_message("You've already joined", e.channel) if pug.joined?(e.user_id)
34
+ if pug.joined?(e.user_id)
35
+ return send_embedded_message(
36
+ description: "You've already joined",
37
+ channel: e.channel
38
+ )
39
+ end
35
40
 
36
41
  join_pug(e, pug)
37
42
  start_pug(pug, e) if pug.full?
38
43
  end
39
44
  end
40
45
 
41
- bot.command :status do |event, *args|
46
+ bot.command :choose do |event, *args|
42
47
  setup_pug(event) do |e, pug|
43
- return send_embedded_message('No PUG has been started. `!join` to create', e.channel) unless pug.active?
44
-
45
- footer = [
46
- pug.game_map,
47
- "#{pug.player_slots} joined",
48
- ].compact.join(MSG_SNIPPET_DELIMITER)
48
+ if pug.joined_players.count.odd?
49
+ return send_embedded_message(
50
+ description: "Can't choose teams with odd number of players",
51
+ channel: event.channel
52
+ )
53
+ end
49
54
 
50
- send_embedded_message(nil, e.channel) do |embed|
51
- embed.footer = Discordrb::Webhooks::EmbedFooter.new(
52
- text: footer
55
+ if args.any? && args.first.to_i < 1
56
+ return send_embedded_message(
57
+ description: "Choose a number higher than 0; e.g. `!choose 2`",
58
+ channel: e.channel
53
59
  )
60
+ end
54
61
 
55
- pug.teams.each do |team_no, player_ids|
56
- team_display_names = player_ids.map do |player_id|
57
- e.display_name_for(player_id)
58
- end
62
+ iteration = if args.any?
63
+ args.first.to_i - 1
64
+ else
65
+ 0
66
+ end
59
67
 
60
- embed.add_field(
61
- Discordrb::Webhooks::EmbedField.new(
62
- {
63
- inline: true,
64
- name: team_name(team_no),
65
- value: team_display_names.join("\n")
66
- }
67
- )
68
- )
69
- end
68
+ if choose_fair_teams(pug: pug, event: e, iteration: iteration)
69
+ status(pug: pug, event: e)
70
70
  end
71
71
  end
72
72
  end
73
73
 
74
- bot.command :teamsize do |event, *args|
74
+ bot.command :status do |event, *args|
75
75
  setup_pug(event) do |e, pug|
76
- return send_embedded_message("Team size is #{pug.teamsize}", e.channel) unless args.any?
77
-
78
- new_teamsize = args[0].to_i
79
- return send_embedded_message('Team size should be a number higher than 0', e.channel) unless new_teamsize > 0
76
+ if !pug.active?
77
+ return send_embedded_message(
78
+ description: "No PUG has been started. `!join` to create",
79
+ channel: e.channel
80
+ )
81
+ end
80
82
 
81
- if new_teamsize
82
- pug.teamsize = new_teamsize
83
+ status(pug: pug, event: e)
84
+ end
85
+ end
83
86
 
84
- send_embedded_message(
85
- [
86
- "Team size set to #{pug.teamsize}",
87
+ bot.command :teamsize do |event, *args|
88
+ setup_pug(event) do |e, pug|
89
+ unless args.any?
90
+ return send_embedded_message(
91
+ description: [
92
+ "Each team has #{pug.teamsize} players",
87
93
  "#{pug.player_slots} joined"
88
94
  ].join(MSG_SNIPPET_DELIMITER),
89
- e.channel
95
+ channel: e.channel
90
96
  )
97
+ end
91
98
 
92
- start_pug(pug, e) if pug.full?
93
- else
94
- send_embedded_message(
95
- [
96
- "Current team size is #{pug.teamsize}",
97
- "#{pug.player_slots} joined"
98
- ].join(MSG_SNIPPET_DELIMITER),
99
- e.channel
99
+ new_teamsize = args[0].to_i
100
+
101
+ if new_teamsize < 1
102
+ return send_embedded_message(
103
+ description: "Team size should be 1 or more",
104
+ channel: e.channel
100
105
  )
101
106
  end
107
+
108
+ pug.teamsize = new_teamsize
109
+
110
+ send_embedded_message(
111
+ description: [
112
+ "Each team has #{pug.teamsize} players",
113
+ "#{pug.player_slots} joined"
114
+ ].join(MSG_SNIPPET_DELIMITER),
115
+ channel: e.channel
116
+ )
117
+
118
+ start_pug(pug, e) if pug.full?
102
119
  end
103
120
  end
104
121
 
105
122
  bot.command :leave do |event, *args|
106
123
  setup_pug(event) do |e, pug|
107
- return send_embedded_message(no_active_pug_message, e.channel) unless pug.active?
108
- return send_embedded_message("You're not in the PUG", e.channel) unless pug.joined?(e.user_id)
124
+ unless pug.active?
125
+ return send_embedded_message(
126
+ description: no_active_pug_message,
127
+ channel: e.channel
128
+ )
129
+ end
130
+
131
+ unless pug.joined?(e.user_id)
132
+ return send_embedded_message(
133
+ description: "You're not in the PUG",
134
+ channel: e.channel
135
+ )
136
+ end
109
137
 
110
138
  pug.leave(e.user_id)
111
139
 
@@ -114,11 +142,12 @@ class QwtfDiscordBotPug # :nodoc:
114
142
  "#{pug.player_slots} remain"
115
143
  ]
116
144
 
117
- snippets << "#{pug.slots_left} more #{pug.notify_roles}" if pug.slots_left == 1
145
+ message = "#{pug.slots_left} more #{pug.notify_roles}" if pug.slots_left == 1
118
146
 
119
147
  send_embedded_message(
120
- snippets.join(MSG_SNIPPET_DELIMITER),
121
- e.channel
148
+ message: message,
149
+ description: snippets.join(MSG_SNIPPET_DELIMITER),
150
+ channel: e.channel
122
151
  )
123
152
 
124
153
  end_pug(pug, e.channel) if pug.empty?
@@ -127,12 +156,26 @@ class QwtfDiscordBotPug # :nodoc:
127
156
 
128
157
  bot.command :kick do |event, *args|
129
158
  setup_pug(event) do |e, pug|
130
- return send_embedded_message("Kick who? e.g. `!kick @#{e.display_name}`", e.channel) unless args.any?
131
- return send_embedded_message(no_active_pug_message, e.channel) unless pug.active?
159
+ unless args.any?
160
+ return send_embedded_message(
161
+ description: "Kick who? e.g. `!kick @#{e.display_name}`",
162
+ channel: e.channel
163
+ )
164
+ end
165
+
166
+ unless pug.active?
167
+ return send_embedded_message(
168
+ description: no_active_pug_message,
169
+ channel: e.channel
170
+ )
171
+ end
132
172
 
133
173
  args.each do |arg|
134
174
  unless arg.match(/<@!\d+>/)
135
- send_embedded_message("#{arg} isn't a valid mention", e.channel)
175
+ send_embedded_message(
176
+ description: "#{arg} isn't a valid mention",
177
+ channel: e.channel
178
+ )
136
179
  next
137
180
  end
138
181
 
@@ -140,7 +183,10 @@ class QwtfDiscordBotPug # :nodoc:
140
183
  display_name = e.display_name_for(user_id) || arg
141
184
 
142
185
  unless pug.joined?(user_id)
143
- send_embedded_message("#{display_name} isn't in the PUG", e.channel)
186
+ send_embedded_message(
187
+ description: "#{display_name} isn't in the PUG",
188
+ channel: e.channel
189
+ )
144
190
  next
145
191
  end
146
192
 
@@ -151,11 +197,12 @@ class QwtfDiscordBotPug # :nodoc:
151
197
  "#{pug.player_slots} remain"
152
198
  ]
153
199
 
154
- snippets << "#{pug.slots_left} more #{pug.notify_roles}" if pug.slots_left == 1
200
+ message = "#{pug.slots_left} more #{pug.notify_roles}" if pug.slots_left == 1
155
201
 
156
202
  send_embedded_message(
157
- snippets.join(MSG_SNIPPET_DELIMITER),
158
- e.channel
203
+ message: message,
204
+ description: snippets.join(MSG_SNIPPET_DELIMITER),
205
+ channel: e.channel
159
206
  )
160
207
 
161
208
  break end_pug(pug, e.channel) if pug.empty?
@@ -165,29 +212,50 @@ class QwtfDiscordBotPug # :nodoc:
165
212
 
166
213
  bot.command :team do |event, *args|
167
214
  setup_pug(event) do |e, pug|
168
- return send_embedded_message("Which team? E.G. `!team 1`", e.channel) unless args.any?
169
- return send_embedded_message("Choose a team between 0 and 2", e.channel) unless ["0", "1", "2"].any?(args.first)
215
+ unless args.any?
216
+ return send_embedded_message(
217
+ description: "Which team? E.G. `!team 1`",
218
+ channel: e.channel
219
+ )
220
+ end
221
+
222
+ unless ["1", "2"].any?(args.first)
223
+ return send_embedded_message(
224
+ description: "Choose `!team 1`, `!team 2`, or `!unteam` to leave team",
225
+ channel: e.channel
226
+ )
227
+ end
170
228
 
171
229
  team_no = args.first.to_i
172
230
  pug_already_full = pug.full?
173
231
 
174
232
  if args.count == 1
175
233
  user_id = e.user_id
176
- return send_embedded_message("You're already in #{TEAM_NAMES[team_no]}", e.channel) if pug.team(team_no).include?(user_id)
234
+
235
+ if pug.team(team_no).include?(user_id)
236
+ return send_embedded_message(
237
+ description: "You're already in #{TEAM_NAMES[team_no]}",
238
+ channel: e.channel
239
+ )
240
+ end
177
241
 
178
242
  join_pug(e, pug) unless pug.joined?(user_id)
179
243
  pug.join_team(team_no: team_no, player_id: user_id)
180
244
 
181
245
  send_embedded_message(
182
- [
246
+ description: [
183
247
  "#{e.display_name} joins #{TEAM_NAMES[team_no]}",
184
248
  "#{pug.team_player_count(team_no)}/#{pug.teamsize}"
185
- ].join(MSG_SNIPPET_DELIMITER), e.channel
249
+ ].join(MSG_SNIPPET_DELIMITER),
250
+ channel: e.channel
186
251
  )
187
252
  else
188
253
  args[1..-1].each do |mention|
189
254
  unless mention.match(/<@!\d+>/)
190
- send_embedded_message("#{arg} isn't a valid mention", e.channel)
255
+ send_embedded_message(
256
+ description: "#{arg} isn't a valid mention",
257
+ channel: e.channel
258
+ )
191
259
  next
192
260
  end
193
261
 
@@ -195,17 +263,21 @@ class QwtfDiscordBotPug # :nodoc:
195
263
  display_name = e.display_name_for(user_id) || arg
196
264
 
197
265
  unless pug.joined?(user_id)
198
- send_embedded_message("#{display_name} isn't in the PUG", e.channel)
266
+ send_embedded_message(
267
+ description: "#{display_name} isn't in the PUG",
268
+ channel: e.channel
269
+ )
199
270
  next
200
271
  end
201
272
 
202
273
  pug.join_team(team_no: team_no, player_id: user_id)
203
274
 
204
275
  send_embedded_message(
205
- [
276
+ description: [
206
277
  "#{display_name} joins #{TEAM_NAMES[team_no]}",
207
278
  "#{pug.team_player_count(team_no)}/#{pug.teamsize}"
208
- ].join(MSG_SNIPPET_DELIMITER), e.channel
279
+ ].join(MSG_SNIPPET_DELIMITER),
280
+ channel: e.channel
209
281
  )
210
282
  end
211
283
  end
@@ -217,26 +289,66 @@ class QwtfDiscordBotPug # :nodoc:
217
289
  bot.command :unteam do |event, *args|
218
290
  setup_pug(event) do |e, pug|
219
291
  user_id = e.user_id
220
- return send_embedded_message('No PUG has been started. `!join` to create', e.channel) unless pug.active?
221
- return send_embedded_message("You aren't in this PUG", e.channel) unless pug.joined?(user_id)
222
- return send_embedded_message("You aren't in a team", e.channel) if pug.team(0).include?(user_id)
292
+
293
+ unless pug.active?
294
+ return send_embedded_message(
295
+ description: 'No PUG has been started. `!join` to create',
296
+ channel: e.channel
297
+ )
298
+ end
299
+
300
+ unless pug.joined?(user_id)
301
+ return send_embedded_message(
302
+ description: "You aren't in this PUG",
303
+ channel: e.channel
304
+ )
305
+ end
306
+
307
+ if pug.team(0).include?(user_id)
308
+ return send_embedded_message(
309
+ description: "You aren't in a team",
310
+ channel: e.channel
311
+ )
312
+ end
223
313
 
224
314
  pug.join_team(team_no: 0, player_id: user_id)
225
- send_embedded_message("#{e.display_name} has no team", e.channel)
315
+
316
+ send_embedded_message(
317
+ description: "#{e.display_name} leaves team",
318
+ channel: e.channel
319
+ )
226
320
  end
227
321
  end
228
322
 
229
323
  bot.command :win do |event, *args|
230
324
  setup_pug(event) do |e, pug|
231
- return send_embedded_message(no_active_pug_message, e.channel) unless pug.active?
232
- return send_embedded_message("Specify winning team; e.g. `!win 1`", e.channel) unless args.any?
233
- return send_embedded_message("Invalid team number", e.channel) unless ["1", "2"].any?(args.first)
325
+ unless pug.active?
326
+ return send_embedded_message(
327
+ description: no_active_pug_message,
328
+ channel: e.channel
329
+ )
330
+ end
331
+
332
+ unless args.any?
333
+ return send_embedded_message(
334
+ description: "Specify winning team; e.g. `!win 1`",
335
+ channel: e.channel
336
+ )
337
+ end
338
+
339
+ unless ["1", "2"].any?(args.first)
340
+ return send_embedded_message(
341
+ description: "Invalid team number",
342
+ channel: e.channel
343
+ )
344
+ end
234
345
 
235
346
  winning_team_no = args.first.to_i
236
347
 
237
348
  if pug.actual_teams.count < 2
238
349
  return send_embedded_message(
239
- "There must be at least two teams with players to submit a result", e.channel
350
+ description: "There must be at least two teams with players to submit a result",
351
+ channel: e.channel
240
352
  )
241
353
  end
242
354
 
@@ -262,17 +374,26 @@ class QwtfDiscordBotPug # :nodoc:
262
374
  }.to_json
263
375
  )
264
376
 
265
- send_embedded_message("#{TEAM_NAMES[winning_team_no]} wins. [Ratings](http://ratings.fortressone.org)", e.channel)
377
+ send_embedded_message(
378
+ description: "#{TEAM_NAMES[winning_team_no]} wins. [Ratings](http://ratings.fortressone.org)",
379
+ channel: e.channel
380
+ )
266
381
  end
267
382
  end
268
383
 
269
384
  bot.command :draw do |event, *args|
270
385
  setup_pug(event) do |e, pug|
271
- return send_embedded_message(no_active_pug_message, e.channel) unless pug.active?
386
+ unless pug.active?
387
+ return send_embedded_message(
388
+ description: no_active_pug_message,
389
+ channel: e.channel
390
+ )
391
+ end
272
392
 
273
393
  if pug.actual_teams.count < 2
274
394
  return send_embedded_message(
275
- "There must be at least two teams with players to submit a result", e.channel
395
+ description: "There must be at least two teams with players to submit a result",
396
+ channel: e.channel
276
397
  )
277
398
  end
278
399
 
@@ -297,13 +418,21 @@ class QwtfDiscordBotPug # :nodoc:
297
418
  }.to_json
298
419
  )
299
420
 
300
- send_embedded_message("Match drawn. [Ratings](http://ratings.fortressone.org)", e.channel)
421
+ send_embedded_message(
422
+ description: "Match drawn. [Ratings](http://ratings.fortressone.org)",
423
+ channel: e.channel
424
+ )
301
425
  end
302
426
  end
303
427
 
304
428
  bot.command :end do |event, *args|
305
429
  setup_pug(event) do |e, pug|
306
- return send_embedded_message(no_active_pug_message, e.channel) unless pug.active?
430
+ unless pug.active?
431
+ return send_embedded_message(
432
+ description: no_active_pug_message,
433
+ channel: e.channel
434
+ )
435
+ end
307
436
 
308
437
  end_pug(pug, e.channel)
309
438
  end
@@ -312,47 +441,105 @@ class QwtfDiscordBotPug # :nodoc:
312
441
  bot.command :addmap do |event, *args|
313
442
  setup_pug(event) do |e, pug|
314
443
  maps = args
315
- return send_embedded_message("What map? e.g. `!addmap 2fort5r`", e.channel) unless maps.any?
444
+
445
+ unless maps.any?
446
+ return send_embedded_message(
447
+ description: "What map? e.g. `!addmap 2fort5r`",
448
+ channel: e.channel
449
+ )
450
+ end
316
451
 
317
452
  pug.add_maps(maps)
318
- send_embedded_message("#{maps.join(', ')} added to maps", e.channel)
453
+
454
+ send_embedded_message(
455
+ description: "#{maps.join(', ')} added to maps",
456
+ channel: e.channel)
319
457
  end
320
458
  end
321
459
 
322
460
  bot.command :removemap do |event, *args|
323
461
  setup_pug(event) do |e, pug|
324
462
  maps = args
325
- return send_embedded_message("What map? e.g. `!removemap 2fort5r`", e.channel) unless maps.any?
463
+
464
+ unless maps.any?
465
+ return send_embedded_message(
466
+ description: "What map? e.g. `!removemap 2fort5r`",
467
+ channel: e.channel
468
+ )
469
+ end
326
470
 
327
471
  pug.remove_maps(maps)
328
- send_embedded_message("#{maps.join(', ')} removed from maps", e.channel)
472
+
473
+ send_embedded_message(
474
+ description: "#{maps.join(', ')} removed from maps",
475
+ channel: e.channel
476
+ )
329
477
  end
330
478
  end
331
479
 
332
480
  bot.command :maps do |event, *args|
333
481
  setup_pug(event) do |e, pug|
334
482
  maps = pug.maps
335
- return send_embedded_message('No maps have been added. `!addmap`', e.channel) unless maps.any?
483
+ unless maps.any?
484
+ return send_embedded_message(
485
+ description: 'No maps have been added. `!addmap`',
486
+ channel: e.channel
487
+ )
488
+ end
336
489
 
337
- send_embedded_message(maps.join(', '), e.channel)
490
+ send_embedded_message(
491
+ description: maps.join(', '),
492
+ channel: e.channel
493
+ )
338
494
  end
339
495
  end
340
496
 
341
497
  bot.command :map do |event, *args|
342
498
  setup_pug(event) do |e, pug|
343
499
  maps = pug.maps
344
- return send_embedded_message('No maps have been added. `!addmap`', e.channel) unless maps.any?
345
- return send_embedded_message(no_active_pug_message, e.channel) unless pug.active?
500
+
501
+ unless maps.any?
502
+ return send_embedded_message(
503
+ description: 'No maps have been added. `!addmap`',
504
+ channel: e.channel
505
+ )
506
+ end
507
+
508
+ unless pug.active?
509
+ return send_embedded_message(
510
+ description: no_active_pug_message,
511
+ channel: e.channel
512
+ )
513
+ end
346
514
 
347
515
  if args.empty?
348
- return send_embedded_message('No map has been set for the current PUG', e.channel) unless pug.game_map
349
- send_embedded_message("Current map is #{pug.game_map}", e.channel)
516
+ unless pug.game_map
517
+ return send_embedded_message(
518
+ description: 'No map has been set for the current PUG',
519
+ channel: e.channel
520
+ )
521
+ end
522
+
523
+ send_embedded_message(
524
+ description: "Current map is #{pug.game_map}",
525
+ channel: e.channel
526
+ )
350
527
  else
351
528
  game_map = args.first
352
- return send_embedded_message("#{game_map} isn't in the map list. `!addmap` to add it.", e.channel) unless maps.include?(game_map)
529
+
530
+ unless maps.include?(game_map)
531
+ return send_embedded_message(
532
+ description: "#{game_map} isn't in the map list. `!addmap` to add it.",
533
+ channel: e.channel
534
+ )
535
+ end
353
536
 
354
537
  pug.game_map = game_map
355
- send_embedded_message("Map set to #{game_map}", e.channel)
538
+
539
+ send_embedded_message(
540
+ description: "Map set to #{game_map}",
541
+ channel: e.channel
542
+ )
356
543
  end
357
544
  end
358
545
  end
@@ -368,7 +555,10 @@ class QwtfDiscordBotPug # :nodoc:
368
555
  "Notification role set to #{roles}"
369
556
  end
370
557
 
371
- send_embedded_message(msg, e.channel)
558
+ send_embedded_message(
559
+ description: msg,
560
+ channel: e.channel
561
+ )
372
562
  end
373
563
  end
374
564
 
@@ -378,6 +568,8 @@ class QwtfDiscordBotPug # :nodoc:
378
568
  private
379
569
 
380
570
  def team_name(team_no)
571
+ return "No team" if team_no == 0
572
+
381
573
  [team_no, TEAM_NAMES[team_no]].join(" ")
382
574
  end
383
575
 
@@ -389,13 +581,18 @@ class QwtfDiscordBotPug # :nodoc:
389
581
  pug.join(e.user_id)
390
582
 
391
583
  if pug.joined_player_count == 1
392
- snippets = ["#{e.display_name} creates a PUG", pug.player_slots, pug.notify_roles]
584
+ snippets = ["#{e.display_name} creates a PUG", "#{pug.player_slots} joined"]
585
+ message = pug.notify_roles
393
586
  else
394
- snippets = ["#{e.display_name} joins the PUG", pug.player_slots]
395
- snippets << "#{pug.slots_left} more #{pug.notify_roles}" if pug.slots_left.between?(1, 3)
587
+ snippets = ["#{e.display_name} joins the PUG", "#{pug.player_slots} joined"]
588
+ message = "#{pug.slots_left} more #{pug.notify_roles}" if pug.slots_left.between?(1, 3)
396
589
  end
397
590
 
398
- send_embedded_message(snippets.join(MSG_SNIPPET_DELIMITER), e.channel)
591
+ send_embedded_message(
592
+ message: message,
593
+ description: snippets.join(MSG_SNIPPET_DELIMITER),
594
+ channel: e.channel
595
+ )
399
596
  end
400
597
 
401
598
  def setup_pug(event)
@@ -405,31 +602,93 @@ class QwtfDiscordBotPug # :nodoc:
405
602
  nil # stop discordrb printing return value
406
603
  end
407
604
 
408
- def start_pug(pug, event)
409
- if !pug.actual_teams.any?
410
- send_embedded_message("Choosing fair teams...", event.channel)
411
- teams = get_fair_teams(pug.joined_players)
605
+ def choose_fair_teams(pug:, event:, iteration: 0)
606
+ if !pug.full?
607
+ return send_embedded_message(
608
+ description: "Can't choose teams until PUG is full",
609
+ channel: event.channel
610
+ ) && nil
611
+ end
612
+
613
+ send_embedded_message(
614
+ description: "Choosing fair teams...",
615
+ channel: event.channel
616
+ )
617
+
618
+ combinations = get_fair_teams(pug.joined_players)
619
+ teams = combinations[iteration]
412
620
 
413
- teams.each do |team_no, player_ids|
414
- player_ids.each do |player_id|
415
- pug.join_team(team_no: team_no, player_id: player_id)
621
+ if !teams
622
+ return send_embedded_message(
623
+ description: "There are only #{combinations.count} possible combinations",
624
+ channel: event.channel
625
+ ) && nil
626
+ end
627
+
628
+ teams.each do |team_no, player_ids|
629
+ player_ids.each do |player_id|
630
+ pug.join_team(team_no: team_no, player_id: player_id)
631
+ end
632
+ end
633
+ end
634
+
635
+ def status(pug:, event:)
636
+ footer = [
637
+ pug.game_map,
638
+ "#{pug.player_slots} joined"
639
+ ].compact.join(MSG_SNIPPET_DELIMITER)
640
+
641
+ send_embedded_message(
642
+ description: nil,
643
+ channel: event.channel
644
+ ) do |embed|
645
+ embed.footer = Discordrb::Webhooks::EmbedFooter.new(
646
+ text: footer
647
+ )
648
+
649
+ pug.teams.each do |team_no, player_ids|
650
+ team_display_names = player_ids.map do |player_id|
651
+ event.display_name_for(player_id)
416
652
  end
653
+
654
+ embed.add_field(
655
+ Discordrb::Webhooks::EmbedField.new(
656
+ {
657
+ inline: true,
658
+ name: team_name(team_no),
659
+ value: team_display_names.join("\n")
660
+ }
661
+ )
662
+ )
417
663
  end
418
664
  end
665
+ end
666
+
667
+ def start_pug(pug, event)
668
+ choose_fair_teams(pug: pug, event: event) unless pug.actual_teams.any?
419
669
 
420
670
  footer = [
421
671
  pug.game_map,
422
672
  "#{pug.player_slots} joined",
423
673
  ].compact.join(MSG_SNIPPET_DELIMITER)
424
674
 
425
- send_embedded_message("Time to play!", event.channel) do |embed|
675
+ mentions = pug.joined_players.map do |player_id|
676
+ event.mention_for(player_id)
677
+ end
678
+
679
+ mention_line = "Time to play! #{mentions.join(" ")}"
680
+
681
+ send_embedded_message(
682
+ message: mention_line,
683
+ channel: event.channel
684
+ ) do |embed|
426
685
  embed.footer = Discordrb::Webhooks::EmbedFooter.new(
427
686
  text: footer
428
687
  )
429
688
 
430
689
  pug.teams.each do |team_no, player_ids|
431
690
  team_mentions = player_ids.map do |player_id|
432
- event.mention_for(player_id)
691
+ event.display_name_for(player_id)
433
692
  end
434
693
 
435
694
  embed.add_field(
@@ -447,24 +706,29 @@ class QwtfDiscordBotPug # :nodoc:
447
706
 
448
707
  def end_pug(pug, channel_id)
449
708
  pug.end_pug
450
- send_embedded_message('PUG ended', channel_id)
709
+
710
+ send_embedded_message(
711
+ description: 'PUG ended',
712
+ channel: channel_id
713
+ )
451
714
  end
452
715
 
453
716
  def no_active_pug_message
454
717
  "There's no active PUG"
455
718
  end
456
719
 
457
- def send_embedded_message(message, channel)
720
+ def send_embedded_message(message: nil, description: nil, channel:)
458
721
  embed = Discordrb::Webhooks::Embed.new
459
- embed.description = message
722
+ embed.description = description
460
723
  yield(embed) if block_given?
461
- channel.send_embed(nil, embed) && puts(message)
724
+ channel.send_embed(message, embed) && puts(message)
462
725
  end
463
726
 
464
727
  def post_results(json)
465
728
  uri = URI("#{ENV['RATINGS_API_URL']}matches/")
466
729
  req = Net::HTTP::Post.new(uri, 'Content-Type' => 'application/json')
467
730
  req.body = json
731
+
468
732
  Net::HTTP.start(uri.hostname, uri.port) do |http|
469
733
  http.request(req)
470
734
  end
@@ -475,9 +739,11 @@ class QwtfDiscordBotPug # :nodoc:
475
739
  params = { 'players[]' => players }
476
740
  uri.query = URI.encode_www_form(params)
477
741
  req = Net::HTTP::Get.new(uri)
742
+
478
743
  res = Net::HTTP.start(uri.hostname, uri.port) do |http|
479
744
  http.request(req)
480
745
  end
481
- JSON.parse(res.body).first.to_h
746
+
747
+ JSON.parse(res.body).map(&:to_h)
482
748
  end
483
749
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: qwtf_discord_bot
3
3
  version: !ruby/object:Gem::Version
4
- version: 5.4.4
4
+ version: 5.4.9
5
5
  platform: ruby
6
6
  authors:
7
7
  - Sheldon Johnson
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2020-10-26 00:00:00.000000000 Z
11
+ date: 2020-10-28 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: discordrb