/// <summary>
        /// Override OnCreate method
        /// </summary>
        /// <param name="savedInstanceState"></param>
        protected override void OnCreate(Bundle savedInstanceState)
        {
            base.OnCreate(savedInstanceState);
            SetContentView(Resource.Layout.activity_achievements);

            // Set the activity back button on the actionbar
            SupportActionBar.SetDisplayHomeAsUpEnabled(true);

            #region Initialization
            mProgressBar  = FindViewById <ProgressBar>(Resource.Id.achProgressBar);
            mRecyclerView = FindViewById <RecyclerView>(Resource.Id.recyclerView);
            AchievementsDA achievementsDA = new AchievementsDA();

            Task.Factory.StartNew(() =>
            {
                RunOnUiThread(() => mProgressBar.Visibility = ViewStates.Visible);
                if (DbConnector.OpenSQLConnection())
                {
                    mAchievementsCompl = achievementsDA.GetAllAchievements(Intent.GetStringExtra("Username"));
                }
                DbConnector.CloseSQLConnection();

                // Handle columns on orientation change
                if (Resources.Configuration.Orientation == Android.Content.Res.Orientation.Portrait)
                {
                    mLayoutManager = new GridLayoutManager(this, 1);
                }
                else
                {
                    mLayoutManager = new GridLayoutManager(this, 2);
                }

                // Load achievements into CardViews
                RunOnUiThread(() =>
                {
                    mRecyclerView.SetLayoutManager(mLayoutManager);
                    mAdapter = new RecyclerAdapter(mAchievementsCompl, this);
                    mRecyclerView.SetAdapter(mAdapter);
                    mProgressBar.Visibility = ViewStates.Gone;
                });
            });
            #endregion
        }
