aboutsummaryrefslogtreecommitdiff
path: root/api/user/games.py
diff options
context:
space:
mode:
authorlonkaars <l.leblansch@gmail.com>2021-04-11 18:03:11 +0200
committerlonkaars <l.leblansch@gmail.com>2021-04-11 18:03:11 +0200
commitdeb09e5c749e3353c927d7fe94bbd35f25ff85ee (patch)
tree702ba982530aa98a96ddece365a32be842dae3c2 /api/user/games.py
parent28f104de9ae9abe4b42abafbf3865ede5687996c (diff)
dprint yapf continuation align style edit
Diffstat (limited to 'api/user/games.py')
-rw-r--r--api/user/games.py56
1 files changed, 28 insertions, 28 deletions
diff --git a/api/user/games.py b/api/user/games.py
index 83d721a..92799bd 100644
--- a/api/user/games.py
+++ b/api/user/games.py
@@ -12,26 +12,26 @@ 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")]
+ ' '.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\"",
- ]
- )
+ 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])
@@ -40,18 +40,18 @@ def sum_games(user_id): #! SANITIZE USER_ID FIRST
# 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)
+ "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]
+ "select game_id from games where player_1_id = ? or player_2_id = ? order by created desc",
+ [user_id, user_id]
).fetchmany(count)
export = []
@@ -73,21 +73,21 @@ def index():
token = request.cookies.get("token") or ""
if not user_id and \
- not token:
+ not token:
return "", 400
if token and not user_id:
user_id = token_login(token)
if not cursor.execute(
- "select user_id from users where user_id = ?", [user_id]
+ "select user_id from users where user_id = ?", [user_id]
).fetchone():
return "", 403
export = {}
merge(
- export, {"totals": sum_games(user_id)},
- {"games": fetch_games(user_id, 20)}
+ export, {"totals": sum_games(user_id)},
+ {"games": fetch_games(user_id, 20)}
)
return export, 200