Class: GameMachine::Clients::TestClient

Inherits:
Object
  • Object
show all
Includes:
GameMachine
Defined in:
server/lib/game_machine/clients/test_client.rb

Constant Summary

Constants included from GameMachine

VERSION

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from GameMachine

app_root, env, java_root

Constructor Details

#initialize(username, name) ⇒ TestClient


34
35
36
37
38
39
40
41
42
# File 'server/lib/game_machine/clients/test_client.rb', line 34

def initialize(username,name)
  @username = username
  @authtoken = 'authtoken'
  node = self.class.node_for(name)
  @client = JavaLib::UdpClient.new(node[:udp_port].to_i,3000)
  unless client.connect(node[:host],node[:udp_port].to_i)
    raise "Unable to connect to #{node[:host]} #{node[:udp_port]}"
  end
end

Instance Attribute Details

#authtokenObject (readonly)

Returns the value of attribute authtoken


33
34
35
# File 'server/lib/game_machine/clients/test_client.rb', line 33

def authtoken
  @authtoken
end

#clientObject (readonly)

Returns the value of attribute client


33
34
35
# File 'server/lib/game_machine/clients/test_client.rb', line 33

def client
  @client
end

#usernameObject (readonly)

Returns the value of attribute username


33
34
35
# File 'server/lib/game_machine/clients/test_client.rb', line 33

def username
  @username
end

Class Method Details

.hostObject


25
26
27
# File 'server/lib/game_machine/clients/test_client.rb', line 25

def self.host
  ENV['CLUSTER_TEST_HOST'] || '127.0.0.1'
end

.node_for(name) ⇒ Object


29
30
31
# File 'server/lib/game_machine/clients/test_client.rb', line 29

def self.node_for(name)
  nodes.fetch(name.to_sym)
end

.nodesObject


7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
# File 'server/lib/game_machine/clients/test_client.rb', line 7

def self.nodes
  akka_port = 2551
  udp_port = 24130
  tcp_port = 8910
  www_port = 3001
  nodes = {}
  nodes[:seed] = {:host => host, :akka_port => akka_port, :udp_port => udp_port, :tcp_port => tcp_port, :www_port => www_port}
  10.times do |i|
    name = "node#{i+1}".to_sym
    akka_port += 1
    tcp_port += 1
    udp_port += 1
    www_port += 1
    nodes[name] = {:host => host, :akka_port => akka_port, :udp_port => udp_port, :tcp_port => tcp_port, :www_port => www_port}
  end
  nodes
end

Instance Method Details

#client_messageObject


120
121
122
123
124
125
# File 'server/lib/game_machine/clients/test_client.rb', line 120

def client_message
  player = MessageLib::Player.new.set_id(username).set_authtoken(authtoken)
  client_message = MessageLib::ClientMessage.new
  client_message.set_connection_type(0)
  client_message.set_player(player)
end

#connect_messageObject


116
117
118
# File 'server/lib/game_machine/clients/test_client.rb', line 116

def connect_message
  client_message.set_player_connect(MessageLib::PlayerConnect.new)
end

#entity_message(entity) ⇒ Object


106
107
108
# File 'server/lib/game_machine/clients/test_client.rb', line 106

def entity_message(entity)
  client_message.add_entity(entity)
end

#kill_messageObject


96
97
98
99
100
# File 'server/lib/game_machine/clients/test_client.rb', line 96

def kill_message
  entity = new_entity.set_poison_pill(MessageLib::PoisonPill.new)
  entity.set_destination("GameMachine/Killswitch")
  message = entity_message(entity)
end

#kill_nodeObject


91
92
93
94
# File 'server/lib/game_machine/clients/test_client.rb', line 91

def kill_node
  
  send_message(kill_message)
end

#loginObject


63
64
65
66
67
68
69
70
# File 'server/lib/game_machine/clients/test_client.rb', line 63

def 
  send_message(connect_message)
  return true if receive_message
  3.times do
    return true if remote_echo
  end
  false
end

#login_nowaitObject


72
73
74
# File 'server/lib/game_machine/clients/test_client.rb', line 72

def 
  send_message(connect_message)
end

#logoutObject


76
77
78
# File 'server/lib/game_machine/clients/test_client.rb', line 76

def logout
  send_message(logout_message)
end

#logout_messageObject


110
111
112
113
114
# File 'server/lib/game_machine/clients/test_client.rb', line 110

def logout_message
  client_message.set_player_logout(
    MessageLib::PlayerLogout.new.set_authtoken(authtoken).set_player_id(username)
  )
end

#new_entityObject


102
103
104
# File 'server/lib/game_machine/clients/test_client.rb', line 102

def new_entity
  MessageLib::Entity.new.set_id('test')
end

#receive_messageObject


48
49
50
51
52
53
54
55
56
57
58
59
60
61
# File 'server/lib/game_machine/clients/test_client.rb', line 48

def receive_message
  if bytes = client.receive
    client_message = MessageLib::ClientMessage.parse_from(bytes)
    if client_message.get_entity_count >= 1
      entity = client_message.get_entity(0)
      puts "Received #{entity.component_names.to_a.inspect}"
      entity
    else
      puts "Received message without entity"
    end
  else
    nil
  end
end

#remote_echoObject


85
86
87
88
89
# File 'server/lib/game_machine/clients/test_client.rb', line 85

def remote_echo
  entity = new_entity.set_echo_test(MessageLib::EchoTest.new.set_message('test'))
  send_message(entity_message(entity))
  receive_message
end

#send_chat_message(type, channel, message) ⇒ Object


127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
# File 'server/lib/game_machine/clients/test_client.rb', line 127

def send_chat_message(type,channel,message)
  entity = new_entity
  entity.set_id("chatmessage")
          
  chat_message = MessageLib::ChatMessage.new
  chat_channel = MessageLib::ChatChannel.new

  chat_channel.set_name(channel)
  chat_message.set_chat_channel(chat_channel)
  chat_message.set_message(message)

  chat_message.set_type(type)

  chat_message.set_sender_id(username)
  entity.set_chat_message(chat_message)
  send_message(entity_message(entity))
end

#send_message(message) ⇒ Object


44
45
46
# File 'server/lib/game_machine/clients/test_client.rb', line 44

def send_message(message)
  client.send(message.to_byte_array)
end

#send_remote_echoObject


80
81
82
83
# File 'server/lib/game_machine/clients/test_client.rb', line 80

def send_remote_echo
  entity = new_entity.set_echo_test(MessageLib::EchoTest.new.set_message('test'))
  send_message(entity_message(entity))
end