Move playcount logic into EnterLevelController

This commit is contained in:
LumaLivy 2021-11-08 07:03:01 -05:00
parent 81befb4cd8
commit a82e7e0467
2 changed files with 77 additions and 27 deletions

View file

@ -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<IActionResult> 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<VisitedLevel> 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<IActionResult> 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<VisitedLevel> 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();
}
}

View file

@ -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<IActionResult> 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<Score> 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();
}
}
}