diff options
author | lonkaars <l.leblansch@gmail.com> | 2021-03-28 12:19:28 +0200 |
---|---|---|
committer | lonkaars <l.leblansch@gmail.com> | 2021-03-28 12:19:28 +0200 |
commit | 2f4536d6b08b69168ebf3e718cbd8e3002b9af5a (patch) | |
tree | 5307692fb341d7f924ee9b73f3751e7e56cfb192 /api/game | |
parent | 1f897d3f5ad11178cf4776ae4070c9d3e832f5f3 (diff) |
added comments
Diffstat (limited to 'api/game')
-rw-r--r-- | api/game/accept.py | 1 | ||||
-rw-r--r-- | api/game/cleanup.py | 2 | ||||
-rw-r--r-- | api/game/info.py | 10 | ||||
-rw-r--r-- | api/game/new.py | 2 | ||||
-rw-r--r-- | api/game/random.py | 3 | ||||
-rw-r--r-- | api/game/socket.py | 1 | ||||
-rw-r--r-- | api/game/voerbak_connector.py | 8 |
7 files changed, 26 insertions, 1 deletions
diff --git a/api/game/accept.py b/api/game/accept.py index 7aab697..073f422 100644 --- a/api/game/accept.py +++ b/api/game/accept.py @@ -12,6 +12,7 @@ from game.new import start_game join_game = Blueprint('game_accept', __name__) +# join a game by game_id (public or private) @join_game.route('/accept', methods = ['POST']) @auth_required("user") def index(game_id): diff --git a/api/game/cleanup.py b/api/game/cleanup.py index 3c285e1..0a9aa46 100644 --- a/api/game/cleanup.py +++ b/api/game/cleanup.py @@ -2,6 +2,7 @@ from db import cursor, connection import threading import time +# cleanup function that's ran every five minutes def cleanup(): now = int( time.time() * 1000 ) old_games = cursor.execute("select game_id from games where (status = \"wait_for_opponent\" or status = \"in_progress\") and last_activity < ?", [now - 5 * 60 * 1e3]).fetchall() @@ -17,5 +18,6 @@ def set_interval(func, sec): # https://stackoverflow.com/questions/2697039/pytho t.start() return t +# run every five minutes set_interval(cleanup, 5 * 60) 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) diff --git a/api/game/new.py b/api/game/new.py index 5d8d5b7..9868099 100644 --- a/api/game/new.py +++ b/api/game/new.py @@ -36,6 +36,8 @@ new_game = Blueprint('new_game', __name__) @new_game.route('/new', methods = ["GET", "POST"]) @auth_required("user") def index(user_id): + # create a new private game (join by link) + #TODO: friend invites + notifications game_id = create_game(user_id, True) return { "id": game_id }, 200 diff --git a/api/game/random.py b/api/game/random.py index 7e4c512..4d70b56 100644 --- a/api/game/random.py +++ b/api/game/random.py @@ -14,13 +14,16 @@ random_game = Blueprint('random', __name__) @random_game.route('/random') @auth_required("user") def index(user_id): + # get public_games (random opponent queue) public_games = cursor.execute("select game_id from games where private = FALSE and status = \"wait_for_opponent\"").fetchall() game_started = False + # create a new public game if the queue is empty if len(public_games) == 0: game_id = create_game(user_id) player_1 = True + # otherwise join a random public game else: game_id = random.choice(public_games)[0] diff --git a/api/game/socket.py b/api/game/socket.py index 5f0d710..cf45eb6 100644 --- a/api/game/socket.py +++ b/api/game/socket.py @@ -18,6 +18,7 @@ class game: self.player_1_id = player_1_id self.player_2_id = player_2_id + # drop a disc in `column` def move(self, user_id, column): if user_id != self.player_1_id and user_id != self.player_2_id: return move = self.player_1_id if self.board.player_1 else self.player_2_id diff --git a/api/game/voerbak_connector.py b/api/game/voerbak_connector.py index 2e90ad4..048a5d1 100644 --- a/api/game/voerbak_connector.py +++ b/api/game/voerbak_connector.py @@ -25,6 +25,7 @@ class bord: stderr=None) self.process.stdin.flush() + # get output from voerbak without trailing newline character (this might break on windows because crlf) def get_output(self): return self.process.stdout.readline().decode()[:-1] @@ -32,24 +33,30 @@ class bord: self.process.stdin.write(bytearray("0", "utf-8")) self.process.stdin.flush() + # read messages from voerbak def update_board(self): buffer = self.get_output() while not buffer.isdigit(): + # win message if buffer.startswith("w:"): self.win_positions.append(buffer[2:].split("-")) log.info(f"won: {buffer[2:].split('-')}") self.kill_voerbak() + # error message elif buffer.startswith("e:"): log.warning(buffer[2:]) + # turn update message elif buffer.startswith("m:"): substr = buffer[2:] self.player_1 = True if substr == "true" else False + # draw game message elif buffer.startswith("d:"): self.board_full = True self.kill_voerbak() buffer = self.get_output() self.board = buffer + # debug board print function def print(self): for y in range(self.height -1, -1, -1): for x in range(self.width): @@ -66,6 +73,7 @@ class bord: self.process.stdin.flush() self.update_board() +# debug game def main(): gert = bord(7, 6) while True: |