Class: Warchat::Chat::Client

Inherits:
Object
  • Object
show all
Defined in:
lib/warchat/chat/client.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeClient

Returns a new instance of Client.



10
11
12
13
14
15
16
# File 'lib/warchat/chat/client.rb', line 10

def initialize
  @session = Warchat::Network::Session.new

  [:receive,:establish,:error].each do |m| 
    session.send("on_#{m}=".to_sym, method("session_#{m}".to_sym)) 
  end
end

Instance Attribute Details

#character_nameObject (readonly)

Returns the value of attribute character_name.



8
9
10
# File 'lib/warchat/chat/client.rb', line 8

def character_name
  @character_name
end

#character_realmObject (readonly)

Returns the value of attribute character_realm.



8
9
10
# File 'lib/warchat/chat/client.rb', line 8

def character_realm
  @character_realm
end

#on_chat_logoutObject

Returns the value of attribute on_chat_logout.



6
7
8
# File 'lib/warchat/chat/client.rb', line 6

def on_chat_logout
  @on_chat_logout
end

#on_establishObject

Returns the value of attribute on_establish.



5
6
7
# File 'lib/warchat/chat/client.rb', line 5

def on_establish
  @on_establish
end

#on_failObject

Returns the value of attribute on_fail.



5
6
7
# File 'lib/warchat/chat/client.rb', line 5

def on_fail
  @on_fail
end

#on_logoutObject

Returns the value of attribute on_logout.



5
6
7
# File 'lib/warchat/chat/client.rb', line 5

def on_logout
  @on_logout
end

#on_messageObject

Returns the value of attribute on_message.



5
6
7
# File 'lib/warchat/chat/client.rb', line 5

def on_message
  @on_message
end

#on_message_afkObject

Returns the value of attribute on_message_afk.



6
7
8
# File 'lib/warchat/chat/client.rb', line 6

def on_message_afk
  @on_message_afk
end

#on_message_dndObject

Returns the value of attribute on_message_dnd.



6
7
8
# File 'lib/warchat/chat/client.rb', line 6

def on_message_dnd
  @on_message_dnd
end

#on_message_guild_chatObject

Returns the value of attribute on_message_guild_chat.



6
7
8
# File 'lib/warchat/chat/client.rb', line 6

def on_message_guild_chat
  @on_message_guild_chat
end

#on_message_motdObject

Returns the value of attribute on_message_motd.



6
7
8
# File 'lib/warchat/chat/client.rb', line 6

def on_message_motd
  @on_message_motd
end

#on_message_officer_chatObject

Returns the value of attribute on_message_officer_chat.



6
7
8
# File 'lib/warchat/chat/client.rb', line 6

def on_message_officer_chat
  @on_message_officer_chat
end

#on_message_whisperObject

Returns the value of attribute on_message_whisper.



6
7
8
# File 'lib/warchat/chat/client.rb', line 6

def on_message_whisper
  @on_message_whisper
end

#on_presenceObject

Returns the value of attribute on_presence.



5
6
7
# File 'lib/warchat/chat/client.rb', line 5

def on_presence
  @on_presence
end

#sessionObject (readonly)

Returns the value of attribute session.



8
9
10
# File 'lib/warchat/chat/client.rb', line 8

def session
  @session
end

Instance Method Details

#chat(response) ⇒ Object



60
61
62
63
64
65
66
67
68
69
70
71
72
# File 'lib/warchat/chat/client.rb', line 60

def chat response
  response.extend(Warchat::Chat::ChatResponse)
  if response.ack?
    
  elsif response.message?
    message = Warchat::Chat::Message.new(response)
    [on_message,send("on_message_#{message.type}".to_sym)].compact.each do |m| m.call(message) end
  elsif response.presence?
    on_presence and on_presence.call(Presence.new(response))
  else
    puts "unhandled chat type: #{response.chat_type}"
  end
end

#chat_login(response) ⇒ Object



55
56
57
58
# File 'lib/warchat/chat/client.rb', line 55

def  response
  puts "Logged into chat"
  @chat_session_id = response["chatSessionId"]
end

#chat_logout(response) ⇒ Object



48
49
50
51
52
53
# File 'lib/warchat/chat/client.rb', line 48

def chat_logout response
  puts 'Logged out of chat'
  @timer and @timer.stop
  on_chat_logout and on_chat_logout.call response
  session.close
end

#closeObject



79
80
81
82
# File 'lib/warchat/chat/client.rb', line 79

def close
  close_nonblock
  sleep(0.1) until session.is_closed?
end

#close_nonblockObject



74
75
76
77
# File 'lib/warchat/chat/client.rb', line 74

def close_nonblock
  request = Warchat::Network::Request.new("/chat-logout",:chatSessionId=>@chat_session_id)
  session.send_request(request)
end

#keep_aliveObject



84
85
86
87
88
# File 'lib/warchat/chat/client.rb', line 84

def keep_alive
  puts 'keep alive'
  request = Warchat::Network::Request.new("/ah-mail",:n=>character_name,:r=>character_realm)
  session.send_request(request)
end

#login(*args) ⇒ Object



36
37
38
39
40
41
42
# File 'lib/warchat/chat/client.rb', line 36

def  *args
  @character_name,@character_realm = args
  [character_name,character_realm].each do |s| s.respond_to? :force_encoding and s.force_encoding(__ENCODING__) end
  request = Warchat::Network::Request.new("/chat-login",:options=>{:mature_filter=>'false'},:n=>character_name,:r=>character_realm)
  session.send_request(request)
  @timer = Warchat::Timer.new(30) do keep_alive end
end

#logoutObject



44
45
46
# File 'lib/warchat/chat/client.rb', line 44

def logout
  request = Warchat::Network::Request.new('/chat-logout',:chatSessionId=>@chat_session_id)
end

#message(msg, chat_type = Message.CHAT_MSG_TYPE_GUILD_CHAT) ⇒ Object



90
91
92
93
# File 'lib/warchat/chat/client.rb', line 90

def message(msg, chat_type = Message.CHAT_MSG_TYPE_GUILD_CHAT)
  request = Warchat::Network::Request.new("/chat-guild",:type=>chat_type,:body=>msg,:chatSessionId=>@chat_session_id)
  session.send_request(request)
end

#session_error(response) ⇒ Object



23
24
25
# File 'lib/warchat/chat/client.rb', line 23

def session_error response
  on_fail and on_fail.call(response["body"]) if response.target == "/chat-login"
end

#session_establish(response) ⇒ Object



27
28
29
# File 'lib/warchat/chat/client.rb', line 27

def session_establish response
  on_establish and on_establish.call(response)
end

#session_receive(response) ⇒ Object



31
32
33
34
# File 'lib/warchat/chat/client.rb', line 31

def session_receive response
  m = response.target.gsub('/','').underscore.to_sym
  send(m,response) if respond_to? m
end

#start(username, password) ⇒ Object



18
19
20
21
# File 'lib/warchat/chat/client.rb', line 18

def start username, password
  [username,password].each do |s| s.respond_to? :force_encoding and s.force_encoding(__ENCODING__) end
  self.session.start(username,password)
end

#whisper(msg, name) ⇒ Object



95
96
97
98
# File 'lib/warchat/chat/client.rb', line 95

def whisper(msg, name)
  request = Warchat::Network::Request.new("/chat-whisper",:to=>"character:#{name}:#{character_realm.downcase}",:body=>msg,:chatSessionId=>@chat_session_id)
  session.send_request(request)
end