slack_game 0.1.3 → 0.2.0

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
  SHA1:
3
- metadata.gz: 1714a9ce57da98a5744300173827bacd3572fd82
4
- data.tar.gz: 973ede97c8c5c5605154da43ebabfe37119a33ec
3
+ metadata.gz: 01f77ed835aa5b2d8e53d359b7e91f8f4f39ef3e
4
+ data.tar.gz: 6ced2289003b0962059447d43a08173c977fb7b2
5
5
  SHA512:
6
- metadata.gz: edc9cc44a9b9fe3441bbfd351bdacbd5e41b034669b60bb898ea8139d855be9b83b7e08ac3cd62746296da7c87fd37d440c77bc45395e3da2de4dcd5715cb7e5
7
- data.tar.gz: 8eff5e97d1e156505f29a94987d4d7433760b6dd1d3db2ffc197a90cbe26ade5c72ca7696db51f3799dda7eff6635b06bfdca380409a1ebf3cd351ec8ebcbeac
6
+ metadata.gz: 89f3d14578e39414f112375c1684d525298f7fbda2c9b4d2cad41e98c82ca8c08a837da50dee3c503843cc69c1a0e94b861af89bc9362813d0e1699589e10962
7
+ data.tar.gz: 3c850b0eadb61a88e4f4b1649cf808e95bc8f3f484065ecf1e08d9f70071f7febb6e173109d4b6b3d565c94810205bc4c3c693a043da632451590df60c3e919c
@@ -4,3 +4,9 @@ require 'slack_game/canvas'
4
4
  require 'slack_game/controller'
5
5
  require 'slack_game/game'
6
6
  require "slack_game/version"
7
+
8
+ module SlackGame
9
+ Slack.configure do |conf|
10
+ conf.token = ENV['SLACK_TOKEN'] || raise(StandardError.new("environment variable 'SLACK_TOKEN' required"))
11
+ end
12
+ end
@@ -1,7 +1,7 @@
1
1
  module SlackGame
2
2
  class Canvas
3
- attr_reader :channel, :last_update
4
- attr_accessor :matrix
3
+ attr_reader :channel
4
+ attr_accessor :dot_matrix
5
5
 
6
6
  def self.inherited(subclass)
7
7
  subclass.class_variable_set(:@@dot, {})
@@ -16,36 +16,38 @@ module SlackGame
16
16
  end
17
17
 
18
18
  def initialize(channel, x, y)
19
- @slack = Slack::Client.new(token: ENV['SLACK_TOKEN'])
19
+ @slack = Slack::Web::Client.new
20
20
  @channel = resolve_channel(channel)
21
- @matrix = y.times.inject([]) { |acc| acc << Array.new(x) }
21
+ @dot_matrix = y.times.inject([]) { |acc| acc << Array.new(x) }
22
22
  end
23
23
 
24
- def draw
25
- encoded_matrix = encode(matrix)
26
- result = if last_update
27
- @slack.chat_update(ts: last_update, channel: channel, text: encoded_matrix)
24
+ def draw(parsers = nil)
25
+ emoji_matrix = dot2emoji(dot_matrix)
26
+ result = if @last_update
27
+ @slack.chat_update(ts: @last_update, channel: channel, text: emoji_matrix)
28
28
  else
29
- @slack.chat_postMessage(channel: channel, text: encoded_matrix, as_user: true)
29
+ posted = @slack.chat_postMessage(channel: channel, text: emoji_matrix, as_user: true)
30
+ if parsers
31
+ Array(parsers).select { |p| p.instance_of?(SlackGame::Controller::ReactionParser) }.each do |parser|
32
+ @slack.reactions_add(name: parser.emoji, channel: channel, timestamp: posted['ts'])
33
+ end
34
+ end
35
+ posted
30
36
  end
31
37
  @last_update = result['ok'] ? result['ts'] : raise
32
38
  end
33
39
 
34
40
  private
35
41
 
36
- def encode(matrix)
37
- encoded = matrix.map { |l| line_encode(l) }
38
- encoded.join("\n")
42
+ def dot2emoji(matrix)
43
+ matrix.map { |l| line_convert(l) }.join("\n")
39
44
  end
