diff --git a/ProjectLighthouse/Controllers/GameApi/Slots/CollectionController.cs b/ProjectLighthouse/Controllers/GameApi/Slots/CollectionController.cs index bcf0f382..7c08add8 100644 --- a/ProjectLighthouse/Controllers/GameApi/Slots/CollectionController.cs +++ b/ProjectLighthouse/Controllers/GameApi/Slots/CollectionController.cs @@ -74,8 +74,13 @@ public class CollectionController : ControllerBase [HttpGet("searches/{endpointName}")] public async Task GetCategorySlots(string endpointName, [FromQuery] int pageStart, [FromQuery] int pageSize) { - User? user = await this.database.UserFromGameRequest(this.Request); - if (user == null) return this.StatusCode(403, ""); + (User, GameToken)? userAndToken = await this.database.UserAndGameTokenFromRequest(this.Request); + + if (userAndToken == null) return this.StatusCode(403, ""); + + // ReSharper disable once PossibleInvalidOperationException + User user = userAndToken.Value.Item1; + GameToken gameToken = userAndToken.Value.Item2; Category? category = CollectionHelper.Categories.FirstOrDefault(c => c.Endpoint == endpointName); if (category == null) return this.NotFound(); @@ -96,7 +101,7 @@ public class CollectionController : ControllerBase totalSlots = category.GetTotalSlots(this.database); } - string slotsSerialized = slots.Aggregate(string.Empty, (current, slot) => current + slot.Serialize()); + string slotsSerialized = slots.Aggregate(string.Empty, (current, slot) => current + slot.Serialize(gameToken.GameVersion)); return this.Ok ( diff --git a/ProjectLighthouse/Controllers/GameApi/Slots/ListController.cs b/ProjectLighthouse/Controllers/GameApi/Slots/ListController.cs index d7afe176..e51c56b7 100644 --- a/ProjectLighthouse/Controllers/GameApi/Slots/ListController.cs +++ b/ProjectLighthouse/Controllers/GameApi/Slots/ListController.cs @@ -44,7 +44,7 @@ public class ListController : ControllerBase .Take(Math.Min(pageSize, 30)) .AsEnumerable(); - string response = queuedLevels.Aggregate(string.Empty, (current, q) => current + q.Slot.Serialize()); + string response = queuedLevels.Aggregate(string.Empty, (current, q) => current + q.Slot.Serialize(gameVersion)); return this.Ok ( @@ -116,7 +116,7 @@ public class ListController : ControllerBase .Take(Math.Min(pageSize, 30)) .AsEnumerable(); - string response = heartedLevels.Aggregate(string.Empty, (current, q) => current + q.Slot.Serialize()); + string response = heartedLevels.Aggregate(string.Empty, (current, q) => current + q.Slot.Serialize(gameVersion)); return this.Ok ( diff --git a/ProjectLighthouse/Controllers/GameApi/Slots/PublishController.cs b/ProjectLighthouse/Controllers/GameApi/Slots/PublishController.cs index c0e3e6f4..b7128d8d 100644 --- a/ProjectLighthouse/Controllers/GameApi/Slots/PublishController.cs +++ b/ProjectLighthouse/Controllers/GameApi/Slots/PublishController.cs @@ -130,7 +130,7 @@ public class PublishController : ControllerBase this.database.Entry(oldSlot).CurrentValues.SetValues(slot); await this.database.SaveChangesAsync(); - return this.Ok(oldSlot.Serialize()); + return this.Ok(oldSlot.Serialize(gameToken.GameVersion)); } //TODO: parse location in body @@ -162,7 +162,7 @@ public class PublishController : ControllerBase $"**{user.Username}** just published a new level: [**{slot.Name}**]({ServerSettings.Instance.ExternalUrl}/slot/{slot.SlotId})\n{slot.Description}" ); - return this.Ok(slot.Serialize()); + return this.Ok(slot.Serialize(gameToken.GameVersion)); } [HttpPost("unpublish/{id:int}")] diff --git a/ProjectLighthouse/Controllers/GameApi/Slots/SearchController.cs b/ProjectLighthouse/Controllers/GameApi/Slots/SearchController.cs index f99e2322..b073cd11 100644 --- a/ProjectLighthouse/Controllers/GameApi/Slots/SearchController.cs +++ b/ProjectLighthouse/Controllers/GameApi/Slots/SearchController.cs @@ -3,6 +3,7 @@ using System.Collections.Generic; using System.Linq; using System.Threading.Tasks; using LBPUnion.ProjectLighthouse.Serialization; +using LBPUnion.ProjectLighthouse.Types; using LBPUnion.ProjectLighthouse.Types.Levels; using Microsoft.AspNetCore.Mvc; using Microsoft.EntityFrameworkCore; @@ -23,6 +24,14 @@ public class SearchController : ControllerBase [HttpGet("slots/search")] public async Task SearchSlots([FromQuery] string query, [FromQuery] int pageSize, [FromQuery] int pageStart) { + (User, GameToken)? userAndToken = await this.database.UserAndGameTokenFromRequest(this.Request); + + if (userAndToken == null) return this.StatusCode(403, ""); + + // ReSharper disable once PossibleInvalidOperationException + User user = userAndToken.Value.Item1; + GameToken gameToken = userAndToken.Value.Item2; + if (query == null) return this.BadRequest(); query = query.ToLower(); @@ -46,7 +55,7 @@ public class SearchController : ControllerBase List slots = await dbQuery.Skip(pageStart - 1).Take(Math.Min(pageSize, 30)).ToListAsync(); - string response = slots.Aggregate("", (current, slot) => current + slot.Serialize()); + string response = slots.Aggregate("", (current, slot) => current + slot.Serialize(gameToken.GameVersion)); return this.Ok(LbpSerializer.TaggedStringElement("slots", response, "total", dbQuery.Count())); } diff --git a/ProjectLighthouse/Controllers/GameApi/Slots/SlotsController.cs b/ProjectLighthouse/Controllers/GameApi/Slots/SlotsController.cs index d297fd2a..26084d73 100644 --- a/ProjectLighthouse/Controllers/GameApi/Slots/SlotsController.cs +++ b/ProjectLighthouse/Controllers/GameApi/Slots/SlotsController.cs @@ -55,7 +55,7 @@ public class SlotsController : ControllerBase .Skip(pageStart - 1) .Take(Math.Min(pageSize, ServerSettings.Instance.EntitledSlots)), string.Empty, - (current, slot) => current + slot.Serialize() + (current, slot) => current + slot.Serialize(token.GameVersion) ); return this.Ok @@ -94,7 +94,7 @@ public class SlotsController : ControllerBase RatedLevel? ratedLevel = await this.database.RatedLevels.FirstOrDefaultAsync(r => r.SlotId == id && r.UserId == user.UserId); VisitedLevel? visitedLevel = await this.database.VisitedLevels.FirstOrDefaultAsync(r => r.SlotId == id && r.UserId == user.UserId); - return this.Ok(slot.Serialize(ratedLevel, visitedLevel)); + return this.Ok(slot.Serialize(gameVersion, ratedLevel, visitedLevel)); } [HttpGet("slots/cool")] @@ -131,7 +131,7 @@ public class SlotsController : ControllerBase IQueryable slots = this.getSlots(gameVersion).OrderByDescending(s => s.FirstUploaded).Skip(pageStart - 1).Take(Math.Min(pageSize, 30)); - string response = Enumerable.Aggregate(slots, string.Empty, (current, slot) => current + slot.Serialize()); + string response = Enumerable.Aggregate(slots, string.Empty, (current, slot) => current + slot.Serialize(gameVersion)); return this.Ok ( @@ -165,7 +165,7 @@ public class SlotsController : ControllerBase .OrderByDescending(s => s.LastUpdated) .Skip(pageStart - 1) .Take(Math.Min(pageSize, 30)); - string response = Enumerable.Aggregate(slots, string.Empty, (current, slot) => current + slot.Serialize()); + string response = Enumerable.Aggregate(slots, string.Empty, (current, slot) => current + slot.Serialize(gameVersion)); return this.Ok ( @@ -196,7 +196,7 @@ public class SlotsController : ControllerBase IEnumerable slots = this.getSlots(gameVersion).OrderBy(_ => EF.Functions.Random()).Take(Math.Min(pageSize, 30)); - string response = slots.Aggregate(string.Empty, (current, slot) => current + slot.Serialize()); + string response = slots.Aggregate(string.Empty, (current, slot) => current + slot.Serialize(gameVersion)); return this.Ok ( @@ -240,7 +240,7 @@ public class SlotsController : ControllerBase .Skip(pageStart - 1) .Take(Math.Min(pageSize, 30)); - string response = slots.Aggregate(string.Empty, (current, slot) => current + slot.Serialize()); + string response = slots.Aggregate(string.Empty, (current, slot) => current + slot.Serialize(token.GameVersion)); return this.Ok ( @@ -298,7 +298,7 @@ public class SlotsController : ControllerBase .Skip(pageStart - 1) .Take(Math.Min(pageSize, 30)); - string response = slots.Aggregate(string.Empty, (current, slot) => current + slot.Serialize()); + string response = slots.Aggregate(string.Empty, (current, slot) => current + slot.Serialize(token.GameVersion)); return this.Ok ( @@ -342,7 +342,7 @@ public class SlotsController : ControllerBase .Skip(pageStart - 1) .Take(Math.Min(pageSize, 30)); - string response = slots.Aggregate(string.Empty, (current, slot) => current + slot.Serialize()); + string response = slots.Aggregate(string.Empty, (current, slot) => current + slot.Serialize(token.GameVersion)); return this.Ok ( diff --git a/ProjectLighthouse/Types/Levels/Slot.cs b/ProjectLighthouse/Types/Levels/Slot.cs index 027f7602..23a9d495 100644 --- a/ProjectLighthouse/Types/Levels/Slot.cs +++ b/ProjectLighthouse/Types/Levels/Slot.cs @@ -125,10 +125,8 @@ public class Slot [XmlIgnore] [NotMapped] [JsonIgnore] - public int Comments - { - get - { + public int Comments { + get { using Database database = new(); return database.Comments.Count(c => c.Type == CommentType.Level && c.TargetId == this.SlotId); @@ -254,9 +252,14 @@ public class Slot LbpSerializer.StringElement("sizeOfResources", this.Resources.Sum(FileHelper.ResourceSize)); } - public string Serialize(RatedLevel? yourRatingStats = null, VisitedLevel? yourVisitedStats = null, Review? yourReview = null) + public string Serialize + ( + GameVersion gameVersion = GameVersion.LittleBigPlanet1, + RatedLevel? yourRatingStats = null, + VisitedLevel? yourVisitedStats = null, + Review? yourReview = null + ) { - string slotData = LbpSerializer.StringElement("name", this.Name) + LbpSerializer.StringElement("id", this.SlotId) + LbpSerializer.StringElement("game", (int)this.GameVersion) + @@ -287,13 +290,9 @@ public class Slot LbpSerializer.StringElement("lbp1PlayCount", this.PlaysLBP1) + LbpSerializer.StringElement("lbp1CompletionCount", this.PlaysLBP1Complete) + LbpSerializer.StringElement("lbp1UniquePlayCount", this.PlaysLBP1Unique) + - LbpSerializer.StringElement("lbp2PlayCount", this.PlaysLBP2) + - LbpSerializer.StringElement("lbp2CompletionCount", this.PlaysLBP2Complete) + - LbpSerializer.StringElement("lbp2UniquePlayCount", this.PlaysLBP2Unique) + // not actually used ingame, as per above comment LbpSerializer.StringElement("lbp3PlayCount", this.PlaysLBP3) + LbpSerializer.StringElement("lbp3CompletionCount", this.PlaysLBP3Complete) + LbpSerializer.StringElement("lbp3UniquePlayCount", this.PlaysLBP3Unique) + - LbpSerializer.StringElement("vitaCrossControlRequired", this.CrossControllerRequired) + LbpSerializer.StringElement("thumbsup", this.Thumbsup) + LbpSerializer.StringElement("thumbsdown", this.Thumbsdown) + LbpSerializer.StringElement("averageRating", this.RatingLBP1) + @@ -303,13 +302,26 @@ public class Slot LbpSerializer.StringElement("yourLBP1PlayCount", yourVisitedStats?.PlaysLBP1) + 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 yourReview?.Serialize("yourReview") + LbpSerializer.StringElement("reviewsEnabled", ServerSettings.Instance.LevelReviewsEnabled) + LbpSerializer.StringElement("commentsEnabled", ServerSettings.Instance.LevelCommentsEnabled) + LbpSerializer.StringElement("reviewCount", this.ReviewCount); + if (gameVersion == GameVersion.LittleBigPlanetVita) + { + slotData += LbpSerializer.StringElement("yourLBP2PlayCount", yourVisitedStats?.PlaysLBPVita) + + LbpSerializer.StringElement("lbp2PlayCount", this.PlaysLBPVita) + + LbpSerializer.StringElement("lbp2CompletionCount", this.PlaysLBPVitaComplete) + + LbpSerializer.StringElement("lbp2UniquePlayCount", this.PlaysLBPVitaUnique); + } + else + { + slotData += LbpSerializer.StringElement("yourLBP2PlayCount", yourVisitedStats?.PlaysLBPVita) + + LbpSerializer.StringElement("lbp2PlayCount", this.PlaysLBP2) + + LbpSerializer.StringElement("lbp2CompletionCount", this.PlaysLBP2Complete) + + LbpSerializer.StringElement("lbp2UniquePlayCount", this.PlaysLBP2Unique); // not actually used ingame, as per above comment + } + return LbpSerializer.TaggedStringElement("slot", slotData, "type", "user"); } } \ No newline at end of file