Skip to content

Commit

Permalink
Different bugs fixed, tested
Browse files Browse the repository at this point in the history
  • Loading branch information
MarkTheHopeful committed Aug 28, 2020
1 parent f028b33 commit f479e49
Show file tree
Hide file tree
Showing 6 changed files with 65 additions and 31 deletions.
Binary file modified app.db
Binary file not shown.
10 changes: 6 additions & 4 deletions app/functions.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
from exceptions.GameExceptions import GameException, GameUserIsAlreadyInException, GameUserHasNoGamesException, \
GameNotYourTurnException, GameUserIsAlreadyInQueueException, GameNotInQueueException, \
GameNotEnoughtPlayersException, GameIsAlreadyStartedException, GameIsNotStartedException, GameNoSuchPlayerException
from utils import gen_token
from utils import gen_token, full_stack
from game.constants import BASE_FUNCTIONS, BASE_OPERATORS


Expand Down Expand Up @@ -55,14 +55,14 @@ def wrapped(*args, **kwargs):
code, data = result_function(*args, **kwargs)
except DBException as e:
code = e.code
data = json.dumps({"Error": str(e)})
data = json.dumps({"Error": str(e), "Stack": full_stack()})
print("DBException:", e)
except GameException as e:
code = 410
data = json.dumps({"Error": str(e)})
data = json.dumps({"Error": str(e), "Stack": full_stack()})
print("GameException:", e)
except Exception as e:
data = json.dumps({"Error": str(e)})
data = json.dumps({"Error": str(e), "Stack": full_stack()})
print(e)
return str(Response(code, data))

Expand Down Expand Up @@ -276,6 +276,8 @@ def make_turn(token, op_ind, fun_indexes, is_latex):
return 200, json.dumps({"Result Function": lat_result})
except GameNotYourTurnException as e:
return 409, json.dumps({})
except GameIsNotStartedException:
return 415, json.dumps({})
except GameException as e:
print(e)
raise e
Expand Down
17 changes: 14 additions & 3 deletions app/game_manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -54,11 +54,16 @@ def check_and_create_game(self, player_1):
player_2r = self.make_player(opponent)
game = Game(player_1r, player_2r, len(self.current_games))
self.current_games.append(game)
self.users_to_games[player_1] = game.game_id
self.users_to_games[opponent] = game.game_id
return self.get_game_information(player_1)

def confirm_game_start(self, username):
try:
game = self.current_games[self.users_to_games[username]]
ind = self.users_to_games[username]
if ind == NOT_STARTED:
raise KeyError()
game = self.current_games[ind]
except KeyError or IndexError:
raise GameUserHasNoGamesException()
if game.state != NOT_STARTED:
Expand Down Expand Up @@ -97,7 +102,10 @@ def make_operators(self, player_username):

def get_game_information(self, player_username):
try:
game = self.current_games[self.users_to_games[player_username]]
ind = self.users_to_games[player_username]
if ind == NOT_STARTED:
raise KeyError()
game = self.current_games[ind]
except KeyError:
raise GameUserHasNoGamesException()

Expand All @@ -108,7 +116,10 @@ def get_game_information(self, player_username):

def make_turn(self, player_username, operator_index, functions, is_latex):
try:
game = self.current_games[self.users_to_games[player_username]]
ind = self.users_to_games[player_username]
if ind == NOT_STARTED:
raise KeyError()
game = self.current_games[ind]
except KeyError:
raise GameNoSuchPlayerException()

Expand Down
1 change: 1 addition & 0 deletions exceptions/error_messages.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
412: "You are not in the queue",
413: "Not enough players in queue to create the game now",
414: "This game is already started",
415: "This game is not started",

500: "Unknown server error",
502: "Internal database error",
Expand Down
50 changes: 27 additions & 23 deletions other_clients/http_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ def register():
if code == 200 or code == 405:
return try_again, state
try:
return try_again, state + ":\n:: " + data["Error"]
return state + ":\n:: " + data["Error"] + "\n" + data["Stack"]
except KeyError:
return try_again, state + ":\n" + "Error information was not received"

Expand All @@ -57,7 +57,7 @@ def login():
return try_again, state

try:
return try_again, state + ":\n:: " + data["Error"]
return state + ":\n:: " + data["Error"] + "\n" + data["Stack"]
except KeyError:
return try_again, state + ":\n" + "Error information was not received"

Expand All @@ -67,7 +67,7 @@ def get_status():
if code == 200:
return state
try:
return state + ":\n:: " + data["Error"]
return state + ":\n:: " + data["Error"] + "\n" + data["Stack"]
except KeyError:
return state + ":\n" + "Error information was not received"

