def battle(loop): bots = find_participants() bot_map = {bot.safe_full_name: bot for bot in bots} logging.info('Starting compilation') # TODO compile async compilation_success = all(bot.compile() for bot in bots) if not compilation_success: # Save errors on bots access.add_all(bots) logging.warning('Compilation failed') return logging.info('Compilation done') logging.info('Starting graph generation') graph = generate_graph(bot_map.keys()) logging.info('Graph generated: %s', ' | '.join(graph)) with tempfile.TemporaryFile('w+') as tmp_logfile: cmd_map = { name: bot.sandboxed_run_cmd for name, bot in bot_map.items() } game = arbiter.Game(cmd_map, graph, MAX_STEPS, tmp_logfile) player_map = game.players.copy() logging.info('Starting match') with Timed() as timings: loop.run_until_complete(game.play_async()) logging.info('Stopping match') winner = game.winner() if winner: winner = bot_map.get(winner.name) logging.info('{} won'.format(winner) if winner else 'Draw') # Save match outcome to database match = Match(winner=winner, start_time=timings.start_time, end_time=timings.end_time) for name, bot in bot_map.items(): warnings = '\n'.join(player_map[name].warnings) participation = MatchParticipation(bot=bot, errors=warnings) match.participations.append(participation) try: # Saves match object, but also adds an ID so we know where to save # the log of the match to. db.add(match) db.commit() # Store the log file to match.log_path tmp_logfile.seek(0) match.save_log(tmp_logfile.read()) except: logging.exception("Database had to do a rollback.") db.rollback() raise
def battle(loop): bots = find_participants() bot_map = {bot.safe_full_name: bot for bot in bots} logging.info('Starting compilation') # TODO compile async compilation_success = all(bot.compile() for bot in bots) if not compilation_success: # Save errors on bots access.add_all(bots) logging.warning('Compilation failed') return logging.info('Compilation done') logging.info('Starting graph generation') graph = generate_graph(bot_map.keys()) logging.info('Graph generated: %s', ' | '.join(graph)) with tempfile.TemporaryFile('w+') as tmp_logfile: cmd_map = {name: bot.sandboxed_run_cmd for name, bot in bot_map.items()} game = arbiter.Game(cmd_map, graph, MAX_STEPS, tmp_logfile) player_map = game.players.copy() logging.info('Starting match') with Timed() as timings: loop.run_until_complete(game.play_async()) logging.info('Stopping match') winner = game.winner() if winner: winner = bot_map.get(winner.name) logging.info('{} won'.format(winner) if winner else 'Draw') # Save match outcome to database match = Match(winner=winner, start_time=timings.start_time, end_time=timings.end_time) for name, bot in bot_map.items(): warnings = '\n'.join(player_map[name].warnings) participation = MatchParticipation(bot=bot, errors=warnings) match.participations.append(participation) try: # Saves match object, but also adds an ID so we know where to save # the log of the match to. db.add(match) db.commit() # Store the log file to match.log_path tmp_logfile.seek(0) match.save_log(tmp_logfile.read()) except: logging.exception("Database had to do a rollback.") db.rollback() raise
import datetime as d from battlebots.database import session from battlebots.database.models import User, Bot, Match, MatchParticipation user1 = User('tester1', '*****@*****.**', 'test_pw_1') user2 = User('tester2', '*****@*****.**', 'test_pw_2') bot1 = Bot(name='bot1', user=user1, compile_cmd='', run_cmd='', score=1000) bot2 = Bot(name='bot2', user=user2, compile_cmd='', run_cmd='', score=1100) match1 = Match(winner=bot1, start_time=d.datetime.today(), end_time=d.datetime.today()) mp1 = MatchParticipation(match=match1, bot=bot1, errors="test error 1") mp2 = MatchParticipation(match=match1, bot=bot2) l = [user1, user2, bot1, bot1, match1] for x in l: session.add(x) session.commit()