Skip to content

Commit

Permalink
Make the search faster, hopefully! (#17)
Browse files Browse the repository at this point in the history
* Make search faster! Refactor models! Also trims whitespaces a lot

* Make main page link the leftmost one as is conventional
  • Loading branch information
SaphireLattice authored Aug 13, 2024
1 parent 4841599 commit f7d7e2d
Show file tree
Hide file tree
Showing 35 changed files with 716 additions and 699 deletions.
21 changes: 15 additions & 6 deletions ReplayBrowser/Data/Models/Account/Account.cs
Original file line number Diff line number Diff line change
@@ -1,25 +1,34 @@
namespace ReplayBrowser.Data.Models.Account;
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Metadata.Builders;

public class Account
namespace ReplayBrowser.Data.Models.Account;

public class Account : IEntityTypeConfiguration<Account>
{
// Primary key
public int Id { get; set; }

public Guid Guid { get; set; }
public string Username { get; set; }
public bool IsAdmin { get; set; } = false;
public AccountSettings Settings { get; set; } = new();

/// <summary>
/// Replays that the user has favorited.
/// </summary>
public List<int> FavoriteReplays { get; set; } = new();

/// <summary>
/// Profiles that the user "watches".
/// </summary>
public List<Guid> SavedProfiles { get; set; } = new();
public List<HistoryEntry> History { get; set; } = new();

public bool Protected { get; set; } = false;

public void Configure(EntityTypeBuilder<Account> builder)
{
builder.HasIndex(a => a.Guid).IsUnique();
builder.HasIndex(a => a.Username);
}
}
Original file line number Diff line number Diff line change
@@ -1,49 +1,59 @@
using System.Text.Json.Serialization;
using System.ComponentModel.DataAnnotations.Schema;
using System.Text.Json.Serialization;
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Metadata.Builders;

namespace ReplayBrowser.Data;
namespace ReplayBrowser.Data.Models;

/// <summary>
/// Represents a player's data over all replays.
/// </summary>
public class CollectedPlayerData
public class CollectedPlayerData : IEntityTypeConfiguration<CollectedPlayerData>
{
[JsonIgnore]
public DateTime GeneratedAt { get; set; } = DateTime.Now;

[JsonIgnore]
public Guid PlayerGuid { get; set; }

public PlayerData PlayerData { get; set; } = new();

/// <summary>
/// Characters played by the player
/// </summary>
public List<CharacterData> Characters { get; set; } = new();

/// <summary>
/// Represents the estimated total playtime of the player. This is calculated by summing the roundtime of all replays the player has played.
/// </summary>
public TimeSpan TotalEstimatedPlaytime { get; set; }

/// <summary>
/// Represents the total amount of rounds the player has played.
/// </summary>
public int TotalRoundsPlayed { get; set; }

/// <summary>
/// Represents the total amount of antag rounds the player has played.
/// </summary>
public int TotalAntagRoundsPlayed { get; set; }

public List<JobCountData> JobCount { get; set; } = new();

public DateTime LastSeen { get; set; } = DateTime.MinValue;

/// <summary>
/// Is this profile currently being "watched" by the user?
/// </summary>
public bool IsWatched { get; set; } = false;


public void Configure(EntityTypeBuilder<CollectedPlayerData> builder)
{
builder.HasKey(p => p.PlayerGuid);
builder.HasIndex(p => p.PlayerGuid).IsUnique();
}


public override bool Equals(object? obj)
{
if (obj is not CollectedPlayerData other)
Expand All @@ -53,7 +63,7 @@ public override bool Equals(object? obj)

return PlayerData.Equals(other.PlayerData);
}

public override int GetHashCode()
{
return PlayerData.GetHashCode();
Expand All @@ -71,7 +81,7 @@ public class CharacterData
public class JobCountData
{
public int Id { get; set; }

public string JobPrototype { get; set; }
public int RoundsPlayed { get; set; }
public DateTime LastPlayed { get; set; } = DateTime.MinValue;
Expand Down
21 changes: 16 additions & 5 deletions ReplayBrowser/Data/Models/Player.cs
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
using YamlDotNet.Serialization;
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Metadata.Builders;
using YamlDotNet.Serialization;

namespace ReplayBrowser.Data.Models;

public class Player
public class Player : IEntityTypeConfiguration<Player>
{
public int Id { get; set; }

[YamlMember(Alias = "antagPrototypes")]
public List<string> AntagPrototypes { get; set; }
[YamlMember(Alias = "jobPrototypes")]
Expand All @@ -18,12 +20,21 @@ public class Player
public string PlayerOocName { get; set; }
[YamlMember(Alias = "antag")]
public bool Antag { get; set; }

// Foreign key

public int? ReplayId { get; set; }
public Replay? Replay { get; set; }


public void Configure(EntityTypeBuilder<Player> builder)
{
builder.HasIndex(p => p.PlayerGuid);
builder.HasIndex(p => p.PlayerIcName);
builder.HasIndex(p => p.PlayerOocName);
builder.HasIndex(p => p.ReplayId);
}

public void RedactInformation(bool wasGdpr = false)
{
if (wasGdpr)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
using System.ComponentModel.DataAnnotations.Schema;
using System.Text.Json.Serialization;

namespace ReplayBrowser.Data;
namespace ReplayBrowser.Data.Models;

/// <summary>
/// Represents collected player data from replays. This is used to generate leaderboards and other statistics.
Expand All @@ -10,11 +10,11 @@ public class PlayerData
{
[JsonIgnore]
public int Id { get; set; }

public Guid? PlayerGuid { get; set; }

public string Username { get; set; }


public override bool Equals(object? obj)
{
Expand All @@ -25,7 +25,7 @@ public override bool Equals(object? obj)

return PlayerGuid == other.PlayerGuid;
}

public override int GetHashCode()
{
return PlayerGuid.GetHashCode();
Expand Down
58 changes: 48 additions & 10 deletions ReplayBrowser/Data/Models/Replay.cs
Original file line number Diff line number Diff line change
@@ -1,26 +1,29 @@
using System.ComponentModel.DataAnnotations.Schema;
using System.Text.Json.Serialization;
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Metadata.Builders;
using NpgsqlTypes;
using ReplayBrowser.Models;
using YamlDotNet.Serialization;

namespace ReplayBrowser.Data.Models;

public class Replay
public class Replay : IEntityTypeConfiguration<Replay>
{
public int Id { get; set; }

public string? Link { get; set; }

[YamlMember(Alias = "roundId")]
public int? RoundId { get; set; }

[YamlMember(Alias = "server_name")]
public string? ServerName { get; set; }
public DateTime? Date { get; set; }

[YamlMember(Alias = "map")]
public string? Map { get; set; }

[YamlMember(Alias = "maps")]
public List<string>? Maps { get; set; }
[YamlMember(Alias = "gamemode")]
Expand All @@ -43,10 +46,10 @@ public class Replay
public int UncompressedSize { get; set; }
[YamlMember(Alias = "endTime")]
public string EndTime { get; set; }

[JsonIgnore]
public NpgsqlTsVector RoundEndTextSearchVector { get; set; }

/// <summary>
/// Determines if a replay is marked as a favorite.
/// </summary>
Expand All @@ -59,14 +62,49 @@ public class Replay
// None yet.

#endregion


public void Configure(EntityTypeBuilder<Replay> builder)
{
builder.HasIndex(r => r.Map);
builder.HasIndex(r => r.Gamemode);
builder.HasIndex(r => r.ServerId);
builder.HasIndex(r => r.ServerName);

builder.HasGeneratedTsVectorColumn(
p => p.RoundEndTextSearchVector,
"english",
r => new { r.RoundEndText }
)
.HasIndex(r => r.RoundEndTextSearchVector)
.HasMethod("GIN");
}

public ReplayResult ToResult()
{
return new ReplayResult {
Id = Id,
Link = Link,
ServerId = ServerId,
ServerName = ServerName,
Gamemode = Gamemode,
Map = Map,
Maps = Maps,

Duration = Duration,
Date = Date,
RoundId = RoundId,
Size = Size,
UncompressedSize = UncompressedSize
};
}

public void RedactInformation(Guid? accountGuid)
{
if (accountGuid == null)
{
return;
}

if (RoundEndPlayers != null)
{
foreach (var player in RoundEndPlayers)
Expand Down
Loading

0 comments on commit f7d7e2d

Please sign in to comment.