Reviews now appear ingame

This commit is contained in:
LumaLivy 2021-12-11 01:03:46 -05:00
commit d596eeefc2
5 changed files with 108 additions and 40 deletions

View file

@ -146,10 +146,13 @@ namespace LBPUnion.ProjectLighthouse.Controllers
Random rand = new(); Random rand = new();
Review? yourReview = await this.database.Reviews.FirstOrDefaultAsync(r => r.ReviewerId == user.UserId && r.SlotId == slotId && r.Slot.GameVersion <= gameVersion); 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); 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); Slot? slot = await this.database.Slots.FirstOrDefaultAsync(s => s.SlotId == slotId);
if (slot == null) return this.BadRequest(); if (slot == null) return this.BadRequest();
Boolean canNowReviewLevel = visitedLevel != null && yourReview == null;
Boolean canNowReviewLevel = slot.CreatorId != user.UserId && visitedLevel != null && yourReview == null;
if (canNowReviewLevel) if (canNowReviewLevel)
{ {
RatedLevel? ratedLevel = await this.database.RatedLevels.FirstOrDefaultAsync(r => r.UserId == user.UserId && r.SlotId == slotId && r.Slot.GameVersion <= gameVersion); 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.Thumb = ratedLevel?.Rating == null ? 0 : ratedLevel.Rating;
yourReview.Slot = slot; yourReview.Slot = slot;
yourReview.SlotId = slotId; yourReview.SlotId = slotId;
yourReview.Deleted = false;
yourReview.DeletedBy = DeletedBy.None; yourReview.DeletedBy = DeletedBy.None;
yourReview.Text = "You haven't reviewed this level yet. Edit this blank review to upload it!"; yourReview.Text = "You haven't reviewed this level yet. Edit this blank review to upload it!";
yourReview.LabelCollection = ""; yourReview.LabelCollection = "";
yourReview.Deleted = false;
yourReview.Timestamp = TimeHelper.UnixTimeMilliseconds(); yourReview.Timestamp = TimeHelper.UnixTimeMilliseconds();
} }
@ -226,9 +229,9 @@ namespace LBPUnion.ProjectLighthouse.Controllers
string inner = Enumerable.Aggregate(reviews, string.Empty, (current, review) => 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); //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<string, object> string response = LbpSerializer.TaggedStringElement("reviews", inner, new Dictionary<string, object>

View file

