From aeeb819759a4372f4406f387691dcaef3f13c6ee Mon Sep 17 00:00:00 2001 From: jvyden Date: Thu, 17 Feb 2022 00:57:02 -0500 Subject: [PATCH] Increase information in UserStatus, add platform for lastcontacts and gametokens --- .../GameApi/Matching/MatchController.cs | 2 +- ProjectLighthouse/Database.cs | 3 +- .../Helpers/LastContactHelper.cs | 3 +- ProjectLighthouse/Helpers/RoomHelper.cs | 9 ++++ ...AddPlatformForLastContactsAndGameTokens.cs | 41 +++++++++++++++++++ .../Migrations/DatabaseModelSnapshot.cs | 6 +++ ProjectLighthouse/Startup/Startup.cs | 3 +- ProjectLighthouse/Types/GameToken.cs | 2 + ProjectLighthouse/Types/Match/Room.cs | 21 +++++++--- ProjectLighthouse/Types/Match/RoomSlot.cs | 4 +- .../Types/Profiles/LastContact.cs | 2 + .../Types/Profiles/UserStatus.cs | 10 ++++- 12 files changed, 93 insertions(+), 13 deletions(-) create mode 100644 ProjectLighthouse/Migrations/20220217045519_AddPlatformForLastContactsAndGameTokens.cs diff --git a/ProjectLighthouse/Controllers/GameApi/Matching/MatchController.cs b/ProjectLighthouse/Controllers/GameApi/Matching/MatchController.cs index 7ba10605..f173e1bc 100644 --- a/ProjectLighthouse/Controllers/GameApi/Matching/MatchController.cs +++ b/ProjectLighthouse/Controllers/GameApi/Matching/MatchController.cs @@ -74,7 +74,7 @@ public class MatchController : ControllerBase #endregion - await LastContactHelper.SetLastContact(user, gameToken.GameVersion); + await LastContactHelper.SetLastContact(user, gameToken.GameVersion, gameToken.Platform); #region Process match data diff --git a/ProjectLighthouse/Database.cs b/ProjectLighthouse/Database.cs index 6ae7f4e2..abb0977a 100644 --- a/ProjectLighthouse/Database.cs +++ b/ProjectLighthouse/Database.cs @@ -82,6 +82,7 @@ public class Database : DbContext UserId = user.UserId, UserLocation = userLocation, GameVersion = npTicket.GameVersion, + Platform = npTicket.Platform, }; this.GameTokens.Add(gameToken); @@ -151,7 +152,7 @@ public class Database : DbContext else { Slot? targetSlot = await this.Slots.FirstOrDefaultAsync(u => u.SlotId == targetId); - if(targetSlot == null) return false; + if (targetSlot == null) return false; } this.Comments.Add diff --git a/ProjectLighthouse/Helpers/LastContactHelper.cs b/ProjectLighthouse/Helpers/LastContactHelper.cs index cdc7a06d..8f76bf22 100644 --- a/ProjectLighthouse/Helpers/LastContactHelper.cs +++ b/ProjectLighthouse/Helpers/LastContactHelper.cs @@ -11,7 +11,7 @@ public static class LastContactHelper { private static readonly Database database = new(); - public static async Task SetLastContact(User user, GameVersion gameVersion) + public static async Task SetLastContact(User user, GameVersion gameVersion, Platform platform) { LastContact? lastContact = await database.LastContacts.Where(l => l.UserId == user.UserId).FirstOrDefaultAsync(); @@ -28,6 +28,7 @@ public static class LastContactHelper lastContact.Timestamp = TimestampHelper.Timestamp; lastContact.GameVersion = gameVersion; + lastContact.Platform = platform; await database.SaveChangesAsync(); } diff --git a/ProjectLighthouse/Helpers/RoomHelper.cs b/ProjectLighthouse/Helpers/RoomHelper.cs index 7cca021b..cc81af01 100644 --- a/ProjectLighthouse/Helpers/RoomHelper.cs +++ b/ProjectLighthouse/Helpers/RoomHelper.cs @@ -152,6 +152,15 @@ public class RoomHelper return createIfDoesNotExist ? CreateRoom(user, roomVersion) : null; } + public static Room? FindRoomByUserId(int userId) + { + lock(Rooms) + foreach (Room room in Rooms.Where(room => room.Players.Any(player => player.UserId == userId))) + return room; + + return null; + } + [SuppressMessage("ReSharper", "InvertIf")] public static void CleanupRooms(User? host = null, Room? newRoom = null) { diff --git a/ProjectLighthouse/Migrations/20220217045519_AddPlatformForLastContactsAndGameTokens.cs b/ProjectLighthouse/Migrations/20220217045519_AddPlatformForLastContactsAndGameTokens.cs new file mode 100644 index 00000000..78342cfe --- /dev/null +++ b/ProjectLighthouse/Migrations/20220217045519_AddPlatformForLastContactsAndGameTokens.cs @@ -0,0 +1,41 @@ +using LBPUnion.ProjectLighthouse; +using Microsoft.EntityFrameworkCore.Infrastructure; +using Microsoft.EntityFrameworkCore.Migrations; + +#nullable disable + +namespace ProjectLighthouse.Migrations +{ + [DbContext(typeof(Database))] + [Migration("20220217045519_AddPlatformForLastContactsAndGameTokens")] + public partial class AddPlatformForLastContactsAndGameTokens : Migration + { + protected override void Up(MigrationBuilder migrationBuilder) + { + migrationBuilder.AddColumn( + name: "Platform", + table: "LastContacts", + type: "int", + nullable: false, + defaultValue: -1); + + migrationBuilder.AddColumn( + name: "Platform", + table: "GameTokens", + type: "int", + nullable: false, + defaultValue: -1); + } + + protected override void Down(MigrationBuilder migrationBuilder) + { + migrationBuilder.DropColumn( + name: "Platform", + table: "LastContacts"); + + migrationBuilder.DropColumn( + name: "Platform", + table: "GameTokens"); + } + } +} diff --git a/ProjectLighthouse/Migrations/DatabaseModelSnapshot.cs b/ProjectLighthouse/Migrations/DatabaseModelSnapshot.cs index 21676d01..23e9069d 100644 --- a/ProjectLighthouse/Migrations/DatabaseModelSnapshot.cs +++ b/ProjectLighthouse/Migrations/DatabaseModelSnapshot.cs @@ -81,6 +81,9 @@ namespace ProjectLighthouse.Migrations b.Property("GameVersion") .HasColumnType("int"); + b.Property("Platform") + .HasColumnType("int"); + b.Property("Used") .HasColumnType("tinyint(1)"); @@ -458,6 +461,9 @@ namespace ProjectLighthouse.Migrations b.Property("GameVersion") .HasColumnType("int"); + b.Property("Platform") + .HasColumnType("int"); + b.Property("Timestamp") .HasColumnType("bigint"); diff --git a/ProjectLighthouse/Startup/Startup.cs b/ProjectLighthouse/Startup/Startup.cs index 5cc33fe0..97c6833f 100644 --- a/ProjectLighthouse/Startup/Startup.cs +++ b/ProjectLighthouse/Startup/Startup.cs @@ -250,7 +250,8 @@ public class Startup if (gameToken != null && gameToken.GameVersion == GameVersion.LittleBigPlanet1) // Ignore UserFromGameToken null because user must exist for a token to exist - await LastContactHelper.SetLastContact((await database.UserFromGameToken(gameToken))!, GameVersion.LittleBigPlanet1); + await LastContactHelper.SetLastContact + ((await database.UserFromGameToken(gameToken))!, GameVersion.LittleBigPlanet1, gameToken.Platform); } #nullable disable diff --git a/ProjectLighthouse/Types/GameToken.cs b/ProjectLighthouse/Types/GameToken.cs index d084c034..0e57d7fd 100644 --- a/ProjectLighthouse/Types/GameToken.cs +++ b/ProjectLighthouse/Types/GameToken.cs @@ -20,6 +20,8 @@ public class GameToken public GameVersion GameVersion { get; set; } + public Platform Platform { get; set; } + // Set by /authentication webpage public bool Approved { get; set; } diff --git a/ProjectLighthouse/Types/Match/Room.cs b/ProjectLighthouse/Types/Match/Room.cs index aa659041..1541cb10 100644 --- a/ProjectLighthouse/Types/Match/Room.cs +++ b/ProjectLighthouse/Types/Match/Room.cs @@ -1,23 +1,34 @@ using System.Collections.Generic; using System.Diagnostics.CodeAnalysis; +using System.Text.Json.Serialization; using LBPUnion.ProjectLighthouse.Types.Levels; namespace LBPUnion.ProjectLighthouse.Types.Match; public class Room { - public List Players; - public int RoomId; + [JsonIgnore] + public List Players { get; set; } - public GameVersion RoomVersion; - public RoomSlot Slot; - public RoomState State; + public int RoomId { get; set; } + [JsonIgnore] + public GameVersion RoomVersion { get; set; } + + public RoomSlot Slot { get; set; } + public RoomState State { get; set; } + + [JsonIgnore] public bool IsInPod => this.Slot.SlotType == SlotType.Pod; + + [JsonIgnore] public bool IsLookingForPlayers => this.State == RoomState.PlayingLevel || this.State == RoomState.DivingInWaiting; + [JsonIgnore] public User Host => this.Players[0]; + public int PlayerCount => this.Players.Count; + #nullable enable public override bool Equals(object? obj) { diff --git a/ProjectLighthouse/Types/Match/RoomSlot.cs b/ProjectLighthouse/Types/Match/RoomSlot.cs index 063715d4..b3d2c0e4 100644 --- a/ProjectLighthouse/Types/Match/RoomSlot.cs +++ b/ProjectLighthouse/Types/Match/RoomSlot.cs @@ -4,6 +4,6 @@ namespace LBPUnion.ProjectLighthouse.Types.Match; public class RoomSlot { - public int SlotId; - public SlotType SlotType; + public int SlotId { get; set; } + public SlotType SlotType { get; set; } } \ No newline at end of file diff --git a/ProjectLighthouse/Types/Profiles/LastContact.cs b/ProjectLighthouse/Types/Profiles/LastContact.cs index 7fb0593a..14073106 100644 --- a/ProjectLighthouse/Types/Profiles/LastContact.cs +++ b/ProjectLighthouse/Types/Profiles/LastContact.cs @@ -10,4 +10,6 @@ public class LastContact public long Timestamp { get; set; } public GameVersion GameVersion { get; set; } = GameVersion.Unknown; + + public Platform Platform { get; set; } = Platform.Unknown; } \ No newline at end of file diff --git a/ProjectLighthouse/Types/Profiles/UserStatus.cs b/ProjectLighthouse/Types/Profiles/UserStatus.cs index 67cde0f6..c9192a35 100644 --- a/ProjectLighthouse/Types/Profiles/UserStatus.cs +++ b/ProjectLighthouse/Types/Profiles/UserStatus.cs @@ -1,6 +1,7 @@ #nullable enable using System.Linq; using LBPUnion.ProjectLighthouse.Helpers; +using LBPUnion.ProjectLighthouse.Types.Match; namespace LBPUnion.ProjectLighthouse.Types.Profiles; @@ -8,6 +9,8 @@ public class UserStatus { public StatusType StatusType { get; set; } public GameVersion? CurrentVersion { get; set; } + public Platform? CurrentPlatform { get; set; } + public Room? CurrentRoom { get; set; } public UserStatus() {} @@ -25,16 +28,19 @@ public class UserStatus { StatusType = StatusType.Online; CurrentVersion = lastContact.GameVersion; + CurrentPlatform = lastContact.Platform; } + + CurrentRoom = RoomHelper.FindRoomByUserId(userId); } public override string ToString() { CurrentVersion ??= GameVersion.Unknown; - + CurrentPlatform ??= Platform.Unknown; return this.StatusType switch { - StatusType.Online => $"Currently online on {((GameVersion)this.CurrentVersion).ToPrettyString()}", + StatusType.Online => $"Currently online on {((GameVersion)this.CurrentVersion).ToPrettyString()} on {((Platform)this.CurrentPlatform)}", StatusType.Offline => "Offline", _ => "Unknown", };