diff --git a/ProjectLighthouse/Controllers/EnterLevelController.cs b/ProjectLighthouse/Controllers/EnterLevelController.cs index d3a22774..47378dca 100644 --- a/ProjectLighthouse/Controllers/EnterLevelController.cs +++ b/ProjectLighthouse/Controllers/EnterLevelController.cs @@ -1,5 +1,7 @@ #nullable enable +using System.Linq; using System.Threading.Tasks; +using LBPUnion.ProjectLighthouse.Types; using LBPUnion.ProjectLighthouse.Types.Levels; using Microsoft.AspNetCore.Mvc; using Microsoft.EntityFrameworkCore; @@ -7,7 +9,7 @@ using Microsoft.EntityFrameworkCore; namespace LBPUnion.ProjectLighthouse.Controllers { [ApiController] - [Route("LITTLEBIGPLANETPS3_XML/enterLevel")] + [Route("LITTLEBIGPLANETPS3_XML/")] // [Produces("text/plain")] public class EnterLevelController : ControllerBase { @@ -18,17 +20,87 @@ namespace LBPUnion.ProjectLighthouse.Controllers this.database = database; } + + [HttpPost("play/user/{slotId}")] + public async Task PlayLevel(int slotId) + { + 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); + if (slot == null) return this.StatusCode(403, ""); + + Token? token = await this.database.TokenFromRequest(this.Request); + if (token == null) return this.StatusCode(403, ""); + + GameVersion gameVersion = token.GameVersion; + + IQueryable visited = this.database.VisitedLevels.Where(s => s.SlotId == slotId && s.UserId == user.UserId && s.GameVersion == gameVersion); + if (!visited.Any()) + { + switch (gameVersion) + { + case GameVersion.LittleBigPlanet2: + slot.PlaysLBP2Unique++; + break; + case GameVersion.LittleBigPlanet3: + slot.PlaysLBP3Unique++; + break; + default: + return this.BadRequest(); + } + + VisitedLevel v = new(); + v.SlotId = slotId; + v.UserId = user.UserId; + v.GameVersion = gameVersion; + this.database.VisitedLevels.Add(v); + await this.database.SaveChangesAsync(); + + } + + switch (gameVersion) + { + case GameVersion.LittleBigPlanet2: + slot.PlaysLBP2++; + break; + case GameVersion.LittleBigPlanet3: + slot.PlaysLBP3++; + break; + default: + return this.BadRequest(); + } + + return this.Ok(); + } + // Only used in LBP1 [HttpGet("enterLevel/{id:int}")] public async Task EnterLevel(int id) { - /*Slot? slot = await this.database.Slots.FirstOrDefaultAsync(s => s.SlotId == id); + 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 == id); if (slot == null) return this.NotFound(); - slot.Plays++; + IQueryable visited = this.database.VisitedLevels.Where(s => s.SlotId == id && s.UserId == user.UserId && s.GameVersion == GameVersion.LittleBigPlanet1); + if (!visited.Any()) + { + slot.PlaysLBP1Unique++; + + VisitedLevel v = new(); + v.SlotId = id; + v.UserId = user.UserId; + v.GameVersion = GameVersion.LittleBigPlanet1; + this.database.VisitedLevels.Add(v); + + } + + slot.PlaysLBP1++; await this.database.SaveChangesAsync(); - */ + return this.Ok(); } } diff --git a/ProjectLighthouse/Controllers/SlotsController.cs b/ProjectLighthouse/Controllers/SlotsController.cs index 3f2e06a6..aeaa2e7a 100644 --- a/ProjectLighthouse/Controllers/SlotsController.cs +++ b/ProjectLighthouse/Controllers/SlotsController.cs @@ -122,28 +122,6 @@ namespace LBPUnion.ProjectLighthouse.Controllers return this.Ok(LbpSerializer.TaggedStringElement("slots", response, "hint_start", pageStart + Math.Min(pageSize, 30))); } - [HttpPost("play/user/{slotId}")] - public async Task PlayLevel(int slotId, [FromQuery] bool lbp1 = false, [FromQuery] bool lbp2 = false, [FromQuery] bool lbp3 = false) - { - 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); - if (slot == null) return this.StatusCode(403, ""); - - if (lbp1) slot.PlaysLBP1++; - if (lbp2) slot.PlaysLBP2++; - if (lbp3) slot.PlaysLBP3++; - - IQueryable existingScore = this.database.Scores.Where(s => s.SlotId == slotId && s.PlayerIdCollection.Contains(user.Username)); - if (!existingScore.Any()) - { - if (lbp1) slot.PlaysLBP1Unique++; - if (lbp2) slot.PlaysLBP2Unique++; - if (lbp3) slot.PlaysLBP3Unique++; - } - - await this.database.SaveChangesAsync(); - return this.Ok(); - } + } } \ No newline at end of file