Ejemplo n.º 1
0
        /// <summary>
        /// Finds or creates the love with the given peaces.
        /// </summary>
        /// <param name="db">The database connection.</param>
        /// <param name="light">The hash list of lights.</param>
        /// <param name="create">True (default) to create a new love if non-existant, otherwise false.</param>
        /// <returns>The love.</returns>
        public static Love FindLove(ISdwDatabase db, string light, bool create = true)
        {
            var hash   = new Hashids("GodisLove");
            var peaces = hash.Decode(light);

            if (peaces.Length <= 0)
            {
                return(null);
            }
            hash.Order = true;
            var orderPeace = hash.Encode(peaces);
            var love       = db.Love.Get(l => l.PeaceId == orderPeace).FirstOrDefault();

            if (love == null && create)
            {
                love = new Love {
                    Modified = DateTime.Now, PeaceId = orderPeace, Truths = new HashSet <Truth> (), Peaces = new HashSet <Peace> ()
                };
                int index = 0;
                foreach (var p in peaces)
                {
                    love.Peaces.Add(new Peace {
                        Order = index, Light = db.Light.Get(p)
                    });
                    index++;
                }
                db.Love.Insert(love);
                db.Save();
            }
            if (love != null && love.Truths == null)
            {
                love.Truths = new HashSet <Truth> ();
            }
            return(love);
        }
Ejemplo n.º 2
0
        public ActionResult TruthLinks(string lights)
        {
            var hash     = new Hashids("GodisLove");
            var lightIds = hash.Decode(lights).ToList();
            var combos   = GetCombinations(lightIds);
            var light    = this.Database.Light.Get(l => lightIds.Contains(l.Id)).ToList();
            var items    = new Dictionary <string, string> ();

            foreach (var combo in combos)
            {
                var hashId = hash.Encode(combo);
                var text   = string.Empty;
                foreach (var id in combo.OrderBy(lightIds.IndexOf))
                {
                    var li = light.FirstOrDefault(l => l.Id == id);
                    if (li != null)
                    {
                        if (!string.IsNullOrEmpty(text))
                        {
                            text += " | ";
                        }
                        text += li.Text;
                    }
                }
                items.Add(hashId, text);
            }
            if (lightIds.Count > 2)
            {
                var permus = new List <List <int> > ();
                foreach (var lId in lightIds)
                {
                    int id   = lId;
                    var perm = new List <int> {
                        id
                    };
                    perm.AddRange(lightIds.Where(i => i != id));
                    permus.Add(perm);
                }
                foreach (var perm in permus)
                {
                    perm.Insert(0, 0);
                    var hashId = hash.Encode(perm);
                    var text   = "P";
                    foreach (var id in perm)
                    {
                        var li = light.FirstOrDefault(l => l.Id == id);
                        if (li != null)
                        {
                            text += " | " + li.Text;
                        }
                    }
                    items.Add(hashId, text);
                }
            }
            return(PartialView(items));
        }
Ejemplo n.º 3
0
        public ActionResult VersionLinks(string lights)
        {
            var loves    = new List <Love> ();
            var hash     = new Hashids("GodisLove");
            var lightIds = hash.Decode(lights).ToList();

            if (lightIds.Count > 1)
            {
                var first = lightIds.First();
                lightIds.RemoveAt(0);
                var light      = this.Database.Light.Get(first);
                var testLights = new List <int> ();
                foreach (var truth in light.Truths)
                {
                    foreach (var test in truth.Love.Truths.Where(t => t.Light != null))
                    {
                        if (!testLights.Contains(test.Light.Id) && !lightIds.Contains(test.Light.Id) && test.Light.Id != first)
                        {
                            testLights.Add(test.Light.Id);
                        }
                    }
                }
                hash.Order = true;
                if (testLights.Count > 0)
                {
                    foreach (var testLight in testLights)
                    {
                        lightIds.Add(testLight);
                        var peaceId = hash.Encode(lightIds);
                        var love    = this.Database.Love.Get(l => l.PeaceId == peaceId).FirstOrDefault();
                        if (love != null)
                        {
                            loves.Add(love);
                        }
                        lightIds.Remove(testLight);
                    }
                }
            }
            return(PartialView(loves));
        }
