aboutsummaryrefslogtreecommitdiff
path: root/api/game/info.py
diff options
context:
space:
mode:
authorlonkaars <l.leblansch@gmail.com>2021-03-28 12:19:28 +0200
committerlonkaars <l.leblansch@gmail.com>2021-03-28 12:19:28 +0200
commit2f4536d6b08b69168ebf3e718cbd8e3002b9af5a (patch)
tree5307692fb341d7f924ee9b73f3751e7e56cfb192 /api/game/info.py
parent1f897d3f5ad11178cf4776ae4070c9d3e832f5f3 (diff)
added comments
Diffstat (limited to 'api/game/info.py')
-rw-r--r--api/game/info.py10
1 files changed, 9 insertions, 1 deletions
diff --git a/api/game/info.py b/api/game/info.py
index 030c6ae..76a3ef8 100644
--- a/api/game/info.py
+++ b/api/game/info.py
@@ -22,12 +22,19 @@ def format_game(game_id, user_id = None):
"status", # 12
"private", # 13
]) + " from games where game_id = ?", [game_id]).fetchone()
+
is_player_1 = game[4] != user_id
+
+ # get opponent from perspective of `user_id`
opponent = game[4] if is_player_1 else game[3]
+
+ # parse moves into list and return empty list if moves string is empty
+ moves = [] if len(game[2]) == 0 else [int(move) for move in str(game[2] + "0").split(",")]
+
return {
"id": game[0],
"parent": game[1],
- "moves": [] if len(game[2]) == 0 else [int(move) for move in str(game[2] + "0").split(",")],
+ "moves": moves,
"opponent": None if not opponent else format_user(opponent),
"outcome": None if not game[5] else outcome(game[5], is_player_1),
"created": game[6],
@@ -40,6 +47,7 @@ def format_game(game_id, user_id = None):
"private": bool(game[13]),
}
+# check if game_id exists in database
def valid_game_id(game_id):
query = cursor.execute("select game_id from games where game_id = ?", [game_id]).fetchone()
return bool(query)