diff options
author | lonkaars <l.leblansch@gmail.com> | 2021-03-08 21:07:02 +0100 |
---|---|---|
committer | lonkaars <l.leblansch@gmail.com> | 2021-03-08 21:07:02 +0100 |
commit | febc3c4912bf8fd624d5f626e19cfdf72714ccb5 (patch) | |
tree | 9eb263d67ac83fd9024fa3eab87a98b90c7cbba5 /api/game | |
parent | b33d057044a337f494e44ca10d461ec9f044e506 (diff) |
class based -> function based + proper game sub + good game start
Diffstat (limited to 'api/game')
-rw-r--r-- | api/game/random.py | 9 | ||||
-rw-r--r-- | api/game/socket.py | 10 |
2 files changed, 12 insertions, 7 deletions
diff --git a/api/game/random.py b/api/game/random.py index 283190a..bc9518d 100644 --- a/api/game/random.py +++ b/api/game/random.py @@ -4,7 +4,7 @@ from randid import new_uuid import time import json import random -from game.socket import io, games, game +from game.socket import io, game, games, listeners from auth.login_token import token_login random_game = Blueprint('random', __name__) @@ -25,6 +25,8 @@ def index(): timestamp = int( time.time() * 1000 ) + game_started = False + if len(public_games) == 0: game_id = new_uuid("games") @@ -39,8 +41,11 @@ def index(): players = cursor.execute("select player_1_id, player_2_id from games where game_id = ?", [game_id]).fetchone() games[game_id] = game(game_id, io, players[0], players[1]) + games[game_id].send("gameStart", ""); + player_1 = False + game_started = True - return { "id": game_id, "player_1": player_1 }, 200 + return { "id": game_id, "player_1": player_1, "game_started": game_started }, 200 dynamic_route = ["/game", random_game] diff --git a/api/game/socket.py b/api/game/socket.py index 69e0144..78e7969 100644 --- a/api/game/socket.py +++ b/api/game/socket.py @@ -32,20 +32,20 @@ class game: self.send("fieldUpdate", { "field": self.board.board }) self.send("turnUpdate", { "player1": self.board.player_1 }) if len(self.board.win_positions) > 0 or self.board.board_full: - winner = self.board.board[int(self.board.win_positions[0][0])] - self.close("finished", "w" if winner == "2" else "l") + outcome = "d" + if not self.board.board_full: + winner = self.board.board[int(self.board.win_positions[0][0])] + outcome = "w" if winner == "2" else "l" self.send("finish", { "winPositions": self.board.win_positions, "boardFull": self.board.board_full }) + self.close("finished", outcome) now = int( time.time() * 1000 ) cursor.execute("update games set last_activity = ?, moves = moves || ? || ',' where game_id = ?", [now, column, self.game_id]) connection.commit() - if self.board.board_full: - self.close("finished", "d") - def resign(self): self.board.kill_voerbak() self.send("resign", "") |