Ejemplo n.º 4
0
        public ActionResult Love(string light, string truth, string versions, string truthLinks)
        {
            if (string.IsNullOrWhiteSpace(truth))
            {
                return(this.Fail("No truth given"));
            }
            var love = Helper.FindLove(this.Database, light);

            if (love == null)
            {
                return(this.Fail("No light given"));
            }
            var truths = truth.Trim().Split(new [] { '\n' }, StringSplitOptions.RemoveEmptyEntries);

            foreach (var t in truths)
            {
                var truthData  = t.Split('|');
                var lightText  = truthData [2].Trim();
                var order      = string.IsNullOrWhiteSpace(truthData [0]) ? null : (int?)Convert.ToInt32(truthData [0]);
                var number     = string.IsNullOrWhiteSpace(truthData [1]) ? null : (int?)Convert.ToInt32(truthData [1]);
                var truthLight = this.Database.Light.Get(l => l.Text == lightText).FirstOrDefault() ??
                                 new Light {
                    Text = lightText, Modified = DateTime.Now
                };
                love.Truths.Add(new Truth {
                    Light = truthLight, Order = order, Number = number
                });
            }
            this.Database.Save();
            foreach (var t in love.Truths)
            {
                LightSearch.AddOrUpdateIndex(t.Light);
            }
            var hash = new Hashids("GodisLove");

            if (!string.IsNullOrEmpty(truthLinks))
            {
                var links = truthLinks.Split(new[] { ',' }, StringSplitOptions.RemoveEmptyEntries);
                foreach (var t in love.Truths)
                {
                    var l = Helper.FindLove(this.Database, t.Light.Id);
                    foreach (var link in links)
                    {
                        var lightIds = hash.Decode(link).ToList();
                        if (lightIds [0] == 0)
                        {
                            lightIds.RemoveAt(0);
                            var li = this.Database.Light.Get(lightIds [0]);
                            lightIds.RemoveAt(0);
                            var lov = Helper.FindLove(this.Database, lightIds);
                            if (lov.Truths.Any(tr => tr.Light != null && tr.Light.Id == li.Id && !tr.ParentId.HasValue))
                            {
                                continue;
                            }
                            lov.Truths.Add(new Truth {
                                Light = li
                            });
                        }
                        else
                        {
                            if (lightIds.Count == 1)
                            {
                                if (l.Truths.Any(tr => tr.Light != null && tr.Light.Id == lightIds [0] && !tr.ParentId.HasValue))
                                {
                                    continue;
                                }
                                l.Truths.Add(new Truth {
                                    Light = this.Database.Light.Get(lightIds [0])
                                });
                            }
                            else
                            {
                                var lov = Helper.FindLove(this.Database, link);
                                if (l.Truths.Any(tr => tr.Light == null && tr.ParentId.HasValue && tr.ParentId == lov.Id))
                                {
                                    continue;
                                }
                                l.Truths.Add(new Truth {
                                    ParentId = lov.Id
                                });
                            }
                        }
                    }
                }
                this.Database.Save();
            }
            if (!string.IsNullOrEmpty(versions))
            {
                var loveIds = hash.Decode(versions);
                if (loveIds.Length > 0)
                {
                    foreach (var loveId in loveIds)
                    {
                        var linkedLove = this.Database.Love.Get(loveId);
                        foreach (var addTruth in love.Truths.Where(t => t.Number.HasValue))
                        {
                            var lt = linkedLove.Truths.FirstOrDefault(tr => tr.Number == addTruth.Number);
                            if (lt != null)
                            {
                                var truthLove = Helper.FindLove(this.Database, lt.Light.Id);
                                truthLove.Truths.Add(new Truth {
                                    Light = addTruth.Light, ParentId = love.Id
                                });
                                truthLove = Helper.FindLove(this.Database, addTruth.Light.Id);
                                truthLove.Truths.Add(new Truth {
                                    Light = lt.Light, ParentId = linkedLove.Id
                                });
                            }
                        }
                    }
                    this.Database.Save();
                }
            }
            return(this.Success());
        }
