mirror of
https://github.com/LBPUnion/ProjectLighthouse.git
synced 2025-05-30 12:42:27 +00:00
Move servers to LBPU.PL.Servers
This commit is contained in:
parent
545b5a0709
commit
b2ec7eae57
116 changed files with 173 additions and 162 deletions
|
@ -0,0 +1,125 @@
|
|||
#nullable enable
|
||||
using LBPUnion.ProjectLighthouse.Types;
|
||||
using LBPUnion.ProjectLighthouse.Types.Levels;
|
||||
using Microsoft.AspNetCore.Mvc;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
|
||||
namespace LBPUnion.ProjectLighthouse.Servers.GameServer.Controllers.Matching;
|
||||
|
||||
[ApiController]
|
||||
[Route("LITTLEBIGPLANETPS3_XML/")]
|
||||
// [Produces("text/plain")]
|
||||
public class EnterLevelController : ControllerBase
|
||||
{
|
||||
private readonly Database database;
|
||||
|
||||
public EnterLevelController(Database database)
|
||||
{
|
||||
this.database = database;
|
||||
}
|
||||
|
||||
[HttpPost("play/user/{slotId}")]
|
||||
public async Task<IActionResult> PlayLevel(int slotId)
|
||||
{
|
||||
User? user = await this.database.UserFromGameRequest(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, "");
|
||||
|
||||
GameToken? token = await this.database.GameTokenFromRequest(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);
|
||||
VisitedLevel? v;
|
||||
if (!visited.Any())
|
||||
{
|
||||
switch (gameVersion)
|
||||
{
|
||||
case GameVersion.LittleBigPlanet2:
|
||||
slot.PlaysLBP2Unique++;
|
||||
break;
|
||||
case GameVersion.LittleBigPlanet3:
|
||||
slot.PlaysLBP3Unique++;
|
||||
break;
|
||||
case GameVersion.LittleBigPlanetVita:
|
||||
slot.PlaysLBPVitaUnique++;
|
||||
break;
|
||||
default: return this.BadRequest();
|
||||
}
|
||||
|
||||
v = new VisitedLevel();
|
||||
v.SlotId = slotId;
|
||||
v.UserId = user.UserId;
|
||||
this.database.VisitedLevels.Add(v);
|
||||
}
|
||||
else
|
||||
{
|
||||
v = await visited.FirstOrDefaultAsync();
|
||||
}
|
||||
|
||||
if (v == null) return this.NotFound();
|
||||
|
||||
switch (gameVersion)
|
||||
{
|
||||
case GameVersion.LittleBigPlanet2:
|
||||
slot.PlaysLBP2++;
|
||||
v.PlaysLBP2++;
|
||||
break;
|
||||
case GameVersion.LittleBigPlanet3:
|
||||
slot.PlaysLBP3++;
|
||||
v.PlaysLBP3++;
|
||||
break;
|
||||
case GameVersion.LittleBigPlanetVita:
|
||||
slot.PlaysLBPVita++;
|
||||
v.PlaysLBPVita++;
|
||||
break;
|
||||
case GameVersion.LittleBigPlanetPSP: throw new NotImplementedException();
|
||||
case GameVersion.Unknown:
|
||||
default:
|
||||
return this.BadRequest();
|
||||
}
|
||||
|
||||
await this.database.SaveChangesAsync();
|
||||
|
||||
return this.Ok();
|
||||
}
|
||||
|
||||
// Only used in LBP1
|
||||
[HttpGet("enterLevel/{id:int}")]
|
||||
public async Task<IActionResult> EnterLevel(int id)
|
||||
{
|
||||
User? user = await this.database.UserFromGameRequest(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();
|
||||
|
||||
IQueryable<VisitedLevel> visited = this.database.VisitedLevels.Where(s => s.SlotId == id && s.UserId == user.UserId);
|
||||
VisitedLevel? v;
|
||||
if (!visited.Any())
|
||||
{
|
||||
slot.PlaysLBP1Unique++;
|
||||
|
||||
v = new VisitedLevel();
|
||||
v.SlotId = id;
|
||||
v.UserId = user.UserId;
|
||||
this.database.VisitedLevels.Add(v);
|
||||
}
|
||||
else
|
||||
{
|
||||
v = await visited.FirstOrDefaultAsync();
|
||||
}
|
||||
|
||||
if (v == null) return this.NotFound();
|
||||
|
||||
slot.PlaysLBP1++;
|
||||
v.PlaysLBP1++;
|
||||
|
||||
await this.database.SaveChangesAsync();
|
||||
|
||||
return this.Ok();
|
||||
}
|
||||
}
|
|
@ -0,0 +1,141 @@
|
|||
#nullable enable
|
||||
using System.Text.Json;
|
||||
using LBPUnion.ProjectLighthouse.Helpers;
|
||||
using LBPUnion.ProjectLighthouse.Helpers.Extensions;
|
||||
using LBPUnion.ProjectLighthouse.Logging;
|
||||
using LBPUnion.ProjectLighthouse.Types;
|
||||
using LBPUnion.ProjectLighthouse.Types.Match;
|
||||
using Microsoft.AspNetCore.Mvc;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
|
||||
namespace LBPUnion.ProjectLighthouse.Servers.GameServer.Controllers.Matching;
|
||||
|
||||
[ApiController]
|
||||
[Route("LITTLEBIGPLANETPS3_XML/")]
|
||||
[Produces("text/xml")]
|
||||
public class MatchController : ControllerBase
|
||||
{
|
||||
private readonly Database database;
|
||||
|
||||
public MatchController(Database database)
|
||||
{
|
||||
this.database = database;
|
||||
}
|
||||
|
||||
[HttpPost("match")]
|
||||
[Produces("text/plain")]
|
||||
public async Task<IActionResult> Match()
|
||||
{
|
||||
(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;
|
||||
|
||||
#region Parse match data
|
||||
|
||||
// Example POST /match: [UpdateMyPlayerData,["Player":"FireGamer9872"]]
|
||||
|
||||
string bodyString = await new StreamReader(this.Request.Body).ReadToEndAsync();
|
||||
|
||||
if (bodyString.Length == 0 || bodyString[0] != '[') return this.BadRequest();
|
||||
|
||||
Logger.LogInfo("Received match data: " + bodyString, LogArea.Match);
|
||||
|
||||
IMatchData? matchData;
|
||||
try
|
||||
{
|
||||
matchData = MatchHelper.Deserialize(bodyString);
|
||||
}
|
||||
catch(Exception e)
|
||||
{
|
||||
Logger.LogError("Exception while parsing matchData: ", LogArea.Match);
|
||||
Logger.LogError(e.ToDetailedException(), LogArea.Match);
|
||||
|
||||
return this.BadRequest();
|
||||
}
|
||||
|
||||
if (matchData == null)
|
||||
{
|
||||
Logger.LogError($"Could not parse match data: {nameof(matchData)} is null", LogArea.Match);
|
||||
return this.BadRequest();
|
||||
}
|
||||
|
||||
Logger.LogInfo($"Parsed match from {user.Username} (type: {matchData.GetType()})", LogArea.Match);
|
||||
|
||||
#endregion
|
||||
|
||||
await LastContactHelper.SetLastContact(user, gameToken.GameVersion, gameToken.Platform);
|
||||
|
||||
#region Process match data
|
||||
|
||||
if (matchData is UpdateMyPlayerData playerData)
|
||||
{
|
||||
MatchHelper.SetUserLocation(user.UserId, gameToken.UserLocation);
|
||||
Room? room = RoomHelper.FindRoomByUser(user, gameToken.GameVersion, gameToken.Platform, true);
|
||||
|
||||
if (playerData.RoomState != null)
|
||||
if (room != null && Equals(room.Host, user))
|
||||
room.State = (RoomState)playerData.RoomState;
|
||||
}
|
||||
|
||||
// Check how many people are online in release builds, disabled for debug for ..well debugging.
|
||||
#if DEBUG
|
||||
if (matchData is FindBestRoom diveInData)
|
||||
#else
|
||||
if (matchData is FindBestRoom diveInData && MatchHelper.UserLocations.Count > 1)
|
||||
#endif
|
||||
{
|
||||
FindBestRoomResponse? response = RoomHelper.FindBestRoom
|
||||
(user, gameToken.GameVersion, diveInData.RoomSlot, gameToken.Platform, gameToken.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}]");
|
||||
}
|
||||
|
||||
if (matchData is CreateRoom createRoom && MatchHelper.UserLocations.Count >= 1)
|
||||
{
|
||||
List<User> users = new();
|
||||
foreach (string playerUsername in createRoom.Players)
|
||||
{
|
||||
User? player = await this.database.Users.FirstOrDefaultAsync(u => u.Username == playerUsername);
|
||||
// ReSharper disable once ConditionIsAlwaysTrueOrFalse
|
||||
if (player != null) users.Add(player);
|
||||
else return this.BadRequest();
|
||||
}
|
||||
|
||||
// Create a new one as requested
|
||||
RoomHelper.CreateRoom(users, gameToken.GameVersion, gameToken.Platform, createRoom.RoomSlot);
|
||||
}
|
||||
|
||||
if (matchData is UpdatePlayersInRoom updatePlayersInRoom)
|
||||
{
|
||||
Room? room = RoomHelper.Rooms.FirstOrDefault(r => r.Host == user);
|
||||
|
||||
if (room != null)
|
||||
{
|
||||
List<User> users = new();
|
||||
foreach (string playerUsername in updatePlayersInRoom.Players)
|
||||
{
|
||||
User? 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.Players = users;
|
||||
RoomHelper.CleanupRooms(null, room);
|
||||
}
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
return this.Ok("[{\"StatusCode\":200}]");
|
||||
}
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue