Exemplo n.º 1
0
def user_contest_game(user_id, contest_id, game_id):
    game = query_db("""select games.date, games.contest_id
                          from games, users, contests
                          where (users.id=contests.user_id and
                                 games.contest_id=contests.id and
                                 users.id=? and
                                 contests.id=? and
                                 games.id=?)""",
                    (user_id, contest_id, game_id), one=True)
    if not game:
        return json_response(404, 'No such game')
    if request.method == 'DELETE':
        check_session_auth(user_id)
        g.db.execute("""delete from games where id=?""", (game_id,))
        g.db.execute("""delete from scores where game_id=?""", (game_id,))
        g.db.commit()
        return json_response()
    else:
        scores = query_db("""select player_id, score
                                from scores
                                where game_id=?""",
                          (game_id,))
        ordered = sorted(scores, key=lambda x: x["score"], reverse=True)
        ranking = map(lambda x: int(x["player_id"]), ordered)
        game["ranking"] = ranking
        return json_response(data={"game": game})
Exemplo n.º 2
0
def user_contests(user_id):
    valid = '{"name": "contest name (string)", "type": "contest type (string)"}'
    user = query_db('select id from users where id=?',
                    (user_id,), one=True)
    if not user:
        return json_response(404, 'No such user')
    if request.method == 'POST':
        check_session_auth(user_id)
        try:
            data = get_request_data(valid)
        except (NoDataError, BadDataError, SchemaError), e:
            return json_response(400, e.message)
        name = data['name']
        c_type = data['type']
        try:
            contest = create_contest(user_id, name, c_type)
        except ContestError, e:
            return json_response(400, e.message)
Exemplo n.º 3
0
def user_contest_games(user_id, contest_id):
    contest = query_db("""select contests.name, contests.id,
                                 contests.user_id, contests.type
                                          from users, contests
                                          where (users.id=contests.user_id and
                                                 users.id=? and
                                                 contests.id=?)""",
                       (user_id, contest_id), one=True)
    contest = load_contest(user_id, contest_id)
    if not contest:
        return json_response(404, 'No such contest')
    if request.method == 'POST':
        check_session_auth(user_id)
        try:
            print contest.get_game_schema()
            data = get_request_data(contest.get_game_schema())
            game_id = contest.create_game(**data)
            return json_response(data={"id": game_id})
        except (NoDataError, BadDataError, SchemaError, GameValidationError), e:
            return json_response(400, e.message)
Exemplo n.º 4
0
def user_contest(user_id, contest_id):
    contest = query_db("""select contests.name, contests.id, contests.user_id
                                 from users, contests
                                          where (users.id=contests.user_id and
                                                 users.id=? and
                                                 contests.id=?)""",
                    (user_id, contest_id), one=True)
    if not contest:
        return json_response(404, 'No such contest')
    if request.method == 'PUT':
        check_session_auth(user_id)
        try:
            name = str(request.form['name'])
            g.db.execute("""update contests set user_id=?, name=?
                                            where id=?""",
                         (user_id, name, contest_id))
            g.db.commit()
            return json_response()
        except KeyError, e:
            return json_response(400,
                                 "Required argument %s not found" % e.message)
Exemplo n.º 5
0
def user_contest_player(user_id, contest_id, player_id):
    player = query_db("""select players.name, players.id, players.user_id
                          from users, contests, players
                          where (users.id=contests.user_id and
                                 players.contest_id=contests.id and
                                 users.id=? and
                                 contests.id=? and
                                 players.id=?)""",
                    (user_id, contest_id, player_id), one=True)
    if not player:
        return json_response(404, 'No such player')
    if request.method == 'PUT':
        check_session_auth(user_id)
        try:
            name = str(request.form['name'])
            player_user_id = None  # int(request.form['player_id'])
            g.db.execute("""update players set name=?, user_id=?
                                            where id=?""",
                         (name, player_user_id, player_id))
            g.db.commit()
            return json_response()
        except KeyError, e:
            return json_response(400,
                                 "Required argument %s not found" % e.message)
Exemplo n.º 6
0
def user_contest_players(user_id, contest_id):
    contest = query_db("""select contests.name, contests.id, contests.user_id
                                          from users, contests
                                          where (users.id=contests.user_id and
                                                 users.id=? and
                                                 contests.id=?)""",
                       (user_id, contest_id), one=True)
    if not contest:
        return json_response(404, 'No such contest')
    if request.method == 'POST':
        check_session_auth(user_id)
        try:
            name = request.form['name']
            player_user_id = None  # int(request.form['player_id'])
            cur = g.db.cursor()
            cur.execute("""insert into players (contest_id, name, user_id)
                                        values (?, ?, ?)""",
                         (contest["id"], name, player_user_id))
            g.db.commit()
            player_id = cur.lastrowid
            return json_response(data={"id": player_id})
        except KeyError, e:
            return json_response(400,
                                 "Required argument %s not found" % e.message)
Exemplo n.º 7
0
    if not contest:
        return json_response(404, 'No such contest')
    if request.method == 'PUT':
        check_session_auth(user_id)
        try:
            name = str(request.form['name'])
            g.db.execute("""update contests set user_id=?, name=?
                                            where id=?""",
                         (user_id, name, contest_id))
            g.db.commit()
            return json_response()
        except KeyError, e:
            return json_response(400,
                                 "Required argument %s not found" % e.message)
    elif request.method == 'DELETE':
        check_session_auth(user_id)
        g.db.execute("""delete from contests where id=?""", (contest_id,))
        g.db.commit()
        return json_response()
    else:
        return json_response(data={"contest": contest})


# List and add participants
@app.route('/users/<int:user_id>/contests/<int:contest_id>/players',
           methods=['POST', 'GET'])
@requires_auth
def user_contest_players(user_id, contest_id):
    contest = query_db("""select contests.name, contests.id, contests.user_id
                                          from users, contests
                                          where (users.id=contests.user_id and