Class: SlackGame::Canvas
- Inherits:
-
Object
- Object
- SlackGame::Canvas
- Defined in:
- lib/slack_game/canvas.rb
Direct Known Subclasses
Game::Demo::Canvas, Game::Lifegame::Canvas, Game::Tetris::Canvas
Instance Attribute Summary collapse
-
#channel ⇒ Object
readonly
Returns the value of attribute channel.
-
#last_update ⇒ Object
readonly
Returns the value of attribute last_update.
-
#matrix ⇒ Object
Returns the value of attribute matrix.
Class Method Summary collapse
Instance Method Summary collapse
- #draw ⇒ Object
-
#initialize(channel, x, y) ⇒ Canvas
constructor
A new instance of Canvas.
Constructor Details
#initialize(channel, x, y) ⇒ Canvas
Returns a new instance of Canvas.
18 19 20 21 22 |
# File 'lib/slack_game/canvas.rb', line 18 def initialize(channel, x, y) @slack = Slack::Client.new(token: ENV['SLACK_TOKEN']) @channel = resolve_channel(channel) @matrix = y.times.inject([]) { |acc| acc << Array.new(x) } end |
Instance Attribute Details
#channel ⇒ Object (readonly)
Returns the value of attribute channel.
3 4 5 |
# File 'lib/slack_game/canvas.rb', line 3 def channel @channel end |
#last_update ⇒ Object (readonly)
Returns the value of attribute last_update.
3 4 5 |
# File 'lib/slack_game/canvas.rb', line 3 def last_update @last_update end |
#matrix ⇒ Object
Returns the value of attribute matrix.
4 5 6 |
# File 'lib/slack_game/canvas.rb', line 4 def matrix @matrix end |
Class Method Details
.inherited(subclass) ⇒ Object
6 7 8 9 10 11 12 13 14 15 16 |
# File 'lib/slack_game/canvas.rb', line 6 def self.inherited(subclass) subclass.class_variable_set(:@@dot, {}) def subclass.dot(id, emoji) self.dot_map[id] = emoji end def subclass.dot_map self.class_variable_get(:@@dot) end end |
Instance Method Details
#draw ⇒ Object
24 25 26 27 28 29 30 31 32 |
# File 'lib/slack_game/canvas.rb', line 24 def draw encoded_matrix = encode(matrix) result = if last_update @slack.chat_update(ts: last_update, channel: channel, text: encoded_matrix) else @slack.chat_postMessage(channel: channel, text: encoded_matrix, as_user: true) end @last_update = result['ok'] ? result['ts'] : raise end |