Ejemplo n.º 5
0
        public ActionResult Love(string data)
        {
            if (string.IsNullOrEmpty(data))
            {
                return(this.Fail("No data given to import!"));
            }
            var hash   = new Hashids("GodisLove");
            var import = JsonConvert.DeserializeObject <ImportList> (data);

            foreach (var group in import.Groups)
            {
                if (group.Lights.Count <= 0)
                {
                    return(this.Fail("The light was not given."));
                }
                var love = Helper.FindLove(this.Database, hash.Encode(Helper.GetLightIds(this.Database, group.Lights)));
                foreach (var t in group.Truths)
                {
                    var truth = t;
                    if (t.Parent.HasValue)
                    {
                        if (t.Id.HasValue)
                        {
                            if (!love.Truths.Any(tr => tr.ParentId.HasValue && tr.ParentId == t.Parent.Value && tr.Light != null && tr.Light.Id == truth.Id))
                            {
                                love.Truths.Add(new Truth {
                                    Light    = this.Database.Light.Get(t.Id.Value),
                                    ParentId = t.Parent,
                                    Number   = t.Number,
                                    Order    = t.Order
                                });
                            }
                        }
                        else
                        {
                            if (!love.Truths.Any(tr => tr.ParentId.HasValue && tr.ParentId == t.Parent.Value && tr.Light == null))
                            {
                                love.Truths.Add(new Truth {
                                    ParentId = t.Parent,
                                    Number   = t.Number,
                                    Order    = t.Order
                                });
                            }
                        }
                    }
                    else
                    {
                        if (t.Id.HasValue)
                        {
                            if (!love.Truths.Any(tr => !tr.ParentId.HasValue && tr.Light != null && tr.Light.Id == truth.Id))
                            {
                                love.Truths.Add(new Truth {
                                    Light  = this.Database.Light.Get(t.Id.Value),
                                    Number = t.Number,
                                    Order  = t.Order
                                });
                            }
                        }
                        else
                        {
                            var light = this.Database.Light.Get(l => l.Text == truth.Text).FirstOrDefault() ??
                                        new Light {
                                Text = truth.Text, Modified = DateTime.Now
                            };
                            love.Truths.Add(new Truth {
                                Light = light, Order = t.Order, Number = t.Number
                            });
                        }
                    }
                }
                this.Database.Save();
                foreach (var t in love.Truths)
                {
                    LightSearch.AddOrUpdateIndex(t.Light);
                }

                /*if (!string.IsNullOrEmpty (truthLinks)) {
                 * var links = truthLinks.Split (new [] { ',' }, StringSplitOptions.RemoveEmptyEntries);
                 * foreach (var t in love.Truths) {
                 *    var l = this.FindLove (hash.Encode (t.Light.Id));
                 *    foreach (var link in links) {
                 *       var lightIds = hash.Decode (link).ToList ();
                 *       if (lightIds [0] == 0) {
                 *          lightIds.RemoveAt (0);
                 *          var li = this.Database.Light.Get (lightIds [0]);
                 *          lightIds.RemoveAt (0);
                 *          var lov = this.FindLove (hash.Encode (lightIds));
                 *          if (lov.Truths.Any (tr => tr.Light != null && tr.Light.Id == li.Id && !tr.ParentId.HasValue))
                 *             continue;
                 *          lov.Truths.Add (new Truth { Light = li });
                 *       } else {
                 *          if (lightIds.Count == 1) {
                 *             if (l.Truths.Any (tr => tr.Light != null && tr.Light.Id == lightIds [0] && !tr.ParentId.HasValue))
                 *                continue;
                 *             l.Truths.Add (new Truth { Light = this.Database.Light.Get (lightIds [0]) });
                 *          } else {
                 *             var lov = this.FindLove (link);
                 *             if (l.Truths.Any (tr => tr.Light == null && tr.ParentId.HasValue && tr.ParentId == lov.Id))
                 *                continue;
                 *             l.Truths.Add (new Truth { ParentId = lov.Id });
                 *          }
                 *       }
                 *    }
                 * }
                 * this.Database.Save ();
                 * }*/
            }
            //if (string.IsNullOrWhiteSpace (truth)) return this.Fail ("No truth given");
            //var love = FindLove (light);
            //if (love == null) return this.Fail ("No light given");
            //var truths = truth.Trim ().Split (new [] { '\n' }, StringSplitOptions.RemoveEmptyEntries);
            //foreach (var t in truths) {
            //   var truthData = t.Split ('|');
            //   var lightText = truthData [2].Trim ();
            //   var order = string.IsNullOrWhiteSpace (truthData [0]) ? null : (int?)Convert.ToInt32 (truthData [0]);
            //   var number = string.IsNullOrWhiteSpace (truthData [1]) ? null : (int?) Convert.ToInt32 (truthData [1]);
            //   var truthLight = this.Database.Light.Get (l => l.Text == lightText).FirstOrDefault () ??
            //                    new Light {Text = lightText, Modified = DateTime.Now};
            //   love.Truths.Add (new Truth { Light = truthLight, Order = order, Number = number });
            //}
            //var hash = new Hashids ("GodisLove");

            //if (!string.IsNullOrEmpty (versions)) {
            //   var loveIds = hash.Decode (versions);
            //   if (loveIds.Length > 0) {
            //      foreach (var loveId in loveIds) {
            //         var linkedLove = this.Database.Love.Get (loveId);
            //         foreach (var addTruth in love.Truths.Where (t => t.Number.HasValue)) {
            //            var lt = linkedLove.Truths.FirstOrDefault (tr => tr.Number == addTruth.Number);
            //            if (lt != null) {
            //               var truthLove = this.FindLove (hash.Encode (lt.Light.Id));
            //               truthLove.Truths.Add (new Truth {Light = addTruth.Light, ParentId = love.Id});
            //               truthLove = this.FindLove (hash.Encode (addTruth.Light.Id));
            //               truthLove.Truths.Add (new Truth {Light = lt.Light, ParentId = linkedLove.Id});
            //            }
            //         }
            //      }
            //      this.Database.Save ();
            //   }
            //}
            return(this.Success());
        }
