Class: Mjai::TCPPlayer

Inherits:
Player
  • Object
show all
Defined in:
lib/mjai/tcp_player.rb

Constant Summary collapse

TIMEOUT_SEC =
60

Instance Attribute Summary

Attributes inherited from Player

#attributes, #extra_anpais, #furos, #game, #ho, #id, #name, #reach_ho_index, #reach_state, #score, #sutehais, #tehais

Instance Method Summary collapse

Methods inherited from Player

#anpais, #can_hora?, #can_reach?, #can_ryukyoku?, #context, #create_action, #delete_tehai, #double_reach?, #furiten?, #get_pais_combinations, #inspect, #ippatsu_chance?, #jikaze, #kuikae_dahais, #possible_actions, #possible_dahais, #possible_dahais_after_furo, #possible_furo_actions, #rank, #reach?, #rinshan?, #tenpai?, #update_state

Constructor Details

#initialize(socket, name) ⇒ TCPPlayer

Returns a new instance of TCPPlayer.



14
15
16
17
18
# File 'lib/mjai/tcp_player.rb', line 14

def initialize(socket, name)
  super()
  @socket = socket
  self.name = name
end

Instance Method Details

#closeObject



58
59
60
# File 'lib/mjai/tcp_player.rb', line 58

def close()
  @socket.close()
end

#respond_to_action(action) ⇒ Object



20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
# File 'lib/mjai/tcp_player.rb', line 20

def respond_to_action(action)
  
  begin
    
    puts("server -> player %d\t%s" % [self.id, action.to_json()])
    @socket.puts(action.to_json())
    line = nil
    Timeout.timeout(TIMEOUT_SEC) do
      line = @socket.gets()
    end
    if line
      puts("server <- player %d\t%s" % [self.id, line])
      return Action.from_json(line.chomp(), self.game)
    else
      puts("server :  Player %d has disconnected." % self.id)
      return Action.new({:type => :none})
    end
    
  rescue Timeout::Error
    return create_action({
        :type => :error,
        :message => "Timeout. No response in %d sec." % TIMEOUT_SEC,
    })
  rescue JSON::ParserError => ex
    return create_action({
        :type => :error,
        :message => "JSON syntax error: %s" % ex.message,
    })
  rescue ValidationError => ex
    return create_action({
        :type => :error,
        :message => ex.message,
    })
    
  end
  
end