Fix vita slot counts

This commit is contained in:
jvyden 2021-11-15 01:55:33 -05:00
parent 8eccfe316d
commit 740a7f7803
No known key found for this signature in database
GPG key ID: 18BCF2BE0262B278
4 changed files with 48 additions and 17 deletions

View file

@ -69,8 +69,13 @@ namespace LBPUnion.ProjectLighthouse.Controllers
[HttpGet("myFriends")] [HttpGet("myFriends")]
public async Task<IActionResult> MyFriends() public async Task<IActionResult> MyFriends()
{ {
User? user = await this.database.UserFromRequest(this.Request); (User, Token)? userAndToken = await this.database.UserAndTokenFromRequest(this.Request);
if (user == null) return this.StatusCode(403, "");
if (userAndToken == null) return this.StatusCode(403, "");
// ReSharper disable once PossibleInvalidOperationException
User user = userAndToken.Value.Item1;
Token token = userAndToken.Value.Item2;
if (!FriendHelper.FriendIdsByUserId.TryGetValue(user.UserId, out int[]? friendIds) || friendIds == null) if (!FriendHelper.FriendIdsByUserId.TryGetValue(user.UserId, out int[]? friendIds) || friendIds == null)
{ {
@ -83,7 +88,7 @@ namespace LBPUnion.ProjectLighthouse.Controllers
User? friend = await this.database.Users.Include(u => u.Location).FirstOrDefaultAsync(u => u.UserId == friendId); User? friend = await this.database.Users.Include(u => u.Location).FirstOrDefaultAsync(u => u.UserId == friendId);
if (friend == null) continue; if (friend == null) continue;
friends += friend.Serialize(); friends += friend.Serialize(token.GameVersion);
} }
return this.Ok(LbpSerializer.StringElement("myFriends", friends)); return this.Ok(LbpSerializer.StringElement("myFriends", friends));

View file

@ -152,8 +152,11 @@ namespace LBPUnion.ProjectLighthouse.Controllers
#region Users #region Users
[HttpGet("favouriteUsers/{username}")] [HttpGet("favouriteUsers/{username}")]
public IActionResult GetFavouriteUsers(string username) public async Task<IActionResult> GetFavouriteUsers(string username)
{ {
Token? token = await this.database.TokenFromRequest(this.Request);
if (token == null) return this.StatusCode(403, "");
IEnumerable<HeartedProfile> heartedProfiles = this.database.HeartedProfiles.Include IEnumerable<HeartedProfile> heartedProfiles = this.database.HeartedProfiles.Include
(q => q.User) (q => q.User)
.Include(q => q.HeartedUser) .Include(q => q.HeartedUser)
@ -161,7 +164,7 @@ namespace LBPUnion.ProjectLighthouse.Controllers
.Where(q => q.User.Username == username) .Where(q => q.User.Username == username)
.AsEnumerable(); .AsEnumerable();
string response = heartedProfiles.Aggregate(string.Empty, (current, q) => current + q.HeartedUser.Serialize()); string response = heartedProfiles.Aggregate(string.Empty, (current, q) => current + q.HeartedUser.Serialize(token.GameVersion));
return this.Ok(LbpSerializer.TaggedStringElement("favouriteUsers", response, "total", 1)); return this.Ok(LbpSerializer.TaggedStringElement("favouriteUsers", response, "total", 1));
} }

View file

@ -26,26 +26,35 @@ namespace LBPUnion.ProjectLighthouse.Controllers
this.database = database; this.database = database;
} }
public async Task<string> GetSerializedUser(string username) public async Task<string> GetSerializedUser(string username, GameVersion gameVersion = GameVersion.LittleBigPlanet1)
{ {
User? user = await this.database.Users.Include(u => u.Location).FirstOrDefaultAsync(u => u.Username == username); User? user = await this.database.Users.Include(u => u.Location).FirstOrDefaultAsync(u => u.Username == username);
return user?.Serialize(); return user?.Serialize(gameVersion);
} }
[HttpGet("user/{username}")] [HttpGet("user/{username}")]
public async Task<IActionResult> GetUser(string username) { public async Task<IActionResult> GetUser(string username)
string? user = await this.GetSerializedUser(username); {
Token? token = await this.database.TokenFromRequest(this.Request);
if (token == null) return this.StatusCode(403, "");
string? user = await this.GetSerializedUser(username, token.GameVersion);
if (user == null) return this.NotFound(); if (user == null) return this.NotFound();
return this.Ok(user); return this.Ok(user);
} }
[HttpGet("users")] [HttpGet("users")]
public async Task<IActionResult> GetUserAlt([FromQuery] string[] u) public async Task<IActionResult> GetUserAlt([FromQuery] string[] u)
{ {
Token? token = await this.database.TokenFromRequest(this.Request);
if (token == null) return this.StatusCode(403, "");
List<string> serializedUsers = new(); List<string> serializedUsers = new();
foreach (string userId in u) foreach (string userId in u)
{ {
serializedUsers.Add(await this.GetSerializedUser(userId)); serializedUsers.Add(await this.GetSerializedUser(userId, token.GameVersion));
} }
string serialized = serializedUsers.Aggregate(string.Empty, (current, u) => u == null ? current : current + u); string serialized = serializedUsers.Aggregate(string.Empty, (current, u) => u == null ? current : current + u);

View file

@ -93,11 +93,11 @@ namespace LBPUnion.ProjectLighthouse.Types
} }
} }
public string Serialize() public string Serialize(GameVersion gameVersion = GameVersion.LittleBigPlanet1)
{ {
string user = LbpSerializer.TaggedStringElement("npHandle", this.Username, "icon", this.IconHash) + string user = LbpSerializer.TaggedStringElement("npHandle", this.Username, "icon", this.IconHash) +
LbpSerializer.StringElement("game", this.Game) + LbpSerializer.StringElement("game", this.Game) +
this.SerializeSlots() + this.SerializeSlots(gameVersion == GameVersion.LittleBigPlanetVita) +
LbpSerializer.StringElement("lists", this.Lists) + LbpSerializer.StringElement("lists", this.Lists) +
LbpSerializer.StringElement("lists_quota", ServerSettings.ListsQuota) + // technically not a part of the user but LBP expects it LbpSerializer.StringElement("lists_quota", ServerSettings.ListsQuota) + // technically not a part of the user but LBP expects it
LbpSerializer.StringElement("biography", this.Biography) + LbpSerializer.StringElement("biography", this.Biography) +
@ -149,18 +149,32 @@ namespace LBPUnion.ProjectLighthouse.Types
"lbp2", "lbp3", "crossControl", "lbp2", "lbp3", "crossControl",
}; };
private string SerializeSlots() private string SerializeSlots(bool isVita = false)
{ {
string slots = string.Empty; string slots = string.Empty;
slots += LbpSerializer.StringElement("lbp1UsedSlots", this.GetUsedSlotsForGame(GameVersion.LittleBigPlanet1)); string[] slotTypesLocal;
slots += LbpSerializer.StringElement("lbp2UsedSlots", this.GetUsedSlotsForGame(GameVersion.LittleBigPlanet2));
slots += LbpSerializer.StringElement("lbp3UsedSlots", this.GetUsedSlotsForGame(GameVersion.LittleBigPlanet3)); if (isVita)
{
slots += LbpSerializer.StringElement("lbp2UsedSlots", this.GetUsedSlotsForGame(GameVersion.LittleBigPlanetVita));
slotTypesLocal = new[]
{
"lbp2",
};
}
else
{
slots += LbpSerializer.StringElement("lbp1UsedSlots", this.GetUsedSlotsForGame(GameVersion.LittleBigPlanet1));
slots += LbpSerializer.StringElement("lbp2UsedSlots", this.GetUsedSlotsForGame(GameVersion.LittleBigPlanet2));
slots += LbpSerializer.StringElement("lbp3UsedSlots", this.GetUsedSlotsForGame(GameVersion.LittleBigPlanet3));
slotTypesLocal = slotTypes;
}
slots += LbpSerializer.StringElement("entitledSlots", ServerSettings.EntitledSlots); slots += LbpSerializer.StringElement("entitledSlots", ServerSettings.EntitledSlots);
slots += LbpSerializer.StringElement("freeSlots", this.FreeSlots); slots += LbpSerializer.StringElement("freeSlots", this.FreeSlots);
foreach (string slotType in slotTypes) foreach (string slotType in slotTypesLocal)
{ {
slots += LbpSerializer.StringElement(slotType + "EntitledSlots", ServerSettings.EntitledSlots); slots += LbpSerializer.StringElement(slotType + "EntitledSlots", ServerSettings.EntitledSlots);
// ReSharper disable once StringLiteralTypo // ReSharper disable once StringLiteralTypo