diff options
-rw-r--r-- | api/ruleset.py | 30 | ||||
-rw-r--r-- | api/user/games.py | 59 | ||||
-rw-r--r-- | requirements.txt | 1 |
3 files changed, 90 insertions, 0 deletions
diff --git a/api/ruleset.py b/api/ruleset.py new file mode 100644 index 0000000..3dc59f2 --- /dev/null +++ b/api/ruleset.py @@ -0,0 +1,30 @@ +from mergedeep import merge +import json + +rulesets = { + "default": { + "timelimit": { + "enabled": False, + "minutes": 0, + "seconds": 0, + "addmove": 0, + "shared": False, + }, + "ranked": True, + } +} + +def resolve_ruleset(ruleset): + export = {} + try: + export = json.loads(ruleset) + merged = dict(rulesets["default"]) + merge(merged, export) + export = merged + except ValueError as e: + if ruleset in rulesets: + export = rulesets[ruleset] + if not export: + export = rulesets["default"] + return export + diff --git a/api/user/games.py b/api/user/games.py new file mode 100644 index 0000000..61d145f --- /dev/null +++ b/api/user/games.py @@ -0,0 +1,59 @@ +from flask import Blueprint, request +from db import cursor +from auth.login_token import token_login +from ruleset import resolve_ruleset +import json + +def game_info(game_id, user_id = None): + game = cursor.execute("select " + ", ".join([ + "game_id", # 0 + "parent_game", # 1 + "moves", # 2 + "player_1_id", # 3 + "player_2_id", # 4 + "outcome", # 5 + "created", # 6 + "started", # 7 + "duration", # 8 + "rating_delta_player_1", # 9 + "rating_delta_player_2", # 10 + "ruleset", # 11 + "status", # 12 + "private", # 13 + ]) + " from games where game_id = ?", [game_id]).fetchone() + return { + "id": game[0], + "parent": game[1], + "moves": game[2], + "opponent": game[3] if game[3] != user_id else game[4], + "outcome": game[5], + "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], + "ruleset": resolve_ruleset(game[11]), + "status": game[12], + "private": game[13], + } + +def sum_games(user_id): + print('gert') + +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) + export = [] + + for game_id in game_ids: + export.append(game_info(game_id[0], user_id)) + + return export + +games = Blueprint('games', __name__) + +@games.route('/games', methods = ['GET', 'POST']) +def index(): + print(fetch_games("4577c119-c768-4ad5-afec-b53a5c19baf4", 10)) + return "", 200 + +dynamic_route = ["/user", games] diff --git a/requirements.txt b/requirements.txt index bdad4e3..aabd38e 100644 --- a/requirements.txt +++ b/requirements.txt @@ -13,6 +13,7 @@ itsdangerous==1.1.0 jedi==0.18.0 Jinja2==2.11.2 MarkupSafe==1.1.1 +mergedeep==1.3.4 msgpack==1.0.2 neovim==0.3.1 parso==0.8.1 |