Module: Gamefic::Mud::Adapter::Tcp

Includes:
Common
Defined in:
lib/gamefic-mud/adapter/tcp.rb

Overview

The TCP client adapter module.

Instance Attribute Summary collapse

Attributes included from Common

#character, #plot, #state

Instance Method Summary collapse

Methods included from Common

#[], #[]=, #session, #start

Instance Attribute Details

#ip_addrString (readonly)

Returns:

  • (String)


13
14
15
# File 'lib/gamefic-mud/adapter/tcp.rb', line 13

def ip_addr
  @ip_addr
end

#portInteger (readonly)

Returns:

  • (Integer)


16
17
18
# File 'lib/gamefic-mud/adapter/tcp.rb', line 16

def port
  @port
end

Instance Method Details

#post_initObject



18
19
20
21
22
23
# File 'lib/gamefic-mud/adapter/tcp.rb', line 18

def post_init
  port, ip = Socket.unpack_sockaddr_in(get_peername)
  @port = port
  @ip_addr = ip
  puts "Connection received from #{ip}:#{port}"
end

#receive_data(data) ⇒ Object



45
46
47
# File 'lib/gamefic-mud/adapter/tcp.rb', line 45

def receive_data data
  state.process data.to_s.strip
end

#send_raw(data) ⇒ Object



41
42
43
# File 'lib/gamefic-mud/adapter/tcp.rb', line 41

def send_raw data
  send_data data
end

#unbindObject



49
50
51
52
53
# File 'lib/gamefic-mud/adapter/tcp.rb', line 49

def unbind
  puts "Disconnecting from #{ip_addr}:#{port}"
  # @todo Right way to remove player from game?
  plot.destroy character unless character.nil?
end

#update(output) ⇒ Object



25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
# File 'lib/gamefic-mud/adapter/tcp.rb', line 25

def update output
  # TCP connections are assumed to be terminal clients. Convert messages and options into
  # an HTML block and convert it to ANSI text.
  text = output[:messages].to_s
  unless output[:options].nil?
    list = '<ol>'
    output[:options].each do |o|
      list += "<li>#{o}</li>"
    end
    list += "</ol>"
    text += list
  end
  # @todo The line endings should probably be a function of HtmlToAnsi.
  send_data HtmlToAnsi.convert(text).gsub(/\n/, "\r\n")
end