diff options
-rw-r--r-- | api/game/random.py | 2 | ||||
-rw-r--r-- | api/game/socket.py | 35 |
2 files changed, 35 insertions, 2 deletions
diff --git a/api/game/random.py b/api/game/random.py index 68ba44c..52a6d1f 100644 --- a/api/game/random.py +++ b/api/game/random.py @@ -26,7 +26,7 @@ def index(): if len(public_games) == 0: game_id = new_uuid("games") - cursor.execute("insert into games values (?, NULL, NULL, ?, NULL, NULL, 0, NULL, NULL, NULL, \"wait_for_opponent\", \"default\", FALSE) ", (game_id, user_id)) + cursor.execute("insert into games values (?, NULL, \"\", ?, NULL, NULL, 0, NULL, NULL, NULL, \"wait_for_opponent\", \"default\", FALSE) ", (game_id, user_id)) connection.commit() player_1 = True else: diff --git a/api/game/socket.py b/api/game/socket.py index 3828c35..f1a591e 100644 --- a/api/game/socket.py +++ b/api/game/socket.py @@ -2,7 +2,7 @@ from flask import Blueprint, request, make_response from flask_socketio import SocketIO, emit, Namespace from game.voerbak_connector import bord from auth.login_token import token_login -from db import cursor +from db import cursor, connection import time import json from socket_io import io @@ -21,6 +21,7 @@ class game: 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 if user_id != move: return + self.board.drop_fisje(column) self.io.emit("fieldUpdate", { "field": self.board.board }) self.io.emit("turnUpdate", { "player1": self.board.player_1 }) @@ -30,14 +31,46 @@ class game: "boardFull": self.board.board_full }) + cursor.execute("update games set moves = moves || ? || ',' where game_id = ?", [column, self.game_id]) + connection.commit() + + if self.board.board_full: + self.close("finished", "d") + + def close(self, new_status, outcome): + cursor.execute(" ".join([ + "update games set", + "moves = moves || '0',", + "duration = ?,", + "status = ?,", + "outcome = ?", + "where game_id = ?" + ]), [ + int( time.time() * 1000 ) - cursor.execute("select timestamp from games where game_id = ?", [self.game_id]).fetchone()[0], + new_status, + outcome, + self.game_id + ]) + connection.commit() + @io.on("newMove") def new_move(data): if not data["game_id"] or \ not data["move"] or \ not data["token"]: return if not data["game_id"] in games: return + game = games[data["game_id"]] if(len(game.board.win_positions) > 0 or game.board.board_full): return user_id = token_login(data["token"])[0] game.move(user_id, data["move"]) +@io.on("resign") +def resign(data): + if not data["game_id"] or \ + not data["token"]: return + if not data["game_id"] in games: return + + game = games[data["game_id"]] + game.move(user_id, data["move"]) + |