Class: GameMachine::Bot::Client

Inherits:
Actor::Base
  • Object
show all
Defined in:
lib/game_machine/bot/client.rb

Constant Summary

Constants inherited from Actor::Base

Actor::Base::ON_RECEIVE_HOOKS

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Actor::Base

add_hashring, aspect, aspects, find, find_by_address, find_distributed, find_distributed_local, find_remote, hashring, hashrings, local_path, model_filter, #onReceive, player_controller, #receive_message, reset_hashrings, #schedule_message, #sender, set_player_controller

Class Method Details

.start(name, port) ⇒ Object



9
10
11
12
13
14
15
16
17
18
19
20
# File 'lib/game_machine/bot/client.rb', line 9

def self.start(name,port)
  Actor::Builder.new(self).with_name(name).start
  client = start_udt_client(name,port)
  master_ref = self.find(name)
  master_ref.tell(client)
  ctx = nil
  loop do
   ctx = master_ref.ask('ctx',10)
   break if ctx
  end
  master_ref.tell('start_players')
end

.start_udt_client(name, port) ⇒ Object



5
6
7
# File 'lib/game_machine/bot/client.rb', line 5

def self.start_udt_client(name,port)
  UdtClient.start(name,'localhost',port)
end

Instance Method Details

#on_receive(message) ⇒ Object



33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
# File 'lib/game_machine/bot/client.rb', line 33

def on_receive(message)
  if message.is_a?(JavaLib::DefaultChannelHandlerContext)
    @ctx = message
  elsif message.is_a?(UdtClient)
    @client = message
  elsif message == 'ctx'
    sender.tell(@ctx) if @ctx
  elsif message == 'start_players'
    GameMachine.logger.info "#{@player_id} started"
    #start_player
    player_id = rand(10000).to_s
    m = GameMachine::Helpers::GameMessage.new(player_id)
    m.player_logout
    @client.send_to_server(m.to_byte_array,@ctx)
  else
    client_message = ClientMessage.parse_from(message.bytes)
    #@player.tell(client_message,get_self)
  end
end

#post_init(*args) ⇒ Object



22
23
24
25
# File 'lib/game_machine/bot/client.rb', line 22

def post_init(*args)
  @client = nil
  @ctx = nil
end

#start_playerObject



27
28
29
30
31
# File 'lib/game_machine/bot/client.rb', line 27

def start_player
  name = "player#{@player_id}"
  child = Actor::Builder.new(Bot::Chat,@client,@ctx,name,@player_id).with_parent(context).with_name(name).start
  @player = child
end