コード例 #1
0
 def addFriendRequest(self,username,otherUsername):
     """
         Input: username: String, otherUsername: String
         Output: Success[String] or Failure
         Description: username should be the current user and otherUsername
             should be the user they are requesting to be friends with.
     """
     isRealPlayers = self.playersExist([username,otherUsername])
     if (not isRealPlayers):
         return Failure("User does not exist!")
     else:
         userOnerequests = self.getFriendRequestsForUser(username)
         userTwoRequests = self.getFriendRequestsForUser(otherUsername)
         if (userOnerequests.isFailure()):
             return userOnerequests
         elif (userTwoRequests.isFailure()):
             return userTwoRequests
         else:
             userOneRequestsList = csvToList(userOnerequests.getOrElse(""))
             userTwoRequestsList = csvToList(userTwoRequests.getOrElse(""))
             if (username in userTwoRequestsList):
                 return Failure("User has already made this request")
             elif (otherUsername in new userOneRequestsList):
                 return Failure("The other user has already sent this user a request")
             else:
                 userOneRequestsList.append(otherUsername)
                 result = self.database.updateFriendRequestList(username,listToCsvString(userOneRequestsList))
                 return result
コード例 #2
0
 def deleteGame(self,gameId,user):
     """
         Input: gameId: String, user: String
         Output: Success[String] or Failure
         Description: Deletes a game given a gameId and the user deleting the game.
     """
     print("User: '******', is deleting a game with gameId: " + gameId)
     maybeGame = self.getGameFromGameId(gameId)
     if (maybeGame.isFailure()):
         return maybeGame
     else:
         game = maybeGame.get()
         players = game.getPlayers()
         if (user not in players):
             return Failure("User is not a part of this game")
         else:
             maybeDeltedGameA = self.deleteGameIdForUser(gameId,players[0])
             maybeDeltedGameB = self.deleteGameIdForUser(gameId,players[0])
             maybeDeletedGameData = self.database.deleteGameByGameId(gameId)
             if (maybeDeltedGameA.isFailure()):
                 return maybeDeltedGameA
             elif (maybeDeltedGameB.isFailure()):
                 return maybeDeltedGameB
             elif (maybeDeletedGameData.isFailure()):
                 return Failure("Failed to delete game data ~" +
                     maybeDeletedGameData.getErrorMessage())
コード例 #3
0
 def removeFriend(self,username,friend):
     """
         Input: username: String, friend: String
         Output: Success[String] or Failure
     """
     isRealPlayers = self.playersExist([username,friend])
     if (not isRealPlayers):
         return Failure("User does not exist!")
     else:
         userOneFriends = self.getFriendsForUser(username)
         userTwoFriends  = self.getFriendsForUser(friend)
         if (userOneFriends.isFailure()):
             return userOneFriends
         if (userTwoFriends.isFailure()):
             return userTwoFriendsCsv
         else:
             userOneFriendsList = csvToList(userOneFriends.getOrElse([]))
             userTwoFriendsList = csvToList(userTwoFriends.getOrElse([]))
             if (friend not in userOneFriendsList):
                 return Failure("User is not friends with this other user")
             elif (username not in userTwoFriendsList):
                 return Failure("Friend is not friends with this user")
             else:
                 userOneFriendsList.remove(friend)
                 userTwoFriendsList.remove(username)
                 resultOne = self.database.updateFriendList(username,listToCsvString(userOneFriendsList))
                 resultTwo = self.database.updateFriendList(friend,listToCsvString(userTwoFriendsList))
                 if (resultOne.isFailure()):
                     return resultOne
                 elif (resultTwo.isFailure()):
                     return resultTwo
                 else:
                     return Success("Friend Removed")
コード例 #4
0
 def getGameFromGameId(self,gameId):
     """
         Input: gameId: String
         Output: Success[Game] or Failure
     """
     if (self.database.gameIdExists(gameId).getOrElse([(0,)])[0][0] != 1):
         return Failure("Game Id doesn't exist")
     else:
         gameRowResult = self.database.getByGameId(gameId)
         if (gameRowResult.isFailure()):
             return Failure("Failed to get game from the database")
         else:
             gameData = gameRowResult.get()[0][0]
             decodedGame = self.jsonDecoder.decode(gameData)
             return Success(decodedGame)
コード例 #5
0
 def writeQuery(self, queryString, parameters):
     try:
         self.cursor.execute(queryString, parameters)
         return Success("Write Query Successful")
     except:
         print("Error executing write query")
         return Failure("Error executing write query")
コード例 #6
0
 def deleteGameIdForUser(self,gameId,username):
     """
         Input: gameId: String, username: String
         Output: Success[String] or Failure
     """
     if (self.database.userExists(username).getOrElse([(0,)])[0][0] != 1):
         return Failure("User does not exist!")
     else:
         maybeGameIds = self.database.getGameIdsForUser(username)
         gameIds = maybeGameIds.getOrElse([("",)])[0][0].split(",")
         if (gameId not in gameIds):
             return Failure("GameId does not exist for user")
         else:
             gameIds.remove(gameId)
             newGameIdsCsv = listToCsvString(gameIds)
             return self.database.updateGameIdsForUser(newGameIdsCsv,username)
コード例 #7
0
 def getQuery(self, queryString, parameters):
     try:
         self.cursor.execute(queryString, parameters)
     except:
         print("Error executing get query")
         return Failure("Error executing get query")
     rows = self.cursor.fetchall()
     result = Success(rows)
     return result
