qwtf_discord_bot 5.1.11 → 5.2.5

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 9f929cda85f6a53ffc63d04e3a85b6ca4bcbcadf72b2c0ab70eab8a1a549843a
4
- data.tar.gz: 45b0e4b9bba67fdfb8139d3904176ce3443d43ea7cb74c750588f4eb6ea1fb8d
3
+ metadata.gz: 27e4374d157a5f3cad0f5c1cc58ffb1e9fc0b9f6ca9b89391509635260bc460a
4
+ data.tar.gz: 2775f0952e46afb2af7dbff0a72b388e34f343ed896b3d909ca0b9d069f76ab4
5
5
  SHA512:
6
- metadata.gz: cca2b1392fbbdca102e22ea27e79774da07478e5cfa2a9a2ee14c4b6ae48ae1ea3c8e4c8906461d123a1f32f3e2e0cef519f107a3b7abf42d1853a5e870bf342
7
- data.tar.gz: d29bd7a09c9ffed879429965497118d0f63332bac8084c83ea498601d924d11227adad67694de73f97658b73ed9c7c90236fc855bb8c82a809a522fe0ecb78f6
6
+ metadata.gz: 6cf2acfe24299674b69051e0d28ce628cea334840910b5bbd083d73e10d91cdf2d7c96312371ffed448424b769abb86c77a63de05b4a23136d05e92398bf2ff1
7
+ data.tar.gz: 1a5830ef4f29e3c331d5534a84837e8d3e29bccf016e478e439aa8727836fe957d20f4865265264aa69116110f9e7ba7cf322869f6738aff5ef365f4d860137d
data/Gemfile CHANGED
@@ -12,6 +12,6 @@ group :development do
12
12
  end
13
13
 
14
14
  group :test do
15
- gem 'rspec'
16
15
  gem 'factory_bot'
16
+ gem 'rspec'
17
17
  end
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- qwtf_discord_bot (5.0.4)
4
+ qwtf_discord_bot (5.1.12)
5
5
  discordrb (~> 3.3)
6
6
  redis (~> 4.1)
7
7
  thor (~> 0.20)
data/README.md CHANGED
@@ -91,6 +91,9 @@ This responds to discord messages:
91
91
  - `!status`
92
92
  - `!kick <@player>`
93
93
  - `!teamsize <no_of_players>`
94
+ - `!team <team_no>`
95
+ - `!unteam`
96
+ - `!win <team_no>`
94
97
  - `!notify <@role @role2>`
95
98
  - `!end`
96
99
 
data/VERSION CHANGED
@@ -1 +1 @@
1
- 5.1.11
1
+ 5.2.5
data/bin/bundle CHANGED
@@ -8,46 +8,46 @@
8
8
  # this file is here to facilitate running it.
9
9
  #
10
10
 
11
- require "rubygems"
11
+ require 'rubygems'
12
12
 
13
13
  m = Module.new do
14
- module_function
14
+ module_function
15
15
 
16
16
  def invoked_as_script?
17
17
  File.expand_path($0) == File.expand_path(__FILE__)
18
18
  end
19
19
 
20
20
  def env_var_version
21
- ENV["BUNDLER_VERSION"]
21
+ ENV['BUNDLER_VERSION']
22
22
  end
23
23
 
24
24
  def cli_arg_version
25
25
  return unless invoked_as_script? # don't want to hijack other binstubs
26
- return unless "update".start_with?(ARGV.first || " ") # must be running `bundle update`
26
+ return unless 'update'.start_with?(ARGV.first || ' ') # must be running `bundle update`
27
+
27
28
  bundler_version = nil
28
29
  update_index = nil
29
30
  ARGV.each_with_index do |a, i|
