mirror of
https://github.com/LBPUnion/ProjectLighthouse.git
synced 2025-05-14 13:52:28 +00:00
* Initial work for serialization refactor * Experiment with new naming conventions * Mostly implement user and slot serialization. Still needs to be fine tuned to match original implementation Many things are left in a broken state like website features/api endpoints/lbp3 categories * Fix release building * Migrate scores, reviews, and more to new serialization system. Many things are still broken but progress is steadily being made * Fix Api responses and migrate serialization for most types * Make serialization better and fix bugs Fix recursive PrepareSerialization when recursive item is set during root item's PrepareSerialization, items, should be properly indexed in order but it's only tested to 1 level of recursion * Fix review serialization * Fix user serialization producing malformed SQL query * Remove DefaultIfEmpty query * MariaDB doesn't like double nested queries * Fix LBP1 tag counter * Implement lbp3 categories and add better deserialization handling * Implement expression tree caching to speed up reflection and write new serializer tests * Remove Game column from UserEntity and rename DatabaseContextModelSnapshot.cs back to DatabaseModelSnapshot.cs * Make UserEntity username not required * Fix recursive serialization of lists and add relevant unit tests * Actually commit the migration * Fix LocationTests to use new deserialization class * Fix comments not serializing the right author username * Replace all occurrences of StatusCode with their respective ASP.NET named result instead of StatusCode(403) everything is now in the form of Forbid() * Fix SlotBase.ConvertToEntity and LocationTests * Fix compilation error * Give Location a default value in GameUserSlot and GameUser * Reimplement stubbed website functions * Convert grief reports to new serialization system * Update DatabaseModelSnapshot and bump dotnet tool version * Remove unused directives * Fix broken type reference * Fix rated comments on website * Don't include banned users in website comments * Optimize score submission * Fix slot id calculating in in-game comment posting * Move serialization interfaces to types folder and add more documentation * Allow uploading of versus scores
153 lines
No EOL
5.6 KiB
C#
153 lines
No EOL
5.6 KiB
C#
#nullable enable
|
|
using System.Text.Json;
|
|
using LBPUnion.ProjectLighthouse.Database;
|
|
using LBPUnion.ProjectLighthouse.Extensions;
|
|
using LBPUnion.ProjectLighthouse.Helpers;
|
|
using LBPUnion.ProjectLighthouse.Logging;
|
|
using LBPUnion.ProjectLighthouse.Types.Entities.Profile;
|
|
using LBPUnion.ProjectLighthouse.Types.Entities.Token;
|
|
using LBPUnion.ProjectLighthouse.Types.Logging;
|
|
using LBPUnion.ProjectLighthouse.Types.Matchmaking;
|
|
using LBPUnion.ProjectLighthouse.Types.Matchmaking.MatchCommands;
|
|
using LBPUnion.ProjectLighthouse.Types.Matchmaking.Rooms;
|
|
using Microsoft.AspNetCore.Authorization;
|
|
using Microsoft.AspNetCore.Mvc;
|
|
using Microsoft.EntityFrameworkCore;
|
|
|
|
namespace LBPUnion.ProjectLighthouse.Servers.GameServer.Controllers.Matching;
|
|
|
|
[ApiController]
|
|
[Authorize]
|
|
[Route("LITTLEBIGPLANETPS3_XML/")]
|
|
[Produces("text/xml")]
|
|
public class MatchController : ControllerBase
|
|
{
|
|
private readonly DatabaseContext database;
|
|
|
|
public MatchController(DatabaseContext database)
|
|
{
|
|
this.database = database;
|
|
}
|
|
|
|
[HttpPost("gameState")]
|
|
[Produces("text/plain")]
|
|
public IActionResult GameState() => this.Ok("VALID");
|
|
|
|
[HttpPost("match")]
|
|
[Produces("text/plain")]
|
|
public async Task<IActionResult> Match()
|
|
{
|
|
GameTokenEntity token = this.GetToken();
|
|
|
|
UserEntity? user = await this.database.UserFromGameToken(token);
|
|
if (user == null) return this.Forbid();
|
|
|
|
#region Parse match data
|
|
|
|
// Example POST /match: [UpdateMyPlayerData,["Player":"FireGamer9872"]]
|
|
|
|
string bodyString = await this.ReadBodyAsync();
|
|
|
|
if (bodyString.Length == 0 || bodyString[0] != '[') return this.BadRequest();
|
|
|
|
Logger.Debug("Received match data: " + bodyString, LogArea.Match);
|
|
|
|
IMatchCommand? matchData;
|
|
try
|
|
{
|
|
matchData = MatchHelper.Deserialize(bodyString);
|
|
}
|
|
catch(Exception e)
|
|
{
|
|
Logger.Error("Exception while parsing matchData: ", LogArea.Match);
|
|
Logger.Error(e.ToDetailedException(), LogArea.Match);
|
|
|
|
return this.BadRequest();
|
|
}
|
|
|
|
if (matchData == null)
|
|
{
|
|
Logger.Error($"Could not parse match data: {nameof(matchData)} is null", LogArea.Match);
|
|
return this.BadRequest();
|
|
}
|
|
|
|
Logger.Info($"Parsed match from {user.Username} (type: {matchData.GetType()})", LogArea.Match);
|
|
|
|
#endregion
|
|
|
|
await LastContactHelper.SetLastContact(this.database, user, token.GameVersion, token.Platform);
|
|
|
|
#region Process match data
|
|
|
|
switch (matchData)
|
|
{
|
|
case UpdateMyPlayerData playerData:
|
|
{
|
|
MatchHelper.SetUserLocation(user.UserId, token.UserLocation);
|
|
Room? room = RoomHelper.FindRoomByUser(user.UserId, token.GameVersion, token.Platform, true);
|
|
|
|
if (playerData.RoomState != null)
|
|
if (room != null && Equals(room.HostId, user.UserId))
|
|
room.State = (RoomState)playerData.RoomState;
|
|
break;
|
|
}
|
|
// Check how many people are online in release builds, disabled for debug for ..well debugging.
|
|
#if DEBUG
|
|
case FindBestRoom diveInData:
|
|
#else
|
|
case FindBestRoom diveInData when MatchHelper.UserLocations.Count > 1:
|
|
#endif
|
|
{
|
|
FindBestRoomResponse? response = RoomHelper.FindBestRoom
|
|
(user, token.GameVersion, diveInData.RoomSlot, token.Platform, token.UserLocation);
|
|
|
|
if (response == null) return this.NotFound();
|
|
|
|
string serialized = JsonSerializer.Serialize(response, typeof(FindBestRoomResponse));
|
|
foreach (Player player in response.Players) MatchHelper.AddUserRecentlyDivedIn(user.UserId, player.User.UserId);
|
|
|
|
return this.Ok($"[{{\"StatusCode\":200}},{serialized}]");
|
|
}
|
|
case CreateRoom createRoom when MatchHelper.UserLocations.Count >= 1:
|
|
{
|
|
List<int> users = new();
|
|
foreach (string playerUsername in createRoom.Players)
|
|
{
|
|
UserEntity? player = await this.database.Users.FirstOrDefaultAsync(u => u.Username == playerUsername);
|
|
// ReSharper disable once ConditionIsAlwaysTrueOrFalse
|
|
if (player != null) users.Add(player.UserId);
|
|
else return this.BadRequest();
|
|
}
|
|
|
|
// Create a new one as requested
|
|
RoomHelper.CreateRoom(users, token.GameVersion, token.Platform, createRoom.RoomSlot);
|
|
break;
|
|
}
|
|
case UpdatePlayersInRoom updatePlayersInRoom:
|
|
{
|
|
Room? room = RoomHelper.Rooms.FirstOrDefault(r => r.HostId == user.UserId);
|
|
|
|
if (room != null)
|
|
{
|
|
List<UserEntity> users = new();
|
|
foreach (string playerUsername in updatePlayersInRoom.Players)
|
|
{
|
|
UserEntity? player = await this.database.Users.FirstOrDefaultAsync(u => u.Username == playerUsername);
|
|
// ReSharper disable once ConditionIsAlwaysTrueOrFalse
|
|
if (player != null) users.Add(player);
|
|
else return this.BadRequest();
|
|
}
|
|
|
|
room.PlayerIds = users.Select(u => u.UserId).ToList();
|
|
await RoomHelper.CleanupRooms(null, room);
|
|
}
|
|
|
|
break;
|
|
}
|
|
}
|
|
|
|
#endregion
|
|
|
|
return this.Ok("[{\"StatusCode\":200}]");
|
|
}
|
|
} |