def register(): form = RegisterForm() if form.validate_on_submit(): user = User(form.nickname.data, form.email.data, form.password.data) db.add(user) flash('Thanks for registering!') return redirect(url_for('login')) elif request.method == "POST": flash('Registering failed. Please supply all information', 'error') return render_template("users/register.html", form=form, error=form.errors) return render_template('users/register.html', form=form)
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) # Saves match object, but also adds an ID so we know where to save the # log of the match to. db.expunge(match) access.add(match) # Store the log file to match.log_path tmp_logfile.seek(0) match.save_log(tmp_logfile.read())
def add_bot(user, form): # Save code to <BOT_CODE_DIR>/<user>/<botname>/<codename> files = request.files.getlist('files') parent = p.join(config.BOT_CODE_DIR, user.nickname, form.botname.data) make_files(files, parent) bot = Bot(user=user, name=form.botname.data, compile_cmd=form.compile_cmd.data, run_cmd=form.run_cmd.data, compiled=False if form.compile_cmd.data else True) db.add(bot)
def add_bot(user, form): # Save code to <BOT_CODE_DIR>/<user>/<botname>/<codename> files = request.files.getlist('files') parent = p.join(config.BOT_CODE_DIR, user.nickname, form.botname.data) make_files(files, parent) bot = Bot( user=user, name=form.botname.data, compile_cmd=form.compile_cmd.data, run_cmd=form.run_cmd.data, compiled=False if form.compile_cmd.data else True ) db.add(bot)