class Logic: def __init__(self): # Create SDK object and current user variable: self.s = Sdk() self.current_user = None def login(self, username, password): # Receive either a response error message or a user dictionary: response = self.s.login(username, password) # Check if user was received by type-checking: if isinstance(response, dict): self.current_user = response # Success message forwarded return "Success!" else: # Error Message forwarded return response def logout(self): # Sets current user to null: self.current_user = None def create_user(self, user_dict): # Response Message forwarded: return self.s.create_user( user_dict["first_name"], user_dict["last_name"], user_dict["username"], user_dict["password"], user_dict["email"], ) def create_game(self, game_name, host_controls, map_size): # Response Message forwarded: return self.s.create_game(self.current_user, game_name, host_controls, map_size) def join_game(self, opponent_controls, game_id): # Reserve spot: Join game if it's open: status = self.s.join_game(self.current_user["id"], game_id) # Check if the game was successfully joined: if status is not "Game closed": # Play game: Start game by sending controls: status = self.s.start_game(self.current_user, opponent_controls, game_id) # Check if the current user won: WIN if status["opponent"]["winner"]: # Here; the opponent key is always the current user # Calculate the score difference: point_difference = status["opponent"]["score"] - status["host"]["score"] # String forwarded: return "You won the game by " + str(point_difference) + " points!" # Check if the host won: LOSS elif status["host"]["winner"]: # Calculate the score difference; vars in reverse order: point_difference = status["host"]["score"] - status["opponent"]["score"] # String forwarded: return "You lost the game by " + str(point_difference) + " points" # If nobody won: DRAW else: # String forwarded: return "It's a draw!" else: # Response Message forwarded: return status def delete_game(self, game_id): # Response Message forwarded: return self.s.delete_game(game_id) def get_open_games(self): # List of game dictionaries forwarded: return self.s.get_open_games() def get_high_score(self): # List of high score dictionaries forwarded: return self.s.get_high_scores() def get_config(self): # Create string to store config data: config_data = "" # Loop through keys in config from sdk: for key in sorted(self.s.config): # Dictionary is sorted to display SQL-credentials together. # Add keys and their respective value to the config_data string. config_data += key + ": " + self.s.config[key] + "\n" # Represented for human reading # Config data forwarded return config_data @staticmethod def is_dict_full(dictionary): # Loops through dictionary to find empty values: for value in dictionary.values(): if value == "" or value is None: return False # If no values were empty or null; return True. return True @staticmethod def valid_moves(moves, map_size): # Check if moves fit map size: if len(moves) > int(map_size): return False # Loop through each char in the string: for m in moves: # Check if the moves are valid: if m.lower() not in ["w", "a", "s", "d"]: return False # If all moves are valid; return True. return True