Exemplo n.º 1
0
    private bool LoadUserData()
    {
        if (File.Exists(Application.persistentDataPath + "/" + userInfoFileName))
        {
            try
            {
                BinaryFormatter     bf   = new BinaryFormatter();
                FileStream          file = File.Open(Application.persistentDataPath + "/" + userInfoFileName, FileMode.Open);
                TwitchLoginUserInfo data = (TwitchLoginUserInfo)bf.Deserialize(file);
                userData = data;
                file.Close();
            }
            catch
            {
                return(false);
            }

            if (EventList.TwitchUserLoadedFromFile != null)
            {
                EventList.TwitchUserLoadedFromFile(userData);
            }

            return(true);
        }

        return(false);
    }
    private void Awake()
    {
        if (instance != null)
        {
            Debug.LogError("There were two FirebaseManagers and there can only be one. Deleting the gameobject that had the second one.");
            Destroy(this.gameObject);
            return;
        }

        instance = this.gameObject;

        if (TwitchUserManager.instance != null)
        {
            TwitchLoginUserInfo userInfo = TwitchUserManager.instance.GetComponent <TwitchUserManager>().userInfo;
            channelId = userInfo.user_id;
            hash      = userInfo.hash;
        }
        else if (string.IsNullOrEmpty(channelId) || string.IsNullOrEmpty(hash))
        {
            Debug.LogError("Could not get channel Id or hash. Unable to set up Firebase.");
            return;
        }

        recentLaunches  = new Queue <string>();
        deletedLaunches = new List <string>();
        deletedUpgrades = new List <string>();
        updatedUsers    = new List <User>();

        RemoteSettingsUpdated();
        remoteSettingsUpdatedHandler = new RemoteSettings.UpdatedEventHandler(RemoteSettingsUpdated);
        RemoteSettings.Updated      += remoteSettingsUpdatedHandler;

        FirebaseApp.DefaultInstance.SetEditorDatabaseUrl(databaseUrl);
        launchesRef             = FirebaseDatabase.DefaultInstance.GetReference(string.Format("{0}/{1}", launchesRoot, channelId));
        launchesRef.ChildAdded += OnLaunchAdded;

        EventList.PublishUserUpdate += UserInfoUpdatedHandler;

        playersRef = FirebaseDatabase.DefaultInstance.GetReference(string.Format("{0}/{1}", playersRoot, channelId));
        playersRef.GetValueAsync().ContinueWith(task => {
            if (task.IsFaulted)
            {
                Debug.LogError(task.Exception.Message);
                return;
            }

            LoadPlayerData(task.Result);
        });
        playersRef.ChildAdded   += OnPlayerChanged;
        playersRef.ChildChanged += OnPlayerChanged;

        upgradesRef                 = FirebaseDatabase.DefaultInstance.GetReference(string.Format("{0}/{1}", upgradesName, channelId));
        upgradesRef.ChildAdded     += OnUpgradeAdded;
        EventList.UpgradeProcessed += OnUpgradeProcessed;

        EventList.MissionStarted += MissionStartedHandler;
    }
    public IEnumerator CheckTwitchToken()
    {
        if (string.IsNullOrEmpty(token))
        {
            if (EventList.TwitchUserLogOut != null)
            {
                EventList.TwitchUserLogOut();
            }
            yield break;
        }
        else
        {
            //UnityWebRequest req = UnityWebRequest.Get("https://id.twitch.tv/oauth2/validate");
            UnityWebRequest req = UnityWebRequest.Get(validateURL);
            //req.SetRequestHeader("Authorization", "OAuth " + token);
            req.SetRequestHeader("Authorization", token);
            yield return(req.SendWebRequest());

            if (req.isNetworkError || req.isHttpError)
            {
                string debugMsg = "";
                if (req.isNetworkError)
                {
                    debugMsg = "Network error when checking Twitch token.";
                }
                else if (req.isHttpError)
                {
                    debugMsg = "HTTP error when checking Twitch token.";
                }

                Debug.LogError(debugMsg);
                Debug.LogError(req.error);
                if (EventList.TwitchUserLogOut != null)
                {
                    EventList.TwitchUserLogOut();
                }
                yield break;
            }
            else
            {
                //Debug.Log(req.downloadHandler.text);
                TwitchLoginUserInfo userInfo = JsonUtility.FromJson <TwitchLoginUserInfo>(req.downloadHandler.text);
                userInfo.access_token = token;
                if (string.IsNullOrEmpty(userInfo.login) == false && string.IsNullOrEmpty(userInfo.user_id) == false && EventList.TwitchUserLogin != null)
                {
                    EventList.TwitchUserLogin(userInfo);
                }
                else if (EventList.TwitchUserLogOut != null)
                {
                    EventList.TwitchUserLogOut();
                }

                yield break;
            }
        }
    }
Exemplo n.º 4
0
    public void SignInOutClick()
    {
        if (userInfo == null)
        {
            // user is not signed in, show the connect screen
            if (ConnectPromptParent != null)
            {
                if (ConnectPromptParent.transform.position.x > -1 == false)
                {
                    ConnectPromptParent.transform.Translate(new Vector3(translateAmt, 0, 0));
                }
                //ConnectPromptParent.SetActive(true);
                Browser browser = ConnectPromptParent.GetComponentInChildren <Browser>();
                browser.Zoom = BrowserZoomLevel;
                browser.Url  = twitchLoginURL;
            }
        }
        else
        {
            // user is signed in, sign them out
            if (EventList.TwitchUserLogOut != null)
            {
                EventList.TwitchUserLogOut();
            }

            if (SignInOutButton != null)
            {
                Text buttonText = SignInOutButton.GetComponentInChildren <Text>();
                if (buttonText != null)
                {
                    buttonText.text = CONNECT_TO_TWITCH;
                }

                if (usernameText != null)
                {
                    usernameText.text = NOT_CONNECTED;
                }
            }

            userInfo = null;
        }
    }
Exemplo n.º 5
0
    public void SetUserData(TwitchLoginUserInfo userObj)
    {
        userInfo = userObj;
        if (usernameText != null)
        {
            usernameText.text = userObj.login;
        }

        if (SignInOutButton != null)
        {
            Text buttonText = SignInOutButton.GetComponentInChildren <Text>();
            if (buttonText != null)
            {
                buttonText.text = SIGN_OUT;
            }
        }

        StartCoroutine(SetProfilePicture());

        ConnectPromptBackClick();
    }
Exemplo n.º 6
0
 private void OnTwitchUserLogin(TwitchLoginUserInfo userInfo)
 {
     userData = userInfo;
     SaveUserData();
 }
Exemplo n.º 7
0
 public void SetUserData(TwitchLoginUserInfo userObj)
 {
     userInfo = userObj;
 }
Exemplo n.º 8
0
 private void LogoutHandler()
 {
     userInfo = null;
 }
 private void UserLoadedHandler(TwitchLoginUserInfo loadedData)
 {
     token = loadedData.access_token;
     StartCoroutine(CheckTwitchToken());
 }