Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Notify a user when their highscore is beaten #1083

Merged
merged 7 commits into from
Jan 11, 2025
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
#nullable enable

Check warning on line 1 in ProjectLighthouse.Servers.GameServer/Controllers/Slots/ScoreController.cs

View workflow job for this annotation

GitHub Actions / Qodana for .NET

Redundant nullable directive

Redundant nullable directive
using LBPUnion.ProjectLighthouse.Database;
using LBPUnion.ProjectLighthouse.Extensions;
using LBPUnion.ProjectLighthouse.Helpers;
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;
Expand All @@ -24,7 +25,7 @@
{
private readonly DatabaseContext database;

public ScoreController(DatabaseContext database)

Check notice on line 28 in ProjectLighthouse.Servers.GameServer/Controllers/Slots/ScoreController.cs

View workflow job for this annotation

GitHub Actions / Qodana for .NET

Convert constructor into primary constructor

Convert into primary constructor
{
this.database = database;
}
Expand All @@ -33,7 +34,7 @@
{
UserFriendData? store = UserFriendStore.GetUserFriendData(userId);
List<int>? friendIds = store?.FriendIds;
friendIds ??= new List<int>();

Check notice on line 37 in ProjectLighthouse.Servers.GameServer/Controllers/Slots/ScoreController.cs

View workflow job for this annotation

GitHub Actions / Qodana for .NET

Use collection expression syntax

Use collection expression
friendIds.Add(userId);

return friendIds.Distinct().ToArray();
Expand Down Expand Up @@ -63,7 +64,7 @@
// Workaround for parsing player ids of versus levels
if (score.PlayerIds.Length == 1)
{
char[] delimiters = { ':', ',', };

Check notice on line 67 in ProjectLighthouse.Servers.GameServer/Controllers/Slots/ScoreController.cs

View workflow job for this annotation

GitHub Actions / Qodana for .NET

Use collection expression syntax

Use collection expression
score.PlayerIds = score.PlayerIds[0].Split(delimiters).Distinct().ToArray();
}

Expand Down Expand Up @@ -159,7 +160,7 @@

await this.database.SaveChangesAsync();

return this.Ok(await this.GetScores(new LeaderboardOptions
ScoreboardResponse scores = await this.GetScores(new LeaderboardOptions
{
RootName = "scoreboardSegment",
PageSize = 5,
Expand All @@ -169,7 +170,17 @@
ScoreType = score.Type,
TargetUser = token.UserId,
TargetPlayerIds = null,
}));
});

if (score.Type == 1 && scores.YourRank == 1 && scores.Total > 1)

Check notice on line 175 in ProjectLighthouse.Servers.GameServer/Controllers/Slots/ScoreController.cs

View workflow job for this annotation

GitHub Actions / Qodana for .NET

Invert 'if' statement to reduce nesting

Invert 'if' statement to reduce nesting

Check notice on line 175 in ProjectLighthouse.Servers.GameServer/Controllers/Slots/ScoreController.cs

View workflow job for this annotation

GitHub Actions / Qodana for .NET

Merge null/pattern checks into complex pattern

Merge into pattern
{
GameScore? second = scores.Scores[1];
UserEntity? user = await this.database.UserFromGameToken(token);

await this.database.SendNotification(second.UserId, $"{user?.InfoXml} beat your highscore (<em>{second.Points}</em>) on {slot.InfoXml} with a score of <em>{score.Points}</em>.", true);

Check warning on line 180 in ProjectLighthouse.Servers.GameServer/Controllers/Slots/ScoreController.cs

View workflow job for this annotation

GitHub Actions / Qodana for .NET

Redundant argument with default value

The parameter 'prefix' has the same default value
}

return this.Ok(scores);
}

