コード例 #1
0
ファイル: Post.cs プロジェクト: townerr/Reddit-Clone
        // ToString override
        public override string ToString()
        {
            User      postAuthor = Program.idDictionary[AuthorID] as User;
            Subreddit sub        = Program.idDictionary[SubHome] as Subreddit;

            // Allows for post to be displayed as locked
            string lockString = "";

            if (Locked == true)
            {
                lockString = "**LOCKED**";
            }

            return("<" + ID + "> [" + sub.Name + "] (" + Score + ") " + Title + " " + lockString + " - " + PostContent + " - " +
                   postAuthor.Name + " |" + TimeStamp + "|");
        }
コード例 #2
0
ファイル: Subreddit.cs プロジェクト: townerr/Reddit-Clone
        //interfaces
        public int CompareTo(object obj)
        {
            // Checks for null values
            if (obj == null)
            {
                throw new ArgumentNullException();
            }

            Subreddit rightOp = obj as Subreddit;

            // Protect against failed typcasting
            if (rightOp != null)
            {
                return(name.CompareTo(rightOp.name));
            }
            else
            {
                throw new ArgumentException("[Subreddit]:CompareTo argument is not a Subreddit");
            }
        }
コード例 #3
0
ファイル: CreatePost.cs プロジェクト: townerr/Reddit-Clone
        // Create post
        private void Button1_Click(object sender, EventArgs e)
        {
            Subreddit sub = subsCombo.SelectedItem as Subreddit;

            if (titleText.Text != null || titleText.Text != "")
            {
                if (postText.Text != null || postText.Text != "")
                {
                    f1.CreatePost(sub, titleText.Text, postText.Text);
                    MessageBox.Show("Post Created Successfully!");
                    this.Close();
                }
                else
                {
                    MessageBox.Show("Invalid Post Contents.");
                }
            }
            else
            {
                MessageBox.Show("Invalid Post Title.");
            }
        }
コード例 #4
0
ファイル: Form1.cs プロジェクト: townerr/Reddit-Clone
        private void subredditsComboBox_SelectedIndexChanged(object sender, EventArgs e)
        {
            Subreddit curSubreddit = subredditsComboBox.SelectedItem as Subreddit;

            Program.displayPosts.Clear();
            postPanel.Controls.Clear();


            // Displays all posts when "all" or home is selected
            if (subredditsComboBox.SelectedIndex == 1 || subredditsComboBox.SelectedIndex == 0) // "all" or home was selected
            {
                foreach (Subreddit s in Program.subreddits)
                {
                    //numMembersLabel.Text = "";
                    //numActiveLabel.Text = "";

                    foreach (Post p in s)
                    {
                        DisplayPost dp = new DisplayPost(p, 0);
                        dp.PostPanel.Location = new Point(0, Program.displayPosts.Count * DisplayPost.TYPE0_HEIGHT);

                        Program.displayPosts.Add(dp);
                        postPanel.Controls.Add(dp.PostPanel);
                    }
                }
            } // Displays all of the selected subreddits posts
            else if (curSubreddit != null)
            {
                foreach (Post p in curSubreddit)
                {
                    DisplayPost dp = new DisplayPost(p, 0);
                    dp.PostPanel.Location = new Point(0, Program.displayPosts.Count * DisplayPost.TYPE0_HEIGHT);

                    Program.displayPosts.Add(dp);
                    postPanel.Controls.Add(dp.PostPanel);
                }
            }
        }
コード例 #5
0
ファイル: Program.cs プロジェクト: townerr/Reddit-Clone
        /***************************************************************
        *  DeletePost
        *  Use: Deletes a post from our reddit clone as specified by the user.
        *  This method iterates through all comments and replies and
        *  removes all references of the post and its children from the
        *  parent subreddit and the ID dictionary.
        *
        *  Parameters: Post - the post to be deleted
        *
        *  Returns: nothing
        ***************************************************************/
        public static void DeletePost(Post post)
        {
            // Remove post from parent sub
            Subreddit parentSub = idDictionary[post.SubHome] as Subreddit;

            parentSub.removePost(post);

            // Remove all references to post, comments and replies

            // Remove reference to post in idDictionary
            idDictionary.Remove(post.ID);

            // Remove references to comments
            foreach (Comment comment in post)
            {
                idDictionary.Remove(comment.ID);

                // Remove references to all comments
                foreach (Comment reply in comment)
                {
                    idDictionary.Remove(reply.ID);
                }
            }
        }
