module Gamefic::Mud::Adapter::Tcp

The TCP client adapter module.

Attributes

ip_addr[R]

@return [String]

port[R]

@return [Integer]

Public Instance Methods

post_init() click to toggle source
# 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) click to toggle source
# File lib/gamefic-mud/adapter/tcp.rb, line 25
def receive_data data
  state.process data
end
send_raw(data) click to toggle source
# File lib/gamefic-mud/adapter/tcp.rb, line 45
def send_raw data
  send_data data
end
unbind() click to toggle source
# File lib/gamefic-mud/adapter/tcp.rb, line 53
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) click to toggle source
# File lib/gamefic-mud/adapter/tcp.rb, line 29
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]
  unless output[:options].nil?
    list = '<ol>'
    state[: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