private void dataGrid1_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            TimeCapsule tc = (TimeCapsule)this.dataGrid1.SelectedItem;

            if (tc != null)
            {
                Window w = new TimeCapsuleWindow(tc);
                w.Closed += new EventHandler(window_Closed);
                this._windows.Add(w);
                w.Show();
            }

            // Clear selected items.
            this.dataGrid1.UnselectAll();
        }
        public List <TimeCapsule> getTimeCapsules(int fromPage, int toPage)
        {
            bool          work     = true;
            string        jsonText = "";
            int           cnt      = fromPage;
            string        url      = "https://subnautica.unknownworlds.com/api/time-capsules-voting-queue?page=";
            string        splitA   = "{\"capsules\":[";
            string        splitB   = "},{\\\"_id\\\":";
            List <string> capsules = new List <string>();

            List <TimeCapsule> timecapsules = new List <TimeCapsule>();

            using (WebClient wc = new WebClient())
            {
                while (work)
                {
                    capsules.Clear();

                    jsonText = wc.DownloadString(url + cnt.ToString());

                    if (jsonText.CompareTo("{\"capsules\":[]}") == 0)
                    {
                        work = false;
                    }
                    else
                    {
                        jsonText = jsonText.Substring(splitA.Length);
                        jsonText = jsonText.Substring(0, jsonText.Length - 2);
                        string[] lines = Regex.Split(jsonText, splitB);
                        if (lines.Length > 0)
                        {
                            capsules.Add(lines[0] + "}");
                            int i = 1;
                            while (i < lines.Length)
                            {
                                capsules.Add("{\"_id\":" + lines[i] + "}");
                                ++i;
                            }
                        }

                        foreach (string capsule in capsules)
                        {
                            TimeCapsule tmp = parseTimeCapsule(capsule);
                            if (tmp != null)
                            {
                                timecapsules.Add(tmp);
                            }
                        }
                    }

                    // Go to next page
                    ++cnt;

                    if (cnt > toPage)
                    {
                        work = false;
                    }
                }
            }
            return(timecapsules);
        }
        public TimeCapsule parseTimeCapsule(string timecapsulejson)
        {
            TimeCapsule    result = new TimeCapsule();
            JsonTextParser parser = new JsonTextParser();
            JsonObject     obj    = parser.Parse(timecapsulejson);

            foreach (JsonObject field in obj as JsonObjectCollection)
            {
                string name  = field.Name;
                string value = string.Empty;
                string type  = string.Empty;
                if (field.GetValue() == null)
                {
                    type = "null";
                }
                else
                {
                    type = field.GetValue().GetType().Name;
                }

                // Try to get value.
                switch (type)
                {
                case "String":
                    value = (string)field.GetValue();
                    break;

                case "Double":
                    value = field.GetValue().ToString();
                    break;

                case "Boolean":
                    value = field.GetValue().ToString();
                    break;

                case "null":
                    value = "null";
                    break;

                default:
                    value = "NotSupportedException";
                    break;
                    // DEBUG: in this sample we'll not parse nested arrays or objects.
                    //throw new NotSupportedException();
                }

                switch (name)
                {
                case "_id":
                    result._id = value;
                    break;

                case "platform":
                    result.platform = value;
                    break;

                case "platform_user_id":
                    result.platform_user_id = value;
                    break;

                case "user_name":
                    result.user_name = value;
                    break;

                case "title":
                    result.title = value;
                    break;

                case "text":
                    result.text = value;
                    break;

                case "language":
                    result.language = value;
                    break;

                case "items":
                    result.items = value;
                    break;

                case "is_active":
                    result.is_active = Convert.ToBoolean(value);
                    break;

                case "votes_up":
                    result.votes_up = Convert.ToInt32(value);
                    break;

                case "votes_down":
                    result.votes_down = Convert.ToInt32(value);
                    break;

                case "copies_found":
                    result.copies_found = Convert.ToInt32(value);
                    break;

                case "class_id":
                    result.class_id = value;
                    break;

                case "modified_at":
                    result.modified_at = value;
                    break;

                case "updated_at":
                    result.updated_at = value;
                    break;

                case "created_at":
                    result.created_at = value;
                    break;

                case "image":
                    result.image = value;
                    break;

                case "time_ago":
                    result.time_ago = value;
                    break;

                case "item_list":
                    result.item_list = value;
                    break;

                default:
                    throw new NotSupportedException();
                }
                // DEBUG
                //Console.WriteLine("{0} {1} {2}", name.PadLeft(15), type.PadLeft(10), value.PadLeft(15));
            }

            if (obj != null)
            {
                obj = null;
            }
            if (parser != null)
            {
                parser = null;
            }
            return(result);
        }
예제 #4
0
        public TimeCapsuleWindow(TimeCapsule tc)
        {
            InitializeComponent();

            this._tc = tc;

            // Set capsule title if there is one, otherwise remove it.
            if (tc.title.Length > 0)
            {
                this.lbl_CapsuleTitle.Content = tc.title;
            }
            else
            {
                rootStackPanel.Children.RemoveAt(0);
            }

            // Set capsule image.
            string imgURL = tc.getImageUrl();

            if (imgURL.Length > 0)
            {
                this.img_CapsuleImage.Source = new BitmapImage(new Uri(imgURL));
            }

            // Set capsule text.
            this.tb_CapsuleText.Text = tc.text;

            // Set capsule items list.
            this.tb_CapsuleItems.Text = "TimeCapsule items: " + tc.item_list;

            // Set capsule author.
            string authorStr = "Created";

            if (tc.platform.Length > 0 && tc.platform.CompareTo("Null") != 0)
            {
                authorStr += " on " + tc.platform;
            }
            authorStr += " by ";
            if (tc.user_name.Length > 0)
            {
                authorStr += tc.user_name;
                if (tc.platform_user_id.Length > 0)
                {
                    authorStr += " (ID: " + tc.platform_user_id + ")";
                }
            }
            else if (tc.platform_user_id.Length > 0)
            {
                authorStr += tc.platform_user_id;
            }
            else
            {
                authorStr += "?";
            }
            if (tc.time_ago.Length > 0)
            {
                authorStr += " " + tc.time_ago;
            }
            authorStr += ".";
            this.tb_CapsuleAuthor.Text = authorStr;

            // Set capsule upvotes.
            this.tb_CapsuleUpVotes.Text = "Up votes: " + Convert.ToString(tc.votes_up);

            // Set capsule downvotes.
            this.tb_CapsuleDownVotes.Text = "Down votes: " + Convert.ToString(tc.votes_down);

            // Set capsule creation datetime.
            this.tb_CapsuleCreated.Text = "Created at: " + tc.created_at;

            // Set capsule update datetime.
            this.tb_CapsuleUpdated.Text = "Updated at: " + tc.updated_at;

            // Set capsule modification datetime.
            this.tb_CapsuleModified.Text = "Modified at: " + tc.modified_at;

            // Set capsule language.
            this.tb_CapsuleLanguage.Text = "Language: " + tc.language;

            // Set number of capsule copies found.
            this.tb_CapsuleCopies.Text = "Copies found: " + Convert.ToString(tc.copies_found);

            // Set capsule "is active?".
            this.tb_CapsuleIsActive.Text = "Is active: " + Convert.ToString(tc.is_active);
        }