@ -2,6 +2,7 @@ using System.Xml.Serialization;
namespace LBPUnion.ProjectLighthouse.Types namespace LBPUnion.ProjectLighthouse.Types
{ {
[XmlRoot("deleted_by")]
public enum DeletedBy public enum DeletedBy
{ {
[XmlEnum(Name = "none")] [XmlEnum(Name = "none")]

View file

@ -6,6 +6,7 @@ using System.Xml.Serialization;
using LBPUnion.ProjectLighthouse.Helpers; using LBPUnion.ProjectLighthouse.Helpers;
using LBPUnion.ProjectLighthouse.Serialization; using LBPUnion.ProjectLighthouse.Serialization;
using LBPUnion.ProjectLighthouse.Types.Profiles; using LBPUnion.ProjectLighthouse.Types.Profiles;
using LBPUnion.ProjectLighthouse.Types.Reviews;
namespace LBPUnion.ProjectLighthouse.Types.Levels namespace LBPUnion.ProjectLighthouse.Types.Levels
{ {
@ -40,7 +41,8 @@ namespace LBPUnion.ProjectLighthouse.Types.Levels
[NotMapped] [NotMapped]
[XmlElement("resource")] [XmlElement("resource")]
public string[] Resources { public string[] Resources
{
get => this.ResourceCollection.Split(","); get => this.ResourceCollection.Split(",");
set => this.ResourceCollection = string.Join(',', value); set => this.ResourceCollection = string.Join(',', value);
} }
@ -102,8 +104,10 @@ namespace LBPUnion.ProjectLighthouse.Types.Levels
[XmlIgnore] [XmlIgnore]
[NotMapped] [NotMapped]
public int Hearts { public int Hearts
get { {
get
{
using Database database = new(); using Database database = new();
return database.HeartedLevels.Count(s => s.SlotId == this.SlotId); return database.HeartedLevels.Count(s => s.SlotId == this.SlotId);
@ -160,8 +164,10 @@ namespace LBPUnion.ProjectLighthouse.Types.Levels
[NotMapped] [NotMapped]
[XmlElement("thumbsup")] [XmlElement("thumbsup")]
public int Thumbsup { public int Thumbsup
get { {
get
{
using Database database = new(); using Database database = new();
return database.RatedLevels.Count(r => r.SlotId == this.SlotId && r.Rating == 1); return database.RatedLevels.Count(r => r.SlotId == this.SlotId && r.Rating == 1);
@ -170,8 +176,10 @@ namespace LBPUnion.ProjectLighthouse.Types.Levels
[NotMapped] [NotMapped]
[XmlElement("thumbsdown")] [XmlElement("thumbsdown")]
public int Thumbsdown { public int Thumbsdown
get { {
get
{
using Database database = new(); using Database database = new();
return database.RatedLevels.Count(r => r.SlotId == this.SlotId && r.Rating == -1); return database.RatedLevels.Count(r => r.SlotId == this.SlotId && r.Rating == -1);
@ -180,8 +188,10 @@ namespace LBPUnion.ProjectLighthouse.Types.Levels
[NotMapped] [NotMapped]
[XmlElement("averageRating")] [XmlElement("averageRating")]
public double RatingLBP1 { public double RatingLBP1
get { {
get
{
using Database database = new(); using Database database = new();
IQueryable<RatedLevel> ratedLevels = database.RatedLevels.Where(r => r.SlotId == this.SlotId && r.RatingLBP1 > 0); IQueryable<RatedLevel> 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")] [XmlElement("leveltype")]
public string LevelType { get; set; } = ""; public string LevelType { get; set; } = "";
@ -200,7 +222,7 @@ namespace LBPUnion.ProjectLighthouse.Types.Levels
LbpSerializer.StringElement("sizeOfResources", this.Resources.Sum(FileHelper.ResourceSize)); 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) + string slotData = LbpSerializer.StringElement("name", this.Name) +
@ -249,7 +271,11 @@ namespace LBPUnion.ProjectLighthouse.Types.Levels
LbpSerializer.StringElement("yourLBP2PlayCount", yourVisitedStats?.PlaysLBP2) + LbpSerializer.StringElement("yourLBP2PlayCount", yourVisitedStats?.PlaysLBP2) +
LbpSerializer.StringElement("yourLBP3PlayCount", yourVisitedStats?.PlaysLBP3) + LbpSerializer.StringElement("yourLBP3PlayCount", yourVisitedStats?.PlaysLBP3) +
LbpSerializer.StringElement 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"); return LbpSerializer.TaggedStringElement("slot", slotData, "type", "user");
} }

View file

@ -1,10 +1,11 @@
#nullable enable #nullable enable
using System; using System;
using System.IO;
using System.ComponentModel.DataAnnotations; using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema; using System.ComponentModel.DataAnnotations.Schema;
using System.Linq;
using LBPUnion.ProjectLighthouse.Types.Levels; using LBPUnion.ProjectLighthouse.Types.Levels;
using LBPUnion.ProjectLighthouse.Serialization; using LBPUnion.ProjectLighthouse.Serialization;
using System.Xml;
using System.Xml.Serialization; using System.Xml.Serialization;
namespace LBPUnion.ProjectLighthouse.Types.Reviews 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) 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) + string reviewData = LbpSerializer.TaggedStringElement("slot_id", this.SlotId, "type", this.Slot.Type) +
LbpSerializer.StringElement("reviewer", this.Reviewer.Username) + LbpSerializer.StringElement("reviewer", this.Reviewer.Username) +
LbpSerializer.StringElement("thumb", this.Thumb) + LbpSerializer.StringElement("thumb", this.Thumb) +
LbpSerializer.StringElement("timestamp", this.Timestamp) + LbpSerializer.StringElement("timestamp", this.Timestamp) +
LbpSerializer.StringElement("labels", this.LabelCollection) + LbpSerializer.StringElement("labels", this.LabelCollection) +
LbpSerializer.StringElement("deleted", this.Deleted) + LbpSerializer.StringElement("deleted", this.Deleted) +
LbpSerializer.StringElement("deleted_by", this.DeletedBy) + deletedBy +
LbpSerializer.StringElement("text", this.Text) + LbpSerializer.StringElement("text", this.Text) +
LbpSerializer.StringElement("thumbsup", this.ThumbsUp) + LbpSerializer.StringElement("thumbsup", this.ThumbsUp) +
LbpSerializer.StringElement("thumbsdown", this.ThumbsDown) + LbpSerializer.StringElement("thumbsdown", this.ThumbsDown) +

View file

@ -26,27 +26,40 @@ namespace LBPUnion.ProjectLighthouse.Types
public string Biography { get; set; } public string Biography { get; set; }
[NotMapped] [NotMapped]
public int Reviews => 0; public int Reviews
{
get
{
using Database database = new();
return database.Reviews.Count(r => r.ReviewerId == this.UserId);
}
}
[NotMapped] [NotMapped]
public int Comments { public int Comments
get { {
get
{
using Database database = new(); using Database database = new();
return database.Comments.Count(c => c.TargetUserId == this.UserId); return database.Comments.Count(c => c.TargetUserId == this.UserId);
} }
} }
[NotMapped] [NotMapped]
public int PhotosByMe { public int PhotosByMe
get { {
get
{
using Database database = new(); using Database database = new();
return database.Photos.Count(p => p.CreatorId == this.UserId); return database.Photos.Count(p => p.CreatorId == this.UserId);
} }
} }
[NotMapped] [NotMapped]
public int PhotosWithMe { public int PhotosWithMe
get { {
get
{
using Database database = new(); using Database database = new();
return Enumerable.Sum(database.Photos, photo => photo.Subjects.Count(subject => subject.User.UserId == this.UserId)); 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; } public Location Location { get; set; }
[NotMapped] [NotMapped]
public int HeartedLevels { public int HeartedLevels
get { {
get
{
using Database database = new(); using Database database = new();
return database.HeartedLevels.Count(p => p.UserId == this.UserId); return database.HeartedLevels.Count(p => p.UserId == this.UserId);
} }
} }
[NotMapped] [NotMapped]
public int HeartedUsers { public int HeartedUsers
get { {
get
{
using Database database = new(); using Database database = new();
return database.HeartedProfiles.Count(p => p.UserId == this.UserId); return database.HeartedProfiles.Count(p => p.UserId == this.UserId);
} }
} }
[NotMapped] [NotMapped]
public int QueuedLevels { public int QueuedLevels
get { {
get
{
using Database database = new(); using Database database = new();
return database.QueuedLevels.Count(p => p.UserId == this.UserId); return database.QueuedLevels.Count(p => p.UserId == this.UserId);
} }
@ -88,8 +107,10 @@ namespace LBPUnion.ProjectLighthouse.Types
public string PlanetHash { get; set; } = ""; public string PlanetHash { get; set; } = "";
public int Hearts { public int Hearts
get { {
get
{
using Database database = new(); using Database database = new();
return database.HeartedProfiles.Count(s => s.HeartedUserId == this.UserId); return database.HeartedProfiles.Count(s => s.HeartedUserId == this.UserId);
@ -104,10 +125,12 @@ namespace LBPUnion.ProjectLighthouse.Types
public string BooHash { get; set; } = ""; public string BooHash { get; set; } = "";
public string MehHash { get; set; } = ""; public string MehHash { get; set; } = "";
#nullable enable #nullable enable
[NotMapped] [NotMapped]
public string Status { public string Status
get { {
get
{
using Database database = new(); using Database database = new();
LastContact? lastMatch = database.LastContacts.Where LastContact? lastMatch = database.LastContacts.Where
(l => l.UserId == this.UserId) (l => l.UserId == this.UserId)
@ -118,7 +141,7 @@ namespace LBPUnion.ProjectLighthouse.Types
return "Currently online on " + lastMatch.GameVersion.ToPrettyString(); return "Currently online on " + lastMatch.GameVersion.ToPrettyString();
} }
} }
#nullable disable #nullable disable
public string Serialize(GameVersion gameVersion = GameVersion.LittleBigPlanet1) public string Serialize(GameVersion gameVersion = GameVersion.LittleBigPlanet1)
{ {
@ -156,8 +179,10 @@ namespace LBPUnion.ProjectLighthouse.Types
/// The number of used slots on the earth /// The number of used slots on the earth
/// </summary> /// </summary>
[NotMapped] [NotMapped]
public int UsedSlots { public int UsedSlots
get { {
get
{
using Database database = new(); using Database database = new();
return database.Slots.Count(s => s.CreatorId == this.UserId); return database.Slots.Count(s => s.CreatorId == this.UserId);
} }
@ -218,7 +243,7 @@ namespace LBPUnion.ProjectLighthouse.Types
#endregion Slots #endregion Slots
#nullable enable #nullable enable
public override bool Equals(object? obj) public override bool Equals(object? obj)
{ {
if (obj is User user) return user.UserId == UserId; if (obj is User user) return user.UserId == UserId;
@ -238,6 +263,6 @@ namespace LBPUnion.ProjectLighthouse.Types
[SuppressMessage("ReSharper", "NonReadonlyMemberInGetHashCode")] [SuppressMessage("ReSharper", "NonReadonlyMemberInGetHashCode")]
public override int GetHashCode() => this.UserId; public override int GetHashCode() => this.UserId;
#nullable disable #nullable disable
} }
} }