def parse_args_and_start_reproducer(): parser = OptionParser() parser.add_option('-o', '--online_log', type='string', help='Tenhou log with specified player and round number. ' 'Example: http://tenhou.net/0/?log=2017041516gm-0089-0000-23b4752d&tw=3&ts=2') parser.add_option('-l', '--local_log', type='string', help='Path to local log file') parser.add_option('-d', '--dry_run', action='store_true', default=False, help='Special option for tenhou log reproducer. ' 'If true, it will print all available tags in the round') parser.add_option('-t', '--tag', type='string', help='Special option for tenhou log reproducer. It indicates where to stop parse round tags') opts, _ = parser.parse_args() if not opts.online_log and not opts.local_log: print('Please, set -o or -l option') return if opts.online_log and not opts.dry_run and not opts.tag: print('Please, set -t for real run of the online log') return if opts.online_log: if '?' not in opts.online_log and '&' not in opts.online_log: print('Wrong tenhou log format, please provide log link with player position and round number') return reproducer = TenhouLogReproducer(opts.online_log, opts.tag) reproducer.reproduce(opts.dry_run) else: set_up_logging() client = TenhouClient(SocketMock(opts.local_log)) try: client.connect() client.authenticate() client.start_game() except (Exception, KeyboardInterrupt) as e: logger.exception('', exc_info=e) client.end_game()
def parse_args_and_start_reproducer(): parser = OptionParser() parser.add_option('-o', '--online_log', type='string', help='Tenhou log with specified player and round number. ' 'Example: http://tenhou.net/0/?log=2017041516gm-0089-0000-23b4752d&tw=3&ts=2') parser.add_option('-l', '--local_log', type='string', help='Path to local log file') parser.add_option('-d', '--dry_run', action='store_true', default=False, help='Special option for tenhou log reproducer. ' 'If true, it will print all available tags in the round') parser.add_option('-t', '--tag', type='string', help='Special option for tenhou log reproducer. It indicates where to stop parse round tags') opts, _ = parser.parse_args() if not opts.online_log and not opts.local_log: print('Please, set -o or -l option') return if opts.online_log and not opts.dry_run and not opts.tag: print('Please, set -t for real run of the online log') return if opts.online_log: if '?' not in opts.online_log and '&' not in opts.online_log: print('Wrong tenhou log format, please provide log link with player position and round number') return reproducer = TenhouLogReproducer(opts.online_log, opts.tag) reproducer.reproduce(opts.dry_run) else: set_up_logging(save_to_file=False) client = TenhouClient(SocketMock(opts.local_log)) try: client.connect() client.authenticate() client.start_game() except (Exception, KeyboardInterrupt) as e: logger.exception('', exc_info=e) client.end_game()
def connect_and_play(): logger.info('Bot AI enabled: {}'.format(settings.ENABLE_AI)) client = TenhouClient() s = socket.socket(socket.AF_INET, socket.SOCK_STREAM) s.connect((settings.TENHOU_HOST, settings.TENHOU_PORT)) client.setSocketObject(s) try: was_auth = client.authenticate() if was_auth: client.start_game() else: client.end_game() except KeyboardInterrupt: logger.info('Ending the game... can take 15 seconds') client.end_game()
def connect_and_play(): logger.info('Bot AI enabled: {}'.format(settings.ENABLE_AI)) client = TenhouClient() client.connect() try: was_auth = client.authenticate() if was_auth: client.start_game() else: client.end_game() except KeyboardInterrupt: logger.info('Ending the game...') client.end_game() except Exception as e: logger.exception('Unexpected exception', exc_info=e) logger.info('Ending the game...') client.end_game(False)
def connect_and_play(): logger = set_up_logging() client = TenhouClient(logger) client.connect() try: was_auth = client.authenticate() if was_auth: client.start_game() else: client.end_game() except KeyboardInterrupt: logger.info("Ending the game...") client.end_game() except Exception as e: logger.exception("Unexpected exception", exc_info=e) logger.info("Ending the game...") client.end_game(False)
def connect_and_play(): logger.info('AI: {}, {}'.format(settings.AI_CLASS.version, settings.AI_PACKAGE)) client = TenhouClient() client.connect() try: was_auth = client.authenticate() if was_auth: client.start_game() else: client.end_game() except KeyboardInterrupt: logger.info('Ending the game...') client.end_game() except Exception as e: logger.exception('Unexpected exception', exc_info=e) logger.info('Ending the game...') client.end_game(False)
def run(self): logger = set_up_logging() client = TenhouClient(logger, bot_config=self.bot_config) for _ in range(self.opt.num_games): client.connect() try: was_auth = client.authenticate() if was_auth: client.start_game() else: client.end_game() except KeyboardInterrupt: logger.info("Ending the game...") client.end_game() except Exception as e: logger.exception("Unexpected exception", exc_info=e) logger.info("Ending the game...") client.end_game(False) client.table.player.ai.write_buffer()
class TenhouClientTestCase(unittest.TestCase): def setUp(self): self.client = None def tearDown(self): self.client.end_game(False) def test_fixed_crash_after_called_kan(self): log = """ Get: <HELO uname="Name" auth="20170415-1111111" /> Get: <LN/> Get: <GO type="137" lobby="0" gpid=""/> <UN n0="1" n1="2" n2="3" n3="4" dan="11,12,13,11" rate="1500,1500,1500,1500" sx="M,M,M,M"/> <TAIKYOKU oya="3" log="123"/> Get: <INIT seed="6,2,2,5,0,37" ten="203,96,474,207" oya="1" hai="90,83,14,33,132,119,129,117,26,52,121,134,29"/> <U/> Get: <E32/> <V/> Get: <F108/> <W/> Get: <G55/> <T89/> Get: <D52/> <U/> Get: <E109/> <V/> Get: <F124/> <W/> Get: <G92/> Get: <T77/> Get: <D121/> <U/> Get: <E125/> <V/> Get: <f123/> <W/> Get: <G10/> <T38/> Get: <D129/> <U/> Get: <e0/> <V/> Get: <F130/> <W/> Get: <G15/> <T42/> Get: <D14/> <U/> Get: <E64/> Get: <V/> Get: <f115/> Get: <N who="3" m="44075" /> Get: <G8/> <T5/> Get: <D5/> <U/> Get: <E70/> Get: <V/> Get: <f80/> <W/> Get: <g111/> <T68/> Get: <D68/> <U/> Get: <E113/> <V/> Get: <F21/> <W/> Get: <G128/> <T78/> Get: <D38/> <U/> Get: <E82/> <V/> Get: <f81/> <W/> Get: <g6/> <T116/> Get: <D42/> Get: <N who="1" m="15915" /> Get: <E50/> <V/> Get: <F3/> <W/> Get: <G135 t="1"/> Get: <N who="0" m="51755" /> Get: <D77/> <U/> Get: <E103/> Get: <V/> Get: <f76/> <W/> Get: <G118 t="3"/> Get: <N who="0" m="30211" /> <T39/> """ self.client = TenhouClient(SocketMock(None, log)) with self.assertRaises(KeyboardInterrupt) as context: self.client.connect() self.client.authenticate() self.client.start_game() # close all threads self.client.end_game() # end of commands is correct way to end log reproducing self.assertTrue('End of commands' in str(context.exception))