diff --git a/src/Controllers/Common/RatingController.cs b/src/Controllers/Common/RatingController.cs index f589074..77f51eb 100644 --- a/src/Controllers/Common/RatingController.cs +++ b/src/Controllers/Common/RatingController.cs @@ -1,13 +1,24 @@ using Microsoft.AspNetCore.Http; using Microsoft.AspNetCore.Mvc; +using Microsoft.Extensions.Options; +using sodoff.Attributes; +using sodoff.Configuration; using sodoff.Model; using sodoff.Schema; +using sodoff.Services; using sodoff.Util; +using System.Security.Cryptography; namespace sodoff.Controllers.Common; public class RatingController : Controller { + private readonly DBContext ctx; + + public RatingController(DBContext ctx) { + this.ctx = ctx; + } + [HttpPost] [Produces("application/xml")] [Route("MissionWebService.asmx/GetPayout")] // used by World Of Jumpstart @@ -103,4 +114,177 @@ public class RatingController : Controller return Ok(5); } + // This method is the only thing that adds ratings. + public RatingInfo SubmitRating(Viking viking, int category, int? eID, string? uID, int value) { + RatingRank? rank; + Rating? rating = viking.Ratings.FirstOrDefault( + r => category == r.CategoryID && r.RatedEntityID == eID && r.RatedUserID == uID + ); + bool newRating = rating == null; + if (newRating) { + rating = new Rating { + OwnerId = viking.Id, + CategoryID = category, + RatedEntityID = eID, + RatedUserID = uID + }; + ctx.Ratings.Add(rating); + rank = ctx.RatingRanks.FirstOrDefault(rr => rr.CategoryID == category && rr.RatedEntityID == eID && rr.RatedUserID == uID); + } else { + rank = rating.Rank; + } + if (rank == null) { + rank = new RatingRank { + CategoryID = category, + RatedEntityID = eID, + RatedUserID = uID, + Rank = 0 // Start here, work way down. + }; + ctx.RatingRanks.Add(rank); + } + rank.TotalVotes = rank.Ratings?.Count??1; + if (newRating) { + rating.Rank = rank; + rank.TotalVotes++; + } + if (rank.Ratings != null) { + rank.RatingAverage = 0; + foreach (Rating r in rank.Ratings) { + if (r == rating) continue; + rank.RatingAverage += (float)((decimal)r.Value / (decimal)rank.TotalVotes); + } + rank.RatingAverage += (float)((decimal)value / (decimal)rank.TotalVotes); + } else { + rank.RatingAverage = value; + } + if (eID != -1 || uID != null) { + RatingRank[] ranks = ctx.RatingRanks + .Where(rr => rr.CategoryID == category) // Only rank by category. + .OrderBy(rr => rr.Rank) + .ToArray(); + bool resortOthers = false; + for (int i=0;i r.CategoryID == categoryID && r.RatedEntityID == ratedEntityID && r.RatedUserID == null) + .Select(r => new RatingInfo { + Id = r.Id, + OwnerUid = r.Owner.Uid, + CategoryID = r.CategoryID, + RatedEntityID = r.RatedEntityID, + Value = r.Value, + Date = r.Date + } + ).ToArray(); + } + + [HttpPost] + [Route("RatingWebService.asmx/DeleteEntityRating")] + [VikingSession] + public IActionResult DeleteRating(Viking viking, [FromForm] int categoryID, [FromForm] int ratedEntityID) { + Rating? rating = viking.Ratings.FirstOrDefault( + r => categoryID == r.CategoryID && r.RatedEntityID == ratedEntityID && r.RatedUserID == null + ); + if (rating != null) ctx.Ratings.Remove(rating); + return Ok(); + } + + [HttpPost] + [Produces("application/xml")] + [Route("RatingWebService.asmx/GetTopRatedByCategoryID")] + public RatingRankInfo[] GetRanks([FromForm] int categoryID, [FromForm] int numberOfRecord) { + return ctx.RatingRanks + .Where(rr => categoryID == rr.CategoryID && rr.RatedUserID == null) + .Take(numberOfRecord) + .Select(rr => new RatingRankInfo(rr)) + .ToArray(); + } + + [HttpPost] + [Produces("application/xml")] + [Route("RatingWebService.asmx/GetTopRatedUserByCategoryID")] + public IActionResult GetUserRanks([FromForm] int categoryID, [FromForm] int numberOfRecord) { + return Ok(new ArrayOfUserRatingRankInfo { + UserRatingRankInfo = ctx.RatingRanks + .Where(rr => rr.RatedUserID != null && (categoryID == rr.CategoryID + || (categoryID == 4 && rr.CategoryID == 5) // The party board searches for 4 but the pod rating is set by 5. + )) + .Take(numberOfRecord) + .Select(rr => new UserRatingRankInfo { RankInfo = new RatingRankInfo(rr) }) + .ToArray() + }); + } + + [HttpPost] + [Produces("application/xml")] + [Route("RatingWebService.asmx/GetEntityRatedRank")] + public IActionResult GetRank([FromForm] int categoryID, [FromForm] int ratedEntityID) { + // TODO: Add a shortcut here for shipwreck lagoon tracks. + RatingRank? rank = ctx.RatingRanks.FirstOrDefault(rr => categoryID == rr.CategoryID && rr.RatedEntityID == ratedEntityID); + if (rank == null) return Ok(); + return Ok(new RatingRankInfo(rank)); + } + + [HttpPost] + [Produces("application/xml")] + [Route("RatingWebService.asmx/GetRatingForRatedUser")] + public IActionResult GetUserRating([FromForm] int categoryID, [FromForm] string ratedUserID) { + Rating? rating = ctx.Ratings.FirstOrDefault( + r => categoryID == r.CategoryID && r.RatedEntityID == null && r.RatedUserID == ratedUserID + ); + return Ok(rating?.Value ?? 0); + } + + [HttpPost] + [Produces("application/xml")] + [Route("RatingWebService.asmx/GetRatingForRatedEntity")] + public IActionResult GetRating([FromForm] int categoryID, [FromForm] int ratedEntityID) { + Rating? rating = ctx.Ratings.FirstOrDefault( + r => categoryID == r.CategoryID && r.RatedEntityID == ratedEntityID && r.RatedUserID == null + ); + return Ok(rating?.Value ?? 0); + } } diff --git a/src/Model/DBContext.cs b/src/Model/DBContext.cs index 0d287df..45c5356 100644 --- a/src/Model/DBContext.cs +++ b/src/Model/DBContext.cs @@ -26,6 +26,8 @@ public class DBContext : DbContext { public DbSet Neighborhoods { get; set; } = null!; // we had a brief debate on whether it's neighborhoods or neighborheed public DbSet Groups { get; set; } = null!; + public DbSet Ratings { get; set; } = null!; + public DbSet RatingRanks { get; set; } = null!; private readonly IOptions config; @@ -140,6 +142,9 @@ public class DBContext : DbContext { builder.Entity().HasMany(v => v.Groups) .WithMany(e => e.Vikings); + builder.Entity().HasMany(v => v.Ratings) + .WithOne(r => r.Owner); + // Dragons builder.Entity().HasOne(d => d.Viking) .WithMany(e => e.Dragons) @@ -260,5 +265,17 @@ public class DBContext : DbContext { // Groups builder.Entity().HasMany(r => r.Vikings) .WithMany(e => e.Groups); + + // Rating + builder.Entity().HasOne(r => r.Owner) + .WithMany(v => v.Ratings) + .HasForeignKey(r => r.OwnerId); + + builder.Entity().HasOne(r => r.Rank) + .WithMany(rr => rr.Ratings) + .HasForeignKey(r => r.RankId); + + builder.Entity().HasMany(rr => rr.Ratings) + .WithOne(r => r.Rank); } } diff --git a/src/Model/Rating.cs b/src/Model/Rating.cs new file mode 100644 index 0000000..bcbe2eb --- /dev/null +++ b/src/Model/Rating.cs @@ -0,0 +1,28 @@ +using sodoff.Schema; +using System.ComponentModel.DataAnnotations; + +namespace sodoff.Model; + +public class Rating { + [Key] + public int Id { get; set; } + + /// Viking that controls this data. + public virtual Viking? Owner { get; set; } + + public virtual RatingRank? Rank { get; set; } + + /// VikingId + public int OwnerId { get; set; } + + public int RankId { get; set; } // Done this to prevent it from generating an unnecessary pairs table. + + public int CategoryID { get; set; } + + public int? RatedEntityID { get; set; } + public string? RatedUserID { get; set; } + + public int Value { get; set; } + + public DateTime Date { get; set; } +} diff --git a/src/Model/RatingRank.cs b/src/Model/RatingRank.cs new file mode 100644 index 0000000..1e2d12f --- /dev/null +++ b/src/Model/RatingRank.cs @@ -0,0 +1,25 @@ +using System.ComponentModel.DataAnnotations; +using System.Xml.Serialization; + +namespace sodoff.Model; + +public class RatingRank { + [Key] + public int Id { get; set; } + + public int CategoryID { get; set; } + + public int? RatedEntityID { get; set; } + public string? RatedUserID { get; set; } + + public int Rank { get; set; } + + /// On a scale of 1-5 + public float RatingAverage { get; set; } + + public int TotalVotes { get; set; } + + public DateTime UpdateDate { get; set; } + + public virtual ICollection Ratings { get; set; } = null!; +} diff --git a/src/Model/Viking.cs b/src/Model/Viking.cs index 418a1dc..048c413 100644 --- a/src/Model/Viking.cs +++ b/src/Model/Viking.cs @@ -39,6 +39,7 @@ public class Viking { public virtual ICollection MMORoles { get; set; } = null!; public virtual Neighborhood? Neighborhood { get; set; } = null!; public virtual ICollection Groups { get; set; } = null!; + public virtual ICollection Ratings { get; set; } = null!; public virtual Dragon? SelectedDragon { get; set; } public DateTime? CreationDate { get; set; } diff --git a/src/Schema/ArrayOfUserRatingRankInfo.cs b/src/Schema/ArrayOfUserRatingRankInfo.cs new file mode 100644 index 0000000..2ebcdd7 --- /dev/null +++ b/src/Schema/ArrayOfUserRatingRankInfo.cs @@ -0,0 +1,10 @@ +using System.Xml.Serialization; + +namespace sodoff.Schema; + +[XmlRoot(ElementName = "ArrayOfUserRatingRankInfo", Namespace = "")] +[Serializable] +public class ArrayOfUserRatingRankInfo { + [XmlElement(ElementName = "UserRatingRankInfo")] + public UserRatingRankInfo[] UserRatingRankInfo; +} diff --git a/src/Schema/RatingInfo.cs b/src/Schema/RatingInfo.cs new file mode 100644 index 0000000..42eef8e --- /dev/null +++ b/src/Schema/RatingInfo.cs @@ -0,0 +1,26 @@ +using sodoff.Model; +using System.Xml.Serialization; + +namespace sodoff.Schema; + +[XmlRoot(ElementName = "RatingInfo", Namespace = "")] +[Serializable] +public class RatingInfo { + [XmlElement(ElementName = "ID")] + public int Id; + + [XmlElement(ElementName = "UID")] + public Guid OwnerUid; + + [XmlElement(ElementName = "CID")] + public int CategoryID; + + [XmlElement(ElementName = "EID")] + public int? RatedEntityID; + + [XmlElement(ElementName = "RV")] + public int Value; + + [XmlElement(ElementName = "RD")] + public DateTime Date; +} diff --git a/src/Schema/RatingRankInfo.cs b/src/Schema/RatingRankInfo.cs new file mode 100644 index 0000000..fd576d7 --- /dev/null +++ b/src/Schema/RatingRankInfo.cs @@ -0,0 +1,41 @@ +using sodoff.Model; +using System.Xml.Serialization; + +namespace sodoff.Schema; + +[XmlRoot(ElementName = "RatingRankInfo", Namespace = "")] +[Serializable] +public class RatingRankInfo { + + public RatingRankInfo() {} + public RatingRankInfo(RatingRank rank) { + Id = rank.Id; + CategoryID = rank.CategoryID; + RatedEntityID = rank.RatedEntityID; + Rank = rank.Rank; + RatingAverage = rank.RatingAverage; + TotalVotes = rank.TotalVotes; + UpdateDate = rank.UpdateDate; + } + + [XmlElement(ElementName = "ID")] + public int Id; + + [XmlElement(ElementName = "CID")] + public int CategoryID; + + [XmlElement(ElementName = "EID")] + public int? RatedEntityID; + + [XmlElement(ElementName = "R")] + public int Rank; + + [XmlElement(ElementName = "RA")] + public float RatingAverage; + + [XmlElement(ElementName = "TV")] + public int TotalVotes; + + [XmlElement(ElementName = "UD")] + public DateTime UpdateDate; +} \ No newline at end of file diff --git a/src/Schema/UserRatingRankInfo.cs b/src/Schema/UserRatingRankInfo.cs new file mode 100644 index 0000000..2a94044 --- /dev/null +++ b/src/Schema/UserRatingRankInfo.cs @@ -0,0 +1,14 @@ +using sodoff.Model; +using System.Xml.Serialization; + +namespace sodoff.Schema; + +[XmlRoot(ElementName = "URRI", Namespace = "")] +[Serializable] +public class UserRatingRankInfo { + [XmlElement(ElementName = "RI")] + public RatingRankInfo RankInfo; + + [XmlElement(ElementName = "RUID")] + public Guid RatedUserID; +}