Ejemplo n.º 6
0
        public ActionResult Love(int?id, string items, string history)
        {
            var model = new LoveModel();
            var hash  = new Hashids("GodisLove")
            {
                Order = true
            };
            var ids = hash.Decode(items).ToList();

            if (id == null)
            {
                var lights = this.Database.Light.All(q => q.OrderBy(l => Guid.NewGuid())).Take(25);
                foreach (var light in lights)
                {
                    model.ToAdd.Add(new SdwItem(light));
                }
            }
            else
            {
                var histIds = hash.Decode(history).ToList();
                histIds.Add(id.Value);
                history = hash.Encode(histIds);
                var light = this.Database.Light.Get(id.Value);
                ids.Add(id.Value);
                var loves = (from peace in light.Peaces
                             where peace.Love.Peaces.All(p => ids.Contains(p.Light.Id))
                             select peace.Love).ToList();
                if (loves.Count > 0)
                {
                    var max = loves.Where(l => l.Truths.Any()).Max(l => l.Peaces.Count);
                    foreach (var love in loves.Where(l => l.Peaces.Count == max))
                    {
                        foreach (var truth in love.Truths)
                        {
                            Love truthLove = null;
                            var  truthIds  = new List <int> ();
                            if (truth.Light != null)
                            {
                                truthIds.Add(truth.Light.Id);
                            }
                            if (truth.ParentId.HasValue && (!truth.Order.HasValue || truth.Order.Value > 0))
                            {
                                truthLove = this.Database.Love.Get(truth.ParentId.Value);
                                truthIds.AddRange(truthLove.Peaces.Select(p => p.Light.Id));
                            }
                            if ((truthLove == null && truthIds.All(ids.Contains)) ||
                                (truthIds.All(histIds.Contains) && !truth.Number.HasValue && (!truth.Order.HasValue || truth.Order.Value > 0)) ||
                                (truth.Light != null && truth.Order == null && model.ToAdd.Any(i => i.Id == truth.Light.Id)))
                            {
                                continue;
                            }

                            if (truthLove == null && truthIds.Count <= 0)
                            {
                                continue;
                            }
                            var item = new SdwItem(truth)
                            {
                                History = history
                            };
                            if (truthLove != null)
                            {
                                item.IsLink = true;
                                var text = GetTitle(truthLove.Peaces);
                                if (truth.Light != null)
                                {
                                    item.Title = text;
                                }
                                else
                                {
                                    item.Text = text;
                                    item.Id   = truthLove.Peaces.OrderBy(p => p.Order).Last().Light.Id;
                                }
                                item.Parents = hash.Encode(truthLove.Peaces.Select(p => p.Light.Id));
                            }
                            else if (truth.Light != null)
                            {
                                var parents     = new List <Peace> ();
                                var tempParents = (from peace in truth.Light.Peaces
                                                   from p in peace.Love.Peaces
                                                   where ids.Contains(p.Light.Id)
                                                   select p).ToList();
                                foreach (var tp in tempParents)
                                {
                                    if (parents.Any(p => p.Light.Id == tp.Light.Id))
                                    {
                                        continue;
                                    }
                                    parents.Add(tp);
                                }
                                var parentIds = parents.Select(p => p.Light.Id).ToList();
                                item.Parents    = hash.Encode(parentIds);
                                item.IsSelected = histIds.Contains(truth.Light.Id);
                                item.Title      = GetTitle((parents.Count > 0) ? parents : truth.Number.HasValue || love.Peaces.Count > 1 ? love.Peaces : new List <Peace> ());
                                if (string.IsNullOrEmpty(item.Title))
                                {
                                    item.History = string.Empty;
                                }
                            }
                            model.ToAdd.Add(item);
                        }
                    }
                    SetHeadersAndFooters(model);
                }
            }
            return(PartialView(model));
        }
Ejemplo n.º 7
0
        /// <summary>
        /// Finds or creates the love with the given peaces.
        /// </summary>
        /// <param name="db">The database connection.</param>
        /// <param name="lights">The list of light ids.</param>
        /// <param name="create">True (default) to create a new love if non-existant, otherwise false.</param>
        /// <returns>The love.</returns>
        public static Love FindLove(ISdwDatabase db, IEnumerable <int> lights, bool create = true)
        {
            var hash = new Hashids("GodisLove");

            return(FindLove(db, hash.Encode(lights), create));
        }