Expand All @@ -79,7 +79,7 @@ def start_game_with(opponent_name):
if code == 400 or code == 404 or code == 406 or code == 407:
return state
try:
return state + ":\n:: " + data["Error"]
return state + ":\n:: " + data["Error"] + "\n" + data["Stack"]
except KeyError:
return state + ":\n" + "Error information was not received"

Expand All @@ -91,7 +91,7 @@ def get_game_state():
elif code == 400 or code == 408:
return False, state
try:
return False, state + ":\n:: " + data["Error"]
return False, state + ":\n:: " + data["Error"] + "\n" + data["Stack"]
except KeyError:
return False, state + ":\n" + "Error information was not received"

Expand All @@ -114,11 +114,11 @@ def make_turn():
code, state, data = send_request(f"/game/make_turn/{token}/{operator_ind}/0", j_payload={"fun_indexes": args})
if code == 200:
return f"{state}, result functions is {data['Result Function']}"
elif code == 400 or code == 409:
elif code == 400 or code == 409 or code == 415:
return state
else:
try:
return state + ":\n:: " + data["Error"]
return state + ":\n:: " + data["Error"] + "\n" + data["Stack"]
except KeyError:
return state + ":\n" + "Error information was not received"

Expand All @@ -132,7 +132,7 @@ def get_into_queue():
return state
else:
try:
return state + ":\n:: " + data["Error"]
return state + ":\n:: " + data["Error"] + "\n" + data["Stack"]
except KeyError:
return state + ":\n" + "Error information was not received"

Expand All @@ -143,7 +143,7 @@ def get_queue_size():
return data["Length"]
else:
try:
return state + ":\n:: " + data["Error"]
return state + ":\n:: " + data["Error"] + "\n" + data["Stack"]
except KeyError:
return state + ":\n" + "Error information was not received"

Expand All @@ -159,22 +159,24 @@ def check_if_game_found():
return state
else:
try:
return state + ":\n:: " + data["Error"]
return state + ":\n:: " + data["Error"] + "\n" + data["Stack"]
except KeyError:
return state + ":\n" + "Error information was not received"


def confirm_game_start():
code, state, data = send_request(f"/confirm_game_start/{token}")
if code == 200:
return "Game start confirmed successfully"
elif code == 201 or code == 400 or code == 408 or code == 414:
return state
return True, "Game start confirmed successfully"
elif code == 201:
return True, state
elif code == 400 or code == 408 or code == 414:
return False, state
else:
try:
return state + ":\n:: " + data["Error"]
return False, state + ":\n:: " + data["Error"] + "\n" + data["Stack"]
except KeyError:
return state + ":\n" + "Error information was not received"
return False, state + ":\n" + "Error information was not received"


def get_help_string():
Expand Down Expand Up @@ -298,13 +300,15 @@ def print_game_state(game_state):
elif query == "try to create":
print(check_if_game_found())
elif query == "confirm game":
print(confirm_game_start())
is_ok, info = get_game_state()
if not is_ok:
print("Encountered an error while trying to update your game state. Error:")
print(info)
continue
current_game_state = deserialize_game_state(info)
print_game_state(current_game_state)
is_ok, info = confirm_game_start()
print(info)
if is_ok:
is_ok, info = get_game_state()
if not is_ok:
print("Encountered an error while trying to update your game state. Error:")
print(info)
continue
current_game_state = deserialize_game_state(info)
print_game_state(current_game_state)
else:
print("No such command")
18 changes: 17 additions & 1 deletion utils/__init__.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
import uuid
import datetime
import uuid
import traceback
import sys

from config import Config


Expand All @@ -16,3 +19,16 @@ def convert_array_to_string(array, sep=SEPARATOR, auto_type_caster=str):

def convert_string_to_array(string, sep=SEPARATOR, auto_type_caster=str):
return list(map(auto_type_caster, string.split(sep)))


def full_stack():
exc = sys.exc_info()[0]
stack = traceback.extract_stack()[:-1] # last one would be full_stack()
if exc is not None: # i.e. an exception is present
del stack[-1] # remove call of full_stack, the printed exception
# will contain the caught exception caller instead
trc = 'Traceback (most recent call last):\n'
stack_str = trc + ''.join(traceback.format_list(stack))
if exc is not None:
stack_str += ' ' + traceback.format_exc().lstrip(trc)
return stack_str

0 comments on commit f479e49

Please sign in to comment.