예제 #1
0
def home():
    x = random.randint(-100, 100)
    y = random.randint(-100, 100)
    statement_str = generate_statement_string(2)
    tree = BinTree.build_tree(statement_str)
    statement_result = BinTree.solve_tree(tree, x, y)
    form = forms.TrueOrFalseForm(flask.request.form)
    if form.validate_on_submit():
        if not flask_login.current_user.is_anonymous:
            if form.choice.data == form.hidden.data:
                with db.create_connection() as connection, connection.cursor() as cursor:
                    sql = "UPDATE users SET score = score + 2 WHERE id = %s"
                    cursor.execute(sql, flask_login.current_user.get_id())
                    connection.commit()
                    flask.flash('Correct! +2 points!', 'success')
            else:
                with db.create_connection() as connection, connection.cursor() as cursor:
                    sql = "UPDATE users SET score = score - 1 WHERE id = %s"
                    cursor.execute(sql, flask_login.current_user.primary_id)
                    connection.commit()
                    flask.flash('Incorrect! -1 points!', 'error')
        else:
            if form.choice.data == form.hidden.data:
                flask.flash('Correct!', 'success')
            else:
                flask.flash('Incorrect!', 'error')
    elif form.errors:
        for item in form.errors.items():
            flask.flash(item, 'error')
    return flask.render_template('home.html', x_value=str(x), y_value=str(y), statement=statement_str,
                                 result=str(statement_result), form=form)
예제 #2
0
def tester():
    form = forms.StatementForm()
    if form.validate_on_submit():
        try:
            solution = BinTree.solve_tree(BinTree.build_tree(form.statement.data), 0, 0)
            if type(solution) is not bool:
                raise ValueError
            flask.flash('Given statement is {0}, solution is {1}'.format(form.statement.data, str(solution)))
        except (ValueError, IndexError):
            flask.flash('Invalid statement!', 'error')
    elif form.errors:
        for item in form.errors.items():
            flask.flash(item, 'error')
    return flask.render_template('tester.html', form=form)
예제 #3
0
def home():
    x = random.randint(-100, 100)
    y = random.randint(-100, 100)
    if flask_login.current_user.is_anonymous:
        statement_str = generate_statement_string(2)
    else:
        statement_str = generate_statement_string(
            flask_login.current_user.difficulty)
    tree = BinTree.build_tree(statement_str)
    statement_result = BinTree.solve_tree(tree, x, y)
    form = forms.TrueOrFalseForm(flask.request.form)
    if form.validate_on_submit():
        if not flask_login.current_user.is_anonymous:
            difficulty = flask_login.current_user.difficulty
            if form.choice.data == form.hidden.data:
                with db.create_connection() as connection, connection.cursor(
                ) as cursor:
                    sql = "UPDATE users SET score = score + %s WHERE id = %s"
                    cursor.execute(
                        sql, (difficulty, flask_login.current_user.primary_id))
                    connection.commit()
                    flask.flash('Correct! +{0} points!'.format(difficulty),
                                'success')
            else:
                with db.create_connection() as connection, connection.cursor(
                ) as cursor:
                    sql = "UPDATE users SET score = score - %s WHERE id = %s"
                    cursor.execute(
                        sql,
                        (difficulty / 2, flask_login.current_user.primary_id))
                    connection.commit()
                    flask.flash(
                        'Incorrect! -{0} points!'.format(difficulty / 2),
                        'error')
        else:
            if form.choice.data == form.hidden.data:
                flask.flash('Correct!', 'success')
            else:
                flask.flash('Incorrect!', 'error')
    elif form.errors:
        for item in form.errors.items():
            flask.flash(item, 'error')
    return flask.render_template('home.html',
                                 x_value=str(x),
                                 y_value=str(y),
                                 statement=statement_str,
                                 result=str(statement_result),
                                 form=form)
예제 #4
0
def tester():
    form = forms.StatementForm()
    if form.validate_on_submit():
        try:
            solution = BinTree.solve_tree(
                BinTree.build_tree(form.statement.data), 0, 0)
            if type(solution) is not bool:
                raise ValueError
            flask.flash('Given statement is {0}, solution is {1}'.format(
                form.statement.data, str(solution)))
        except (ValueError, IndexError):
            flask.flash('Invalid statement!', 'error')
    elif form.errors:
        for item in form.errors.items():
            flask.flash(item, 'error')
    return flask.render_template('tester.html', form=form)