class Kraken::Client

A simple client for KrakenLab server. You can use this class to Debug the server with `kraken -c` or implements your game using gosu gem!

a = Kraken::Client.new 'user', 'pass'
a.connect 'localhost'
a.call 'module::krakentrigger', {a: 2, b: [2,3,4], c: 'haha'}
a.call 'module::krakentrigger', 'call only this'
a.call 'module::krakentrigger', ['or this', 3]

Public Class Methods

new(user, pass, version) click to toggle source
# File lib/kraken/core/client.rb, line 14
def initialize(user, pass, version)
  @user = user
  @pass = pass
  @version = version
end

Public Instance Methods

call(method, params) click to toggle source
# File lib/kraken/core/client.rb, line 34
def call(method, params)
  write method
  write to_args(params)
  read_args
end
close() click to toggle source
# File lib/kraken/core/client.rb, line 20
def close
  @socket.close unless @socket.nil?
end
connect(host, port = 3030) click to toggle source
# File lib/kraken/core/client.rb, line 24
def connect(host, port = 3030)
  @socket = TCPSocket.new(host, port)
  write @version
  write @user
  write @pass
  raise 'connection refused' unless read == 'ok'
rescue StandardError
  raise 'connection refused'
end

Private Instance Methods

absolute_value(obj) click to toggle source
# File lib/kraken/core/client.rb, line 104
def absolute_value(obj)
  "a\n#{obj}"
end
hash_protocol(obj) click to toggle source
# File lib/kraken/core/client.rb, line 92
def hash_protocol(obj)
  ret = ''
  obj.each_pair { |key, value| ret += "#{key}\n#{to_args value}\n" }
  ret.chomp
end
read() click to toggle source
# File lib/kraken/core/client.rb, line 42
def read
  r = @socket.gets
  raise 'connection lost' if r.nil?
  r.chomp
end
read_args() click to toggle source
# File lib/kraken/core/client.rb, line 48
def read_args
  type = read
  case type
  when 'h'
    return read_hash
  when 'v'
    return read_vector
  when 'n'
    return nil
  when 'a'
    return read
  end
end
read_hash() click to toggle source
# File lib/kraken/core/client.rb, line 62
def read_hash
  n = read.to_i
  awns = {}
  n.times { awns[read.to_sym] = read_args }
  awns
end
read_vector() click to toggle source
# File lib/kraken/core/client.rb, line 69
def read_vector
  n = read.to_i
  awns = []
  n.times { awns << read_args }
  awns
end
to_args(obj) click to toggle source
# File lib/kraken/core/client.rb, line 80
def to_args(obj)
  case obj
  when Hash
    return "h\n#{obj.size}\n#{hash_protocol obj}"
  when Array
    return "v\n#{obj.size}\n#{vector_protocol obj}"
  when NilClass
    return 'n'
  end
  absolute_value(obj)
end
vector_protocol(obj) click to toggle source
# File lib/kraken/core/client.rb, line 98
def vector_protocol(obj)
  ret = ''
  obj.each { |value| ret += "#{to_args value}\n" }
  ret.chomp
end
write(txt) click to toggle source
# File lib/kraken/core/client.rb, line 76
def write(txt)
  @socket.puts txt.chomp
end