acpc_table_manager 4.0.1 → 4.0.2

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: ee2dea44e92ca39a540609c2223658a34b6bbd6c
4
- data.tar.gz: 7fc6b9d7bf0f79d06b20be90d974761b25e385f3
3
+ metadata.gz: 7369e948b3edcb36e35deeb8f0da2efe2fee95f5
4
+ data.tar.gz: 9028307a76384a8ea83e04330eb9df49f8e4d389
5
5
  SHA512:
6
- metadata.gz: 2de517e6ac65237cf51bee818d81f348492d6cc6dfbdf4e3615dac72b637a6a20d117576beb494e7595daebb8c612a10fd2b6e2eecb33510e31aede98d8d0a0c
7
- data.tar.gz: 502d992178265dced2830a82c6f414865b9c1daa598ec0ce370539296e2e5a649ae8bb516d25b9a351ad50e1531ae735b85dc4bca6278725d97c769674643eed
6
+ metadata.gz: 64e6e2748b7d4d740f4d7fa3ce7c7a2931619a8351afa56cf461372dbb51c6c0e54d4dad3f0b67bd504eb03c411acc9589d2fadf0c8ca21b5179353393b231f9
7
+ data.tar.gz: 7de81c3455a554101da0e3a5577dfe8b4a0756baa7f9003159e7c17070292306631ff7a9b9733a9ea37d2084968bb3936d7900b5bb4e5d919015fcf6849b2524
@@ -4,8 +4,6 @@ require 'acpc_table_manager'
4
4
  require 'json'
5
5
  require 'optparse'
6
6
 
7
- include AcpcTableManager::SimpleLogging
8
-
9
7
  ARGV << '-h' if ARGV.empty?
10
8
 
11
9
  options = {}
@@ -49,156 +47,8 @@ CONFIG_FILE = options[:table_manager_config]
49
47
 
50
48
  AcpcTableManager.load! CONFIG_FILE
51
49
 
52
- game_info = AcpcTableManager.exhibition_config.games[options[:game]]
53
- unless game_info
54
- raise OptionParser::ArgumentError.new(
55
- "\"#{options[:game]}\" is not a recognized game. Registered games: #{AcpcTableManager.exhibition_config.games.keys}."
56
- )
57
- end
58
-
59
- Signal.trap("INT") { exit_and_del_saved }
60
- Signal.trap("TERM") { exit_and_del_saved }
61
-
62
- must_send_ready = AcpcTableManager.config.must_send_ready
63
-
64
- @logger = AcpcTableManager.new_log(
65
- "#{options[:id]}.log",
66
- File.join(AcpcTableManager.config.log_directory, 'proxies')
67
- )
68
- include AcpcTableManager::ProxyUtils
50
+ Signal.trap("INT") { exit }
51
+ Signal.trap("TERM") { exit }
69
52
 
