示例#1
0
def gen_unlock(db, user_id, team_id=None, target=None, type="hints"):
    unlock = Unlocks(user_id=user_id,
                     team_id=team_id,
                     target=target,
                     type=type)
    db.session.add(unlock)
    db.session.commit()
    return unlock
示例#2
0
def hints_view_contest(contestid, hintid):
    contest = Contests.query.filter_by(id=contestid).first()
    if not utils.ctf_started(contest=contest):
        abort(403)
    hint = Hints.query.filter_by(id=hintid).first_or_404()
    chal = Challenges.query.filter_by(id=hint.chal).first()
    unlock = Unlocks.query.filter_by(model='hints',
                                     itemid=hintid,
                                     teamid=session['id']).first()
    if request.method == 'GET':
        if unlock:
            return jsonify({
                'hint': hint.hint,
                'chal': hint.chal,
                'cost': hint.cost
            })
        else:
            return jsonify({'chal': hint.chal, 'cost': hint.cost})
    elif request.method == 'POST':
        if not unlock and utils.ctftime(contest=contest):
            team = Teams.query.filter_by(id=session['id']).first()
            if team.score() < hint.cost:
                return jsonify({'errors': 'Not enough points'})
            unlock = Unlocks(model='hints',
                             teamid=session['id'],
                             itemid=hint.id)
            award = Awards(teamid=session['id'],
                           name='Hint for {}'.format(chal.name),
                           value=(-hint.cost),
                           contestid=contestid)
            db.session.add(unlock)
            db.session.add(award)
            db.session.commit()
            json_data = {
                'hint': hint.hint,
                'chal': hint.chal,
                'cost': hint.cost
            }
            db.session.close()
            return jsonify(json_data)
        elif utils.ctf_ended(contest=contest):
            json_data = {
                'hint': hint.hint,
                'chal': hint.chal,
                'cost': hint.cost
            }
            db.session.close()
            return jsonify(json_data)
        else:
            json_data = {
                'hint': hint.hint,
                'chal': hint.chal,
                'cost': hint.cost
            }
            db.session.close()
            return jsonify(json_data)
示例#3
0
def hints_view(hintid):
    if utils.ctf_started() is False:
        if utils.is_admin() is False:
            abort(403)
    hint = Hints.query.filter_by(id=hintid).first_or_404()
    chal = Challenges.query.filter_by(id=hint.chal).first()
    unlock = Unlocks.query.filter_by(model='hints',
                                     itemid=hintid,
                                     teamid=session['id']).first()
    if request.method == 'GET':
        if unlock:
            return jsonify({
                'hint': hint.hint,
                'chal': hint.chal,
                'cost': hint.cost
            })
        else:
            return jsonify({'chal': hint.chal, 'cost': hint.cost})
    elif request.method == 'POST':
        if unlock is None:  # The user does not have an unlock.
            if utils.ctftime() or (
                    utils.ctf_ended()
                    and utils.view_after_ctf()) or utils.is_admin() is True:
                # It's ctftime or the CTF has ended (but we allow views after)
                team = Teams.query.filter_by(id=session['id']).first()
                if team.score() < hint.cost:
                    return jsonify({'errors': get_tip('NOT_ENOUGH_POINT')})
                unlock = Unlocks(model='hints',
                                 teamid=session['id'],
                                 itemid=hint.id)
                award = Awards(teamid=session['id'],
                               name=text_type(
                                   get_tip('HIT_FOR').format(chal.name)),
                               value=(-hint.cost))
                db.session.add(unlock)
                db.session.add(award)
                db.session.commit()
                json_data = {
                    'hint': hint.hint,
                    'chal': hint.chal,
                    'cost': hint.cost
                }
                db.session.close()
                return jsonify(json_data)
            elif utils.ctf_ended():  # The CTF has ended. No views after.
                abort(403)
        else:  # The user does have an unlock, we should give them their hint.
            json_data = {
                'hint': hint.hint,
                'chal': hint.chal,
                'cost': hint.cost
            }
            db.session.close()
            return jsonify(json_data)