slack_game 0.1.3 → 0.2.0
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 +4 -4
- data/lib/slack_game.rb +6 -0
- data/lib/slack_game/canvas.rb +20 -18
- data/lib/slack_game/controller.rb +49 -14
- data/lib/slack_game/game/demo.rb +5 -5
- data/lib/slack_game/game/demo/controller.rb +4 -0
- data/lib/slack_game/game/lifegame.rb +2 -2
- data/lib/slack_game/version.rb +1 -1
- data/slack_game.gemspec +2 -1
- metadata +20 -6
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 01f77ed835aa5b2d8e53d359b7e91f8f4f39ef3e
|
4
|
+
data.tar.gz: 6ced2289003b0962059447d43a08173c977fb7b2
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 89f3d14578e39414f112375c1684d525298f7fbda2c9b4d2cad41e98c82ca8c08a837da50dee3c503843cc69c1a0e94b861af89bc9362813d0e1699589e10962
|
7
|
+
data.tar.gz: 3c850b0eadb61a88e4f4b1649cf808e95bc8f3f484065ecf1e08d9f70071f7febb6e173109d4b6b3d565c94810205bc4c3c693a043da632451590df60c3e919c
|
data/lib/slack_game.rb
CHANGED
@@ -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
|
data/lib/slack_game/canvas.rb
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
module SlackGame
|
2
2
|
class Canvas
|
3
|
-
attr_reader :channel
|
4
|
-
attr_accessor :
|
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
|
19
|
+
@slack = Slack::Web::Client.new
|
20
20
|
@channel = resolve_channel(channel)
|
21
|
-
@
|
21
|
+
@dot_matrix = y.times.inject([]) { |acc| acc << Array.new(x) }
|
22
22
|
end
|
23
23
|
|
24
|
-
def draw
|
25
|
-
|
26
|
-
result = if last_update
|
27
|
-
@slack.chat_update(ts: last_update, channel: channel, text:
|
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:
|
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
|
37
|
-
|
38
|
-
encoded.join("\n")
|
42
|
+
def dot2emoji(matrix)
|
43
|
+
matrix.map { |l| line_convert(l) }.join("\n")
|
39
44
|
end
|
40
45
|
|
41
|
-
def
|
42
|
-
|
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
|
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
|
-
|
23
|
-
@rtm
|
24
|
-
@rtm.on(:
|
25
|
-
|
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(
|
37
|
-
|
38
|
-
@command =
|
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
|
44
|
+
def take_last_command
|
42
45
|
command = @command
|
43
46
|
@command = nil
|
44
47
|
command
|
45
48
|
end
|
46
49
|
|
47
|
-
|
48
|
-
|
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?(
|
56
|
-
!! pattern.match(
|
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
|
data/lib/slack_game/game/demo.rb
CHANGED
@@ -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.
|
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.
|
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.
|
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.
|
45
|
+
@canvas.dot_matrix = matrix
|
46
46
|
@canvas.draw
|
47
47
|
end
|
48
48
|
|
@@ -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.
|
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.
|
25
|
+
@canvas.dot_matrix = @field.next.to_display
|
26
26
|
@canvas.draw
|
27
27
|
end
|
28
28
|
|
data/lib/slack_game/version.rb
CHANGED
data/slack_game.gemspec
CHANGED
@@ -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-
|
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.
|
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:
|
11
|
+
date: 2016-07-03 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
|
-
name: slack-
|
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: '
|
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: '
|
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
|