70
- @communicator = AcpcTableManager::ProxyCommunicator.new(options[:id])
71
- @communicator.del_saved # Clear stale messages to avoid unpredictable behaviour
72
- @state_index = 0
73
-
74
- last_message_received = Time.now
75
-
76
- begin
77
- log(
78
- __method__,
79
- options: options,
80
- version: AcpcTableManager::VERSION,
81
- send_channel: @communicator.send_channel,
82
- receive_channel: @communicator.receive_channel,
83
- must_send_ready: must_send_ready
84
- )
85
-
86
- proxy = start_proxy(
87
- game_info,
88
- options[:seat],
89
- options[:port],
90
- must_send_ready
91
- ) do |patt|
92
- log 'start_proxy_block', match_state: patt.match_state.to_s
93
- @communicator.publish(
94
- AcpcTableManager::ProxyUtils.players_at_the_table_to_json(
95
- patt,
96
- game_info['num_hands_per_match'],
97
- state_index
98
- )
99
- )
100
- state_index += 1
101
- end
102
-
103
- log 'starting event loop'
104
-
105
- loop do
106
- begin
107
- @communicator.subscribe_with_timeout do |data|
108
- log __method__, data: data
109
-
110
- if data['resend']
111
- log __method__, msg: 'Resending match state'
112
- @communicator.publish(
113
- AcpcTableManager::ProxyUtils.players_at_the_table_to_json(
114
- proxy,
115
- game_info['num_hands_per_match'],
116
- @state_index
117
- )
118
- )
119
- @state_index += 1
120
- elsif data['kill']
121
- log __method__, msg: 'Exiting'
122
- exit_and_del_saved
123
- else
124
- action = data['action']
125
- if action == 'next-hand'
126
- proxy.next_hand! do |patt|
127
- log 'next_hand! block', match_state: patt.match_state.to_s
128
- @communicator.publish(
129
- AcpcTableManager::ProxyUtils.players_at_the_table_to_json(
130
- patt,
131
- game_info['num_hands_per_match'],
132
- @state_index
133
- )
134
- )
135
- @state_index += 1
136
- end
137
-
138
- log(
139
- 'after next_hand!',
140
- users_turn_to_act?: proxy.users_turn_to_act?,
141
- match_ended?: proxy.match_ended?(game_info['num_hands_per_match'])
142
- )
143
- else
144
- log 'before acting', users_turn_to_act?: proxy.users_turn_to_act?,
145
- action: action
146
-
147
- if proxy.users_turn_to_act?
148
- action = PokerAction.new(action) unless action.is_a?(PokerAction)
149
- proxy.play!(action) do |patt|
150
- log 'play! block', match_state: patt.match_state.to_s
151
- @communicator.publish(
152
- AcpcTableManager::ProxyUtils.players_at_the_table_to_json(
153
- patt,
154
- game_info['num_hands_per_match'],
155
- @state_index
156
- )
157
- )
158
- @state_index += 1
159
- end
160
-
161
- log(
162
- 'after play!',
163
- users_turn_to_act?: proxy.users_turn_to_act?,
164
- match_ended?: proxy.match_ended?(game_info['num_hands_per_match'])
165
- )
166
- end
167
- end
168
- end
169
- exit_and_del_saved if proxy.match_ended?(game_info['num_hands_per_match'])
170
- last_message_received = Time.now
171
- end
172
- rescue AcpcTableManager::SubscribeTimeout
173
- if proxy.match_ended? game_info['num_hands_per_match']
174
- exit_and_del_saved
175
- elsif !proxy.users_turn_to_act?
176
- last_message_received = Time.now
177
- elsif (
178
- AcpcTableManager.config.proxy_timeout_s && (
179
- Time.now > (
180
- last_message_received + AcpcTableManager.config.proxy_timeout_s
181
- )
182
- )
183
- )
184
- if AcpcTableManager.config.on_proxy_timeout == 'fold'
185
- play_check_fold! proxy
186
- else
187
- exit_and_del_saved
188
- end
189
- end
190
- end
191
- end
192
- rescue => e
193
- log(
194
- __method__,
195
- {
196
- id: options[:id],
197
- message: e.message,
198
- backtrace: e.backtrace
199
- },
200
- Logger::Severity::ERROR
201
- )
202
- AcpcTableManager.notify e # Send an email notification
203
- end
204
- exit_and_del_saved
53
+ app = AcpcTableManager::AcpcProxy.new(options[:id], options[:game])
54
+ app.start(options[:seat], options[:port])
@@ -14,6 +14,7 @@ require_relative 'acpc_table_manager/monkey_patches'
14
14
  require_relative 'acpc_table_manager/simple_logging'
15
15
  require_relative 'acpc_table_manager/utils'
16
16
  require_relative 'acpc_table_manager/proxy_utils'
17
+ require_relative 'acpc_table_manager/acpc_proxy'
17
18
 
18
19
  using AcpcTableManager::SimpleLogging::MessageFormatting
19
20
 
