Add support for sending/receiving LBP1 ratings

This commit is contained in:
LumaLivy 2021-11-08 21:13:16 -05:00
commit 8c4969b145
2 changed files with 64 additions and 9 deletions

View file

@ -20,29 +20,59 @@ namespace LBPUnion.ProjectLighthouse.Controllers
this.database = database;
}
[HttpPost("dpadrate/user/{slotId}")]
public async Task<IActionResult> DPadRate(int slotId, [FromQuery] int rating)
// LBP1 rating
[HttpPost("rate/user/{slotId}")]
public async Task<IActionResult> Rate(int slotId, [FromQuery] int rating)
{
User? user = await this.database.UserFromRequest(this.Request);
if (user == null) return this.StatusCode(403, "");
Slot? slot = await this.database.Slots.FirstOrDefaultAsync(s => s.SlotId == slotId);
Slot? slot = await this.database.Slots.Include(s => s.Creator).Include(s => s.Location).FirstOrDefaultAsync(s => s.SlotId == slotId);
if (slot == null) return this.StatusCode(403, "");
RatedLevel? ratedLevel = await this.database.RatedLevels.FirstOrDefaultAsync(r => r.SlotId == slotId && r.UserId == user.UserId);
if (ratedLevel == null)
{
ratedLevel = new();
ratedLevel.SlotId = slotId;
ratedLevel.UserId = user.UserId;
ratedLevel.Rating = 0;
this.database.RatedLevels.Add(ratedLevel);
}
ratedLevel.SlotId = slotId;
ratedLevel.UserId = user.UserId;
ratedLevel.Rating = rating;
// Unsupported: ratedLevel.LBP1Rating
ratedLevel.RatingLBP1 = Math.Max(Math.Min(5, rating), 0);
await this.database.SaveChangesAsync();
return this.Ok();
}
// LBP2 and beyond rating
[HttpPost("dpadrate/user/{slotId}")]
public async Task<IActionResult> DPadRate(int slotId, [FromQuery] int rating)
{
User? user = await this.database.UserFromRequest(this.Request);
if (user == null) return this.StatusCode(403, "");
Slot? slot = await this.database.Slots.Include(s => s.Creator).Include(s => s.Location).FirstOrDefaultAsync(s => s.SlotId == slotId);
if (slot == null) return this.StatusCode(403, "");
RatedLevel? ratedLevel = await this.database.RatedLevels.FirstOrDefaultAsync(r => r.SlotId == slotId && r.UserId == user.UserId);
if (ratedLevel == null)
{
ratedLevel = new();
ratedLevel.SlotId = slotId;
ratedLevel.UserId = user.UserId;
ratedLevel.RatingLBP1 = 0;
this.database.RatedLevels.Add(ratedLevel);
}
ratedLevel.Rating = Math.Max(Math.Min(1, rating), -1);
await this.database.SaveChangesAsync();
return this.Ok();
}
}
}

View file

@ -139,6 +139,7 @@ namespace LBPUnion.ProjectLighthouse.Types.Levels
[XmlIgnore]
public int PlaysLBP3Unique { get; set; }
[NotMapped]
[XmlElement("thumbsup")]
public int Thumbsup
{
@ -149,6 +150,8 @@ namespace LBPUnion.ProjectLighthouse.Types.Levels
return database.RatedLevels.Count(r => r.SlotId == this.SlotId && r.Rating == 1);
}
}
[NotMapped]
[XmlElement("thumbsdown")]
public int Thumbsdown
{
@ -159,6 +162,8 @@ namespace LBPUnion.ProjectLighthouse.Types.Levels
return database.RatedLevels.Count(r => r.SlotId == this.SlotId && r.Rating == -1);
}
}
[NotMapped]
[XmlElement("averageRating")]
public double RatingLBP1 { get {
using Database database = new();
@ -167,8 +172,24 @@ namespace LBPUnion.ProjectLighthouse.Types.Levels
if (!ratedLevels.Any()) return 3.0;
return Enumerable.Average(ratedLevels, r => r.RatingLBP1); ;
}
}
}
}
[NotMapped]
[XmlElement("yourRating")]
public double YourRating { get; set; }
[NotMapped]
[XmlElement("yourDPadRating")]
public int YourDPadRating { get; set; }
[NotMapped]
[XmlElement("yourLBP1PlayCount")]
public int YourLBP1PlayCount { get; set; }
[NotMapped]
[XmlElement("yourLBP2PlayCount")]
public int YourLBP2PlayCount { get; set; }
public string SerializeResources()
{
@ -212,6 +233,10 @@ namespace LBPUnion.ProjectLighthouse.Types.Levels
LbpSerializer.StringElement("lbp3UniquePlayCount", this.PlaysLBP3Unique) +
LbpSerializer.StringElement("thumbsup", this.Thumbsup) +
LbpSerializer.StringElement("thumbsdown", this.Thumbsdown) +
LbpSerializer.StringElement("yourRating", this.YourRating) +
LbpSerializer.StringElement("yourDPadRating", this.YourDPadRating) +
LbpSerializer.StringElement("yourLBP1PlayCount", this.YourLBP1PlayCount) +
LbpSerializer.StringElement("yourLBP2PlayCount", this.YourLBP2PlayCount) +
LbpSerializer.StringElement("averageRating", this.RatingLBP1);
return LbpSerializer.TaggedStringElement("slot", slotData, "type", "user");