Example #2
0
        /// <summary>
        /// Handles BtnSignUp click event
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void BtnSignUp_Click(object sender, EventArgs e)
        {
            string username = mTxtUsername.Text.Trim();
            string fullname = mTxtFullName.Text.Trim();
            string email    = mTxtEmail.Text.Trim();

            #region Input validation
            if (username.Length < 1 || fullname.Length < 1 || email.Length < 1 || mTxtDOB.Text.Length < 1 ||
                mTxtPwd.Text.Length < 1 || mTxtConfPwd.Text.Length < 1)
            {
                string error = "Please fill all the fields.";
                AlertGenerator.ShowError(error, this);
                return;
            }
            else if (mTxtPwd.Text.Length < 8)
            {
                string error = "Password must contain at least 8 characters.";
                AlertGenerator.ShowError(error, this);
                return;
            }
            else if (mTxtPwd.Text != mTxtConfPwd.Text)
            {
                string error = "Password and Confirm Password do not match.";
                AlertGenerator.ShowError(error, this);
                return;
            }
            #endregion

            // Disabling controls while saving the record
            DisableEnableControls(false);

            Task.Factory.StartNew(() =>
            {
                if (DbConnector.OpenSQLConnection())
                {
                    // Connection opened
                    UserDA userDA = new UserDA();
                    if (userDA.IsUsernameExists(username))
                    {
                        string error = "The Username entered already exist.";
                        RunOnUiThread(() =>
                        {
                            //Enabling the controls back
                            DisableEnableControls(true);
                            AlertGenerator.ShowError(error, this);
                        });
                    }
                    else
                    {
                        byte[] imgBytes = null;
                        if (mUserImg != null)
                        {
                            // Converting the bitmap into a byte array
                            MemoryStream memStream = new MemoryStream();
                            mUserImg.Compress(Bitmap.CompressFormat.Webp, 100, memStream);
                            imgBytes = memStream.ToArray();
                        }

                        User user = new User()
                        {
                            Username = username,
                            FullName = fullname,
                            Email    = email,
                            DOB      = DateTime.Parse(mTxtDOB.Text),
                            Password = CryptoHasher.Hash(mTxtPwd.Text),
                            Pic      = imgBytes
                        };

                        DbConnector.CloseSQLConnection(); // Close connection to the database
                        if (DbConnector.OpenSQLConnection())
                        {
                            if (userDA.InsertUser(user))
                            {
                                // User was added successfully
                                // Add a record to leaderboard
                                PlayerStats stats = new PlayerStats()
                                {
                                    Username = username,
                                    XP       = 0,
                                    SR       = 0,
                                    PlayTime = TimeSpan.FromSeconds(0),
                                    Golds    = 0,
                                    Silvers  = 0,
                                    Bronzes  = 0
                                };

                                DbConnector.CloseSQLConnection(); // Close connection to the database
                                PlayerStatsDA statsDA = new PlayerStatsDA();
                                if (DbConnector.OpenSQLConnection())
                                {
                                    if (!statsDA.InsertStats(stats))
                                    {
                                        string error = "There was a problem updating your stats.";
                                        RunOnUiThread(() =>
                                        {
                                            AlertGenerator.ShowError(error, this);
                                        });
                                    }
                                }

                                // Add achievements
                                List <AchievementsCompleted> achievementsCompleted = new List <AchievementsCompleted>();
                                for (int i = 1; i < 20; i++)
                                {
                                    achievementsCompleted.Add(new AchievementsCompleted()
                                    {
                                        Username = username,
                                        Progress = 0,
                                        Status   = false
                                    });
                                }

                                DbConnector.CloseSQLConnection(); // Close connection to the database
                                if (DbConnector.OpenSQLConnection())
                                {
                                    AchievementsDA achievementsDA = new AchievementsDA();
                                    if (!achievementsDA.InsertAchievements(achievementsCompleted))
                                    {
                                        string error = "There was a problem updating your stats.";
                                        RunOnUiThread(() =>
                                        {
                                            AlertGenerator.ShowError(error, this);
                                        });
                                    }
                                }

                                // Navigate to Login activity
                                Intent intent = new Intent(this, typeof(LoginActivity));
                                StartActivity(intent);
                                OverridePendingTransition(Resource.Animation.slide_in_left, Resource.Animation.slide_out_right);
                                Finish(); // Close this activity
                            }
                            else
                            {
                                string error = "There was a problem saving the record.";
                                RunOnUiThread(() =>
                                {
                                    //Enabling the controls back
                                    DisableEnableControls(true);
                                    AlertGenerator.ShowError(error, this);
                                });
                            }
                        }
                        else
                        {
                            // Connection could not be opened
                            string error = "Connection to the database could not be established.";
                            RunOnUiThread(() =>
                            {
                                //Enabling the controls back
                                DisableEnableControls(true);
                                AlertGenerator.ShowError(error, this);
                            });
                        }
                    }
                }
                else
                {
                    // Connection could not be opened
                    string error = "Connection to the database could not be established.";
                    RunOnUiThread(() =>
                    {
                        //Enabling the controls back
                        DisableEnableControls(true);
                        AlertGenerator.ShowError(error, this);
                    });
                }

                DbConnector.CloseSQLConnection(); // Close connection to the database
            });
        }