@@ -0,0 +1,204 @@
1
+ require 'optparse'
2
+ require 'acpc_table_manager/simple_logging'
3
+ require 'acpc_table_manager/proxy_utils'
4
+
5
+ module AcpcTableManager
6
+ class AcpcProxy
7
+ include AcpcTableManager::SimpleLogging
8
+ include AcpcTableManager::ProxyUtils
9
+
10
+ attr_reader :game, :id, :game_info, :state_index, :last_message_received
11
+
12
+ def initialize(id, game)
13
+ @game = game
14
+ @id = id
15
+ @game_info = AcpcTableManager.exhibition_config.games[game]
16
+ unless @game_info
17
+ raise OptionParser::ArgumentError.new(
18
+ "\"#{game}\" is not a recognized game. Registered games: #{AcpcTableManager.exhibition_config.games.keys}."
19
+ )
20
+ end
21
+
22
+ @logger = AcpcTableManager.new_log(
23
+ "#{id}.log",
24
+ File.join(AcpcTableManager.config.log_directory, 'proxies')
25
+ )
26
+
27
+ @communicator = AcpcTableManager::ProxyCommunicator.new(id)
28
+ @communicator.del_saved # Clear stale messages to avoid unpredictable behaviour
29
+ @state_index = 0
30
+ @last_message_received = Time.now
31
+ end
32
+
33
+ def must_send_ready() AcpcTableManager.config.must_send_ready end
34
+
35
+ def num_hands_per_match() @game_info['num_hands_per_match'] end
36
+
37
+ def play(action)
38
+ action = PokerAction.new(action) unless action.is_a?(PokerAction)
39
+
40
+ @proxy.play!(action) do |patt|
41
+ log 'play! block', match_state: patt.match_state.to_s
42
+ @communicator.publish(
43
+ AcpcTableManager::ProxyUtils.players_at_the_table_to_json(
44
+ patt,
45
+ num_hands_per_match,
46
+ @state_index
47
+ )
48
+ )
49
+ @state_index += 1
50
+ end
51
+ end
52
+
53
+ def act(action)
54
+ if action == 'next-hand'
55
+ @proxy.next_hand! do |patt|
56
+ log 'next_hand! block', match_state: patt.match_state.to_s
57
+ @communicator.publish(
58
+ AcpcTableManager::ProxyUtils.players_at_the_table_to_json(
59
+ patt,
60
+ num_hands_per_match,
61
+ @state_index
62
+ )
63
+ )
64
+ @state_index += 1
65
+ end
66
+
67
+ log(
68
+ 'after next_hand!',
69
+ users_turn_to_act?: @proxy.users_turn_to_act?,
70
+ match_over?: match_over?
71
+ )
72
+ else
73
+ log 'before play', users_turn_to_act?: @proxy.users_turn_to_act?,
74
+ action: action
75
+
76
+ if @proxy.users_turn_to_act?
77
+ play action
78
+
79
+ log(
80
+ 'after play',
81
+ users_turn_to_act?: @proxy.users_turn_to_act?,
82
+ match_over?: match_over?
83
+ )
84
+ else
85
+ log 'skipped play'
86
+ end
87
+ end
88
+ end
89
+
90
+ def match_over?
91
+ @proxy.match_ended?(num_hands_per_match) ||
92
+ !@proxy.connected?
93
+ end
94
+
95
+ def message_loop
96
+ @communicator.subscribe_with_timeout do |data|
97
+ log __method__, data: data
98
+
99
+ if data['resend']
100
+ log __method__, msg: 'Resending match state'
101
+ @communicator.publish(
102
+ AcpcTableManager::ProxyUtils.players_at_the_table_to_json(
103
+ @proxy,
104
+ num_hands_per_match,
105
+ @state_index
106
+ )
107
+ )
108
+ @state_index += 1
109
+ elsif data['kill']
110
+ log __method__, msg: 'Exiting'
111
+ exit_and_del_saved
112
+ else
113
+ act data['action']
114
+ end
115
+ exit_and_del_saved if match_over?
116
+ @last_message_received = Time.now
117
+ end
118
+ end
119
+
120
+ def action_timeout_reached
121
+ AcpcTableManager.config.proxy_timeout_s && (
122
+ Time.now > (
123
+ @last_message_received + AcpcTableManager.config.proxy_timeout_s
124
+ )
125
+ )
126
+ end
127
+
128
+ def start(seat, port)
129
+ begin
130
+ log(
131
+ __method__,
132
+ id: @id,
133
+ game: @game,
134
+ seat: seat,
135
+ port: port,
136
+ version: AcpcTableManager::VERSION,
137
+ send_channel: @communicator.send_channel,
138
+ receive_channel: @communicator.receive_channel,
139
+ must_send_ready: must_send_ready
140
+ )
141
+
142
+ @proxy = start_proxy(
143
+ @game_info,
144
+ seat,
145
+ port,
146
+ must_send_ready
147
+ ) do |patt|
148
+ log 'start_proxy_block', match_state: patt.match_state.to_s
149
+ @communicator.publish(
150
+ AcpcTableManager::ProxyUtils.players_at_the_table_to_json(
151
+ patt,
152
+ num_hands_per_match,
153
+ @state_index
154
+ )
155
+ )
156
+ @state_index += 1
157
+ end
158
+
159
+ log 'starting event loop'
160
+
161
+ loop do
162
+ begin
163
+ message_loop
164
+ rescue AcpcTableManager::SubscribeTimeout
165
+ match_is_over = match_over?
166
+
167
+ log(
168
+ 'subscription timeout reached',
169
+ {
170
+ match_over?: match_is_over,
171
+ users_turn_to_act?: @proxy.users_turn_to_act?,
172
+ action_timeout_reached: action_timeout_reached,
173
+ on_proxy_timeout: AcpcTableManager.config.on_proxy_timeout
174
+ }
175
+ )
176
+ if match_is_over
177
+ exit_and_del_saved
178
+ elsif !@proxy.users_turn_to_act?
179
+ @last_message_received = Time.now
180
+ elsif action_timeout_reached
181
+ if AcpcTableManager.config.on_proxy_timeout == 'fold'
182
+ play_check_fold! @proxy
183
+ else
184
+ exit_and_del_saved
185
+ end
186
+ end
187
+ end
188
+ end
189
+ rescue => e
190
+ log(
191
+ __method__,
192
+ {
193
+ id: @id,
194
+ message: e.message,
195
+ backtrace: e.backtrace
196
+ },
197
+ Logger::Severity::ERROR
198
+ )
199
+ AcpcTableManager.notify e # Send an email notification
200
+ end
201
+ exit_and_del_saved
202
+ end
203
+ end
204
+ end
@@ -1,3 +1,3 @@
1
1
  module AcpcTableManager
2
- VERSION = '4.0.1'.freeze
2
+ VERSION = '4.0.2'.freeze
3
3
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: acpc_table_manager
3
3
  version: !ruby/object:Gem::Version
4
- version: 4.0.1
4
+ version: 4.0.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - Dustin Morrill
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2017-02-05 00:00:00.000000000 Z
11
+ date: 2017-02-22 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: pony
@@ -217,6 +217,7 @@ files:
217
217
  - exe/acpc_table_manager
218
218
  - exe/acpc_testing_bot
219
219
  - lib/acpc_table_manager.rb
220
+ - lib/acpc_table_manager/acpc_proxy.rb
220
221
  - lib/acpc_table_manager/config.rb
221
222
  - lib/acpc_table_manager/match.rb
222
223
  - lib/acpc_table_manager/monkey_patches.rb