40
45
 
41
- def line_encode(line)
42
- encoded_line = line.map do |t|
43
- dot_emoji(t)
44
- end
45
- encoded_line.join
46
+ def line_convert(line)
47
+ line.map { |t| convert(t) }.join
46
48
  end
47
49
 
48
- def dot_emoji(id)
50
+ def convert(id)
49
51
  self.class.dot_map[id] || ENV['DEFAULT_SPACER'] || ':spacer:'
50
52
  end
51
53
 
@@ -8,6 +8,10 @@ module SlackGame
8
8
  parsers << InputParser.new(command, pattern)
9
9
  end
10
10
 
11
+ def subclass.reaction(command, emoji)
12
+ parsers << ReactionParser.new(command, emoji)
13
+ end
14
+
11
15
  def subclass.parsers
12
16
  self.class_variable_get(:@@parsers)
13
17
  end
@@ -19,41 +23,72 @@ module SlackGame
19
23
  end
20
24
 
21
25
  def init
22
- slack = Slack::Client.new(token: ENV['SLACK_TOKEN'])
23
- @rtm = slack.realtime
24
- @rtm.on(:message) do |m|
25
- input(m['text'])
26
- end
26
+ @rtm = Slack::RealTime::Client.new
27
+ @rtm.on(:message) { |m| input(m) }
28
+ @rtm.on(:reaction_add) { |m| input(m) }
29
+ @rtm.on(:reaction_removed) { |m| input(m) }
27
30
  end
28
31
 
29
32
  def listen
30
33
  @thread = Thread.new do
31
- @rtm.start
34
+ @rtm.start!
32
35
  end
33
36
  @thread.run
34
37
  end
35
38
 
36
- def input(command)
37
- parser = self.class.parsers.find { |p| p.match?(command) }
38
- @command = parser.command if parser
39
+ def input(message)
40
+ matched = parsers.find { |p| p.match?(message) }
41
+ @command = matched.command if matched
39
42
  end
40
43
 
41
- def take
44
+ def take_last_command
42
45
  command = @command
43
46
  @command = nil
44
47
  command
45
48
  end
46
49
 
47
- class InputParser
48
- attr_reader :pattern, :command
50
+ def parsers
51
+ self.class.parsers
52
+ end
53
+
54
+ class Parser
55
+ attr_reader :command
56
+ end
57
+
58
+ class InputParser < Parser
59
+ attr_reader :pattern
49
60
 
50
61
  def initialize(command, pattern)
51
62
  @command = command
52
63
  @pattern = pattern
53
64
  end
54
65
 
55
- def match?(input)
56
- !! pattern.match(input)
66
+ def match?(message)
67
+ !! pattern.match(message['text'])
68
+ end
69
+ end
70
+
71
+ class ReactionParser < Parser
72
+ attr_reader :emoji, :any
73
+
74
+ def initialize(command, emoji)
75
+ @command = command
76
+ @emoji = chomp(emoji)
77
+ end
78
+
79
+ def match?(message)
80
+ return false unless ['reaction_add', 'reaction_removed'].include?(message['type'])
81
+ message['reaction'] == @emoji
82
+ end
83
+
84
+ private
85
+
86
+ def chomp(emoji)
87
+ if emoji.start_with?(':') && emoji.end_with?(':')
88
+ emoji[1..-2]
89
+ else
90
+ emoji
91
+ end
57
92
  end
58
93
  end
59
94
  end
@@ -8,14 +8,14 @@ module SlackGame
8
8
  @controller = Controller.new
9
9
  @canvas = Canvas.new(channel, x, y)
10
10
  @position = Position.new(0, 0, 0..9, 0..9)
11
- @canvas.matrix[@position.y][@position.x] = Canvas::CHARACTOR
12
- @canvas.draw
11
+ @canvas.dot_matrix[@position.y][@position.x] = Canvas::CHARACTOR
12
+ @canvas.draw(@controller.parsers)
13
13
  end
14
14
 
15
15
  def main_loop
