diff options
| author | lonkaars <l.leblansch@gmail.com> | 2021-02-20 13:03:04 +0100 | 
|---|---|---|
| committer | lonkaars <l.leblansch@gmail.com> | 2021-02-20 13:03:04 +0100 | 
| commit | e1978a9b80f3f7f5a36ca4af5f6df62f494a0d6d (patch) | |
| tree | 27b2a142ad1c267f002e7c7e09ea337e4da688ac /api | |
| parent | 16044fbf61b06f4d53e2ffcab67569721b3792e2 (diff) | |
/game/random endpoint
Diffstat (limited to 'api')
| -rw-r--r-- | api/game/new.py | 2 | ||||
| -rw-r--r-- | api/game/random.py | 31 | ||||
| -rw-r--r-- | api/game/voerbak_connector.py | 6 | ||||
| -rw-r--r-- | api/main.py | 2 | ||||
| -rwxr-xr-x | api/tests.sh | 125 | 
5 files changed, 111 insertions, 55 deletions
| diff --git a/api/game/new.py b/api/game/new.py index fd65019..1b63110 100644 --- a/api/game/new.py +++ b/api/game/new.py @@ -19,7 +19,7 @@ def index():      game_id = new_uuid("games")      timestamp = int( time.time() * 1000 ) -    cursor.execute("insert into games values (?, NULL, NULL, ?, NULL, NULL, ?, NULL, NULL, NULL, \"wait_for_opponent\", ?) ", (game_id, user_id, timestamp, json.dumps(game_settings))) +    cursor.execute("insert into games values (?, NULL, NULL, ?, NULL, NULL, ?, NULL, NULL, NULL, \"wait_for_opponent\", ?, TRUE) ", (game_id, user_id, timestamp, json.dumps(game_settings)))      connection.commit()      return { "id": game_id }, 200 diff --git a/api/game/random.py b/api/game/random.py new file mode 100644 index 0000000..bbddedc --- /dev/null +++ b/api/game/random.py @@ -0,0 +1,31 @@ +from flask import Blueprint, request, make_response +from db import cursor, connection +from randid import new_uuid +import time +import json +import random + +random_game = Blueprint('random', __name__) + +@random_game.route('/random', methods = ['POST']) +def index(): +    data = request.get_json() + +    user_id = data.get("user_id") or "" +    if not user_id: +        print("a temporary user should be set up here") + +    public_games = cursor.execute("select game_id from games where private = FALSE and status = \"wait_for_opponent\"").fetchall() +    print(public_games) +    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)) +        connection.commit() +    else: +        game_id = random.choice(public_games)[0] +        timestamp = int( time.time() * 1000 ) +        cursor.execute("update games set player_2_id = ?, status = \"in_progress\", timestamp = ? where game_id = ?", (user_id, timestamp, game_id)) +        connection.commit() + +    return { "id": game_id }, 200 diff --git a/api/game/voerbak_connector.py b/api/game/voerbak_connector.py index 5ccaed8..65551ca 100644 --- a/api/game/voerbak_connector.py +++ b/api/game/voerbak_connector.py @@ -32,12 +32,17 @@ class bord:      def get_output(self):          return self.process.stdout.readline().decode()[:-1] +    def kill_voerbak(self): +        self.process.stdin.write(bytearray("0", "utf-8")) +        self.process.stdin.flush() +      def update_board(self):          buffer = self.get_output()          while not buffer.isdigit():              if buffer.startswith("w:"):                  self.win_positions.append(buffer[2:].split("-"))                  log.info(f"won: {buffer[2:].split('-')}") +                self.kill_voerbak()              elif buffer.startswith("e:"):                  log.warning(buffer[2:])              elif buffer.startswith("m:"): @@ -45,6 +50,7 @@ class bord:                  self.player_1 = True if substr == "true" else False              elif buffer.startswith("d:"):                  self.board_full = True +                self.kill_voerbak()              buffer = self.get_output()          self.board = buffer diff --git a/api/main.py b/api/main.py index 5588138..052a6eb 100644 --- a/api/main.py +++ b/api/main.py @@ -9,6 +9,7 @@ from auth.signup import signup  from auth.login import login  from auth.login_token import token  from game.new import new_game +from game.random import random_game  from game.socket import run  app.register_blueprint(status, url_prefix='/') @@ -17,5 +18,6 @@ app.register_blueprint(signup, url_prefix='/auth')  app.register_blueprint(login, url_prefix='/auth')  app.register_blueprint(token, url_prefix='/auth')  app.register_blueprint(new_game, url_prefix='/game') +app.register_blueprint(random_game, url_prefix='/game')  if __name__ == "__main__": run(app) diff --git a/api/tests.sh b/api/tests.sh index 1b2b216..ce30af3 100755 --- a/api/tests.sh +++ b/api/tests.sh @@ -1,48 +1,48 @@  #!/bin/sh -username="test_$RANDOM" -email="$username@example.com" -password=$(echo $RANDOM | base64) +# username="test_$RANDOM" +# email="$username@example.com" +# password=$(echo $RANDOM | base64) -signup () { -	curl -X POST \ -		-H "Content-Type: application/json" \ -		-d "{ -		\"username\": \"$username\", -		\"email\": \"$email\", -		\"password\": \"$password\" -		}" \ -		localhost:5000/api/auth/signup -} +# signup () { +# 	curl -X POST \ +# 		-H "Content-Type: application/json" \ +# 		-d "{ +# 		\"username\": \"$username\", +# 		\"email\": \"$email\", +# 		\"password\": \"$password\" +# 		}" \ +# 		localhost:5000/api/auth/signup +# } -login_username () { -	curl -X POST \ -		-H "Content-Type: application/json" \ -		-d "{ -		\"email\": \"$username\", -		\"password\": \"$password\" -		}" \ -		localhost:5000/api/auth/login -} +# login_username () { +# 	curl -X POST \ +# 		-H "Content-Type: application/json" \ +# 		-d "{ +# 		\"email\": \"$username\", +# 		\"password\": \"$password\" +# 		}" \ +# 		localhost:5000/api/auth/login +# } -login_email () { -	curl -X POST \ -		-H "Content-Type: application/json" \ -		-d "{ -		\"email\": \"$email\", -		\"password\": \"$password\" -		}" \ -		localhost:5000/api/auth/login -} +# login_email () { +# 	curl -X POST \ +# 		-H "Content-Type: application/json" \ +# 		-d "{ +# 		\"email\": \"$email\", +# 		\"password\": \"$password\" +# 		}" \ +# 		localhost:5000/api/auth/login +# } -user_info () { -	curl -X GET \ -		-H "Content-Type: application/json" \ -		-d '{ -		"username": "loekaars" -		}' \ -		localhost:5000/user/info -} +# user_info () { +# 	curl -X GET \ +# 		-H "Content-Type: application/json" \ +# 		-d '{ +# 		"username": "loekaars" +# 		}' \ +# 		localhost:5000/user/info +# }  # login_token () {  # 	curl -X POST \ @@ -54,23 +54,40 @@ user_info () {  # 		localhost:5000/api/auth/token  # } -new_game () { +# new_game () { +# 	curl -X POST \ +# 		-H "Content-Type: application/json" \ +# 		-d '{ +# 			"user_id": "4577c119-c768-4ad5-afec-b53a5c19baf4", +# 			"settings": { +# 				"ranked": true, +# 				"timelimit": { +# 					"minutes": 5, +# 					"seconds": 30, +# 					"enabled": true, +# 					"shared": "false" +# 				} +# 			} +# 		}' \ +# 		localhost:5000/game/new +# } + +random_game_1 () { +	curl -X POST \ +		-H "Content-Type: application/json" \ +		-d '{ "user_id": "e6162c82-3e60-4479-ac96-a1af508e49c4" }' \ +		localhost:2080/api/game/random +} + +random_game_2 () {  	curl -X POST \  		-H "Content-Type: application/json" \ -		-d '{ -			"user_id": "4577c119-c768-4ad5-afec-b53a5c19baf4", -			"settings": { -				"ranked": true, -				"timelimit": { -					"minutes": 5, -					"seconds": 30, -					"enabled": true, -					"shared": "false" -				} -			} -		}' \ -		localhost:5000/game/new +		-d '{ "user_id": "de960155-7d58-46b3-a4f6-7d33aa034ad9" }' \ +		localhost:2080/api/game/random  } -new_game +sleep 3 +random_game_1 +sleep 10 +random_game_2 |