def get(cls, playerName): """ Class method: GET Endpoint: /player Class method that handles GET requests for the Player Resource. Player data retrieval is accomplished using player id supplied in the URL parameters Args: playerName: Name supplied via URL parameters Returns: 'message' if the player exists return relative data for that player, otherwise error message stating that the user was not found. """ try: #: Object of type PlayerModel: Will store an object of the player we want data from. player = PlayerModel.findByPlayerName(playerName) except: return { 'message': 'There was an error finding the player in the DB!' } if player is not None: return player.json() return {'message': 'Player was not found!'}
def post(self): """ Class methods: POST Endpoint: /player-register Class method that handles post requests for the PlayerRegister Resource. Using payload from the POST request, it will use the 'playerName' and the 'secretKey' that were provided to create a new player. This class will only handle the registering process. Args: Returns: 'message' if either the user already exists or if the player was created succesfully. """ #: object of parsed items set to their appropiate type: This data is reetrieved from payload. data = PlayerRegister.__player_parse.parse_args() # Check if the player name is already taken if (PlayerModel.findByPlayerName(data['playerName'])): return {'message': 'User already exists!'}, 409 #Conflict # Uses the PlayerRegister function save_to_db() function that uses SQLAlchemys save and commit to register the player # to the database player = PlayerModel(data['playerName'], data['secretKey'], 'player', 'none', 'none', 100, 100) player.save_to_db() return {'message': 'Player was created succesfully!'}, 201
def post(self, lobby_id): """Class Method used for POST request for game lobby data. Args: lobby_id: Id of the lobby we want to trieve data from. Return: success message, if the game lobby exists, is not full and player is not part of it error message, if is full, player is part of it or doesn't exists """ # Checking if lobby exists #: Object of type LobbyModel: Used with SQLAlchemy for access and manipulation of obj. in DB lobby = LobbyModel.findById(lobby_id) if lobby is None: return {'message': 'Lobby does not exists!'} if (lobby.lobbySize == 3): return {'message': 'Lobby is full'} # Adding player to the lobby #: str : retrieves the playerName from the current jwt token playerName = current_identity.playerName #: Object of type PlayerModel: Used with SQLAlchemy to access and manipulate the obj. in the DB player = PlayerModel.findByPlayerName(playerName) if (player.currentLobby == lobby_id): return {'message': 'You are already part of the lobby!'} elif (player.currentLobby != -1): return {'message': 'You are part of a different lobby!'} # sets playes lobbyId to the one he joined player.currentLobby = lobby.lobbyId # Increases lobby size to account new player lobby.lobbySize = lobby.lobbySize + 1 lobby.save_to_db() # Create a home for new player home = LocationModel(player.playerName, 'home') home.save_to_db() player.locationId = home.id player.homeId = home.id player.save_to_db() return {'message': 'You have succesfully joined the lobby!'}
def post(self): """ Class method: POST /action """ data = PlayerAction.parse.parse_args() if (current_identity.status == 'sleep' and data['action'] != 'wakeup'): return {'message': 'You must be awake to take action!'} elif (data['action'] == 'attack'): player = PlayerModel.findByPlayerName(current_identity.playerName) target = PlayerModel.findByPlayerName(data['target']) location = LocationModel.findById(player.locationId) if (target.status == 'dead'): return {'message': 'target already dead'} # p is a player from the list of players in the current location for p in location.players: if (data['target'] == p.playerName): # once target is found, call confront method from player who attacked print(player.playerName) winner = player.confront(p) if (winner == player.playerName): target.status = 'dead' target.save_to_db() return {'message': 'kill'} else: player.status = 'dead' player.save_to_db() return {'message': 'dead'} return {'message': 'target was not in the location'} elif (data['action'] == 'sleep'): player = PlayerModel.findByPlayerId(current_identity.id) player.stamina = player.stamina + 10 player.status = 'sleep' player.save_to_db() return {'message': 'You decided to sleep: +10 stamina'} elif (data['action'] == 'wakeup'): player = PlayerModel.findByPlayerId(current_identity.id) player.status = 'none' player.save_to_db() return {'message': 'You are now awake'} elif (data['action'] == 'workout'): player = PlayerModel.findByPlayerId(current_identity.id) if (player.stamina == 0): player.status = "sleep" player.save_to_db() return { 'message': 'You are out of stamina and have fallen asleep!' } player.strength = player.strength + 10 player.stamina = player.stamina - 10 player.status = "sleep" player.save_to_db() return { 'message': 'You decided to sleep: +10 strength -10 stamina' } else: return {'message': 'Action is not supported!'}
def post(self): """ Class method: POST Class method that will handle POST request for the CreateLobby Resource. Users that will hit the '/create-lobby' will use this method. Attributes: Return: return a successful message that the Lobby has been created. return an error message if the lobby could not be made """ playerName = current_identity.playerName player = PlayerModel.findByPlayerName(playerName) # Checks if the user creating the lobby isn't part of one already if (player.currentLobby == -1): try: newLobby = LobbyModel(player.playerName) newLobby.save_to_db() except: return { 'message': 'Could not create lobby. Error with creating lobby.' } try: home = LocationModel(player.playerName, 'home') home.save_to_db() except: return { 'message': 'Could not create lobby. Error with creating player home.' } try: player.currentLobby = newLobby.lobbyId player.homeId = home.id player.locationId = home.id player.save_to_db() except: return { 'message': 'Could not create lobby. Error with setting player details' } try: clerkName = ''.join(reversed(player.playerName)) clerkSecretKey = ''.join(reversed(player.secretKey)) storeClerk = PlayerModel(clerkName, clerkSecretKey, 'npc', 'none', 'none', 100, 100) store = LocationModel(clerkName, 'store') store.save_to_db() storeClerk.homeId = store.id storeClerk.locationId = store.id storeClerk.currentLobby = player.currentLobby storeClerk.save_to_db() except: return { 'message': 'Could not create lobby. Error with creating clerk' } try: policeName = 'Chief Wiggum' policeSecretKey = 'ralph' gameCop = PlayerModel(policeName, policeSecretKey, 'npc', 'none', 'gun', 100, 100) policeStation = LocationModel(clerkName, 'station') policeStation.save_to_db() gameCop.homeId = policeStation.id gameCop.locationId = policeStation.id gameCop.currentLobby = player.currentLobby gameCop.save_to_db() except: return { 'message': 'Could not create lobby. Error with creating cop' } return {'message': 'Lobby was created succesfully!'} return {'message': 'Lobby already exists!'}