public static void CreateNewPlayer(string email, string username,
                                       string password, OnFinishConnectionCallback callback)
    {
        auth
        .CreateUserWithEmailAndPasswordAsync(email, password)
        .ContinueWith(
            task => {
            if (task.IsCanceled)
            {
                callback.ConnectionFinished(CallbackResult.Canceled,
                                            "CreateNewPlayer was canceled.");
                return;
            }
            if (task.IsFaulted)
            {
                callback.ConnectionFinished(CallbackResult.Faulted,
                                            "CreateNewPlayer encountered an error: " +
                                            task.Exception);
                return;
            }
            if (!task.IsCompleted)
            {
                callback.ConnectionFinished(CallbackResult.Invalid,
                                            "CreateNewPlayer failed to complete.");
                return;
            }

            // Firebase user has been created.
            user = task.Result;
            DocumentStore userData = new DocumentStore();
            userData["last_login"] = DateTime.Now.ToShortDateString();
            userData["username"]   = username;

            reference
            .Child("users")
            .Child(user.UserId)
            .SetValueAsync(userData);

            reference
            .Child("statistics")
            .Child(user.UserId)
            .SetValueAsync(initializeStatistics());

            reference
            .Child("progress")
            .Child(user.UserId)
            .SetValueAsync(initializeProgress());

            callback.ConnectionFinished(CallbackResult.Success,
                                        "User succesfully created.");
        }
            );
    }
Exemplo n.º 2
0
    public static void LoginPlayer(string email, string password,
                                   OnFinishConnectionCallback callback)
    {
        auth
        .SignInWithEmailAndPasswordAsync(email, password)
        .ContinueWith(
            task => {
            if (task.IsCanceled)
            {
                callback.ConnectionFinished(CallbackResult.Canceled,
                                            "LoginPlayer was canceled.");
                return;
            }
            if (task.IsFaulted)
            {
                callback.ConnectionFinished(CallbackResult.Faulted,
                                            "LoginPlayer encountered an error: " +
                                            task.Exception);
                return;
            }
            if (!task.IsCompleted)
            {
                callback.ConnectionFinished(CallbackResult.Invalid,
                                            "LoginPlayer failed to complete.");
                return;
            }

            user = task.Result;

            /* Update a user's last login date once they've logged in.
             */
            reference
            .Child("users")
            .Child(user.UserId)
            .Child("last_login")
            .SetValueAsync(DateTime.Now.ToShortDateString());

            callback.ConnectionFinished(CallbackResult.Success,
                                        "User succesfully logged in.");
        }
            );
    }
Exemplo n.º 3
0
    /* Create a new player.
     *
     * Requires the use of the OnFinishConnectionCallback, which must define the implementation
     * of the ConnectionFinished method, which receives a CallbackResult and a message as parameters.
     */
    public static void CreateNewPlayer(string email, string username,
                                       string password, OnFinishConnectionCallback callback)
    {
        auth
        .CreateUserWithEmailAndPasswordAsync(email, password)
        .ContinueWith(
            task => {
            if (task.IsCanceled)
            {
                callback.ConnectionFinished(CallbackResult.Canceled,
                                            "CreateNewPlayer was canceled.");
                return;
            }
            if (task.IsFaulted)
            {
                callback.ConnectionFinished(CallbackResult.Faulted,
                                            "CreateNewPlayer encountered an error: " +
                                            task.Exception);
                return;
            }
            if (!task.IsCompleted)
            {
                callback.ConnectionFinished(CallbackResult.Invalid,
                                            "CreateNewPlayer failed to complete.");
                return;
            }

            // Firebase user has been created.
            user = task.Result;
            DocumentStore userData = new DocumentStore();
            userData["last_login"] = DateTime.Now.ToShortDateString();
            userData["username"]   = username;

            /* Save the user's last login date and username in the database.
             */
            reference
            .Child("users")
            .Child(user.UserId)
            .SetValueAsync(userData);

            /* Initialize at 0:
             * - Number of rejected missions
             * - Number of accepted missions
             * - Number of abandoned missions
             * - Number of failed missions
             * - Number of completed missions
             * - Total play time
             */
            reference
            .Child("statistics")
            .Child(user.UserId)
            .SetValueAsync(initializeStatistics());

            /* Initialize at 0/false, when applicable:
             * - Current level
             * - If the user has seen the game's intro
             * - If the user has passed the math game's tutorial
             */
            reference
            .Child("progress")
            .Child(user.UserId)
            .SetValueAsync(initializeProgress());

            callback.ConnectionFinished(CallbackResult.Success,
                                        "User succesfully created.");
        }
            );
    }