diff options
author | lonkaars <l.leblansch@gmail.com> | 2021-04-16 16:57:26 +0200 |
---|---|---|
committer | lonkaars <l.leblansch@gmail.com> | 2021-04-16 16:57:26 +0200 |
commit | 07c2b124e4348b15f1e5ec18c6cdfd77248c6bc8 (patch) | |
tree | e4a29123d3ebedc1d25500390c904c66b3b02489 /api/user/games.py | |
parent | aa2c999702dadba2afbcf2be9f597f890aafcc87 (diff) |
spaces > tabs in python :(
Diffstat (limited to 'api/user/games.py')
-rw-r--r-- | api/user/games.py | 86 |
1 files changed, 43 insertions, 43 deletions
diff --git a/api/user/games.py b/api/user/games.py index da5f422..d3b650c 100644 --- a/api/user/games.py +++ b/api/user/games.py @@ -11,54 +11,54 @@ import json # get total game outcome amount for user def sum_games(user_id): #! SANITIZE USER_ID FIRST - wld_querys = [ - ' '.join( - [ - "select count(game_id)", - "from games", - "where", - f"player_{x[0]}_id = \"{user_id}\" and", - f"outcome = \"{x[1]}\"", - ] - ) for x in [(1, "w"), (1, "l"), (2, "w"), (2, "l")] - ] - wld_querys.insert( - 0, ' '.join( - [ - "select count(game_id)", - "from games", - "where", - f"(player_1_id = \"{user_id}\" or player_2_id = \"{user_id}\") and", - "outcome = \"d\"", - ] - ) - ) + wld_querys = [ + ' '.join( + [ + "select count(game_id)", + "from games", + "where", + f"player_{x[0]}_id = \"{user_id}\" and", + f"outcome = \"{x[1]}\"", + ] + ) for x in [(1, "w"), (1, "l"), (2, "w"), (2, "l")] + ] + wld_querys.insert( + 0, ' '.join( + [ + "select count(game_id)", + "from games", + "where", + f"(player_1_id = \"{user_id}\" or player_2_id = \"{user_id}\") and", + "outcome = \"d\"", + ] + ) + ) - big_query = "select " + ", ".join([f"({query})" for query in wld_querys]) + big_query = "select " + ", ".join([f"({query})" for query in wld_querys]) - results = cursor.execute(big_query).fetchone() + results = cursor.execute(big_query).fetchone() - # win and lose are calculated from user_id's perspective (player_1_id, player_2_id in db) - return { - "draw": results[0], - "win": results[1] + results[4], - "lose": results[2] + results[3], - "games": reduce(lambda a, b: a + b, results) - } + # win and lose are calculated from user_id's perspective (player_1_id, player_2_id in db) + return { + "draw": results[0], + "win": results[1] + results[4], + "lose": results[2] + results[3], + "games": reduce(lambda a, b: a + b, results) + } # get `count` games that `user_id` participated in, sorted by newest game def fetch_games(user_id, count): - game_ids = cursor.execute( - "select game_id from games where player_1_id = ? or player_2_id = ? order by created desc", - [user_id, user_id] - ).fetchmany(count) - export = [] + game_ids = cursor.execute( + "select game_id from games where player_1_id = ? or player_2_id = ? order by created desc", + [user_id, user_id] + ).fetchmany(count) + export = [] - for game_id in game_ids: - export.append(format_game(game_id[0], user_id)) + for game_id in game_ids: + export.append(format_game(game_id[0], user_id)) - return export + return export games = Blueprint('games', __name__) @@ -67,10 +67,10 @@ games = Blueprint('games', __name__) @games.route('/games', methods=['GET', 'POST']) @one_person def index(user_id, viewer): - return { - "totals": sum_games(user_id), - "games": fetch_games(user_id, 20) - }, 200 + return { + "totals": sum_games(user_id), + "games": fetch_games(user_id, 20) + }, 200 dynamic_route = ["/user", games] |