Class: SlackGame::Canvas

Inherits:
Object
  • Object
show all
Defined in:
lib/slack_game/canvas.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

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

#channelObject (readonly)

Returns the value of attribute channel.



3
4
5
# File 'lib/slack_game/canvas.rb', line 3

def channel
  @channel
end

#last_updateObject (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

#matrixObject

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

#drawObject



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