Beispiel #1
0
def check_password(user_id: str) -> None:
    """Check if user's password(input) is valid.

    Get input(user's password) and search for it in json.
    If the input matches with the password of the user, return user's info to sud.py so that user starts game.
    If the input won't match with the password of user, ask again and print helpful message.
    If the user input invalid password 3 times, print helpful message and quit the game.
    PARAM: user_id, a string
    PRECONDITION: user_id, must be a string
    PRECONDITION: json file must exist
    POSTCONDITION: Print helpful message every time user input invalid password
    POSTCONDITION: Print helpful message when user input invalid password 3 times
    """
    characters = support_function.read_json(
    )  # read Json (dictionaries of characters)
    valid_users = list(
        characters.keys())  # list of characters in order (e.g. chr1, chr2 ..)
    count = 0
    is_pw_matched = False
    while not is_pw_matched:
        user_answer = input("Enter password, please.")
        for user_num in range(len(characters)):
            if user_id == characters[valid_users[user_num]]['Id'] and \
                    user_answer != characters[valid_users[user_num]]['Pw']:
                print("Wrong password.")
                count += 1
                if count == 3:
                    print("Identification failed, restart game please.")
                    is_pw_matched = True
            elif user_id == characters[valid_users[user_num]]['Id'] and\
                    user_answer == characters[valid_users[user_num]]['Pw']:
                sud.play(characters[valid_users[user_num]])  # Start Game
                is_pw_matched = True
Beispiel #2
0
def check_id() -> None:
    """Check user's id (input) is valid.

    Get input(user's id) and search for it in json.
    If it is valid id, invoke check_password().
    If it is invalid id, ask if the user want to create new character.
    If the user wants, invoke create_new_character(), and if the user doesn't, print helpful message and quit the game.
    PRECONDITION: json file must exist.
    POSTCONDITION: Print helpful message and quit the game if user doesn't have valid id and doesn't want to make a new
                    character.
    """
    characters = support_function.read_json(
    )  # read Json (dictionaries of characters)

    chr_list = list(
        characters.values())  # list of dictionaries of character info
    id_list = [chr_list[i]['Id']
               for i in range(len(chr_list))]  # list of Id of characters

    user_name = input("What is your name? ").strip().lower()

    is_valid_user = False
    while not is_valid_user:
        if user_name in id_list:
            check_password(user_name)
        else:
            user_choice = input(
                "%s is not registered, do you want to create Character? (Y/N) "
                % user_name)
            if user_choice == 'y':
                create_new_character()
            else:
                print("Thank you, restart game please.")
        is_valid_user = True
Beispiel #3
0
    def test_dump_to_json_if_writes_correctly(self):
        """Test dump_to_json with dumping dictionary if it writes properly

        The result is expected True
        """
        support_function.dump_to_json(self.my_dump_chr2)  #
        dump_data = support_function.read_json()
        self.assertDictEqual(self.my_dump_chr2, dump_data)
Beispiel #4
0
    def test_read_json_with_different_data(self):
        """Test read_json if it won't read properly what haven't dumped.

        The result is expected True
        """
        with open("characters.json", 'w') as file_object:
            json.dump(self.my_chr, file_object)
        test_data = support_function.read_json()
        self.assertNotEqual(test_data, self.my_chr2)
Beispiel #5
0
    def test_read_json_with_same_data(self):
        """Test read_json if it reads json properly after dumping.

        The result is expected True
        """
        with open("characters.json", 'w') as file_object:
            json.dump(self.my_chr, file_object)
        test_data = support_function.read_json()
        self.assertDictEqual(test_data, self.my_chr)
    def test_save_user_info_if_it_saves_well(self):
        """Test save_user_info if it saves properly when info is dumped.

        The result is expected True
        """
        support_function.dump_to_json(
            {})  # make a new json and dump an empty dictionary
        support_function.add_new_chr_to_json(
            self.save_chr1["Id"],
            self.save_chr1["Pw"])  # dump save_chr1 dict to json
        support_function.add_new_chr_to_json(
            self.save_chr2["Id"],
            self.save_chr2["Pw"])  # add save_chr2 dict to json

        support_function.save_user_info(
            self.changed_chr2)  # changed info of chr2
        json_data = support_function.read_json()  # read info after saved

        self.assertEqual(json_data["chr2"], self.changed_chr2)
Beispiel #7
0
def create_id() -> str:
    """Create id.

    Get user's input(id) and check json if the id is in use.
    If so, ask user to input different id until user inputs the valid id.
    If the id user inputted is not in use, return the id to confirm_id()
    POSTCONDITION: Return valid id
    """
    user_name = input("Which name do you want to use? ")
    characters = support_function.read_json(
    )  # read Json (dictionaries of characters)
    chr_list = list(
        characters.values())  # list of dictionaries of character info
    id_list = [chr_list[i]['Id']
               for i in range(len(chr_list))]  # list of Id of characters

    if user_name in id_list:
        print("Sorry, the id is already in use.")
        create_id()
    else:
        confirm_id(user_name)
    return user_name