Example #1
0
        // Getting BlindtestClass Object from MainPage, then loading list of users
        protected override void OnNavigatedTo(NavigationEventArgs e)
        {
            // If a parameter is given, and if it's a BlindtestClass element
            if (e.Parameter is BlindtestClass)
            {
                // Get main element
                var bt = e.Parameter;
                blindtest = (BlindtestClass)bt;

                // Filling listView with users according to dataTemplate
                ObservableCollection <BTUser> listItems = new ObservableCollection <BTUser>();
                foreach (BTUser user in blindtest.getAllUsers())
                {
                    listItems.Add(user);
                }
                users_listView.ItemsSource = listItems;

                selected_user = blindtest.getSelectedUser();

                if (!selected_user.Equals(default(BTUser)))
                {
                    // Check if user still exist
                    if (blindtest.checkIfUserExist(selected_user) == true)
                    {
                        userInformation_label.Text = "User information - Selected User : " + selected_user.nickname;
                        BitmapImage bmpImg = new BitmapImage();
                        bmpImg.UriSource             = new Uri(selected_user.profile_picture);
                        profilePicture_Image.Source  = bmpImg;
                        users_listView.SelectedIndex = blindtest.getSelectedUserIndex();
                    }
                }
            }
        }
        // Delete playlist
        // 0 : no error
        // 1 : user doesn't exist in database
        // 2 : invalid password
        // 42: Get error string to know what happend
        public int deletePlaylist(BTUser user_src, string password, int playlist_index)
        {
            int    status        = 0;
            bool   user_exists   = false;
            BTUser user_selected = new BTUser();

            try
            {
                // First, check if user exists in db (using userID)
                foreach (BTUser user in database.user)
                {
                    if (user.user_id == user_src.user_id)
                    {
                        user_exists   = true;
                        user_selected = user;
                    }
                }

                // If user doesn't exists
                if (user_exists == false)
                {
                    status = 1;
                }

                else
                {
                    // Check if password is incorrect
                    if (hashPassword(password) != user_selected.password)
                    {
                        status = 2;
                    }

                    else
                    {
                        foreach (BTUser user in database.user)
                        {
                            if (user.user_id == user_selected.user_id)
                            {
                                // Removing playlist from database
                                user.playlists.Remove(user.playlists[playlist_index]);

                                // Saving changes
                                saveDatabase();
                            }
                        }
                    }
                }
            }

            // In case of other error
            catch (Exception ex)
            {
                status = 42;
                error  = ex.Message;
            }

            return(status);
        }
        // Edit user
        // 0 : no error
        // 1 : name, password or URL empty
        // 2 : invalid PP URL
        // 3 : Invalid password
        // 42: Get error string to know what happend
        public int editUser(int user_index, string src_name, string src_profilepicture_url, string password)
        {
            int status = 0;

            try
            {
                // checks if source strings are empty
                if (src_name == "" || src_profilepicture_url == "" || password == "")
                {
                    status = 1;
                }

                // If they aren't empty
                else
                {
                    // Check if source PP URL is valid
                    if (Uri.IsWellFormedUriString(src_profilepicture_url, UriKind.Absolute))
                    {
                        // Creating new user
                        BTUser edited_user = database.user[user_index];
                        edited_user.nickname        = src_name;
                        edited_user.profile_picture = src_profilepicture_url;

                        // Check if password is the same as user's one
                        if (edited_user.password == hashPassword(password))
                        {
                            // Editing user in database
                            database.user[user_index] = edited_user;

                            // Saving changes to database.json file
                            saveDatabase();
                        }

                        else
                        {
                            status = 3;
                        }
                    }

                    else
                    {
                        status = 2;
                    }
                }
            }

            catch (Exception ex)
            {
                status = 42;
                error  = ex.Message;
            }

            return(status);
        }
        // Check if users exists in database (by given BTUser Object)
        // Returns true if user exists, false if it doesn't
        public bool checkIfUserExist(BTUser user_src)
        {
            bool status = false;

            // Check if user exists according to userID
            foreach (BTUser user in database.user)
            {
                if (user.user_id == user_src.user_id)
                {
                    status = true;
                }
            }

            return(status);
        }
        // Get specified user by userID
        public BTUser getUserByUserID(int userID)
        {
            BTUser user_to_return = new BTUser();

            user_to_return.nickname = "INVALID";

            // Check if user exists in database
            foreach (BTUser user in database.user)
            {
                if (user.user_id == userID)
                {
                    user_to_return = user;
                }
            }

            return(user_to_return);
        }