16
16
  loop{
17
17
  begin
18
- update(@controller.take)
18
+ update(@controller.take_last_command)
19
19
  rescue => e
20
20
  puts "GAME OVER #{e.message}"
21
21
  break
@@ -39,10 +39,10 @@ module SlackGame
39
39
  def set_position(x, y)
40
40
  @position.x = x
41
41
  @position.y = y
42
- matrix = @canvas.matrix
42
+ matrix = @canvas.dot_matrix
43
43
  matrix = matrix.map { |n| n.map { |d| d == Canvas::CHARACTOR ? Canvas::PAINTED : d } }
44
44
  matrix[@position.y][@position.x] = Canvas::CHARACTOR
45
- @canvas.matrix = matrix
45
+ @canvas.dot_matrix = matrix
46
46
  @canvas.draw
47
47
  end
48
48
 
@@ -11,6 +11,10 @@ module SlackGame
11
11
  command LEFT, /l/
12
12
  command DOWN, /d/
13
13
  command UP, /u/
14
+ reaction RIGHT, 'point_right'
15
+ reaction LEFT, 'point_left'
16
+ reaction DOWN, 'point_down'
17
+ reaction UP, 'point_up'
14
18
  end
15
19
  end
16
20
  end
@@ -6,7 +6,7 @@ module SlackGame
6
6
  def initialize(channel, size = 10)
7
7
  @canvas = Canvas.new(channel, size, size)
8
8
  @field = Field.new(size)
9
- @canvas.matrix = @field.to_display
9
+ @canvas.dot_matrix = @field.to_display
10
10
  @canvas.draw
11
11
  end
12
12
 
@@ -22,7 +22,7 @@ module SlackGame
22
22
  end
23
23
 
24
24
  def update
25
- @canvas.matrix = @field.next.to_display
25
+ @canvas.dot_matrix = @field.next.to_display
26
26
  @canvas.draw
27
27
  end
28
28
 
@@ -1,3 +1,3 @@
1
1
  module SlackGame
2
- VERSION = "0.1.3"
2
+ VERSION = "0.2.0"
3
3
  end
@@ -18,7 +18,8 @@ Gem::Specification.new do |spec|
18
18
  spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
19
19
  spec.require_paths = ["lib"]
20
20
 
21
- spec.add_runtime_dependency "slack-api", "~> 1.0"
21
+ spec.add_runtime_dependency "slack-ruby-client", "~> 0.7"
22
+ spec.add_runtime_dependency "faye-websocket"
22
23
  spec.add_development_dependency "bundler", "~> 1.10"
23
24
  spec.add_development_dependency "rake", "~> 10.0"
24
25
  spec.add_development_dependency "rspec"
metadata CHANGED
@@ -1,29 +1,43 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: slack_game
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.3
4
+ version: 0.2.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Kinoshita.Yasuhiro
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2015-12-18 00:00:00.000000000 Z
11
+ date: 2016-07-03 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
- name: slack-api
14
+ name: slack-ruby-client
15
15
  requirement: !ruby/object:Gem::Requirement
16
16
  requirements:
17
17
  - - "~>"
18
18
  - !ruby/object:Gem::Version
19
- version: '1.0'
19
+ version: '0.7'
20
20
  type: :runtime
21
21
  prerelease: false
22
22
  version_requirements: !ruby/object:Gem::Requirement
23
23
  requirements:
24
24
  - - "~>"
25
25
  - !ruby/object:Gem::Version
26
- version: '1.0'
26
+ version: '0.7'
27
+ - !ruby/object:Gem::Dependency
28
+ name: faye-websocket
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ">="
32
+ - !ruby/object:Gem::Version
33
+ version: '0'
34
+ type: :runtime
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ">="
39
+ - !ruby/object:Gem::Version
40
+ version: '0'
27
41
  - !ruby/object:Gem::Dependency
28
42
  name: bundler
29
43
  requirement: !ruby/object:Gem::Requirement
@@ -143,7 +157,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
143
157
  version: '0'
144
158
  requirements: []
145
159
  rubyforge_project:
146
- rubygems_version: 2.4.5
160
+ rubygems_version: 2.4.5.1
147
161
  signing_key:
148
162
  specification_version: 4
149
163
  summary: Play games on slack