예제 #1
0
    public void BTN_Submit()
    {
        string userName = nameInputField.text;
        Gender gender   = (Gender)genderDropdown.value;

        bool areBallsVisible     = UIController.AreBallsVisible;
        bool isVibrationActive   = UIController.IsVibrationActive;
        bool isBlinkEffectActive = UIController.IsBlinkEffectActive;
        bool isSoundActive       = UIController.IsSoundActive;

        if (DoesSaveDataFileExist())
        {
            // Some users already exist
            byte[] bytes = File.ReadAllBytes(filePath);

            string   allUsersJson = Encoding.ASCII.GetString(bytes);
            AllUsers allUsers     = JsonUtility.FromJson <AllUsers>(allUsersJson);

            UserData         userData         = new UserData(userName, gender);
            GameData         gameData         = new GameData(areBallsVisible, isVibrationActive, isBlinkEffectActive, isSoundActive);
            SpecificGameData specificGameData = new SpecificGameData(time, isCorrect);

            if (allUsers.ContainsUserData(userData, out userData))
            {
                // The user exists

                if (userData.ContainsGameData(gameData, out gameData))
                {
                    // GameData exists
                    gameData.AddSpecificGame(specificGameData);
                }
                else
                {
                    // GameData does not exist
                    gameData.AddSpecificGame(specificGameData);
                    userData.AddGameData(gameData);
                }
            }
            else
            {
                // The user does not exist
                gameData.AddSpecificGame(specificGameData);
                userData.AddGameData(gameData);
                allUsers.AddUser(userData);
            }

            // Write new json to file
            allUsersJson = JsonUtility.ToJson(allUsers);
            bytes        = Encoding.ASCII.GetBytes(allUsersJson);
            File.WriteAllBytes(filePath, bytes);
        }
        else
        {
            // Create new file with the first user

            var userData = GenerateAndPopulateNewUserData(
                userName, gender,
                areBallsVisible, isVibrationActive, isBlinkEffectActive, isSoundActive);

            AllUsers allUsers = new AllUsers();
            allUsers.AddUser(userData);

            string allUsersJson = JsonUtility.ToJson(allUsers);
            Debug.Log(allUsersJson);

            byte[] bytes = Encoding.ASCII.GetBytes(allUsersJson);
            File.WriteAllBytes(filePath, bytes);
        }

        Time.timeScale = 1;
        SceneManager.LoadScene(0);
    }