aboutsummaryrefslogtreecommitdiff
path: root/api
diff options
context:
space:
mode:
authorlonkaars <l.leblansch@gmail.com>2021-03-10 13:19:34 +0100
committerlonkaars <l.leblansch@gmail.com>2021-03-10 13:19:34 +0100
commit7a3e0e1670ef84ac9ca94514f20d92ed2bd8e468 (patch)
tree8590b2f73cbcef25c5a13d0d5ca09087188ad6c9 /api
parentda697b7bdcac0ffcbfbc3f45a1f09897c3f02c66 (diff)
sum_games working
Diffstat (limited to 'api')
-rw-r--r--api/user/games.py23
1 files changed, 21 insertions, 2 deletions
diff --git a/api/user/games.py b/api/user/games.py
index 61d145f..b188470 100644
--- a/api/user/games.py
+++ b/api/user/games.py
@@ -1,4 +1,5 @@
from flask import Blueprint, request
+from functools import reduce
from db import cursor
from auth.login_token import token_login
from ruleset import resolve_ruleset
@@ -37,8 +38,25 @@ def game_info(game_id, user_id = None):
"private": game[13],
}
-def sum_games(user_id):
- print('gert')
+def sum_games(user_id): #! SANITIZE USER_ID FIRST
+ wld_querys = [' '.join([
+ "select count(game_id)",
+ "from games",
+ "where",
+ f"(player_1_id = \"{user_id}\" or player_2_id = \"{user_id}\") and",
+ f"outcome = \"{x}\"",
+ ]) for x in ["w", "l", "d"]]
+
+ big_query = "select " + ", ".join([f"({query})" for query in wld_querys])
+
+ results = cursor.execute(big_query).fetchone()
+
+ return {
+ "win": results[0],
+ "lose": results[1],
+ "draw": results[2],
+ "games": reduce(lambda a, b: a + b, results)
+ }
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", [user_id, user_id]).fetchmany(count)
@@ -54,6 +72,7 @@ games = Blueprint('games', __name__)
@games.route('/games', methods = ['GET', 'POST'])
def index():
print(fetch_games("4577c119-c768-4ad5-afec-b53a5c19baf4", 10))
+ print(sum_games("4577c119-c768-4ad5-afec-b53a5c19baf4"))
return "", 200
dynamic_route = ["/user", games]