Example #6
0
        // When an item is selected in listView, display info in UI
        private void Users_listView_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            if (users_listView.SelectedIndex != -1)
            {
                selected_user = (BTUser)users_listView.SelectedItem;
                userInformation_label.Text = "User information - Selected User : "******"ms-appx:///Assets/Users/unselected-user.png");
                profilePicture_Image.Source = bmpImg;
            }
        }
        // Create new user
        // 0 : no error
        // 1 : name, password or URL empty
        // 2 : invalid PP URL
        // 3 : username already gaven
        // 42: Get error string to know what happend
        public int createUser(string src_name, string src_profilepicture_url, string password)
        {
            int status = 0;

            try
            {
                // checks if source strings are empty
                if (src_name == "" || src_profilepicture_url == "" || password == "")
                {
                    status = 1;
                }

                // If they aren't empty
                else
                {
                    // Check if source PP URL is valid
                    if (Uri.IsWellFormedUriString(src_profilepicture_url, UriKind.Absolute))
                    {
                        // Default userID is equal to 0 (first one)
                        int userID = 0;

                        // if users already exists, generating random userID between 0 and 10000
                        if (database.user.Count != 0)
                        {
                            List <int> userID_list = new List <int>();

                            // Getting every existing userID
                            foreach (BTUser user in database.user)
                            {
                                userID_list.Add(user.user_id);
                            }

                            // To generate random int between 0 and 10000
                            Random random_int = new Random();

                            bool loop_state = false;
                            while (loop_state == false)
                            {
                                // Generating userID
                                userID = random_int.Next(0, 10000);

                                // Checking if it exists
                                foreach (int i in userID_list)
                                {
                                    // If userID not attributed, exit the loop
                                    if (userID != i)
                                    {
                                        loop_state = true;
                                    }

                                    else
                                    {
                                        loop_state = false;
                                    }
                                }
                            }
                        }

                        // Creating new user
                        BTUser new_user = new BTUser();
                        new_user.user_id         = userID;
                        new_user.nickname        = src_name;
                        new_user.profile_picture = src_profilepicture_url;
                        new_user.playlists       = new List <BTPlaylist>();

                        // Hashing password
                        new_user.password = hashPassword(password);

                        // Check if username is already given to someone in db
                        if (checkIfUserExist(new_user.nickname) == true)
                        {
                            status = 3;
                        }

                        else
                        {
                            // Adding user to database
                            database.user.Add(new_user);

                            // Saving changes to database.json file
                            saveDatabase();
                        }
                    }

                    else
                    {
                        status = 2;
                    }
                }
            }

            catch (Exception ex)
            {
                status = 42;
                error  = ex.Message;
            }

            return(status);
        }
 // Set selected user (in this class)
 public void setSelectedUser(BTUser user_selected, int user_index)
 {
     selected_user       = user_selected;
     selected_user_index = user_index;
 }
        // Delete game
        // 0 : no error
        // 1 : user doesn't exist in database
        // 2 : game doesn't exist in database
        // 3 : game isn't created by given user!
        // 4 : invalid password
        // 42: Get error string to know what happend
        public int deleteGame(BTUser user_src, BTGame game_src, string password)
        {
            int    status = 0;
            bool   user_exists = false, game_exists = false;
            BTUser user_selected = new BTUser();
            BTGame game_selected = new BTGame();

            try
            {
                // First, check if user exists in db (using userID)
                foreach (BTUser user in database.user)
                {
                    if (user.user_id == user_src.user_id)
                    {
                        user_exists   = true;
                        user_selected = user;
                    }
                }

                // If user doesn't exists
                if (user_exists == false)
                {
                    status = 1;
                }

                else
                {
                    // Check if game exists in database
                    foreach (BTGame game in database.game)
                    {
                        if (game.game_id == game_src.game_id)
                        {
                            game_selected = game;
                            game_exists   = true;
                        }
                    }

                    // if game exists
                    if (game_exists == true)
                    {
                        // Check if game is made by given user
                        if (user_src.user_id == game_selected.user_id)
                        {
                            // Check if password is incorrect
                            if (hashPassword(password) != user_selected.password)
                            {
                                status = 4;
                            }

                            else
                            {
                                // Delete game in database
                                database.game.Remove(game_selected);

                                // Save changes
                                saveDatabase();
                            }
                        }

                        else
                        {
                            status = 3;
                        }
                    }

                    else
                    {
                        status = 2;
                    }
                }
            }

            // In case of other error
            catch (Exception ex)
            {
                status = 42;
                error  = ex.Message;
            }

            return(status);
        }