goshrine_bot 0.1.10 → 0.1.11

Sign up to get free protection for your applications and to get access to all the features.
@@ -7,7 +7,7 @@ require 'logger'
7
7
 
8
8
  module GoshrineBot
9
9
 
10
- VERSION = "0.1.10"
10
+ VERSION = "0.1.11"
11
11
 
12
12
  STDOUT.sync = true
13
13
 
@@ -1,3 +1,5 @@
1
+ require 'uri'
2
+
1
3
  module GoshrineBot
2
4
 
3
5
  class Client
@@ -102,11 +104,13 @@ module GoshrineBot
102
104
  end
103
105
  subscribe
104
106
  load_existing_games {
105
- EM::add_periodic_timer( 60 ) {
106
- @games.each do |token, game|
107
- game.idle_check
108
- end
109
- }
107
+ if @options[:idle_shutdown_timeout] > 0
108
+ EM::add_periodic_timer( @options[:idle_shutdown_timeout] ) {
109
+ @games.each do |token, game|
110
+ game.idle_check(@options[:idle_shutdown_timeout])
111
+ end
112
+ }
113
+ end
110
114
  }
111
115
  }
112
116
  end
@@ -152,14 +156,23 @@ module GoshrineBot
152
156
  end
153
157
 
154
158
  def handle_match_request(request)
159
+ active_games = @games.select { |key, game| game.state == "in-play" }
160
+ max_games = @options[:maximum_concurrent_games]
155
161
  game = GameInProgress.new(self)
156
162
  game.update_from_match_request(request)
157
- http = http_get("/match/accept?id=#{game.challenge_id}")
158
- http.callback {
159
- attrs = JSON.parse(http.response)
160
- game.update_from_game_list(attrs)
161
- add_game(game)
162
- }
163
+ if max_games.nil? || active_games.count < max_games
164
+ http = http_get("/match/accept?id=#{game.challenge_id}")
165
+ http.callback {
166
+ attrs = JSON.parse(http.response)
167
+ game.update_from_game_list(attrs)
168
+ add_game(game)
169
+ }
170
+ else
171
+ count = max_games > 1 ? "#{max_games} games" : "1 game"
172
+ reason = "#{@login} only plays #{count} at a time."
173
+ puts "Rejecting match: #{reason}"
174
+ http = http_get("/match/reject?id=#{game.challenge_id}&reason=#{URI.escape(reason)}")
175
+ end
163
176
  end
164
177
 
165
178
  end
@@ -39,6 +39,7 @@ module GoshrineBot
39
39
  #puts "Going to update board"
40
40
  when 'resignedBy'
41
41
  puts "Game resigned by opponent."
42
+ self.state = "finished"
42
43
  stop_gtp_client
43
44
  when 'updateForUndo'
44
45
  # TODO - handle undos?
@@ -49,6 +50,7 @@ module GoshrineBot
49
50
  # ignore
50
51
  when 'gameFinished'
51
52
  stop_gtp_client
53
+ self.state = "finished"
52
54
  if m['data'].nil?
53
55
  puts "Missing data: #{m.inspect}"
54
56
  return
@@ -89,9 +91,9 @@ module GoshrineBot
89
91
  end
90
92
  end
91
93
 
92
- def idle_check
93
- if @last_gtp_access && @last_gtp_access + 60 < Time.now
94
- puts "Shutting down gtp client #{self.token} for idle."
94
+ def idle_check(timeout)
95
+ if timeout > 0 && @last_gtp_access && @last_gtp_access + timeout < Time.now
96
+ puts "Shutting down gtp client #{self.token} after #{timeout} seconds idle."
95
97
  stop_gtp_client
96
98
  end
97
99
  end
@@ -197,13 +199,13 @@ module GoshrineBot
197
199
  gtp = GtpStdioClient.new(@client.gtp_cmd_line, "gtp_#{token}.log")
198
200
  gtp.boardsize(@board_size)
199
201
  gtp.clear_board