Example #3
0
        /// <summary>
        /// Method for updating player stats and achievements once a puzzle is fixed
        /// </summary>
        /// <param name="scores"></param>
        private void UpdatePlayerStats(PlayerStats scores)
        {
            if (mUsername != String.Empty)
            {
                Task.Factory.StartNew(() =>
                {
                    PlayerStats stats = new PlayerStats();

                    #region Update Player Stats
                    if (DbConnector.OpenSQLConnection())
                    {
                        // Connection opened
                        PlayerStatsDA statsDA = new PlayerStatsDA();
                        if (statsDA.IsStatsExist(mUsername))
                        {
                            // Stats exist, therefore update
                            stats           = statsDA.FindStats(mUsername);
                            stats.XP       += scores.XP;
                            stats.SR       += scores.SR;
                            stats.PlayTime += TimeSpan.FromSeconds(mTimeInSeconds);
                            stats.Golds    += scores.Golds;
                            stats.Silvers  += scores.Silvers;
                            stats.Bronzes  += scores.Bronzes;

                            DbConnector.CloseSQLConnection(); // Close connection to the database
                            if (DbConnector.OpenSQLConnection())
                            {
                                if (!statsDA.UpdateStats(stats))
                                {
                                    string error = "There was a problem updating your stats.";
                                    RunOnUiThread(() =>
                                    {
                                        AlertGenerator.ShowError(error, this);
                                    });
                                }
                            }
                            else
                            {
                                // Connection could not be opened
                                string error = "Connection to the database could not be established.";
                                RunOnUiThread(() =>
                                {
                                    AlertGenerator.ShowError(error, this);
                                });
                            }
                        }
                        else
                        {
                            // Stats does not exist, therefore insert
                            stats.Username = mUsername;
                            stats.XP       = scores.XP;
                            stats.SR       = scores.SR;
                            stats.PlayTime = TimeSpan.FromSeconds(mTimeInSeconds);
                            stats.Golds    = scores.Golds;
                            stats.Silvers  = scores.Silvers;
                            stats.Bronzes  = scores.Bronzes;

                            if (!statsDA.InsertStats(stats))
                            {
                                string error = "There was a problem updating your stats.";
                                RunOnUiThread(() =>
                                {
                                    AlertGenerator.ShowError(error, this);
                                });
                            }
                        }
                    }
                    else
                    {
                        // Connection could not be opened
                        string error = "Connection to the database could not be established.";
                        RunOnUiThread(() =>
                        {
                            AlertGenerator.ShowError(error, this);
                        });
                    }

                    DbConnector.CloseSQLConnection(); // Close connection to the database
                    #endregion

                    #region Update Achievements
                    if (DbConnector.OpenSQLConnection())
                    {
                        // Connection opened
                        AchievementsDA achievementsDA = new AchievementsDA();
                        List <AchievementsCompleted> achievementsCompleted = new List <AchievementsCompleted>();
                        achievementsCompleted = achievementsDA.GetAllAchievements(mUsername);
                        DbConnector.CloseSQLConnection(); // Close connection to the database

                        if (achievementsCompleted != null)
                        {
                            // Achievements exist, therefore update
                            achievementsCompleted = AchievementsTracker.UpdateAchievements(achievementsCompleted, mUsername, stats, mCurrentMode, sCurrentLvl);
                            for (int i = 0; i < 19; i++)
                            {
                                if (achievementsCompleted[i].Title != String.Empty)
                                {
                                    string title       = achievementsCompleted[i].Title;
                                    string description = achievementsCompleted[i].Description;
                                    string image       = achievementsCompleted[i].Image;

                                    // Pull up Achievement dialog
                                    FragmentTransaction transaction = FragmentManager.BeginTransaction();
                                    AchDialog dialogFrag            = new AchDialog(title, description, image);
                                    dialogFrag.Show(transaction, "dialog fragment");
                                }
                            }
                            if (DbConnector.OpenSQLConnection())
                            {
                                if (!achievementsDA.UpdateAchievements(achievementsCompleted))
                                {
                                    string error = "There was a problem updating your stats.";
                                    RunOnUiThread(() =>
                                    {
                                        AlertGenerator.ShowError(error, this);
                                    });
                                }
                            }
                            else
                            {
                                // Connection could not be opened
                                string error = "Connection to the database could not be established.";
                                RunOnUiThread(() =>
                                {
                                    AlertGenerator.ShowError(error, this);
                                });
                            }
                        }
                        else
                        {
                            // Achievements do not exist, therefore insert
                            achievementsCompleted = AchievementsTracker.InsertAchievements(achievementsCompleted, mUsername, mCurrentMode);

                            if (DbConnector.OpenSQLConnection())
                            {
                                if (!achievementsDA.InsertAchievements(achievementsCompleted))
                                {
                                    string error = "There was a problem updating your stats.";
                                    RunOnUiThread(() =>
                                    {
                                        AlertGenerator.ShowError(error, this);
                                    });
                                }
                            }
                        }
                    }
                    else
                    {
                        // Connection could not be opened
                        string error = "Connection to the database could not be established.";
                        RunOnUiThread(() =>
                        {
                            //Enabling the controls back
                            AlertGenerator.ShowError(error, this);
                        });
                    }

                    DbConnector.CloseSQLConnection(); // Close connection to the database
                    #endregion
                });
            }
        }