Class: SmartFox::Room

Inherits:
Object
  • Object
show all
Defined in:
lib/smartfox/room.rb

Constant Summary collapse

EVENTS =
[ :message_sent, :message_received, :message_received_self, :message_received_other, :user_joined, :user_left, :user_count_updated ]
ACTION_PUBLISH_MESSAGE =
'pubMsg'
ACTION_USER_ENTER =
'uER'
ACTION_UPDATE_USER_COUNT =
'uCount'

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(client, node) ⇒ Room

Returns a new instance of Room.



9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
# File 'lib/smartfox/room.rb', line 9

def initialize(client, node)
  @client = client
  @id = node['id'].to_i
  @name = node.first.content
  @private = node['priv'] == '1'
  @temporary = node['temp'] == '1'
  @current_users = node['ucnt'].to_i
  @max_users = node['maxu'].to_i
  @game = node['game'] == '1'
  @events = {}
  @transcript = []

  @users = nil
  SmartFox::Logger.debug "SmartFox::Room.new -> #{inspect}"
end

Instance Attribute Details

#gameObject (readonly)

Returns the value of attribute game.



2
3
4
# File 'lib/smartfox/room.rb', line 2

def game
  @game
end

#idObject (readonly)

Returns the value of attribute id.



2
3
4
# File 'lib/smartfox/room.rb', line 2

def id
  @id
end

#max_usersObject (readonly)

Returns the value of attribute max_users.



2
3
4
# File 'lib/smartfox/room.rb', line 2

def max_users
  @max_users
end

#nameObject (readonly)

Returns the value of attribute name.



2
3
4
# File 'lib/smartfox/room.rb', line 2

def name
  @name
end

#privateObject (readonly)

Returns the value of attribute private.



2
3
4
# File 'lib/smartfox/room.rb', line 2

def private
  @private
end

#temporaryObject (readonly)

Returns the value of attribute temporary.



2
3
4
# File 'lib/smartfox/room.rb', line 2

def temporary
  @temporary
end

#transcriptObject (readonly)

Returns the value of attribute transcript.



2
3
4
# File 'lib/smartfox/room.rb', line 2

def transcript
  @transcript
end

#usersObject (readonly)

Returns the value of attribute users.



2
3
4
# File 'lib/smartfox/room.rb', line 2

def users
  @users
end

Class Method Details

.parse(client, node) ⇒ Object



29
30
31
# File 'lib/smartfox/room.rb', line 29

def self.parse(client, node)
  new(client, node)
end

Instance Method Details

#add_handler(event, &proc) ⇒ Object



99
100
101
102
# File 'lib/smartfox/room.rb', line 99

def add_handler(event, &proc)
  @events[event.to_sym] = [] unless @events[event.to_sym]
  @events[event.to_sym] << proc
end

#current_usersObject



25
26
27
# File 'lib/smartfox/room.rb', line 25

def current_users
  @users ? @users.length : @current_users
end

#handle_system_packet(packet) ⇒ Object



67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
# File 'lib/smartfox/room.rb', line 67

def handle_system_packet(packet)
  case packet.action
  when ACTION_PUBLISH_MESSAGE
    text = packet.data.find{|c| c.name == 'txt'}.child.content
    user_id = packet.data.find{|c| c.name == 'user'}['id'].to_i
    user = @client.users[user_id]
    message = SmartFox::Message.new(user, text, self)
    SmartFox::Logger.info "SmartFox::Room did receive message #{message.inspect}"
    @transcript << message unless user.id == @client.user_id

    SmartFox::Logger.info "SmartFox::Room#handle_system_packet ACTION_PUBLISH_MESSAGE #{user.id} == #{@client.user_id}"
    if user.id == @client.user_id
      SmartFox::Logger.debug "SmartFox::Room#handle_system_packet ACTION_PUBLISH_MESSAGE from self"
      raise_event :message_received_self, @client, @room, message
    else
      SmartFox::Logger.debug "SmartFox::Room#handle_system_packet ACTION_PUBLISH_MESSAGE from #{user.name}"
      raise_event :message_received_other, @client, @room, message
    end
    raise_event :message_received, @client, @room, message
  when ACTION_USER_ENTER
    packet.data.each do |u|
      user = @client.parse_user(u)
      @users[user.id] = user
      raise_event :user_joined, @client, self, user
    end
  when ACTION_UPDATE_USER_COUNT
    old_count = @current_users
    @current_users = packet.body['u'].to_i
    raise_event :user_count_updated, @client, self, { :old => old_count, :new => @current_users }
  end
end

#inspectObject



33
34
35
# File 'lib/smartfox/room.rb', line 33

def inspect
  "#<#{self.class.name}:#{object_id} id:#{@id} name:#{@name}#{' temporary' if @temporary}#{' private' if @private} current_users:#{@current_users} max_users:#{@max_users}>"
end

#joinObject



37
38
39
# File 'lib/smartfox/room.rb', line 37

def join
  @client.join_room self
end

#joinedObject



41
42
43
# File 'lib/smartfox/room.rb', line 41

def joined
  @transcript.clear
end

#parse_users(users) ⇒ Object



45
46
47
48
49
50
51
52
# File 'lib/smartfox/room.rb', line 45

def parse_users(users)
  @users = {}

  users.children.each do |child|
    user = @client.parse_user(child)
    @users[user.id] = user
  end
end

#send_message(message) ⇒ Object



54
55
56
57
58
59
60
61
62
63
64
65
# File 'lib/smartfox/room.rb', line 54

def send_message(message)
  SmartFox::Logger.info "SmartFox::Room#send_message('#{message}')"
  @client.send :send_packet, SmartFox::Client::HEADER_SYSTEM, ACTION_PUBLISH_MESSAGE, self.id do |packet|
    packet.txt do |txt|
      txt.cdata! message
    end
  end

  @transcript << SmartFox::Message.new(@client.users[@client.user_id], message, self)

  raise_event :message_sent, @client, self, message
end