Increase information in UserStatus, add platform for lastcontacts and gametokens

This commit is contained in:
jvyden 2022-02-17 00:57:02 -05:00
commit aeeb819759
No known key found for this signature in database
GPG key ID: 18BCF2BE0262B278
12 changed files with 93 additions and 13 deletions

View file

@ -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

View file

@ -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

View file

@ -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();
}

View file

@ -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)
{

View file

@ -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<int>(
name: "Platform",
table: "LastContacts",
type: "int",
nullable: false,
defaultValue: -1);
migrationBuilder.AddColumn<int>(
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");
}
}
}

View file

@ -81,6 +81,9 @@ namespace ProjectLighthouse.Migrations
b.Property<int>("GameVersion")
.HasColumnType("int");
b.Property<int>("Platform")
.HasColumnType("int");
b.Property<bool>("Used")
.HasColumnType("tinyint(1)");
@ -458,6 +461,9 @@ namespace ProjectLighthouse.Migrations
b.Property<int>("GameVersion")
.HasColumnType("int");
b.Property<int>("Platform")
.HasColumnType("int");
b.Property<long>("Timestamp")
.HasColumnType("bigint");

View file

@ -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

View file

@ -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; }

View file

@ -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<User> Players;
public int RoomId;
[JsonIgnore]
public List<User> 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)
{

View file

@ -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; }
}

View file

@ -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;
}

View file

@ -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",
};