200
- #if @handicap > 1
201
- # gtp.fixed_handicap(@handicap)
202
- #end
203
- @handicap_stones.each do |s|
204
- puts "Placing handicap stone at #{s}"
205
- gtp.play('B', sgf_coord_to_gtp_coord(s, board_size))
202
+ gtp.komi(@komi)
203
+
204
+ if @handicap_stones.size > 0
205
+ gtp_coords = @handicap_stones.map {|s| sgf_coord_to_gtp_coord(s, board_size) }
206
+ gtp.set_free_handicap(gtp_coords.join(" "))
206
207
  end
208
+
207
209
  if @moves
208
210
  @moves.each do |m|
209
211
  #puts "Going to play #{m.first}, #{m.last}"
@@ -18,8 +18,9 @@ module GoshrineBot
18
18
  :server_url => "http://goshrine.com/",
19
19
  :gtp_cmd_line => "gnugo --mode gtp",
20
20
  :debug => false,
21
- :pid_path => "./goshrine_bot.pid",
22
- :log_path => "./goshrine_bot.log",
21
+ :idle_shutdown_timeout => 60,
22
+ :maximum_concurrent_games => nil,
23
+ :pid_path => "./goshrine_bot.pid"
23
24
  }
24
25
 
25
26
  cmd_line_options = parse_options
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: goshrine_bot
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.10
4
+ version: 0.1.11
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,11 +9,11 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2011-09-03 00:00:00.000000000Z
12
+ date: 2011-09-04 00:00:00.000000000Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: eventmachine
16
- requirement: &70216509102660 !ruby/object:Gem::Requirement
16
+ requirement: &70281678905860 !ruby/object:Gem::Requirement
17
17
  none: false
18
18
  requirements:
19
19
  - - ! '>='
@@ -21,10 +21,10 @@ dependencies:
21
21
  version: 0.12.10
22
22
  type: :runtime
23
23
  prerelease: false
24
- version_requirements: *70216509102660
24
+ version_requirements: *70281678905860
25
25
  - !ruby/object:Gem::Dependency
26
26
  name: em-http-request
27
- requirement: &70216509101860 !ruby/object:Gem::Requirement
27
+ requirement: &70281678904480 !ruby/object:Gem::Requirement
28
28
  none: false
29
29
  requirements:
30
30
  - - ! '>='
@@ -32,10 +32,10 @@ dependencies:
32
32
  version: 1.0.0.beta.3
33
33
  type: :runtime
34
34
  prerelease: false
35
- version_requirements: *70216509101860
35
+ version_requirements: *70281678904480
36
36
  - !ruby/object:Gem::Dependency
37
37
  name: faye
38
- requirement: &70216509101240 !ruby/object:Gem::Requirement
38
+ requirement: &70281678901700 !ruby/object:Gem::Requirement
39
39
  none: false
40
40
  requirements:
41
41
  - - ! '>='
@@ -43,10 +43,10 @@ dependencies:
43
43
  version: 0.6.0
44
44
  type: :runtime
45
45
  prerelease: false
46
- version_requirements: *70216509101240
46
+ version_requirements: *70281678901700
47
47
  - !ruby/object:Gem::Dependency
48
48
  name: json
49
- requirement: &70216509100540 !ruby/object:Gem::Requirement
49
+ requirement: &70281678900840 !ruby/object:Gem::Requirement
50
50
  none: false
51
51
  requirements:
52
52
  - - ! '>='
@@ -54,10 +54,10 @@ dependencies:
54
54
  version: 1.5.0
55
55
  type: :runtime
56
56
  prerelease: false
57
- version_requirements: *70216509100540
57
+ version_requirements: *70281678900840
58
58
  - !ruby/object:Gem::Dependency
59
59
  name: rspec
60
- requirement: &70216509099520 !ruby/object:Gem::Requirement
60
+ requirement: &70281678899680 !ruby/object:Gem::Requirement
61
61
  none: false
62
62
  requirements:
63
63
  - - ! '>='
@@ -65,7 +65,7 @@ dependencies:
65
65
  version: '0'
66
66
  type: :development
67
67
  prerelease: false
68
- version_requirements: *70216509099520
68
+ version_requirements: *70281678899680
69
69
  description: The GoShrine bot client is a library that allows you connect a local
70
70
  Go playing program that speaks GTP (like gnugo) to http://goshrine.com.
71
71
  email: