From ab2d9176b6ddd88d61816d72c7ec489f7c1411e6 Mon Sep 17 00:00:00 2001 From: Kat Date: Thu, 2 Jan 2025 13:32:08 -0800 Subject: [PATCH 1/5] Notify a user when their highscore is beaten --- .../Controllers/Slots/ScoreController.cs | 14 +++++++++-- .../Database/DatabaseContext.Notifications.cs | 23 +++++++++++-------- .../Types/Entities/Level/SlotEntity.cs | 3 +++ .../Types/Entities/Profile/UserEntity.cs | 3 +++ 4 files changed, 32 insertions(+), 11 deletions(-) diff --git a/ProjectLighthouse.Servers.GameServer/Controllers/Slots/ScoreController.cs b/ProjectLighthouse.Servers.GameServer/Controllers/Slots/ScoreController.cs index 79ae31632..d80d2cbc5 100644 --- a/ProjectLighthouse.Servers.GameServer/Controllers/Slots/ScoreController.cs +++ b/ProjectLighthouse.Servers.GameServer/Controllers/Slots/ScoreController.cs @@ -5,6 +5,7 @@ using LBPUnion.ProjectLighthouse.Logging; using LBPUnion.ProjectLighthouse.StorableLists.Stores; using LBPUnion.ProjectLighthouse.Types.Entities.Level; +using LBPUnion.ProjectLighthouse.Types.Entities.Profile; using LBPUnion.ProjectLighthouse.Types.Entities.Token; using LBPUnion.ProjectLighthouse.Types.Levels; using LBPUnion.ProjectLighthouse.Types.Logging; @@ -159,7 +160,7 @@ public async Task SubmitScore(string slotType, int id, int childI await this.database.SaveChangesAsync(); - return this.Ok(await this.GetScores(new LeaderboardOptions + ScoreboardResponse scores = await this.GetScores(new LeaderboardOptions { RootName = "scoreboardSegment", PageSize = 5, @@ -169,7 +170,16 @@ public async Task SubmitScore(string slotType, int id, int childI ScoreType = score.Type, TargetUser = token.UserId, TargetPlayerIds = null, - })); + }); + + if (score.Type == 1 && scores.YourRank == 1 && scores.Total > 1) + { + GameScore? second = scores.Scores[1]; + UserEntity? user = await this.database.UserFromGameToken(token); + await this.database.SendNotification(second.UserId, $"{user?.InfoXml} beat your highscore ({second.Points}) on {slot.InfoXml} with a score of {score.Points}.", true); + } + + return this.Ok(scores); } [HttpGet("scoreboard/{slotType}/{id:int}")] diff --git a/ProjectLighthouse/Database/DatabaseContext.Notifications.cs b/ProjectLighthouse/Database/DatabaseContext.Notifications.cs index 8fefaec86..9ba8b20ee 100644 --- a/ProjectLighthouse/Database/DatabaseContext.Notifications.cs +++ b/ProjectLighthouse/Database/DatabaseContext.Notifications.cs @@ -15,9 +15,10 @@ public partial class DatabaseContext /// /// The user ID of the target user. /// The message to send. + /// Ignore the prepended server name/timestamp. /// The for the notification. Defaults to ModerationNotification. public async Task SendNotification - (int userId, string text, NotificationType type = NotificationType.ModerationNotification) + (int userId, string text, bool noPrefix = false, NotificationType type = NotificationType.ModerationNotification) { if (!await this.Users.AnyAsync(u => u.UserId == userId)) { @@ -31,15 +32,19 @@ public async Task SendNotification StringBuilder builder = new(text); - // Prepend server name to notification text if enabled - if (ServerConfiguration.Instance.NotificationConfiguration.ShowServerNameInText) + if (!noPrefix) { - builder.Insert(0, $"[{ServerConfiguration.Instance.Customization.ServerName}] "); - } - // Prepend timestamp to notification text if enabled - if (ServerConfiguration.Instance.NotificationConfiguration.ShowTimestampInText) - { - builder.Insert(0, $"[{DateTime.Now:g}] "); + // Prepend server name to notification text if enabled + if (ServerConfiguration.Instance.NotificationConfiguration.ShowServerNameInText) + { + builder.Insert(0, $"[{ServerConfiguration.Instance.Customization.ServerName}] "); + } + + // Prepend timestamp to notification text if enabled + if (ServerConfiguration.Instance.NotificationConfiguration.ShowTimestampInText) + { + builder.Insert(0, $"[{DateTime.Now:g}] "); + } } NotificationEntity notification = new() diff --git a/ProjectLighthouse/Types/Entities/Level/SlotEntity.cs b/ProjectLighthouse/Types/Entities/Level/SlotEntity.cs index 693bf4aa4..bd6a7bb07 100644 --- a/ProjectLighthouse/Types/Entities/Level/SlotEntity.cs +++ b/ProjectLighthouse/Types/Entities/Level/SlotEntity.cs @@ -29,6 +29,9 @@ public class SlotEntity public string IconHash { get; set; } = ""; + [NotMapped] + public string InfoXml => $"{this.Name}"; + public bool IsAdventurePlanet { get; set; } public string RootLevel { get; set; } = ""; diff --git a/ProjectLighthouse/Types/Entities/Profile/UserEntity.cs b/ProjectLighthouse/Types/Entities/Profile/UserEntity.cs index 998c63b84..fb4950349 100644 --- a/ProjectLighthouse/Types/Entities/Profile/UserEntity.cs +++ b/ProjectLighthouse/Types/Entities/Profile/UserEntity.cs @@ -28,6 +28,9 @@ public class UserEntity public string IconHash { get; set; } + [NotMapped] + public string InfoXml => $"{this.Username}"; + /// /// A user-customizable biography shown on the profile card /// From 359b75128cd4ffd73dee65d1dc304355a92edd71 Mon Sep 17 00:00:00 2001 From: Kat <30480654+Metraberryy@users.noreply.github.com> Date: Thu, 2 Jan 2025 13:58:55 -0800 Subject: [PATCH 2/5] formatting Co-authored-by: sudokoko --- .../Controllers/Slots/ScoreController.cs | 1 + 1 file changed, 1 insertion(+) diff --git a/ProjectLighthouse.Servers.GameServer/Controllers/Slots/ScoreController.cs b/ProjectLighthouse.Servers.GameServer/Controllers/Slots/ScoreController.cs index d80d2cbc5..8a859fd73 100644 --- a/ProjectLighthouse.Servers.GameServer/Controllers/Slots/ScoreController.cs +++ b/ProjectLighthouse.Servers.GameServer/Controllers/Slots/ScoreController.cs @@ -176,6 +176,7 @@ public async Task SubmitScore(string slotType, int id, int childI { GameScore? second = scores.Scores[1]; UserEntity? user = await this.database.UserFromGameToken(token); + await this.database.SendNotification(second.UserId, $"{user?.InfoXml} beat your highscore ({second.Points}) on {slot.InfoXml} with a score of {score.Points}.", true); } From 7637923f56b1c32652015f6255fe2da1bd6af4a4 Mon Sep 17 00:00:00 2001 From: Kat Date: Thu, 2 Jan 2025 14:04:13 -0800 Subject: [PATCH 3/5] noPrefix -> prefix --- ProjectLighthouse/Database/DatabaseContext.Notifications.cs | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/ProjectLighthouse/Database/DatabaseContext.Notifications.cs b/ProjectLighthouse/Database/DatabaseContext.Notifications.cs index 9ba8b20ee..d22af72e4 100644 --- a/ProjectLighthouse/Database/DatabaseContext.Notifications.cs +++ b/ProjectLighthouse/Database/DatabaseContext.Notifications.cs @@ -15,10 +15,10 @@ public partial class DatabaseContext /// /// The user ID of the target user. /// The message to send. - /// Ignore the prepended server name/timestamp. + /// Prepend server name/timestamp. /// The for the notification. Defaults to ModerationNotification. public async Task SendNotification - (int userId, string text, bool noPrefix = false, NotificationType type = NotificationType.ModerationNotification) + (int userId, string text, bool prefix = true, NotificationType type = NotificationType.ModerationNotification) { if (!await this.Users.AnyAsync(u => u.UserId == userId)) { @@ -32,7 +32,7 @@ public async Task SendNotification StringBuilder builder = new(text); - if (!noPrefix) + if (prefix) { // Prepend server name to notification text if enabled if (ServerConfiguration.Instance.NotificationConfiguration.ShowServerNameInText) From 045a39ba5fd6b059c8379d58f6c8a10e87a656cb Mon Sep 17 00:00:00 2001 From: Kat Date: Thu, 9 Jan 2025 19:48:44 -0800 Subject: [PATCH 4/5] documentation and fix closing tag --- ProjectLighthouse/Types/Entities/Level/SlotEntity.cs | 3 +++ ProjectLighthouse/Types/Entities/Profile/UserEntity.cs | 5 ++++- 2 files changed, 7 insertions(+), 1 deletion(-) diff --git a/ProjectLighthouse/Types/Entities/Level/SlotEntity.cs b/ProjectLighthouse/Types/Entities/Level/SlotEntity.cs index bd6a7bb07..0a0ffac4a 100644 --- a/ProjectLighthouse/Types/Entities/Level/SlotEntity.cs +++ b/ProjectLighthouse/Types/Entities/Level/SlotEntity.cs @@ -29,6 +29,9 @@ public class SlotEntity public string IconHash { get; set; } = ""; + /// + /// Markup that displays the level name next to its badge + /// [NotMapped] public string InfoXml => $"{this.Name}"; diff --git a/ProjectLighthouse/Types/Entities/Profile/UserEntity.cs b/ProjectLighthouse/Types/Entities/Profile/UserEntity.cs index fb4950349..b6d9979f1 100644 --- a/ProjectLighthouse/Types/Entities/Profile/UserEntity.cs +++ b/ProjectLighthouse/Types/Entities/Profile/UserEntity.cs @@ -28,8 +28,11 @@ public class UserEntity public string IconHash { get; set; } + /// + /// Markup that displays the username next to a polaroid with the user's icon ingame + /// [NotMapped] - public string InfoXml => $"{this.Username}"; + public string InfoXml => $"{this.Username}"; /// /// A user-customizable biography shown on the profile card From 829b067e6eec27b62ca4d94668b9604a6788e79b Mon Sep 17 00:00:00 2001 From: Kat Date: Thu, 9 Jan 2025 19:54:13 -0800 Subject: [PATCH 5/5] better documentation --- ProjectLighthouse/Types/Entities/Level/SlotEntity.cs | 3 ++- ProjectLighthouse/Types/Entities/Profile/UserEntity.cs | 3 ++- 2 files changed, 4 insertions(+), 2 deletions(-) diff --git a/ProjectLighthouse/Types/Entities/Level/SlotEntity.cs b/ProjectLighthouse/Types/Entities/Level/SlotEntity.cs index 0a0ffac4a..07d65c045 100644 --- a/ProjectLighthouse/Types/Entities/Level/SlotEntity.cs +++ b/ProjectLighthouse/Types/Entities/Level/SlotEntity.cs @@ -30,7 +30,8 @@ public class SlotEntity public string IconHash { get; set; } = ""; /// - /// Markup that displays the level name next to its badge + /// Markup that displays the level name next to its badge. + /// This can be used everywhere markup works ingame, e.g. news or notifications /// [NotMapped] public string InfoXml => $"{this.Name}"; diff --git a/ProjectLighthouse/Types/Entities/Profile/UserEntity.cs b/ProjectLighthouse/Types/Entities/Profile/UserEntity.cs index b6d9979f1..6fb653016 100644 --- a/ProjectLighthouse/Types/Entities/Profile/UserEntity.cs +++ b/ProjectLighthouse/Types/Entities/Profile/UserEntity.cs @@ -29,7 +29,8 @@ public class UserEntity public string IconHash { get; set; } /// - /// Markup that displays the username next to a polaroid with the user's icon ingame + /// Markup that displays the username next to a polaroid with the user's icon. + /// This can be used everywhere markup works ingame, e.g. news or notifications /// [NotMapped] public string InfoXml => $"{this.Username}";