[HttpGet("scoreboard/{slotType}/{id:int}")]
Expand All @@ -191,7 +202,7 @@
};
if (!HttpMethods.IsPost(this.Request.Method))
{
List<PlayerScoreboardResponse> scoreboardResponses = new();

Check notice on line 205 in ProjectLighthouse.Servers.GameServer/Controllers/Slots/ScoreController.cs

View workflow job for this annotation

GitHub Actions / Qodana for .NET

Use collection expression syntax

Use collection expression
for (int i = 1; i <= 4; i++)
{
options.ScoreType = i;
Expand Down
23 changes: 14 additions & 9 deletions ProjectLighthouse/Database/DatabaseContext.Notifications.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,10 @@ public partial class DatabaseContext
/// </summary>
/// <param name="userId">The user ID of the target user.</param>
/// <param name="text">The message to send.</param>
/// <param name="prefix">Prepend server name/timestamp.</param>
/// <param name="type">The <see cref="NotificationType"/> for the notification. Defaults to <c>ModerationNotification</c>.</param>
public async Task SendNotification
(int userId, string text, NotificationType type = NotificationType.ModerationNotification)
(int userId, string text, bool prefix = true, NotificationType type = NotificationType.ModerationNotification)
{
if (!await this.Users.AnyAsync(u => u.UserId == userId))
{
Expand All @@ -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 (prefix)
{
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()
Expand Down
3 changes: 3 additions & 0 deletions ProjectLighthouse/Types/Entities/Level/SlotEntity.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,28 +17,31 @@
public class SlotEntity
{
[Key]
public int SlotId { get; set; }

Check notice on line 20 in ProjectLighthouse/Types/Entities/Level/SlotEntity.cs

View workflow job for this annotation

GitHub Actions / Qodana for .NET

Property can be made init-only (non-private accessibility)

Property can be made init-only

public int InternalSlotId { get; set; }

Check notice on line 22 in ProjectLighthouse/Types/Entities/Level/SlotEntity.cs

View workflow job for this annotation

GitHub Actions / Qodana for .NET

Property can be made init-only (non-private accessibility)

Property can be made init-only

public SlotType Type { get; set; } = SlotType.User;

Check notice on line 24 in ProjectLighthouse/Types/Entities/Level/SlotEntity.cs

View workflow job for this annotation

GitHub Actions / Qodana for .NET

Property can be made init-only (non-private accessibility)

Property can be made init-only

public string Name { get; set; } = "";

Check warning on line 26 in ProjectLighthouse/Types/Entities/Level/SlotEntity.cs

View workflow job for this annotation

GitHub Actions / Qodana for .NET

Possible performance issues caused by unlimited string length

Possible performance issues caused by unlimited string length

public string Description { get; set; } = "";

Check warning on line 28 in ProjectLighthouse/Types/Entities/Level/SlotEntity.cs

View workflow job for this annotation

GitHub Actions / Qodana for .NET

Possible performance issues caused by unlimited string length

Possible performance issues caused by unlimited string length

public string IconHash { get; set; } = "";

Check warning on line 30 in ProjectLighthouse/Types/Entities/Level/SlotEntity.cs

View workflow job for this annotation

GitHub Actions / Qodana for .NET

Possible performance issues caused by unlimited string length

Possible performance issues caused by unlimited string length

[NotMapped]
public string InfoXml => $"<slot type=\"{this.Type}\" id=\"{this.SlotId}\" icon=\"{this.IconHash}\">{this.Name}</slot>";
Metraberryy marked this conversation as resolved.
Show resolved Hide resolved

public bool IsAdventurePlanet { get; set; }

public string RootLevel { get; set; } = "";

Check warning on line 37 in ProjectLighthouse/Types/Entities/Level/SlotEntity.cs

View workflow job for this annotation

GitHub Actions / Qodana for .NET

Possible performance issues caused by unlimited string length

Possible performance issues caused by unlimited string length

public string ResourceCollection { get; set; } = "";

Check warning on line 39 in ProjectLighthouse/Types/Entities/Level/SlotEntity.cs

View workflow job for this annotation

GitHub Actions / Qodana for .NET

Possible performance issues caused by unlimited string length

Possible performance issues caused by unlimited string length

[NotMapped]
public string[]? Resources {
get => this.ResourceCollection.Split(",", StringSplitOptions.RemoveEmptyEntries);
set => this.ResourceCollection = string.Join(',', value ?? Array.Empty<string>());

Check notice on line 44 in ProjectLighthouse/Types/Entities/Level/SlotEntity.cs

View workflow job for this annotation

GitHub Actions / Qodana for .NET

Use collection expression syntax

Use collection expression
}

/// <summary>
Expand All @@ -63,13 +66,13 @@
public int CreatorId { get; set; }

[ForeignKey(nameof(CreatorId))]
public UserEntity? Creator { get; set; }

Check notice on line 69 in ProjectLighthouse/Types/Entities/Level/SlotEntity.cs

View workflow job for this annotation

GitHub Actions / Qodana for .NET

Property can be made init-only (non-private accessibility)

Property can be made init-only

public bool InitiallyLocked { get; set; }

public bool LockedByModerator { get; set; }

public string LockedReason { get; set; } = "";

Check warning on line 75 in ProjectLighthouse/Types/Entities/Level/SlotEntity.cs

View workflow job for this annotation

GitHub Actions / Qodana for .NET

Possible performance issues caused by unlimited string length

Possible performance issues caused by unlimited string length

public bool SubLevel { get; set; }

Expand All @@ -77,7 +80,7 @@

public int Shareable { get; set; }

public string AuthorLabels { get; set; } = "";

Check warning on line 83 in ProjectLighthouse/Types/Entities/Level/SlotEntity.cs

View workflow job for this annotation

GitHub Actions / Qodana for .NET

Possible performance issues caused by unlimited string length

Possible performance issues caused by unlimited string length

public string[] LevelTags(DatabaseContext database)
{
Expand All @@ -90,7 +93,7 @@
.ToArray();
}

public string BackgroundHash { get; set; } = "";

Check warning on line 96 in ProjectLighthouse/Types/Entities/Level/SlotEntity.cs

View workflow job for this annotation

GitHub Actions / Qodana for .NET

Possible performance issues caused by unlimited string length

Possible performance issues caused by unlimited string length

public int MinimumPlayers { get; set; }

Expand All @@ -106,13 +109,13 @@

public GameVersion GameVersion { get; set; }

public string LevelType { get; set; } = "";

Check warning on line 112 in ProjectLighthouse/Types/Entities/Level/SlotEntity.cs

View workflow job for this annotation

GitHub Actions / Qodana for .NET

Possible performance issues caused by unlimited string length

Possible performance issues caused by unlimited string length

public bool CrossControllerRequired { get; set; }

public bool Hidden { get; set; }

public string HiddenReason { get; set; } = "";

Check warning on line 118 in ProjectLighthouse/Types/Entities/Level/SlotEntity.cs

View workflow job for this annotation

GitHub Actions / Qodana for .NET

Possible performance issues caused by unlimited string length

Possible performance issues caused by unlimited string length

public int PlaysLBP1 { get; set; }

Expand Down
3 changes: 3 additions & 0 deletions ProjectLighthouse/Types/Entities/Profile/UserEntity.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,24 +14,27 @@

#nullable enable
[Key]
public int UserId { get; set; }

Check notice on line 17 in ProjectLighthouse/Types/Entities/Profile/UserEntity.cs

View workflow job for this annotation

GitHub Actions / Qodana for .NET

Property can be made init-only (non-private accessibility)

Property can be made init-only

public string Username { get; set; } = "";

Check warning on line 19 in ProjectLighthouse/Types/Entities/Profile/UserEntity.cs

View workflow job for this annotation

GitHub Actions / Qodana for .NET

Possible performance issues caused by unlimited string length

Possible performance issues caused by unlimited string length

#nullable enable

Check warning on line 21 in ProjectLighthouse/Types/Entities/Profile/UserEntity.cs

View workflow job for this annotation

GitHub Actions / Qodana for .NET

Redundant nullable directive

Redundant nullable directive
public string? EmailAddress { get; set; }

Check warning on line 22 in ProjectLighthouse/Types/Entities/Profile/UserEntity.cs

View workflow job for this annotation

GitHub Actions / Qodana for .NET

Possible performance issues caused by unlimited string length

Possible performance issues caused by unlimited string length
#nullable disable

public bool EmailAddressVerified { get; set; }

public string Password { get; set; }

Check warning on line 27 in ProjectLighthouse/Types/Entities/Profile/UserEntity.cs

View workflow job for this annotation

GitHub Actions / Qodana for .NET

Possible performance issues caused by unlimited string length

Possible performance issues caused by unlimited string length

public string IconHash { get; set; }

Check warning on line 29 in ProjectLighthouse/Types/Entities/Profile/UserEntity.cs

View workflow job for this annotation

GitHub Actions / Qodana for .NET

Possible performance issues caused by unlimited string length

Possible performance issues caused by unlimited string length

[NotMapped]
public string InfoXml => $"<player icon=\"{this.IconHash}\">{this.Username}</user>";
Metraberryy marked this conversation as resolved.
Show resolved Hide resolved

/// <summary>
/// A user-customizable biography shown on the profile card
/// </summary>
public string Biography { get; set; }

Check warning on line 37 in ProjectLighthouse/Types/Entities/Profile/UserEntity.cs

View workflow job for this annotation

GitHub Actions / Qodana for .NET

Possible performance issues caused by unlimited string length

Possible performance issues caused by unlimited string length

[NotMapped]
public string WebsiteAvatarHash {
Expand Down Expand Up @@ -68,22 +71,22 @@
set => this.LocationPacked = (ulong)value.X << 32 | (uint)value.Y;
}

public string Pins { get; set; } = "";

Check warning on line 74 in ProjectLighthouse/Types/Entities/Profile/UserEntity.cs

View workflow job for this annotation

GitHub Actions / Qodana for .NET

Possible performance issues caused by unlimited string length

Possible performance issues caused by unlimited string length

public string PlanetHashLBP2 { get; set; } = "";

Check warning on line 76 in ProjectLighthouse/Types/Entities/Profile/UserEntity.cs

View workflow job for this annotation

GitHub Actions / Qodana for .NET

Possible performance issues caused by unlimited string length

Possible performance issues caused by unlimited string length

// ReSharper disable once InconsistentNaming
public string PlanetHashLBP2CC { get; set; } = "";

Check warning on line 79 in ProjectLighthouse/Types/Entities/Profile/UserEntity.cs

View workflow job for this annotation

GitHub Actions / Qodana for .NET

Possible performance issues caused by unlimited string length

Possible performance issues caused by unlimited string length

public string PlanetHashLBP3 { get; set; } = "";

Check warning on line 81 in ProjectLighthouse/Types/Entities/Profile/UserEntity.cs

View workflow job for this annotation

GitHub Actions / Qodana for .NET

Possible performance issues caused by unlimited string length

Possible performance issues caused by unlimited string length

public string PlanetHashLBPVita { get; set; } = "";

Check warning on line 83 in ProjectLighthouse/Types/Entities/Profile/UserEntity.cs

View workflow job for this annotation

GitHub Actions / Qodana for .NET

Possible performance issues caused by unlimited string length

Possible performance issues caused by unlimited string length

public bool PasswordResetRequired { get; set; }

public string YayHash { get; set; } = "";

Check warning on line 87 in ProjectLighthouse/Types/Entities/Profile/UserEntity.cs

View workflow job for this annotation

GitHub Actions / Qodana for .NET

Possible performance issues caused by unlimited string length

Possible performance issues caused by unlimited string length
public string BooHash { get; set; } = "";

Check warning on line 88 in ProjectLighthouse/Types/Entities/Profile/UserEntity.cs

View workflow job for this annotation

GitHub Actions / Qodana for .NET

Possible performance issues caused by unlimited string length

Possible performance issues caused by unlimited string length
public string MehHash { get; set; } = "";

Check warning on line 89 in ProjectLighthouse/Types/Entities/Profile/UserEntity.cs

View workflow job for this annotation

GitHub Actions / Qodana for .NET

Possible performance issues caused by unlimited string length

Possible performance issues caused by unlimited string length

public long LastLogin { get; set; }
public long LastLogout { get; set; }
Expand All @@ -101,12 +104,12 @@
public PermissionLevel PermissionLevel { get; set; } = PermissionLevel.Default;

#nullable enable
public string? BannedReason { get; set; }

Check warning on line 107 in ProjectLighthouse/Types/Entities/Profile/UserEntity.cs

View workflow job for this annotation

GitHub Actions / Qodana for .NET

Possible performance issues caused by unlimited string length

Possible performance issues caused by unlimited string length
#nullable disable

public string Language { get; set; } = "en";

Check warning on line 110 in ProjectLighthouse/Types/Entities/Profile/UserEntity.cs

View workflow job for this annotation

GitHub Actions / Qodana for .NET

Possible performance issues caused by unlimited string length

Possible performance issues caused by unlimited string length

public string TimeZone { get; set; } = TimeZoneInfo.Local.Id;

Check warning on line 112 in ProjectLighthouse/Types/Entities/Profile/UserEntity.cs

View workflow job for this annotation

GitHub Actions / Qodana for .NET

Possible performance issues caused by unlimited string length

Possible performance issues caused by unlimited string length

public PrivacyType LevelVisibility { get; set; } = PrivacyType.All;

Expand All @@ -117,9 +120,9 @@

public bool IsTwoFactorSetup => this.TwoFactorBackup?.Length > 0 && this.TwoFactorSecret?.Length > 0;

public string TwoFactorSecret { get; set; } = "";

Check warning on line 123 in ProjectLighthouse/Types/Entities/Profile/UserEntity.cs

View workflow job for this annotation

GitHub Actions / Qodana for .NET

Possible performance issues caused by unlimited string length

Possible performance issues caused by unlimited string length

public string TwoFactorBackup { get; set; } = "";

Check warning on line 125 in ProjectLighthouse/Types/Entities/Profile/UserEntity.cs

View workflow job for this annotation

GitHub Actions / Qodana for .NET

Possible performance issues caused by unlimited string length

Possible performance issues caused by unlimited string length

public ulong LinkedRpcnId { get; set; }

Expand All @@ -132,7 +135,7 @@
// should not be adjustable by user
public bool CommentsEnabled { get; set; } = true;

public string ProfileTag { get; set; } = "";

Check warning on line 138 in ProjectLighthouse/Types/Entities/Profile/UserEntity.cs

View workflow job for this annotation

GitHub Actions / Qodana for .NET

Possible performance issues caused by unlimited string length

Possible performance issues caused by unlimited string length

#nullable enable
public override bool Equals(object? obj)
Expand All @@ -154,4 +157,4 @@

[SuppressMessage("ReSharper", "NonReadonlyMemberInGetHashCode")]
public override int GetHashCode() => this.UserId;
#nullable disable

Check warning on line 160 in ProjectLighthouse/Types/Entities/Profile/UserEntity.cs

View workflow job for this annotation

GitHub Actions / Qodana for .NET

Unused nullable directive

Unused nullable directive
Expand Down
Loading