diff --git a/ProjectLighthouse/Controllers/ReviewController.cs b/ProjectLighthouse/Controllers/ReviewController.cs index 65e007de..55e75962 100644 --- a/ProjectLighthouse/Controllers/ReviewController.cs +++ b/ProjectLighthouse/Controllers/ReviewController.cs @@ -146,10 +146,13 @@ namespace LBPUnion.ProjectLighthouse.Controllers Random rand = new(); Review? yourReview = await this.database.Reviews.FirstOrDefaultAsync(r => r.ReviewerId == user.UserId && r.SlotId == slotId && r.Slot.GameVersion <= gameVersion); + VisitedLevel? visitedLevel = await this.database.VisitedLevels.FirstOrDefaultAsync(v => v.UserId == user.UserId && v.SlotId == slotId && v.Slot.GameVersion <= gameVersion); + Slot? slot = await this.database.Slots.FirstOrDefaultAsync(s => s.SlotId == slotId); if (slot == null) return this.BadRequest(); - Boolean canNowReviewLevel = visitedLevel != null && yourReview == null; + + Boolean canNowReviewLevel = slot.CreatorId != user.UserId && visitedLevel != null && yourReview == null; if (canNowReviewLevel) { RatedLevel? ratedLevel = await this.database.RatedLevels.FirstOrDefaultAsync(r => r.UserId == user.UserId && r.SlotId == slotId && r.Slot.GameVersion <= gameVersion); @@ -160,10 +163,10 @@ namespace LBPUnion.ProjectLighthouse.Controllers yourReview.Thumb = ratedLevel?.Rating == null ? 0 : ratedLevel.Rating; yourReview.Slot = slot; yourReview.SlotId = slotId; + yourReview.Deleted = false; yourReview.DeletedBy = DeletedBy.None; yourReview.Text = "You haven't reviewed this level yet. Edit this blank review to upload it!"; yourReview.LabelCollection = ""; - yourReview.Deleted = false; yourReview.Timestamp = TimeHelper.UnixTimeMilliseconds(); } @@ -226,9 +229,9 @@ namespace LBPUnion.ProjectLighthouse.Controllers string inner = Enumerable.Aggregate(reviews, string.Empty, (current, review) => { - RatedLevel? ratedLevel = this.database.RatedLevels.FirstOrDefault(r => r.SlotId == review.SlotId && r.UserId == user.UserId); + //RatedLevel? ratedLevel = this.database.RatedLevels.FirstOrDefault(r => r.SlotId == review.SlotId && r.UserId == user.UserId); //RatedReview? ratedReview = this.database.RatedReviews.FirstOrDefault(r => r.ReviewId == review.ReviewId && r.UserId == user.UserId); - return current + review.Serialize(ratedLevel/*, ratedReview*/); + return current + review.Serialize(/*, ratedReview*/); }); string response = LbpSerializer.TaggedStringElement("reviews", inner, new Dictionary diff --git a/ProjectLighthouse/Types/DeletedBy.cs b/ProjectLighthouse/Types/DeletedBy.cs index 666e4437..c8beade1 100644 --- a/ProjectLighthouse/Types/DeletedBy.cs +++ b/ProjectLighthouse/Types/DeletedBy.cs @@ -2,6 +2,7 @@ using System.Xml.Serialization; namespace LBPUnion.ProjectLighthouse.Types { + [XmlRoot("deleted_by")] public enum DeletedBy { [XmlEnum(Name = "none")] diff --git a/ProjectLighthouse/Types/Levels/Slot.cs b/ProjectLighthouse/Types/Levels/Slot.cs index ff0c580e..de339f29 100644 --- a/ProjectLighthouse/Types/Levels/Slot.cs +++ b/ProjectLighthouse/Types/Levels/Slot.cs @@ -6,6 +6,7 @@ using System.Xml.Serialization; using LBPUnion.ProjectLighthouse.Helpers; using LBPUnion.ProjectLighthouse.Serialization; using LBPUnion.ProjectLighthouse.Types.Profiles; +using LBPUnion.ProjectLighthouse.Types.Reviews; namespace LBPUnion.ProjectLighthouse.Types.Levels { @@ -40,7 +41,8 @@ namespace LBPUnion.ProjectLighthouse.Types.Levels [NotMapped] [XmlElement("resource")] - public string[] Resources { + public string[] Resources + { get => this.ResourceCollection.Split(","); set => this.ResourceCollection = string.Join(',', value); } @@ -102,8 +104,10 @@ namespace LBPUnion.ProjectLighthouse.Types.Levels [XmlIgnore] [NotMapped] - public int Hearts { - get { + public int Hearts + { + get + { using Database database = new(); return database.HeartedLevels.Count(s => s.SlotId == this.SlotId); @@ -160,8 +164,10 @@ namespace LBPUnion.ProjectLighthouse.Types.Levels [NotMapped] [XmlElement("thumbsup")] - public int Thumbsup { - get { + public int Thumbsup + { + get + { using Database database = new(); return database.RatedLevels.Count(r => r.SlotId == this.SlotId && r.Rating == 1); @@ -170,8 +176,10 @@ namespace LBPUnion.ProjectLighthouse.Types.Levels [NotMapped] [XmlElement("thumbsdown")] - public int Thumbsdown { - get { + public int Thumbsdown + { + get + { using Database database = new(); return database.RatedLevels.Count(r => r.SlotId == this.SlotId && r.Rating == -1); @@ -180,8 +188,10 @@ namespace LBPUnion.ProjectLighthouse.Types.Levels [NotMapped] [XmlElement("averageRating")] - public double RatingLBP1 { - get { + public double RatingLBP1 + { + get + { using Database database = new(); IQueryable ratedLevels = database.RatedLevels.Where(r => r.SlotId == this.SlotId && r.RatingLBP1 > 0); @@ -191,6 +201,18 @@ namespace LBPUnion.ProjectLighthouse.Types.Levels } } + [NotMapped] + [XmlElement("reviewCount")] + public int ReviewCount + { + get + { + using Database database = new(); + + return database.Reviews.Count(r => r.SlotId == this.SlotId); + } + } + [XmlElement("leveltype")] public string LevelType { get; set; } = ""; @@ -200,7 +222,7 @@ namespace LBPUnion.ProjectLighthouse.Types.Levels LbpSerializer.StringElement("sizeOfResources", this.Resources.Sum(FileHelper.ResourceSize)); } - public string Serialize(RatedLevel? yourRatingStats = null, VisitedLevel? yourVisitedStats = null) + public string Serialize(RatedLevel? yourRatingStats = null, VisitedLevel? yourVisitedStats = null, Review? yourReview = null) { string slotData = LbpSerializer.StringElement("name", this.Name) + @@ -249,7 +271,11 @@ namespace LBPUnion.ProjectLighthouse.Types.Levels LbpSerializer.StringElement("yourLBP2PlayCount", yourVisitedStats?.PlaysLBP2) + LbpSerializer.StringElement("yourLBP3PlayCount", yourVisitedStats?.PlaysLBP3) + LbpSerializer.StringElement - ("yourLBPVitaPlayCount", yourVisitedStats?.PlaysLBPVita); // i doubt this is the right name but we'll go with it + ("yourLBPVitaPlayCount", yourVisitedStats?.PlaysLBPVita) + // i doubt this is the right name but we'll go with it + yourReview?.Serialize("yourReview") + + LbpSerializer.StringElement("reviewsEnabled", true) + + LbpSerializer.StringElement("commentsEnabled", false) + + LbpSerializer.StringElement("reviewCount", this.ReviewCount); return LbpSerializer.TaggedStringElement("slot", slotData, "type", "user"); } diff --git a/ProjectLighthouse/Types/Reviews/Review.cs b/ProjectLighthouse/Types/Reviews/Review.cs index 3a25bd06..80ca0e80 100644 --- a/ProjectLighthouse/Types/Reviews/Review.cs +++ b/ProjectLighthouse/Types/Reviews/Review.cs @@ -1,10 +1,11 @@ #nullable enable using System; +using System.IO; using System.ComponentModel.DataAnnotations; using System.ComponentModel.DataAnnotations.Schema; -using System.Linq; using LBPUnion.ProjectLighthouse.Types.Levels; using LBPUnion.ProjectLighthouse.Serialization; +using System.Xml; using System.Xml.Serialization; namespace LBPUnion.ProjectLighthouse.Types.Reviews @@ -67,13 +68,25 @@ namespace LBPUnion.ProjectLighthouse.Types.Reviews public string Serialize(string elementOverride, RatedLevel? yourLevelRating = null, RatedReview? yourRatingStats = null) { + + XmlWriterSettings settings = new XmlWriterSettings(); + settings.OmitXmlDeclaration = true; + + XmlSerializer serializer = new(typeof(DeletedBy)); + StringWriter stringWriter = new StringWriter(); + using (XmlWriter xmlWriter = XmlWriter.Create(stringWriter, settings)) + { + serializer.Serialize(xmlWriter, this.DeletedBy); + } + string deletedBy = stringWriter.ToString(); + string reviewData = LbpSerializer.TaggedStringElement("slot_id", this.SlotId, "type", this.Slot.Type) + LbpSerializer.StringElement("reviewer", this.Reviewer.Username) + LbpSerializer.StringElement("thumb", this.Thumb) + LbpSerializer.StringElement("timestamp", this.Timestamp) + LbpSerializer.StringElement("labels", this.LabelCollection) + LbpSerializer.StringElement("deleted", this.Deleted) + - LbpSerializer.StringElement("deleted_by", this.DeletedBy) + + deletedBy + LbpSerializer.StringElement("text", this.Text) + LbpSerializer.StringElement("thumbsup", this.ThumbsUp) + LbpSerializer.StringElement("thumbsdown", this.ThumbsDown) + diff --git a/ProjectLighthouse/Types/User.cs b/ProjectLighthouse/Types/User.cs index 6f24a385..f88f0f1b 100644 --- a/ProjectLighthouse/Types/User.cs +++ b/ProjectLighthouse/Types/User.cs @@ -26,27 +26,40 @@ namespace LBPUnion.ProjectLighthouse.Types public string Biography { get; set; } [NotMapped] - public int Reviews => 0; + public int Reviews + { + get + { + using Database database = new(); + return database.Reviews.Count(r => r.ReviewerId == this.UserId); + } + } [NotMapped] - public int Comments { - get { + public int Comments + { + get + { using Database database = new(); return database.Comments.Count(c => c.TargetUserId == this.UserId); } } [NotMapped] - public int PhotosByMe { - get { + public int PhotosByMe + { + get + { using Database database = new(); return database.Photos.Count(p => p.CreatorId == this.UserId); } } [NotMapped] - public int PhotosWithMe { - get { + public int PhotosWithMe + { + get + { using Database database = new(); return Enumerable.Sum(database.Photos, photo => photo.Subjects.Count(subject => subject.User.UserId == this.UserId)); } @@ -61,24 +74,30 @@ namespace LBPUnion.ProjectLighthouse.Types public Location Location { get; set; } [NotMapped] - public int HeartedLevels { - get { + public int HeartedLevels + { + get + { using Database database = new(); return database.HeartedLevels.Count(p => p.UserId == this.UserId); } } [NotMapped] - public int HeartedUsers { - get { + public int HeartedUsers + { + get + { using Database database = new(); return database.HeartedProfiles.Count(p => p.UserId == this.UserId); } } [NotMapped] - public int QueuedLevels { - get { + public int QueuedLevels + { + get + { using Database database = new(); return database.QueuedLevels.Count(p => p.UserId == this.UserId); } @@ -88,8 +107,10 @@ namespace LBPUnion.ProjectLighthouse.Types public string PlanetHash { get; set; } = ""; - public int Hearts { - get { + public int Hearts + { + get + { using Database database = new(); return database.HeartedProfiles.Count(s => s.HeartedUserId == this.UserId); @@ -104,10 +125,12 @@ namespace LBPUnion.ProjectLighthouse.Types public string BooHash { get; set; } = ""; public string MehHash { get; set; } = ""; - #nullable enable +#nullable enable [NotMapped] - public string Status { - get { + public string Status + { + get + { using Database database = new(); LastContact? lastMatch = database.LastContacts.Where (l => l.UserId == this.UserId) @@ -118,7 +141,7 @@ namespace LBPUnion.ProjectLighthouse.Types return "Currently online on " + lastMatch.GameVersion.ToPrettyString(); } } - #nullable disable +#nullable disable public string Serialize(GameVersion gameVersion = GameVersion.LittleBigPlanet1) { @@ -156,8 +179,10 @@ namespace LBPUnion.ProjectLighthouse.Types /// The number of used slots on the earth /// [NotMapped] - public int UsedSlots { - get { + public int UsedSlots + { + get + { using Database database = new(); return database.Slots.Count(s => s.CreatorId == this.UserId); } @@ -218,7 +243,7 @@ namespace LBPUnion.ProjectLighthouse.Types #endregion Slots - #nullable enable +#nullable enable public override bool Equals(object? obj) { if (obj is User user) return user.UserId == UserId; @@ -238,6 +263,6 @@ namespace LBPUnion.ProjectLighthouse.Types [SuppressMessage("ReSharper", "NonReadonlyMemberInGetHashCode")] public override int GetHashCode() => this.UserId; - #nullable disable +#nullable disable } } \ No newline at end of file