Class: Mjai::MjaiCommand

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

Class Method Summary collapse

Class Method Details

.execute(command_name, argv) ⇒ Object



15
16
17
18
19
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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
# File 'lib/mjai/mjai_command.rb', line 15

def self.execute(command_name, argv)
  
  Thread.abort_on_exception = true
  case command_name
    
    when "mjai"
      
      action = argv.shift()
      opts = OptionParser.getopts(argv, "",
          "port:11600", "host:127.0.0.1", "room:default", "game_type:one_kyoku",
          "games:1", "repeat", "log_dir:")
      case action
        when "server"
          $stdout.sync = true
          if opts["repeat"]
            num_games = 1.0/0.0
          else
            num_games = opts["games"].to_i()
          end
          server = TCPActiveGameServer.new({
              :host => opts["host"],
              :port => opts["port"].to_i(),
              :room => opts["room"],
              :game_type => opts["game_type"].intern,
              :player_commands => argv,
              :num_games => num_games,
              :log_dir => opts["log_dir"],
          })
          server.run()
        when "convert"
          FileConverter.new().convert(argv.shift(), argv.shift())
        when "stats"
          GameStats.print(argv)
        else
          $stderr.puts(
              "Usage:\n" +
              "  #{$PROGRAM_NAME} server --port=PORT\n" +
              "  #{$PROGRAM_NAME} server --port=PORT " +
                  "[PLAYER1_COMMAND] [PLAYER2_COMMAND] [...]\n" +
              "  #{$PROGRAM_NAME} stats 1.mjson [2.mjson] [...]\n" +
              "  #{$PROGRAM_NAME} convert hoge.mjson hoge.html\n" +
              "  #{$PROGRAM_NAME} convert hoge.mjlog hoge.mjson\n\n" +
              "See here for details:\n" +
              "http://gimite.net/pukiwiki/index.php?" +
              "Mjai%20%CB%E3%BF%FDAI%C2%D0%C0%EF%A5%B5%A1%BC%A5%D0\n")
          exit(1)
      end
      
    when /^mjai-(.+)$/
      
      $stdout.sync = true
      $stderr.sync = true
      player_type = $1
      opts = OptionParser.getopts(argv, "", "t:", "name:")
      url = ARGV.shift()
      
      if !url
        $stderr.puts(
            "Usage:\n" +
            "  #{$PROGRAM_NAME} mjsonp://localhost:11600/default\n")
        exit(1)
      end
      case player_type
        when "tsumogiri"
          player = TsumogiriPlayer.new()
        when "shanten"
          player = Mjai::ShantenPlayer.new({:use_furo => opts["t"] == "f"})
        else
          raise("should not happen")
      end
      game = TCPClientGame.new({
          :player => player,
          :url => url,
          :name => opts["name"] || player_type,
      })
      game.play()
      
    else
      raise("should not happen")
  
  end
  
end