コード例 #6
0
ファイル: Program.cs プロジェクト: townerr/Reddit-Clone
        /***************************************************************
        *  ReadInputFiles
        *  Use: Reads through the input files for Users, Subreddits, Posts,
        *  and comments. For each class it reads in a line from the input file,
        *  tokenizes the line. Parses the tokens into something that can be used
        *  to create a new Object for each class and creates them. These Objects are
        *  then added to their respective SortedSets and the id dictionary.
        *
        *  Parameters: none
        *
        *  Returns: nothing
        ***************************************************************/
        public static void ReadInputFiles()
        {
            String line;

            String[] tokens;

            // Reading in the users
            using (StreamReader inFile = new StreamReader("..\\..\\..\\users.txt"))
            {
                line = inFile.ReadLine();

                // On the assignment site the format is, "unique ID | usertype | name | hashcode | upVotes | downVotes" but the class attributes are "unique ID, name, postScore, commentScore", not sure why
                while (line != null)
                {
                    tokens = line.Split('\t');
                    User user = new User(Convert.ToUInt32(tokens[0]), Convert.ToInt32(tokens[1]), tokens[2], tokens[3], Convert.ToInt32(tokens[4]), Convert.ToInt32(tokens[5]));
                    users.Add(user);
                    idDictionary.Add(user.ID, user);
                    line = inFile.ReadLine();
                }
            }

            // Reads in the subreddits
            using (StreamReader inFile = new StreamReader("..\\..\\..\\subreddits.txt"))
            {
                line = inFile.ReadLine();

                while (line != null)
                {
                    tokens = line.Split('\t');
                    Subreddit sub = new Subreddit(Convert.ToUInt32(tokens[0]), tokens[1], Convert.ToUInt32(tokens[2]), Convert.ToUInt32(tokens[3]));
                    subreddits.Add(sub);
                    idDictionary.Add(sub.ID, sub);

                    line = inFile.ReadLine();
                }
            }

            // Reads in the posts and creates Post objects, putting them in the Subreddits sorted set of Posts
            // 0 locked | 1 unique ID | 2 authorID | 3 title | 4 content | 5 subredditID | 6 upVotes | 7 downVotes | 8 weight | 9 year | 10 month | 11  day | 12 hour | 13 min | 14 sec | 15 silver | 16 gold | 17 plat |
            // (uint _ID, uint _SubHome, uint _AuthorID, string _Title, string _PostContent, DateTime _TimeStamp, uint _UpVotes, uint _DownVotes, uint _Weight)
            using (StreamReader inFile = new StreamReader("..\\..\\..\\posts.txt"))
            {
                line = inFile.ReadLine();

                while (line != null)
                {
                    tokens = line.Split('\t');


                    DateTime timeStamp = new DateTime(Convert.ToInt32(tokens[9]), Convert.ToInt32(tokens[10]), Convert.ToInt32(tokens[11]),
                                                      Convert.ToInt32(tokens[12]), Convert.ToInt32(tokens[13]), Convert.ToInt32(tokens[14]));

                    Post post = new Post(Convert.ToInt32(tokens[0]), Convert.ToUInt32(tokens[1]), Convert.ToUInt32(tokens[5]), Convert.ToUInt32(tokens[2]), tokens[3], tokens[4], timeStamp,
                                         Convert.ToUInt32(tokens[6]), Convert.ToUInt32(tokens[7]), Convert.ToUInt32(tokens[8]), Convert.ToUInt32(tokens[15]), Convert.ToUInt32(tokens[16]), Convert.ToUInt32(tokens[17]));

                    idDictionary.Add(post.ID, post);

                    Subreddit postSubreddit = idDictionary[post.SubHome] as Subreddit;
                    postSubreddit.addPost(post);

                    line = inFile.ReadLine();
                }
            }

            // Reads in comments and creates Comment objects, putting them in the comments SortedSet
            using (StreamReader inFile = new StreamReader("..\\..\\..\\comments.txt"))
            {
                line = inFile.ReadLine();

                while (line != null)
                {
                    tokens = line.Split('\t');

                    uint     uniqueID  = Convert.ToUInt32(tokens[0]);
                    uint     authorID  = Convert.ToUInt32(tokens[1]);
                    string   content   = tokens[2];
                    uint     parentID  = Convert.ToUInt32(tokens[3]);
                    uint     upVotes   = Convert.ToUInt32(tokens[4]);
                    uint     downVotes = Convert.ToUInt32(tokens[5]);
                    DateTime timeStamp = new DateTime(Convert.ToInt32(tokens[6]), Convert.ToInt32(tokens[7]), Convert.ToInt32(tokens[8]), Convert.ToInt32(tokens[9]),
                                                      Convert.ToInt32(tokens[10]), Convert.ToInt32(tokens[11]));
                    uint silver   = Convert.ToUInt32(tokens[12]);
                    uint gold     = Convert.ToUInt32(tokens[13]);
                    uint platinum = Convert.ToUInt32(tokens[14]);

                    Comment comment = new Comment(uniqueID, authorID, content, parentID, upVotes, downVotes, timeStamp, silver, gold, platinum);

                    idDictionary.Add(comment.ID, comment);

                    if (idDictionary[comment.ParentID] is Post)
                    {
                        Post parent = idDictionary[comment.ParentID] as Post;
                        parent.addComment(comment);
                    }
                    else // Parent was a comment
                    {
                        Comment parent = idDictionary[comment.ParentID] as Comment;
                        parent.addComment(comment);
                    }

                    line = inFile.ReadLine();
                }
            }
        }
