slack_game 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 13c361e20aa040633c7531bc913be0e7a28c32f5
4
+ data.tar.gz: 2f1283d07c089e06c5d02d62afffd7f9d7d688c7
5
+ SHA512:
6
+ metadata.gz: 29198950645c816080af7aafd18b4587eaa738aa52c8491b22a4551ba5f9bcbd5d5225c3450085184b6b5eac8a9f8bc648e76099d8c86f7dd847b326121980b7
7
+ data.tar.gz: bcf1ab36f2359f2407ed8a856106c4bca8848a2d63da01936dfeca29b284fcaeebbae64d2cd7946222f3960330e2d7249b6a0f2e29222e8a694db4f62e828ed1
data/.gitignore ADDED
@@ -0,0 +1,9 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /Gemfile.lock
4
+ /_yardoc/
5
+ /coverage/
6
+ /doc/
7
+ /pkg/
8
+ /spec/reports/
9
+ /tmp/
data/.rspec ADDED
@@ -0,0 +1,2 @@
1
+ --format documentation
2
+ --color
data/.travis.yml ADDED
@@ -0,0 +1,4 @@
1
+ language: ruby
2
+ rvm:
3
+ - 2.2.2
4
+ before_install: gem install bundler -v 1.10.6
@@ -0,0 +1,13 @@
1
+ # Contributor Code of Conduct
2
+
3
+ As contributors and maintainers of this project, we pledge to respect all people who contribute through reporting issues, posting feature requests, updating documentation, submitting pull requests or patches, and other activities.
4
+
5
+ We are committed to making participation in this project a harassment-free experience for everyone, regardless of level of experience, gender, gender identity and expression, sexual orientation, disability, personal appearance, body size, race, ethnicity, age, or religion.
6
+
7
+ Examples of unacceptable behavior by participants include the use of sexual language or imagery, derogatory comments or personal attacks, trolling, public or private harassment, insults, or other unprofessional conduct.
8
+
9
+ Project maintainers have the right and responsibility to remove, edit, or reject comments, commits, code, wiki edits, issues, and other contributions that are not aligned to this Code of Conduct. Project maintainers who do not follow the Code of Conduct may be removed from the project team.
10
+
11
+ Instances of abusive, harassing, or otherwise unacceptable behavior may be reported by opening an issue or contacting one or more of the project maintainers.
12
+
13
+ This Code of Conduct is adapted from the [Contributor Covenant](http://contributor-covenant.org), version 1.0.0, available at [http://contributor-covenant.org/version/1/0/0/](http://contributor-covenant.org/version/1/0/0/)
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in slack_game.gemspec
4
+ gemspec
data/LICENSE.md ADDED
@@ -0,0 +1,14 @@
1
+ DO WHAT THE FUCK YOU WANT TO PUBLIC LICENSE
2
+ Version 2, December 2004
3
+
4
+ Copyright (C) 2015 Yasuhiro Kinoshita <WhoIsDissolvedGirl+github@gmail.com>
5
+
6
+ Everyone is permitted to copy and distribute verbatim or modified
7
+ copies of this license document, and changing it is allowed as long
8
+ as the name is changed.
9
+
10
+ DO WHAT THE FUCK YOU WANT TO PUBLIC LICENSE
11
+ TERMS AND CONDITIONS FOR COPYING, DISTRIBUTION AND MODIFICATION
12
+
13
+ 0. You just DO WHAT THE FUCK YOU WANT TO.
14
+
data/README.md ADDED
@@ -0,0 +1,32 @@
1
+ # SlackGame
2
+
3
+ Play games on Slack
4
+
5
+ This gem provide Controller and Canvas
6
+
7
+ Improve your like games!
8
+
9
+ ## Installation
10
+
11
+ Add this line to your application's Gemfile:
12
+
13
+ ```ruby
14
+ gem 'slack_game'
15
+ ```
16
+
17
+ And then execute:
18
+
19
+ $ bundle
20
+
21
+ ## Usage
22
+
23
+ see `lib/slack_game/game/demo.rb`
24
+
25
+ ## LICENSE
26
+
27
+ WTFPL
28
+
29
+ ## Contributing
30
+
31
+ Bug reports and pull requests are welcome on GitHub at https://github.com/[USERNAME]/slack_game. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [Contributor Covenant](contributor-covenant.org) code of conduct.
32
+
data/Rakefile ADDED
@@ -0,0 +1,6 @@
1
+ require "bundler/gem_tasks"
2
+ require "rspec/core/rake_task"
3
+
4
+ RSpec::Core::RakeTask.new(:spec)
5
+
6
+ task :default => :spec
@@ -0,0 +1,54 @@
1
+ module SlackGame
2
+ class Canvas
3
+ attr_reader :channel, :last_update
4
+ attr_accessor :matrix
5
+
6
+ def self.inherited(subclass)
7
+ def subclass.dot(id, emoji)
8
+ dot_map[id] = emoji
9
+ end
10
+
11
+ def subclass.dot_map
12
+ @@dot ||= {}
13
+ end
14
+ end
15
+
16
+ def initialize(channel, x, y)
17
+ @slack = Slack::Client.new(token: ENV['SLACK_TOKEN'])
18
+ @channel = resolve_channel(channel)
19
+ @matrix = y.times.inject([]) { |acc| acc << Array.new(x) }
20
+ end
21
+
22
+ def draw
23
+ decoded_matrix = encode(matrix)
24
+ result = if last_update
25
+ @slack.chat_update(ts: last_update, channel: channel, text: decoded_matrix)
26
+ else
27
+ @slack.chat_postMessage(channel: channel, text: decoded_matrix, as_user: true)
28
+ end
29
+ @last_update = result['ok'] ? result['ts'] : raise
30
+ end
31
+
32
+ private
33
+
34
+ def encode(matrix)
35
+ decoded = matrix.map { |l| line_decode(l) }
36
+ decoded.join("\n")
37
+ end
38
+
39
+ def line_decode(line)
40
+ decoded_line = line.map do |t|
41
+ dot_emoji(t)
42
+ end
43
+ decoded_line.join
44
+ end
45
+
46
+ def dot_emoji(id)
47
+ self.class.dot_map[id] || ENV['DEFAULT_SPACER'] || ':spacer:'
48
+ end
49
+
50
+ def resolve_channel(channel_name)
51
+ @slack.channels_list['channels'].find { |c| c['name'] == channel_name }['id']
52
+ end
53
+ end
54
+ end
@@ -0,0 +1,58 @@
1
+ module SlackGame
2
+ class Controller
3
+
4
+ def self.inherited(subclass)
5
+ def subclass.command(command, pattern)
6
+ parsers << InputParser.new(command, pattern)
7
+ end
8
+
9
+ def subclass.parsers
10
+ @@parsers ||= []
11
+ end
12
+ end
13
+
14
+ def initialize
15
+ init
16
+ listen
17
+ end
18
+
19
+ def init
20
+ slack = Slack::Client.new(token: ENV['SLACK_TOKEN'])
21
+ @rtm = slack.realtime
22
+ @rtm.on(:message) do |m|
23
+ input(m['text'])
24
+ end
25
+ end
26
+
27
+ def listen
28
+ @thread = Thread.new do
29
+ @rtm.start
30
+ end
31
+ @thread.run
32
+ end
33
+
34
+ def input(command)
35
+ parser = self.class.parsers.find { |p| p.match?(command) }
36
+ @command = parser.command if parser
37
+ end
38
+
39
+ def take
40
+ command = @command
41
+ @command = nil
42
+ command
43
+ end
44
+
45
+ class InputParser
46
+ attr_reader :pattern, :command
47
+
48
+ def initialize(command, pattern)
49
+ @command = command
50
+ @pattern = pattern
51
+ end
52
+
53
+ def match?(input)
54
+ !! pattern.match(input)
55
+ end
56
+ end
57
+ end
58
+ end
@@ -0,0 +1,15 @@
1
+ module SlackGame
2
+ module Game
3
+ class Demo
4
+ class Canvas < ::SlackGame::Canvas
5
+ CHARACTOR = 0
6
+ PAINTED = 1
7
+ SPACER = 2
8
+
9
+ dot CHARACTOR, ENV['DEMO_CHARACTOR']
10
+ dot PAINTED, ENV['DEMO_PAINTED']
11
+ dot SPACER, ':spacer:'
12
+ end
13
+ end
14
+ end
15
+ end
@@ -0,0 +1,17 @@
1
+ module SlackGame
2
+ module Game
3
+ class Demo
4
+ class Controller < ::SlackGame::Controller
5
+ RIGHT = 1
6
+ LEFT = 2
7
+ DOWN = 3
8
+ UP = 4
9
+
10
+ command RIGHT, /r/
11
+ command LEFT, /l/
12
+ command DOWN, /d/
13
+ command UP, /u/
14
+ end
15
+ end
16
+ end
17
+ end
@@ -0,0 +1,69 @@
1
+ require 'slack_game/game/demo/controller'
2
+ require 'slack_game/game/demo/canvas'
3
+
4
+ module SlackGame
5
+ module Game
6
+ class Demo
7
+ def initialize(channel, x = 10, y = 10)
8
+ @controller = Controller.new
9
+ @canvas = Canvas.new(channel, x, y)
10
+ @position = Position.new(0, 0, 0..9, 0..9)
11
+ @canvas.matrix[@position.y][@position.x] = Canvas::CHARACTOR
12
+ @canvas.draw
13
+ end
14
+
15
+ def main_loop
16
+ loop{
17
+ begin
18
+ update(@controller.take)
19
+ rescue => e
20
+ puts "GAME OVER #{e.message}"
21
+ break
22
+ end
23
+ }
24
+ end
25
+
26
+ def update(cmd)
27
+ case cmd
28
+ when Controller::LEFT
29
+ set_position(@position.x - 1, @position.y)
30
+ when Controller::RIGHT
31
+ set_position(@position.x + 1, @position.y)
32
+ when Controller::DOWN
33
+ set_position(@position.x, @position.y + 1)
34
+ when Controller::UP
35
+ set_position(@position.x, @position.y - 1)
36
+ end
37
+ end
38
+
39
+ def set_position(x, y)
40
+ @position.x = x
41
+ @position.y = y
42
+ matrix = @canvas.matrix
43
+ matrix = matrix.map { |n| n.map { |d| d == Canvas::CHARACTOR ? Canvas::PAINTED : d } }
44
+ matrix[@position.y][@position.x] = Canvas::CHARACTOR
45
+ @canvas.matrix = matrix
46
+ @canvas.draw
47
+ end
48
+
49
+ class Position
50
+ attr_accessor :x, :y
51
+ attr_reader :range_x, :range_y
52
+ def initialize(x, y, range_x, range_y)
53
+ @x = x
54
+ @y = y
55
+ @range_x = range_x
56
+ @range_y = range_y
57
+ end
58
+
59
+ def x=(new_x)
60
+ @x = range_x.include?(new_x) ? new_x : x
61
+ end
62
+
63
+ def y=(new_y)
64
+ @y = range_y.include?(new_y) ? new_y : y
65
+ end
66
+ end
67
+ end
68
+ end
69
+ end
@@ -0,0 +1,7 @@
1
+ module SlackGame
2
+ module Game
3
+ require 'slack_game/game/tetris'
4
+ require 'slack_game/game/demo'
5
+ end
6
+ end
7
+
@@ -0,0 +1,3 @@
1
+ module SlackGame
2
+ VERSION = "0.1.0"
3
+ end
data/lib/slack_game.rb ADDED
@@ -0,0 +1,6 @@
1
+ require 'slack'
2
+
3
+ require 'slack_game/canvas'
4
+ require 'slack_game/controller'
5
+ require 'slack_game/game'
6
+ require "slack_game/version"
@@ -0,0 +1,27 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'slack_game/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "slack_game"
8
+ spec.version = SlackGame::VERSION
9
+ spec.authors = ["Kinoshita.Yasuhiro"]
10
+ spec.email = ["WhoIsDissolvedGirl+github@gmail.com"]
11
+
12
+ spec.summary = %q{Play games on slack}
13
+ spec.description = %q{Play games on slack}
14
+ spec.homepage = "https://github.com/YasuhiroKinoshita/skack-game"
15
+
16
+ spec.files = `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
17
+ spec.bindir = "exe"
18
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
19
+ spec.require_paths = ["lib"]
20
+
21
+ spec.add_runtime_dependency "slack-api", "~> 1.0"
22
+ spec.add_development_dependency "bundler", "~> 1.10"
23
+ spec.add_development_dependency "rake", "~> 10.0"
24
+ spec.add_development_dependency "rspec"
25
+ spec.add_development_dependency "pry"
26
+ spec.add_development_dependency "wtfpl_init"
27
+ end
metadata ADDED
@@ -0,0 +1,144 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: slack_game
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Kinoshita.Yasuhiro
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2015-11-08 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: slack-api
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '1.0'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '1.0'
27
+ - !ruby/object:Gem::Dependency
28
+ name: bundler
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '1.10'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '1.10'
41
+ - !ruby/object:Gem::Dependency
42
+ name: rake
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '10.0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '10.0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: rspec
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - ">="
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - ">="
67
+ - !ruby/object:Gem::Version
68
+ version: '0'
69
+ - !ruby/object:Gem::Dependency
70
+ name: pry
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - ">="
74
+ - !ruby/object:Gem::Version
75
+ version: '0'
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - ">="
81
+ - !ruby/object:Gem::Version
82
+ version: '0'
83
+ - !ruby/object:Gem::Dependency
84
+ name: wtfpl_init
85
+ requirement: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - ">="
88
+ - !ruby/object:Gem::Version
89
+ version: '0'
90
+ type: :development
91
+ prerelease: false
92
+ version_requirements: !ruby/object:Gem::Requirement
93
+ requirements:
94
+ - - ">="
95
+ - !ruby/object:Gem::Version
96
+ version: '0'
97
+ description: Play games on slack
98
+ email:
99
+ - WhoIsDissolvedGirl+github@gmail.com
100
+ executables: []
101
+ extensions: []
102
+ extra_rdoc_files: []
103
+ files:
104
+ - ".gitignore"
105
+ - ".rspec"
106
+ - ".travis.yml"
107
+ - CODE_OF_CONDUCT.md
108
+ - Gemfile
109
+ - LICENSE.md
110
+ - README.md
111
+ - Rakefile
112
+ - lib/slack_game.rb
113
+ - lib/slack_game/canvas.rb
114
+ - lib/slack_game/controller.rb
115
+ - lib/slack_game/game.rb
116
+ - lib/slack_game/game/demo.rb
117
+ - lib/slack_game/game/demo/canvas.rb
118
+ - lib/slack_game/game/demo/controller.rb
119
+ - lib/slack_game/version.rb
120
+ - slack_game.gemspec
121
+ homepage: https://github.com/YasuhiroKinoshita/skack-game
122
+ licenses: []
123
+ metadata: {}
124
+ post_install_message:
125
+ rdoc_options: []
126
+ require_paths:
127
+ - lib
128
+ required_ruby_version: !ruby/object:Gem::Requirement
129
+ requirements:
130
+ - - ">="
131
+ - !ruby/object:Gem::Version
132
+ version: '0'
133
+ required_rubygems_version: !ruby/object:Gem::Requirement
134
+ requirements:
135
+ - - ">="
136
+ - !ruby/object:Gem::Version
137
+ version: '0'
138
+ requirements: []
139
+ rubyforge_project:
140
+ rubygems_version: 2.4.5
141
+ signing_key:
142
+ specification_version: 4
143
+ summary: Play games on slack
144
+ test_files: []