diff options
| author | lonkaars <l.leblansch@gmail.com> | 2021-03-10 14:00:40 +0100 | 
|---|---|---|
| committer | lonkaars <l.leblansch@gmail.com> | 2021-03-10 14:00:40 +0100 | 
| commit | ba0e25a47082ada94565da07b2451ff00d0e2857 (patch) | |
| tree | 0fa43c75dbac5ed986282729f846e2c2f44ac9d0 /api | |
| parent | 7a3e0e1670ef84ac9ca94514f20d92ed2bd8e468 (diff) | |
/user/games done
Diffstat (limited to 'api')
| -rwxr-xr-x | api/tests.sh | 19 | ||||
| -rw-r--r-- | api/user/games.py | 36 | 
2 files changed, 43 insertions, 12 deletions
diff --git a/api/tests.sh b/api/tests.sh index 5281acf..8ee9d68 100755 --- a/api/tests.sh +++ b/api/tests.sh @@ -91,12 +91,21 @@  # sleep 10  # random_game_2 -search () { -	curl  \ +# search () { +# 	curl  \ +# 		-H "Content-Type: application/json" \ +# 		-d "{ \"query\": \"$1\" }" \ +# 		localhost:2080/api/social/search +# } + +# search loekaars + +games () { +	curl -X POST \  		-H "Content-Type: application/json" \ -		-d "{ \"query\": \"$1\" }" \ -		localhost:2080/api/social/search +		-d "{ \"id\": \"4577c119-c768-4ad5-afec-b53a5c19baf4\" }" \ +		localhost:2080/api/user/games  } -search loekaars +games diff --git a/api/user/games.py b/api/user/games.py index b188470..f08928a 100644 --- a/api/user/games.py +++ b/api/user/games.py @@ -1,5 +1,6 @@  from flask import Blueprint, request  from functools import reduce +from mergedeep import merge  from db import cursor  from auth.login_token import token_login  from ruleset import resolve_ruleset @@ -22,17 +23,20 @@ def game_info(game_id, user_id = None):          "status",                # 12          "private",               # 13          ]) + " from games where game_id = ?", [game_id]).fetchone() +    is_player_1 = game[4] != user_id +    outcome = "d" if game[5] == "d" else \ +        "w" if game[5] == "w" and is_player_1 else "d"      return {          "id": game[0],          "parent": game[1],          "moves": game[2], -        "opponent": game[3] if game[3] != user_id else game[4], -        "outcome": game[5], +        "opponent": game[3] if is_player_1 else game[4], +        "outcome": outcome,          "created": game[6],          "started": game[7],          "duration": game[8], -        "rating": game[9] if game[3] != user_id else game[10], -        "rating_opponent": game[10] if game[3] != user_id else game[9], +        "rating": game[9] if is_player_1 else game[10], +        "rating_opponent": game[10] if is_player_1 else game[9],          "ruleset": resolve_ruleset(game[11]),          "status": game[12],          "private": game[13], @@ -71,8 +75,26 @@ 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 +    data_string = request.data or "{}" +    data = json.loads(data_string) + +    user_id = data.get("id") or "" +    token = request.cookies.get("token") or "" + +    if not user_id and \ +       not token: +           return "", 400 + +    if not cursor.execute("select user_id from users where user_id = ?", [user_id]).fetchone(): return "", 403 + +    if token and not user_id: +        user_id = token_login(token) + +    export = {} +    merge(export, +          {"totals": sum_games(user_id)}, +          {"games": fetch_games(user_id, 20)}) + +    return export, 200  dynamic_route = ["/user", games]  |