コード例 #7
0
ファイル: Form1.cs プロジェクト: townerr/Reddit-Clone
        public void CreatePost(Subreddit sub, string title, string content)
        {
            if (Program.loggedInUserID == -1)
            {
                MessageBox.Show("Must be logged in to create a post.");
                return;
            }

            try // Check for foul language
            {
                Program.CheckFoulLanguage(content);
                Program.CheckFoulLanguage(title);
            }
            catch (Program.FoulLanguageException except) // Caught foul language
            {
                MessageBox.Show("Caught a FoulLanguageException: " + except);
                return;
            }

            DateTime timestamp = DateTime.Now;
            Post     newPost   = new Post(0, Program.GenerateID(), sub.ID, (uint)Program.loggedInUserID, title, content, timestamp, 0, 0, 0, 0, 0, 0);

            Program.idDictionary.Add(newPost.ID, newPost);
            Subreddit postSubreddit = Program.idDictionary[newPost.SubHome] as Subreddit;

            postSubreddit.addPost(newPost);

            //append to file new comment
            using (System.IO.StreamWriter file =
                       new System.IO.StreamWriter(@"..\\..\\..\\posts.txt", true))
            {
                file.WriteLine(
                    "0" + "\t" +
                    newPost.ID + "\t" +
                    Program.loggedInUserID + "\t" +
                    newPost.Title + "\t" +
                    newPost.PostContent + "\t" +
                    newPost.SubHome + "\t" +
                    newPost.UpVotes + "\t" +
                    newPost.DownVotes + "\t" +
                    newPost.Weight + "\t" +
                    newPost.TimeStamp.Year + "\t" +
                    newPost.TimeStamp.Month + "\t" +
                    newPost.TimeStamp.Day + "\t" +
                    newPost.TimeStamp.Hour + "\t" +
                    newPost.TimeStamp.Minute + "\t" +
                    newPost.TimeStamp.Second + "\t" +
                    newPost.Silver + "\t" +
                    newPost.Gold + "\t" +
                    newPost.Platinum);
            }

            //refresh posts
            Program.displayPosts.Clear();
            postPanel.Controls.Clear();

            subredditsComboBox.SelectedIndex = 0;

            foreach (Subreddit s in Program.subreddits)
            {
                foreach (Post p in s)
                {
                    DisplayPost dp = new DisplayPost(p, 0);
                    dp.PostPanel.Location = new Point(0, Program.displayPosts.Count * DisplayPost.TYPE0_HEIGHT);

                    Program.displayPosts.Add(dp);
                    postPanel.Controls.Add(dp.PostPanel);
                }
            }
        }