30
- if update_index && update_index.succ == i && a =~ Gem::Version::ANCHORED_VERSION_PATTERN
31
- bundler_version = a
32
- end
31
+ bundler_version = a if update_index && update_index.succ == i && a =~ Gem::Version::ANCHORED_VERSION_PATTERN
33
32
  next unless a =~ /\A--bundler(?:[= ](#{Gem::Version::VERSION_PATTERN}))?\z/
34
- bundler_version = $1 || ">= 0.a"
33
+
34
+ bundler_version = Regexp.last_match(1) || '>= 0.a'
35
35
  update_index = i
36
36
  end
37
37
  bundler_version
38
38
  end
39
39
 
40
40
  def gemfile
41
- gemfile = ENV["BUNDLE_GEMFILE"]
41
+ gemfile = ENV['BUNDLE_GEMFILE']
42
42
  return gemfile if gemfile && !gemfile.empty?
43
43
 
44
- File.expand_path("../../Gemfile", __FILE__)
44
+ File.expand_path('../Gemfile', __dir__)
45
45
  end
46
46
 
47
47
  def lockfile
48
48
  lockfile =
49
49
  case File.basename(gemfile)
50
- when "gems.rb" then gemfile.sub(/\.rb$/, gemfile)
50
+ when 'gems.rb' then gemfile.sub(/\.rb$/, gemfile)
51
51
  else "#{gemfile}.lock"
52
52
  end
53
53
  File.expand_path(lockfile)
@@ -55,8 +55,10 @@ m = Module.new do
55
55
 
56
56
  def lockfile_version
57
57
  return unless File.file?(lockfile)
58
+
58
59
  lockfile_contents = File.read(lockfile)
59
60
  return unless lockfile_contents =~ /\n\nBUNDLED WITH\n\s{2,}(#{Gem::Version::VERSION_PATTERN})\n/
61
+
60
62
  Regexp.last_match(1)
61
63
  end
62
64
 
@@ -68,24 +70,28 @@ m = Module.new do
68
70
  end
69
71
 
70
72
  def load_bundler!
71
- ENV["BUNDLE_GEMFILE"] ||= gemfile
73
+ ENV['BUNDLE_GEMFILE'] ||= gemfile
72
74
 
73
75
  # must dup string for RG < 1.8 compatibility
74
76
  activate_bundler(bundler_version.dup)
75
77
  end
76
78
 
77
79
  def activate_bundler(bundler_version)
78
- if Gem::Version.correct?(bundler_version) && Gem::Version.new(bundler_version).release < Gem::Version.new("2.0")
79
- bundler_version = "< 2"
80
+ if Gem::Version.correct?(bundler_version) && Gem::Version.new(bundler_version).release < Gem::Version.new('2.0')
81
+ bundler_version = '< 2'
80
82
  end
81
83
  gem_error = activation_error_handling do
82
- gem "bundler", bundler_version
84
+ gem 'bundler', bundler_version
83
85
  end
84
86
  return if gem_error.nil?
87
+
85
88
  require_error = activation_error_handling do
86
- require "bundler/version"
89
+ require 'bundler/version'
90
+ end
91
+ if require_error.nil? && Gem::Requirement.new(bundler_version).satisfied_by?(Gem::Version.new(Bundler::VERSION))
92
+ return
87
93
  end
88
- return if require_error.nil? && Gem::Requirement.new(bundler_version).satisfied_by?(Gem::Version.new(Bundler::VERSION))
94
+
89
95
  warn "Activating bundler (#{bundler_version}) failed:\n#{gem_error.message}\n\nTo install the version of bundler this project requires, run `gem install bundler -v '#{bundler_version}'`"
90
96
  exit 42
91
97
  end
@@ -100,6 +106,4 @@ end
100
106
 
101
107
  m.load_bundler!
102
108
 
103
- if m.invoked_as_script?
104
- load Gem.bin_path("bundler", "bundle")
105
- end
109
+ load Gem.bin_path('bundler', 'bundle') if m.invoked_as_script?
@@ -8,11 +8,11 @@
8
8
  # this file is here to facilitate running it.
9
9
  #
10
10
 
11
- require "pathname"
12
- ENV["BUNDLE_GEMFILE"] ||= File.expand_path("../../Gemfile",
13
- Pathname.new(__FILE__).realpath)
11
+ require 'pathname'
12
+ ENV['BUNDLE_GEMFILE'] ||= File.expand_path('../../Gemfile',
13
+ Pathname.new(__FILE__).realpath)
14
14
 
15
- bundle_binstub = File.expand_path("../bundle", __FILE__)
15
+ bundle_binstub = File.expand_path('bundle', __dir__)
16
16
 
17
17
  if File.file?(bundle_binstub)
18
18
  if File.read(bundle_binstub, 300) =~ /This file was generated by Bundler/
@@ -23,7 +23,7 @@ Replace `bin/bundle` by running `bundle binstubs bundler --force`, then run this
23
23
  end
24
24
  end
25
25
 
26
- require "rubygems"
27
- require "bundler/setup"
26
+ require 'rubygems'
27
+ require 'bundler/setup'
28
28
 
29
- load Gem.bin_path("coderay", "coderay")
29
+ load Gem.bin_path('coderay', 'coderay')
File without changes
@@ -8,11 +8,11 @@
8
8
  # this file is here to facilitate running it.
9
9
  #
10
10
 
11
- require "pathname"
12
- ENV["BUNDLE_GEMFILE"] ||= File.expand_path("../../Gemfile",
13
- Pathname.new(__FILE__).realpath)
11
+ require 'pathname'
12
+ ENV['BUNDLE_GEMFILE'] ||= File.expand_path('../../Gemfile',
13
+ Pathname.new(__FILE__).realpath)
14
14
 
15
- bundle_binstub = File.expand_path("../bundle", __FILE__)
15
+ bundle_binstub = File.expand_path('bundle', __dir__)
16
16
 
17
17
  if File.file?(bundle_binstub)
18
18
  if File.read(bundle_binstub, 300) =~ /This file was generated by Bundler/
@@ -23,7 +23,7 @@ Replace `bin/bundle` by running `bundle binstubs bundler --force`, then run this
23
23
  end
24
24
  end
25
25
 
26
- require "rubygems"
27
- require "bundler/setup"
26
+ require 'rubygems'
27
+ require 'bundler/setup'
28
28
 
29
- load Gem.bin_path("diff-lcs", "htmldiff")
29
+ load Gem.bin_path('diff-lcs', 'htmldiff')
data/bin/ldiff CHANGED
@@ -8,11 +8,11 @@
8
8
  # this file is here to facilitate running it.
9
9
  #
10
10
 
11
- require "pathname"
12
- ENV["BUNDLE_GEMFILE"] ||= File.expand_path("../../Gemfile",
13
- Pathname.new(__FILE__).realpath)
11
+ require 'pathname'
12
+ ENV['BUNDLE_GEMFILE'] ||= File.expand_path('../../Gemfile',
13
+ Pathname.new(__FILE__).realpath)
14
14
 
15
- bundle_binstub = File.expand_path("../bundle", __FILE__)
15
+ bundle_binstub = File.expand_path('bundle', __dir__)
16
16
 
17
17
  if File.file?(bundle_binstub)
18
18
  if File.read(bundle_binstub, 300) =~ /This file was generated by Bundler/
@@ -23,7 +23,7 @@ Replace `bin/bundle` by running `bundle binstubs bundler --force`, then run this
23
23
  end
24
24
  end
25
25
 
26
- require "rubygems"
27
- require "bundler/setup"
26
+ require 'rubygems'
27
+ require 'bundler/setup'
28
28
 
29
- load Gem.bin_path("diff-lcs", "ldiff")
29
+ load Gem.bin_path('diff-lcs', 'ldiff')
data/bin/pry CHANGED
@@ -8,11 +8,11 @@
8
8
  # this file is here to facilitate running it.
9
9
  #
10
10
 
11
- require "pathname"
12
- ENV["BUNDLE_GEMFILE"] ||= File.expand_path("../../Gemfile",
13
- Pathname.new(__FILE__).realpath)
11
+ require 'pathname'
12
+ ENV['BUNDLE_GEMFILE'] ||= File.expand_path('../../Gemfile',
13
+ Pathname.new(__FILE__).realpath)
14
14
 
15
- bundle_binstub = File.expand_path("../bundle", __FILE__)
15
+ bundle_binstub = File.expand_path('bundle', __dir__)
16
16
 
17
17
  if File.file?(bundle_binstub)
18
18
  if File.read(bundle_binstub, 300) =~ /This file was generated by Bundler/
@@ -23,7 +23,7 @@ Replace `bin/bundle` by running `bundle binstubs bundler --force`, then run this
23
23
  end
24
24
  end
25
25
 
26
- require "rubygems"
27
- require "bundler/setup"
26
+ require 'rubygems'
27
+ require 'bundler/setup'
28
28
 
29
- load Gem.bin_path("pry", "pry")
29
+ load Gem.bin_path('pry', 'pry')
@@ -8,11 +8,11 @@
8
8
  # this file is here to facilitate running it.
9
9
  #
10
10
 
11
- require "pathname"
12
- ENV["BUNDLE_GEMFILE"] ||= File.expand_path("../../Gemfile",
13
- Pathname.new(__FILE__).realpath)
11
+ require 'pathname'
12
+ ENV['BUNDLE_GEMFILE'] ||= File.expand_path('../../Gemfile',
13
+ Pathname.new(__FILE__).realpath)
14
14
 
15
- bundle_binstub = File.expand_path("../bundle", __FILE__)
15
+ bundle_binstub = File.expand_path('bundle', __dir__)
16
16
 
17
17
  if File.file?(bundle_binstub)
18
18
  if File.read(bundle_binstub, 300) =~ /This file was generated by Bundler/
@@ -23,7 +23,7 @@ Replace `bin/bundle` by running `bundle binstubs bundler --force`, then run this
23
23
  end
24
24
  end
25
25
 
26
- require "rubygems"
27
- require "bundler/setup"
26
+ require 'rubygems'
27
+ require 'bundler/setup'
28
28
 
29
- load Gem.bin_path("qwtf_discord_bot", "qwtf_discord_bot")
29
+ load Gem.bin_path('qwtf_discord_bot', 'qwtf_discord_bot')
@@ -8,11 +8,11 @@
8
8
  # this file is here to facilitate running it.
9
9
  #
10
10
 
11
- require "pathname"
12
- ENV["BUNDLE_GEMFILE"] ||= File.expand_path("../../Gemfile",
13
- Pathname.new(__FILE__).realpath)
11
+ require 'pathname'
12
+ ENV['BUNDLE_GEMFILE'] ||= File.expand_path('../../Gemfile',
13
+ Pathname.new(__FILE__).realpath)
14
14
 
15
- bundle_binstub = File.expand_path("../bundle", __FILE__)
15
+ bundle_binstub = File.expand_path('bundle', __dir__)
16
16
 
17
17
  if File.file?(bundle_binstub)
18
18
  if File.read(bundle_binstub, 300) =~ /This file was generated by Bundler/
@@ -23,7 +23,7 @@ Replace `bin/bundle` by running `bundle binstubs bundler --force`, then run this
23
23
  end
24
24
  end
25
25
 
26
- require "rubygems"
27
- require "bundler/setup"
26
+ require 'rubygems'
27
+ require 'bundler/setup'
28
28
 
29
- load Gem.bin_path("rest-client", "restclient")
29
+ load Gem.bin_path('rest-client', 'restclient')
data/bin/rspec CHANGED
@@ -8,11 +8,11 @@
8
8
  # this file is here to facilitate running it.
9
9
  #
10
10
 
11
- require "pathname"
12
- ENV["BUNDLE_GEMFILE"] ||= File.expand_path("../../Gemfile",
13
- Pathname.new(__FILE__).realpath)
11
+ require 'pathname'
12
+ ENV['BUNDLE_GEMFILE'] ||= File.expand_path('../../Gemfile',
13
+ Pathname.new(__FILE__).realpath)
14
14
 
15
- bundle_binstub = File.expand_path("../bundle", __FILE__)
15
+ bundle_binstub = File.expand_path('bundle', __dir__)
16
16
 
17
17
  if File.file?(bundle_binstub)
18
18
  if File.read(bundle_binstub, 300) =~ /This file was generated by Bundler/
@@ -23,7 +23,7 @@ Replace `bin/bundle` by running `bundle binstubs bundler --force`, then run this
23
23
  end
24
24
  end
25
25
 
26
- require "rubygems"
27
- require "bundler/setup"
26
+ require 'rubygems'
27
+ require 'bundler/setup'
28
28
 
29
- load Gem.bin_path("rspec-core", "rspec")
29
+ load Gem.bin_path('rspec-core', 'rspec')
data/bin/thor CHANGED
@@ -8,11 +8,11 @@
8
8
  # this file is here to facilitate running it.
9
9
  #
10
10
 
11
- require "pathname"
12
- ENV["BUNDLE_GEMFILE"] ||= File.expand_path("../../Gemfile",
13
- Pathname.new(__FILE__).realpath)
11
+ require 'pathname'
12
+ ENV['BUNDLE_GEMFILE'] ||= File.expand_path('../../Gemfile',
13
+ Pathname.new(__FILE__).realpath)
14
14
 
15
- bundle_binstub = File.expand_path("../bundle", __FILE__)
15
+ bundle_binstub = File.expand_path('bundle', __dir__)
16
16
 
17
17
  if File.file?(bundle_binstub)
18
18
  if File.read(bundle_binstub, 300) =~ /This file was generated by Bundler/
@@ -23,7 +23,7 @@ Replace `bin/bundle` by running `bundle binstubs bundler --force`, then run this
23
23
  end
24
24
  end
25
25
 
26
- require "rubygems"
27
- require "bundler/setup"
26
+ require 'rubygems'
27
+ require 'bundler/setup'
28
28
 
29
- load Gem.bin_path("thor", "thor")
29
+ load Gem.bin_path('thor', 'thor')
@@ -12,7 +12,7 @@ class Endpoint
12
12
 
13
13
  @config.values.each do |settings|
14
14
  settings.each do |setting|
15
- setting["channel_ids"].each do |channel_id|
15
+ setting['channel_ids'].each do |channel_id|
16
16
  channel_ids << channel_id
17
17
  end
18
18
  end
@@ -23,18 +23,20 @@ class EventDecorator
23
23
  server.users
24
24
  end
25
25
 
26
- def mentions_for(user_ids)
27
- find_users(user_ids).map do |user|
28
- user&.mention
29
- end
26
+ def mention_for(user_id)
27
+ find_user(user_id)&.mention
30
28
  end
31
29
 
32
- def display_names_for(user_ids)
33
- find_users(user_ids).map(&:display_name)
30
+ def mentions_for(user_ids)
31
+ find_users(user_ids).map(&:mention)
34
32
  end
35
33
 
36
34
  def display_name_for(user_id)
37
- find_user(user_id) && find_user(user_id).display_name
35
+ find_user(user_id)&.display_name
36
+ end
37
+
38
+ def display_names_for(user_ids)
39
+ find_users(user_ids).map(&:display_name)
38
40
  end
39
41
 
40
42
  private
@@ -57,4 +59,3 @@ class EventDecorator
57
59
  @event.user
58
60
  end
59
61
  end
60
-
@@ -28,7 +28,8 @@ class Player
28
28
  end
29
29
 
30
30
  def team
31
- return 'spec' if !valid_score? || @data['team'].empty? || @data['team'] == "observe"
31
+ return 'spec' if !valid_score? || @data['team'].empty? || @data['team'] == 'observe'
32
+
32
33
  @data['team']
33
34
  end
34
35
 
data/lib/pug.rb CHANGED
@@ -1,6 +1,6 @@
1
1
  class Pug
2
- DEFAULT_teamsize = 4
3
- NO_OF_TEAMS = 2
2
+ DEFAULT_TEAMSIZE = 4
3
+ MIN_NO_OF_TEAMS = 2
4
4
 
5
5
  def self.for(channel_id)
6
6
  new(channel_id)
@@ -10,18 +10,25 @@ class Pug
10
10
  @channel_id = channel_id
11
11
  end
12
12
 
13
- def join(user_id)
13
+ def join(player_id)
14
14
  redis.setnx(pug_key, Time.now)
15
- redis.sadd(players_key, user_id)
15
+
16
+ redis.sadd(team_key(0), player_id)
17
+ end
18
+
19
+ def join_team(team_no:, player_id:)
20
+ leave_teams(player_id)
21
+ redis.sadd(team_key(team_no), player_id)
16
22
  end
17
23
 
18
24
  def joined_players
19
- redis.smembers(players_key).map(&:to_i)
25
+ teams_keys.inject([]) do |players, team|
26
+ players + redis.smembers(team).map(&:to_i)
27
+ end
20
28
  end
21
29
 
22
- def team(no)
23
- index = no - 1
24
- joined_players.each_slice(teamsize).to_a[index]
30
+ def team(number)
31
+ redis.smembers(team_key(number)).map(&:to_i)
25
32
  end
26
33
 
27
34
  def teamsize=(teamsize)
@@ -33,7 +40,11 @@ class Pug
33
40
  end
34
41
 
35
42
  def joined_player_count
36
- redis.scard(players_key).to_i
43
+ joined_players.count
44
+ end
45
+
46
+ def team_player_count(team_no)
47
+ redis.scard(team_key(team_no)).to_i
37
48
  end
38
49
 
39
50
  def player_slots
@@ -49,15 +60,11 @@ class Pug
49
60
  end
50
61
 
51
62
  def notify_roles
52
- redis.get(notify_roles_key) || "@here"
53
- end
54
-
55
- def teamsize=(teamsize)
56
- redis.set(teamsize_key, teamsize)
63
+ redis.get(notify_roles_key) || '@here'
57
64
  end
58
65
 
59
66
  def teamsize
60
- (redis.get(teamsize_key) || DEFAULT_teamsize).to_i
67
+ (redis.get(teamsize_key) || DEFAULT_TEAMSIZE).to_i
61
68
  end
62
69
 
63
70
  def active?
@@ -65,13 +72,14 @@ class Pug
65
72
  end
66
73
 
67
74
  def leave(player_id)
68
- redis.srem(players_key, player_id)
75
+ leave_teams(player_id)
69
76
  end_pug if empty?
70
77
  end
71
78
 
72
79
  def end_pug
73
- redis.del(pug_key)
74
- redis.del(players_key)
80
+ redis.keys([pug_key, "*"].join).each do |key|
81
+ redis.del(key)
82
+ end
75
83
  end
76
84
 
77
85
  def joined?(player_id)
@@ -79,36 +87,64 @@ class Pug
79
87
  end
80
88
 
81
89
  def maxplayers
82
- teamsize * NO_OF_TEAMS
90
+ teamsize * no_of_teams
91
+ end
92
+
93
+ def won_by(team_no)
94
+ { teams: teams, winner: team_no }
95
+ end
96
+
97
+ def teams
98
+ teams_keys.inject({}) do |teams, team|
99
+ teams.merge({ team.split(':').last => redis.smembers(team).map(&:to_i) })
100
+ end
101
+ end
102
+
103
+ def actual_teams
104
+ teams.tap { |team| team.delete("0") }
83
105
  end
84
106
 
85
107
  private
86
108
 
109
+ def leave_teams(player_id)
110
+ teams_keys.each do |team|
111
+ redis.srem(team, player_id)
112
+ end
113
+ end
114
+
115
+ def teams_keys
116
+ redis.keys([pug_key, 'teams:*'].join(':'))
117
+ end
118
+
87
119
  def empty?
88
120
  joined_player_count.zero?
89
121
  end
90
122
 
91
- def teamsize_key
92
- [pug_key, "teamsize"].join(":")
123
+ def team_key(team_no)
124
+ [pug_key, 'teams', team_no].join(':')
93
125
  end
94
126
 
95
- def players_key
96
- [pug_key, "players"].join(":")
127
+ def pug_key
128
+ [channel_key, 'pug'].join(':')
97
129
  end
98
130
 
99
- def pug_key
100
- ["pug", "channel", @channel_id].join(":")
131
+ def channel_key
132
+ ['channel', @channel_id].join(':')
101
133
  end
102
134
 
103
135
  def notify_roles_key
104
- [pug_key, "role"].join(":")
136
+ [channel_key, 'role'].join(':')
105
137
  end
106
138
 
107
139
  def teamsize_key
108
- [pug_key, "teamsize"].join(":")
140
+ [pug_key, 'teamsize'].join(':')
109
141
  end
110
142
 
111
143
  def redis
112
144
  Redis.current
113
145
  end
146
+
147
+ def no_of_teams
148
+ [actual_teams.count, MIN_NO_OF_TEAMS].max
149
+ end
114
150
  end
@@ -23,12 +23,14 @@ class QstatRequest
23
23
 
24
24
  def to_message
25
25
  return server_summary if is_empty?
26
+
26
27
  [server_summary, player_table].join("\n")
27
28
  end
28
29
 
29
30
  def server_summary
30
31
  return "#{@endpoint} isn't responding" unless game_map
31
32
  return "#{name} | #{@endpoint} | #{game_map} | #{numplayers}/#{maxplayers}" unless has_spectators?
33
+
32
34
  "#{name} | #{@endpoint} | #{game_map} | #{numplayers}/#{maxplayers} players | #{numspectators}/#{maxspectators} spectators"
33
35
  end
34
36
 
@@ -46,70 +48,70 @@ class QstatRequest
46
48
 
47
49
  private
48
50
 
49
- def has_spectators?
50
- numspectators && numspectators > 0
51
- end
52
-
53
- def teams
54
- @teams ||= build_roster
55
- end
51
+ def has_spectators?
52
+ numspectators && numspectators > 0
53
+ end
56
54
 
57
- def data
58
- @data ||= JSON.parse(result).first
59
- end
55
+ def teams
56
+ @teams ||= build_roster
57
+ end
60
58
 
61
- def execute
62
- %x[qstat -json -P -qws #{@endpoint}]
63
- end
59
+ def data
60
+ @data ||= JSON.parse(result).first
61
+ end
64
62
 
65
- def player_table
66
- players.sort_by { |player| player.team.number }.map(&:to_row).join("\n")
67
- end
63
+ def execute
64
+ `qstat -json -P -qws #{@endpoint}`
65
+ end
68
66
 
69
- def name
70
- data["name"]
71
- end
67
+ def player_table
68
+ players.sort_by { |player| player.team.number }.map(&:to_row).join("\n")
69
+ end
72
70
 
73
- def address
74
- data["address"]
75
- end
71
+ def name
72
+ data['name']
73
+ end
76
74
 
77
- def game_map
78
- data["map"]
79
- end
75
+ def address
76
+ data['address']
77
+ end
80
78
 
81
- def numplayers
82
- data["numplayers"]
83
- end
79
+ def game_map
80
+ data['map']
81
+ end
84
82
 
85
- def maxplayers
86
- data["maxplayers"]
87
- end
83
+ def numplayers
84
+ data['numplayers']
85
+ end
88
86
 
89
- def numspectators
90
- data["numspectators"]
91
- end
87
+ def maxplayers
88
+ data['maxplayers']
89
+ end
92
90
 
93
- def maxspectators
94
- data["maxspectators"]
95
- end
91
+ def numspectators
92
+ data['numspectators']
93
+ end
96
94
 
97
- def build_roster
98
- return nil if is_empty?
95
+ def maxspectators
96
+ data['maxspectators']
97
+ end
99
98
 
100
- roster = Roster.new
99
+ def build_roster
100
+ return nil if is_empty?
101
101
 
102
- data["players"].map do |player_data|
103
- player = Player.new(player_data)
104
- roster.enroll(player)
105
- end
102
+ roster = Roster.new
106
103
 
107
- roster.teams.sort_by { |team| team.number }
104
+ data['players'].map do |player_data|
105
+ player = Player.new(player_data)
106
+ roster.enroll(player)
108
107
  end
109
108
 
110
- def players
111
- data["players"].map do |player_data|
112
- Player.new(player_data)
113
- end
109
+ roster.teams.sort_by { |team| team.number }
110
+ end
111
+
112
+ def players
113
+ data['players'].map do |player_data|
114
+ Player.new(player_data)
114
115
  end
116
+ end
115
117
  end
@@ -21,6 +21,7 @@ module QwtfDiscordBot # :nodoc:
21
21
  def self.config_file
22
22
  return ENV['QWTF_DISCORD_BOT_CONFIG_FILE'] if ENV['QWTF_DISCORD_BOT_CONFIG_FILE']
23
23
  return "#{Dir.pwd}/config.yaml" if FileTest.exist?("#{Dir.pwd}/config.yaml")
24
+
24
25
  "#{Dir.home}/.config/qwtf_discord_bot/config.yaml"
25
26
  end
26
27
 
@@ -6,20 +6,20 @@ class Config
6
6
  end
7
7
 
8
8
  def token
9
- @token ||= @config["token"]
9
+ @token ||= @config['token']
10
10
  end
11
11
 
12
12
  def client_id
13
- @client_id ||= @config["client_id"]
13
+ @client_id ||= @config['client_id']
14
14
  end
15
15
 
16
16
  def endpoints
17
- @endpoints ||= @config["endpoints"].map do |endpoint|
17
+ @endpoints ||= @config['endpoints'].map do |endpoint|
18
18
  Endpoint.new(endpoint)
19
19
  end
20
20
  end
21
21
 
22
22
  def emojis
23
- @emojis ||= @config["emojis"]
23
+ @emojis ||= @config['emojis']
24
24
  end
25
25
  end
@@ -6,6 +6,8 @@ require 'event_decorator'
6
6
  class QwtfDiscordBotPug # :nodoc:
7
7
  include QwtfDiscordBot
8
8
 
9
+ MSG_SNIPPET_DELIMITER = ' · '
10
+
9
11
  def run
10
12
  bot = Discordrb::Commands::CommandBot.new(
11
13
  token: QwtfDiscordBot.config.token,
@@ -18,52 +20,48 @@ class QwtfDiscordBotPug # :nodoc:
18
20
  setup_pug(event) do |e, pug|
19
21
  return send_msg("You've already joined", e.channel) if pug.joined?(e.user_id)
20
22
 
21
- pug.join(e.user_id)
22
-
23
- if pug.joined_player_count == 1
24
- snippets = ["#{e.display_name} creates a PUG", pug.player_slots, pug.notify_roles]
25
- else
26
- snippets = ["#{e.display_name} joins the PUG", pug.player_slots]
27
- snippets << "#{pug.slots_left} more #{pug.notify_roles}" if pug.slots_left.between?(1, 3)
28
- end
29
-
30
- send_msg(snippets.join(' | '), e.channel)
31
-
23
+ join_pug(e, pug)
32
24
  start_pug(pug, e) if pug.full?
33
25
  end
34
26
  end
35
27
 
36
28
  bot.command :status do |event, *args|
37
29
  setup_pug(event) do |e, pug|
38
- msg = if pug.active?
39
- [
40
- "#{e.display_names_for(pug.joined_players).join(', ')} joined",
41
- pug.player_slots
42
- ].join(' | ')
43
- else
44
- 'No PUG has been started. `!join` to create'
45
- end
30
+ return send_msg('No PUG has been started. `!join` to create', e.channel) unless pug.active?
46
31
 
47
- send_msg(msg, e.channel)
32
+ send_msg(
33
+ [
34
+ "#{pug.player_slots} joined",
35
+ pug_teams_message(pug, e).join("\n")
36
+ ].join("\n"),
37
+ e.channel
38
+ )
48
39
  end
49
40
  end
50
41
 
51
42
  bot.command :teamsize do |event, *args|
52
43
  setup_pug(event) do |e, pug|
53
- new_teamsize = args[0]
44
+ new_teamsize = args[0].to_i
45
+ return send_msg('Team size should be a number higher than 0', e.channel) unless new_teamsize > 0
54
46
 
55
47
  if new_teamsize
56
48
  pug.teamsize = new_teamsize
57
49
 
58
50
  send_msg(
59
- "Team size set to #{pug.teamsize} | #{pug.player_slots} joined",
51
+ [
52
+ "Team size set to #{pug.teamsize}",
53
+ "#{pug.player_slots} joined"
54
+ ].join(MSG_SNIPPET_DELIMITER),
60
55
  e.channel
61
56
  )
62
57
 
63
58
  start_pug(pug, e) if pug.full?
64
59
  else
65
60
  send_msg(
66
- "Current team size is #{pug.teamsize} | #{pug.player_slots} joined",
61
+ [
62
+ "Current team size is #{pug.teamsize}",
63
+ "#{pug.player_slots} joined"
64
+ ].join(MSG_SNIPPET_DELIMITER),
67
65
  e.channel
68
66
  )
69
67
  end
@@ -85,7 +83,7 @@ class QwtfDiscordBotPug # :nodoc:
85
83
  snippets << "#{pug.slots_left} more #{pug.notify_roles}" if pug.slots_left == 1
86
84
 
87
85
  send_msg(
88
- snippets.join(' | '),
86
+ snippets.join(MSG_SNIPPET_DELIMITER),
89
87
  e.channel
90
88
  )
91
89
 
@@ -121,7 +119,7 @@ class QwtfDiscordBotPug # :nodoc:
121
119
  snippets << "#{pug.slots_left} more #{pug.notify_roles}" if pug.slots_left == 1
122
120
 
123
121
  send_msg(
124
- snippets.join(' | '),
122
+ snippets.join(MSG_SNIPPET_DELIMITER),
125
123
  e.channel
126
124
  )
127
125
 
@@ -130,6 +128,67 @@ class QwtfDiscordBotPug # :nodoc:
130
128
  end
131
129
  end
132
130
 
131
+ bot.command :team do |event, *args|
132
+ setup_pug(event) do |e, pug|
133
+ team_no = args[0].to_i
134
+ return send_msg("Choose a team between 1 and 4", e.channel) unless team_no.between?(1, 4)
135
+
136
+ user_id = e.user_id
137
+ return send_msg("You're already in team #{team_no}", e.channel) if pug.team(team_no).include?(user_id)
138
+
139
+ join_pug(e, pug) unless pug.joined?(user_id)
140
+ pug.join_team(team_no: team_no, player_id: user_id)
141
+
142
+ send_msg(
143
+ [
144
+ "#{e.display_name} joins team #{team_no}",
145
+ "#{pug.team_player_count(team_no)}/#{pug.teamsize}"
146
+ ].join(MSG_SNIPPET_DELIMITER), e.channel
147
+ )
148
+
149
+ start_pug(pug, e) if pug.full?
150
+ end
151
+ end
152
+
153
+ bot.command :unteam do |event, *args|
154
+ setup_pug(event) do |e, pug|
155
+ user_id = e.user_id
156
+ return send_msg('No PUG has been started. `!join` to create', e.channel) unless pug.active?
157
+ return send_msg("You aren't in this PUG", e.channel) unless pug.joined?(user_id)
158
+ return send_msg("You aren't in a team", e.channel) if pug.team(0).include?(user_id)
159
+
160
+ pug.join_team(team_no: 0, player_id: user_id)
161
+ send_msg("#{e.display_name} has no team", e.channel)
162
+ end
163
+ end
164
+
165
+ bot.command :win do |event, *args|
166
+ setup_pug(event) do |e, pug|
167
+ return send_msg(no_active_pug_message, e.channel) unless pug.active?
168
+
169
+ winning_team_no = args[0]
170
+
171
+ return send_msg("Not a valid team", e.channel) unless pug.team(winning_team_no).any?
172
+
173
+ pug.won_by(winning_team_no)
174
+
175
+ winning_team = pug.team(winning_team_no).map do |player_id|
176
+ e.display_name_for(player_id)
177
+ end
178
+
179
+ non_winning_teams = pug.actual_teams.tap { |team| team.delete(winning_team_no) }
180
+
181
+ losing_players = non_winning_teams.values.flatten.map do |player_id|
182
+ e.display_name_for(player_id)
183
+ end
184
+
185
+ send_msg(
186
+ "#{winning_team.join(', ')} defeat #{losing_players.join(', ')}",
187
+ e.channel
188
+ )
189
+ end
190
+ end
191
+
133
192
  bot.command :end do |event, *args|
134
193
  setup_pug(event) do |e, pug|
135
194
  return send_msg(no_active_pug_message, e.channel) unless pug.active?
@@ -160,6 +219,19 @@ class QwtfDiscordBotPug # :nodoc:
160
219
 
161
220
  private
162
221
 
222
+ def join_pug(e, pug)
223
+ pug.join(e.user_id)
224
+
225
+ if pug.joined_player_count == 1
226
+ snippets = ["#{e.display_name} creates a PUG", pug.player_slots, pug.notify_roles]
227
+ else
228
+ snippets = ["#{e.display_name} joins the PUG", pug.player_slots]
229
+ snippets << "#{pug.slots_left} more #{pug.notify_roles}" if pug.slots_left.between?(1, 3)
230
+ end
231
+
232
+ send_msg(snippets.join(MSG_SNIPPET_DELIMITER), e.channel)
233
+ end
234
+
163
235
  def setup_pug(event)
164
236
  e = EventDecorator.new(event)
165
237
  pug = Pug.for(e.channel_id)
@@ -168,17 +240,49 @@ class QwtfDiscordBotPug # :nodoc:
168
240
  end
169
241
 
170
242
  def start_pug(pug, event)
243
+ pug_teams = pug.teams.map do |team_no, player_ids|
244
+ team_mentions = player_ids.map do |player_id|
245
+ event.mention_for(player_id)
246
+ end
247
+
248
+ team_status_line(
249
+ team_no: team_no.to_i,
250
+ names: team_mentions,
251
+ teamsize: pug.teamsize
252
+ )
253
+ end
254
+
171
255
  msg = [
172
256
  'Time to play!',
173
- ['Team 1:', event.mentions_for(pug.team(1)).join(' ')].join(' '),
174
- ['Team 2:', event.mentions_for(pug.team(2)).join(' ')].join(' ')
257
+ pug_teams
175
258
  ].join("\n")
176
259
 
177
260
  send_msg(msg, event.channel)
178
261
  end
179
262
 
180
- def start_pug_send_msg(player_slots:, mentions:)
181
- ['Time to play!', player_slots, mentions.join(' ')].join(' | ')
263
+ def pug_teams_message(pug, event)
264
+ pug.teams.map do |team_no, player_ids|
265
+ team_display_names = player_ids.map do |player_id|
266
+ event.display_name_for(player_id)
267
+ end
268
+
269
+ team_status_line(
270
+ team_no: team_no.to_i,
271
+ names: team_display_names,
272
+ teamsize: pug.teamsize
273
+ )
274
+ end
275
+ end
276
+
277
+ def team_status_line(team_no:, names:, teamsize:)
278
+ if team_no.to_i.zero?
279
+ ["No team: #{names.join(', ')}"]
280
+ else
281
+ [
282
+ "Team #{team_no}: #{names.join(', ')}",
283
+ "#{names.count}/#{teamsize}"
284
+ ].join(MSG_SNIPPET_DELIMITER)
285
+ end
182
286
  end
183
287
 
184
288
  def end_pug_message
@@ -27,7 +27,6 @@ class QwtfDiscordBotServer
27
27
  puts message
28
28
  end
29
29
  end
30
-
31
30
  end
32
31
 
33
32
  bot.command :all do |event|
@@ -12,7 +12,7 @@ class QwtfDiscordBotWatcher
12
12
  next if request.is_empty?
13
13
 
14
14
  request.player_names.each do |name|
15
- redis_key = ["watcher", address, name].join(":")
15
+ redis_key = ['watcher', address, name].join(':')
16
16
 
17
17
  unless seen_recently?(redis_key)
18
18
  endpoint.channel_ids.each do |channel_id|
@@ -1,3 +1,3 @@
1
1
  module QwtfDiscordBot
2
- VERSION = File.read("VERSION").chomp.freeze
2
+ VERSION = File.read('VERSION').chomp.freeze
3
3
  end
@@ -1,7 +1,7 @@
1
1
  class Team
2
2
  attr_accessor :name, :number, :players
3
3
 
4
- TEAMS = { "blue" => 1, "red" => 2, "yell" => 3, "gren" => 4, "spec" => 5 }.freeze
4
+ TEAMS = { 'blue' => 1, 'red' => 2, 'yell' => 3, 'gren' => 4, 'spec' => 5 }.freeze
5
5
 
6
6
  def initialize(name)
7
7
  @name = build_name(name)
@@ -19,27 +19,30 @@ class Team
19
19
 
20
20
  private
21
21
 
22
- def field_name
23
- name = @name.capitalize
24
- return "#{name}" unless playing?
25
- "#{name} | #{score}"
26
- end
27
-
28
- def playing?
29
- (1..4).include?(@number)
30
- end
31
-
32
- def player_list
33
- players.map(&:to_row).join("\n")
34
- end
35
-
36
- def score
37
- return nil if @name == "spec"
38
- @players.first.score
39
- end
40
-
41
- def build_name(name)
42
- return name if TEAMS[name]
43
- "spec"
44
- end
22
+ def field_name
23
+ name = @name.capitalize
24
+ return name.to_s unless playing?
25
+
26
+ "#{name} | #{score}"
27
+ end
28
+
29
+ def playing?
30
+ (1..4).include?(@number)
31
+ end
32
+
33
+ def player_list
34
+ players.map(&:to_row).join("\n")
35
+ end
36
+
37
+ def score
38
+ return nil if @name == 'spec'
39
+
40
+ @players.first.score
41
+ end
42
+
43
+ def build_name(name)
44
+ return name if TEAMS[name]
45
+
46
+ 'spec'
47
+ end
45
48
  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.1.11
4
+ version: 5.2.5
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-08-30 00:00:00.000000000 Z
11
+ date: 2020-09-23 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: discordrb