diff --git a/ProjectLighthouse.Servers.API/Controllers/StatisticsEndpoints.cs b/ProjectLighthouse.Servers.API/Controllers/StatisticsEndpoints.cs index dcd9a5c2..76310aec 100644 --- a/ProjectLighthouse.Servers.API/Controllers/StatisticsEndpoints.cs +++ b/ProjectLighthouse.Servers.API/Controllers/StatisticsEndpoints.cs @@ -5,6 +5,7 @@ using LBPUnion.ProjectLighthouse.Helpers; using LBPUnion.ProjectLighthouse.Servers.API.Responses; using LBPUnion.ProjectLighthouse.Types.Users; using Microsoft.AspNetCore.Mvc; +using Microsoft.EntityFrameworkCore; namespace LBPUnion.ProjectLighthouse.Servers.API.Controllers; @@ -49,24 +50,33 @@ public class StatisticsEndpoints : ApiEndpointController GameVersion.LittleBigPlanetPSP, }; + private static readonly List platforms = new() + { + Platform.PS3, + Platform.RPCS3, + Platform.Vita, + Platform.PSP, + }; + /// /// Get player counts for each individual title /// - /// An instance of PlayerCountResponse + /// An instance of PlayerCountByGameResponse [HttpGet("playerCount")] - [ProducesResponseType(typeof(PlayerCountResponse), StatusCodes.Status200OK)] + [HttpGet("playerCount/game")] + [ProducesResponseType(typeof(PlayerCountByGameResponse), StatusCodes.Status200OK)] public async Task GetPlayerCounts() { List gameList = new(); foreach (GameVersion version in gameVersions) { - gameList.Add(new PlayerCountObject + gameList.Add(new PlayerCountByGameObject { Game = version.ToString(), - PlayerCount = await StatisticsHelper.RecentMatchesForGame(this.database, version), + PlayerCount = await StatisticsHelper.RecentMatches(this.database, l => l.GameVersion == version), }); } - PlayerCountResponse response = new() + PlayerCountByGameResponse response = new() { TotalPlayerCount = await StatisticsHelper.RecentMatches(this.database), Games = gameList, @@ -74,4 +84,56 @@ public class StatisticsEndpoints : ApiEndpointController return this.Ok(response); } + + /// + /// Get player counts for each individual platform + /// + /// An instance of PlayerCountByPlatformResponse + [HttpGet("playerCount/platform")] + [ProducesResponseType(typeof(PlayerCountByPlatformResponse), StatusCodes.Status200OK)] + public async Task GetPlayerCountsByPlatform() + { + List platformList = new(); + foreach (Platform platform in platforms) + { + platformList.Add(new PlayerCountByPlatformObject + { + Platform = platform.ToString(), + PlayerCount = await StatisticsHelper.RecentMatches(this.database, l => l.Platform == platform), + }); + } + + PlayerCountByPlatformResponse response = new() + { + TotalPlayerCount = await StatisticsHelper.RecentMatches(this.database), + Platforms = platformList, + }; + + return this.Ok(response); + } + + /// + /// Gets a list of online players + /// + /// An instance of PlayerListResponse + [HttpGet("players")] + [ProducesResponseType(typeof(PlayerListResponse), StatusCodes.Status200OK)] + public async Task GetPlayerList() + { + List players = await this.database.LastContacts.Where(l => TimeHelper.Timestamp - l.Timestamp < 300) + .Select(l => new PlayerListObject + { + Username = l.User!.Username, + Game = l.GameVersion.ToString(), + Platform = l.Platform.ToString(), + }) + .ToListAsync(); + + PlayerListResponse response = new() + { + Players = players, + }; + + return this.Ok(response); + } } \ No newline at end of file diff --git a/ProjectLighthouse.Servers.API/Responses/PlayerCountResponse.cs b/ProjectLighthouse.Servers.API/Responses/PlayerCountResponse.cs index 27e82e30..619a848a 100644 --- a/ProjectLighthouse.Servers.API/Responses/PlayerCountResponse.cs +++ b/ProjectLighthouse.Servers.API/Responses/PlayerCountResponse.cs @@ -1,13 +1,37 @@ -namespace LBPUnion.ProjectLighthouse.Servers.API.Responses; +using System.Text.Json.Serialization; +namespace LBPUnion.ProjectLighthouse.Servers.API.Responses; + +[JsonDerivedType(typeof(PlayerCountByGameObject))] +[JsonDerivedType(typeof(PlayerCountByPlatformObject))] public class PlayerCountObject { - public string Game { get; set; } = ""; public int PlayerCount { get; set; } } +public class PlayerCountByGameObject : PlayerCountObject +{ + public string Game { get; set; } = ""; +} + +public class PlayerCountByPlatformObject : PlayerCountObject +{ + public string Platform { get; set; } = ""; +} + +[JsonDerivedType(typeof(PlayerCountByGameResponse))] +[JsonDerivedType(typeof(PlayerCountByPlatformResponse))] public class PlayerCountResponse { public int TotalPlayerCount { get; set; } +} + +public class PlayerCountByGameResponse : PlayerCountResponse +{ public List Games { get; set; } = new(); +} + +public class PlayerCountByPlatformResponse : PlayerCountResponse +{ + public List Platforms { get; set; } = new(); } \ No newline at end of file diff --git a/ProjectLighthouse.Servers.API/Responses/PlayerListResponse.cs b/ProjectLighthouse.Servers.API/Responses/PlayerListResponse.cs new file mode 100644 index 00000000..55470d4a --- /dev/null +++ b/ProjectLighthouse.Servers.API/Responses/PlayerListResponse.cs @@ -0,0 +1,13 @@ +namespace LBPUnion.ProjectLighthouse.Servers.API.Responses; + +public class PlayerListResponse +{ + public required List Players { get; set; } +} + +public class PlayerListObject +{ + public required string Username { get; set; } + public required string Game { get; set; } + public required string Platform { get; set; } +} diff --git a/ProjectLighthouse.Servers.GameServer/Controllers/StatisticsController.cs b/ProjectLighthouse.Servers.GameServer/Controllers/StatisticsController.cs index 3eb828e4..3f70b3d0 100644 --- a/ProjectLighthouse.Servers.GameServer/Controllers/StatisticsController.cs +++ b/ProjectLighthouse.Servers.GameServer/Controllers/StatisticsController.cs @@ -27,7 +27,8 @@ public class StatisticsController : ControllerBase public IActionResult PlayersInPodCount() => this.Ok(StatisticsHelper.RoomCountForPlatform(this.GetToken().Platform).ToString()); [HttpGet("totalPlayerCount")] - public async Task TotalPlayerCount() => this.Ok((await StatisticsHelper.RecentMatchesForGame(this.database, this.GetToken().GameVersion)).ToString()); + public async Task TotalPlayerCount() => + this.Ok((await StatisticsHelper.RecentMatches(this.database, l => l.GameVersion == this.GetToken().GameVersion)).ToString()); [HttpGet("planetStats")] [Produces("text/xml")] diff --git a/ProjectLighthouse/Helpers/StatisticsHelper.cs b/ProjectLighthouse/Helpers/StatisticsHelper.cs index 0f0070ab..525ae777 100644 --- a/ProjectLighthouse/Helpers/StatisticsHelper.cs +++ b/ProjectLighthouse/Helpers/StatisticsHelper.cs @@ -1,7 +1,10 @@ +using System; using System.Linq; +using System.Linq.Expressions; using System.Threading.Tasks; using LBPUnion.ProjectLighthouse.Database; using LBPUnion.ProjectLighthouse.Filter; +using LBPUnion.ProjectLighthouse.Types.Entities.Profile; using LBPUnion.ProjectLighthouse.Types.Users; using Microsoft.EntityFrameworkCore; @@ -11,8 +14,8 @@ public static class StatisticsHelper { public static async Task RecentMatches(DatabaseContext database) => await database.LastContacts.Where(l => TimeHelper.Timestamp - l.Timestamp < 300).CountAsync(); - public static async Task RecentMatchesForGame(DatabaseContext database, GameVersion gameVersion) - => await database.LastContacts.Where(l => TimeHelper.Timestamp - l.Timestamp < 300 && l.GameVersion == gameVersion).CountAsync(); + public static async Task RecentMatches(DatabaseContext database, Expression> contactFilter) => + await database.LastContacts.Where(l => TimeHelper.Timestamp - l.Timestamp < 300).Where(contactFilter).CountAsync(); public static async Task SlotCount(DatabaseContext database, SlotQueryBuilder queryBuilder) => await database.Slots.Where(queryBuilder.Build()).CountAsync();