예제 #1
0
def register():
    new_priv_key = keys.gen_priv_key()
    new_pub_key = keys.gen_pub_key(new_priv_key)
    new_user = User(public_key=new_pub_key)
    db.add(new_user)
    db.commit()
    return jsonify({'private key': new_priv_key})
예제 #2
0
def user_check(public_key, private_key=None):
    if private_key != None:
        public_key = keys.gen_pub_key(private_key)

    if User.query.filter(User.public_key == public_key).first() == None:
        return False
    else:
        return True
예제 #3
0
def restful(private_key):
    public_key = keys.gen_pub_key(private_key)
    if user_check(public_key) == False:
        return user_not_found()

    if request.method == 'GET':
        return get_all_scores(public_key)

    if request.method == 'POST':
        return add_all_scores(public_key, request.data)

    if request.method == 'DELETE':
        return delete_all_scores(request.data)
예제 #4
0
def simple_delete_score(private_key, id_list):
    public_key = keys.gen_pub_key(private_key)
    if user_check(public_key) == False:
        return user_not_found()

    id_list = id_list.split('|')
    ids_to_delete = []
    for id_ in id_list:
        id_as_dict = {}
        id_as_dict['id'] = id_
        ids_to_delete.append(id_as_dict)

    return delete_all_scores(json.dumps(ids_to_delete))
예제 #5
0
def simple_add_score(private_key, score_list):
    public_key = keys.gen_pub_key(private_key)
    if user_check(public_key) == False:
        return user_not_found()

    scores_to_add = score_list.split('|')
    score_list = []
    for score in scores_to_add:
        new_score = {}
        new_score['name'] = score[score.index('-') + 1:]
        new_score['score'] = score[:score.index('-')]
        score_list.append(new_score)

    return add_all_scores(public_key, json.dumps(score_list))
예제 #6
0
def top_x_scores(amount, private_key):
    public_key = keys.gen_pub_key(private_key)
    if user_check(public_key) == False:
        return user_not_found()

    # sort high to low, slice to get top x
    score_rows = Highscore.query.filter(Highscore.user == public_key).all()
    score_rows.sort(key=lambda k: float_from_string(k.score), reverse=True)
    score_rows = score_rows[:amount]

    score_list = []
    for row in score_rows:
        score_list.append({
            "id": row.uuid,
            "name": row.name,
            "score": row.score
        })
    return jsonify(score_list)
예제 #7
0
def add_and_return_scores(private_key, score_list):
    public_key = keys.gen_pub_key(private_key)
    if user_check(public_key) == False:
        return user_not_found()

    scores_to_add = score_list.split('|')
    score_list = []
    for score in scores_to_add:
        new_score = {}
        new_score['name'] = score[score.index('-') + 1:]
        new_score['score'] = score[:score.index('-')]
        score_list.append(new_score)

    # return an error if there is one
    add_response = add_all_scores(public_key, json.dumps(score_list))
    if add_response != 'OK':
        return add_response

    # otherwise return all scores
    return get_all_scores(public_key)
예제 #8
0
def simple_get_score(private_key):
    public_key = keys.gen_pub_key(private_key)
    if user_check(public_key) == False:
        return user_not_found()

    return get_all_scores(public_key)
예제 #9
0
def get_public_key(private_key):
    public_key = keys.gen_pub_key(private_key)
    if user_check(public_key) == False:
        return user_not_found()

    return public_key
예제 #10
0
def reset_user_database(private_key):
    public_key = keys.gen_pub_key(private_key)
    if user_check(public_key) == False:
        return user_not_found()

    return reset_user_scores(public_key)
예제 #11
0
def private_user_page(private_key):
    public_key = keys.gen_pub_key(private_key)
    return render_template('key_page.html',
                           private_key=private_key,
                           public_key=public_key)
예제 #12
0
def count_scores(private_key):
    public_key = keys.gen_pub_key(private_key)
    if user_check(public_key) == False:
        return user_not_found()

    return str(len(Highscore.query.filter(Highscore.user == public_key).all()))