コード例 #8
0
 def acceptFriendRequest(self,username,otherUsername):
     """
         Input: username: String, otherUsername: String
         Output: Success[String] or Failure
     """
     isRealPlayers = self.playersExist([username,otherUsername])
     if (isRealPlayers):
         return Failure("User does not exist!")
     else:
         userOneRequests = self.getFriendRequestsForUser(username)
         userOneFriends  = self.getFriendsForUser(username)
         userTwoFriends  = self.getFriendsForUser(otherUsername)
         if (userOneRequests.isFailure()):
             return userOneRequests
         elif (userOneFriends.isFailure()):
             return userOneFriends
         elif (userTwoFriends.isFailure()):
             return userTwoFriends
         else:
             newUserOneRequests = userOneRequests.getOrElse([])
             newUserOneFriends  = userOneFriends.getOrElse([])
             newUserTwoFriends  = userTwoFriends.getOrElse([])
             if (otherUsername not in newUserOneRequests):
                 return Failure("Other user is not in users requests")
             elif (otherUsername in newUserOneFriends or username in newUserTwoFriends):
                 return Failure("Users are already friends")
             else:
                 newUserOneRequests.remove(otherUsername)
                 newUserOneFriends.append(otherUsername)
                 newUserTwoFriends.append(username)
                 resultOne   = self.database.updateFriendRequestList(username,listToCsvString(newUserOneRequests))
                 resultTwo   = self.database.updateFriendList(username,listToCsvString(newUserOneFriends))
                 resultThree = self.database.updateFriendList(otherUsername,listToCsvString(newUserTwoFriends))
                 if (resultOne.isFailure()):
                     return resultOne
                 elif (resultTwo.isFailure()):
                     return resultTwo
                 elif (resultThree.isFailure()):
                     return resultThree
                 else:
                     return Success("Friend Request Accepted")
コード例 #9
0
 def getFriendRequestsForUser(self,username):
     """
         Input: username: String
         Output: Success[List[String]] or Failure
     """
     if (self.database.userExists(username).getOrElse(0) != 1):
         return Failure("User does not exist!")
     else:
         maybeFriendRequestsCsv = self.database.getFriendRequestsForUser(username)
         if (maybeFriendRequestsCsv.isFailure()):
             return maybeFriendRequestsCsv
         else:
             friendRequestsCsv = maybeFriendRequestsCsv.get()[0][0]
             return friendRequestsCsv.split(",")
コード例 #10
0
    def createGame(self,gameName,players):
        """
            Input: gameName: String, players: List[String]
            Output: Success[Game] or Failure
        """
        gameId = self.getNextGameId()
        if (not self.playersExist(players)):
            return Failure("User does not exist")
        elif (gameName == "ChessGame"):
            game = ChessGame(str(gameId),players)
            playerOneIds = self.database.getGameIdsForUser(players[0]).get()[0][0]
            newPlayerOneIds = listToCsvString(playerOneIds.split(",").append(gameId))

            playerTwoIds = self.database.getGameIdsForUser(players[1]).get()[0][0]
            newPlayerTwoIds = listToCsvString(playerTwoIds.split(",").append(gameId))

            updateOne = self.database.updateGameIdsForUser(newPlayerOneIds,players[0])
            updateTwo = self.database.updateGameIdsForUser(newPlayerTwoIds,players[1])
            if (updateOne.isSuccess() and updateTwo.isSuccess()):
                return Success(game)
            else:
                Failure("Failed to update game ids for a user")
        else:
            return Failure("Generic Failure")
コード例 #11
0
 def changeUsersPassword(self,username,oldPassword,newPassword):
     playerExists = self.database.userExists(username)
     if (playerExists.getOrElse(0) != 1):
         return Failure("User does not exist!")
     else:
         if (username == "uni"):
             return Failure("Cannot update password")
         else:
             maybePasswordHash = self.database.getUsersPassword(username)
             if (maybePasswordHash.isFailure()):
                 return Failure("Failed to get users old password")
             else:
                 if (encryption_context.verify(oldPassword,maybePasswordHash.getOrElse(""))):
                     result = self.database.changePassword(username,self.encryptString(newPassword))
                     if (result.isFailure()):
                         logResultOne = self.database.enterLogMessage("Update password Query error for user: '******'")
                         if (logResultOne.isFailure()):
                             print(logResultOne.getErrorMessage())
                         return result
                     else:
                         logResultTwo = self.database.enterLogMessage("'" + username + "' changed their password")
                         if (logResultTwo.isFailure()):
                             print(logResultTwo.getErrorMessage())
                         return Success("Password Changed Successfully")
コード例 #12
0
 def declineFriendRequest(self,username,otherUsername):
     """
         Input: username: String, otherUsername: String
         Output: Success[String] or Failure
     """
     userRequests = self.getFriendRequestsForUser(username)
     if (userRequests.isFailure()):
         return userRequests
     else:
         newUserRequests = userRequests.getOrElse([])
         if (otherUsername not in newUserRequests):
             return Failure("Other user not in users friend requests")
         else:
             newUserRequests.remove(otherUsername)
             result = self.database.updateFriendRequestList(username,listToCsvString(newUserRequests))
             return result
コード例 #13
0
def backend():
    if request.method == "POST":
        failure = request.form["failure"]
        count = request.form["count"]
        status = request.form["status"]

        new_failure = Failure(failure, count, status)
        db_session.add(new_failure)
        db_session.commit()

        data = {
            "id": new_failure.id,
            "failure": failure,
            "count": count,
            "status": status}
            
        pusher_client.trigger('my-channel', 'my-event', {'data': data})

        return redirect("/backend", code=302)
    else:
        failures = Failure.query.all()
        return render_template('backend.html', failures=failures)