Skip to content

Commit

Permalink
Stuff
Browse files Browse the repository at this point in the history
  • Loading branch information
Simyon264 committed Mar 7, 2024
1 parent b22fddd commit fc58f9c
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 6 deletions.
10 changes: 9 additions & 1 deletion Client/Components/Pages/Search.razor
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
@page "/search"
@using Replay = Shared.Models.Replay
@using System.Net
@using System.Diagnostics
@inject HttpClient Http
@inject NavigationManager NavigationManager

Expand Down Expand Up @@ -33,13 +34,14 @@
</div>
break;
case 0:
<p>Found 0 replays in @stopWatch.ElapsedMilliseconds ms</p>
<div class="replay-list">
<p>No results... :(</p>
</div>
break;
default:
{
<p>Found @Replays.Count replays</p>
<p>Found @Replays.Count replays in @stopWatch.ElapsedMilliseconds ms</p>
<div class="replay-list">
@foreach (var replay in Replays)
{
Expand All @@ -56,9 +58,11 @@
public bool IsLoading { get; set; } = true;
public string? ErrorMessage { get; set; }
public string? ErrorDetails { get; set; }
public Stopwatch stopWatch { get; set; } = new Stopwatch();

protected override async Task OnInitializedAsync()
{
stopWatch.Start();
// Get mode and query from query string
var uri = new Uri(NavigationManager.Uri);
var query = uri.Query;
Expand All @@ -70,6 +74,7 @@
ErrorMessage = "Invalid search query";
ErrorDetails = await response.Content.ReadAsStringAsync();
ErrorDetails = ErrorDetails.Replace("\n", "<br>");
stopWatch.Stop();
return;
}

Expand All @@ -78,16 +83,19 @@
ErrorMessage = "Failed to load replays";
ErrorDetails = await response.Content.ReadAsStringAsync();
ErrorDetails = ErrorDetails.Replace("\n", "<br>");
stopWatch.Stop();
return;
}

var loadedReplays = await response.Content.ReadFromJsonAsync<List<Replay>>();
if (loadedReplays == null)
{
ErrorMessage = "Failed to load replays";
stopWatch.Stop();
return;
}

stopWatch.Stop();
Replays = loadedReplays;
IsLoading = false;
}
Expand Down
6 changes: 5 additions & 1 deletion Server/Program.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System.Net;
using System.Text.Json.Serialization;
using Microsoft.AspNetCore.Http.Features;
using Microsoft.EntityFrameworkCore;
using Serilog;
Expand Down Expand Up @@ -30,7 +31,10 @@
builder.Configuration.AddJsonFile($"appsettings.{builder.Environment.EnvironmentName}.json", optional: true, reloadOnChange: true);
builder.Configuration.AddJsonFile("appsettings.Secret.json", optional: true, reloadOnChange: true);

builder.Services.AddControllersWithViews();
builder.Services.AddControllersWithViews().AddJsonOptions(options =>
{
options.JsonSerializerOptions.ReferenceHandler = ReferenceHandler.IgnoreCycles;
});
builder.Services.AddDbContext<ReplayDbContext>(options =>
{
options.UseNpgsql(builder.Configuration.GetConnectionString("DefaultConnection"));
Expand Down
23 changes: 19 additions & 4 deletions Server/ReplayParser.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
using System.Text.RegularExpressions;
using HtmlAgilityPack;
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Query;
using Serilog;
using Server.Api;
using Shared;
Expand Down Expand Up @@ -289,7 +290,9 @@ public static Replay ParseReplay(Stream fileStream)
public static List<Replay> SearchReplays(SearchMode mode, string query, ReplayDbContext context)
{
var queryable = context.Replays.AsQueryable();


IIncludableQueryable<Player, Replay?>? players;
IQueryable<int?>? replayIds;
switch (mode)
{
case SearchMode.Map:
Expand All @@ -299,11 +302,23 @@ public static List<Replay> SearchReplays(SearchMode mode, string query, ReplayDb
case SearchMode.ServerId:
return queryable.Where(x => x.ServerId.ToLower().Contains(query.ToLower())).ToList();
case SearchMode.Guid:
return queryable.Where(x => (x.RoundEndPlayers ?? new List<Player> { }).Any(y => y.PlayerGuid.ToString().Contains(query, StringComparison.CurrentCultureIgnoreCase))).ToList();
players = context.Players
.Where(p => p.PlayerGuid.ToString().ToLower().Contains(query.ToLower()))
.Include(p => p.Replay);
replayIds = players.Select(p => p.ReplayId).Distinct();
return context.Replays.Where(r => replayIds.Contains(r.Id)).ToList();
case SearchMode.PlayerIcName:
return queryable.Where(x => (x.RoundEndPlayers ?? new List<Player> { }).Any(y => y.PlayerIcName.ToLower().Contains(query.ToLower()))).ToList();
players = context.Players
.Where(p => p.PlayerIcName.ToLower().Contains(query.ToLower()))
.Include(p => p.Replay);
replayIds = players.Select(p => p.ReplayId).Distinct();
return context.Replays.Where(r => replayIds.Contains(r.Id)).ToList();
case SearchMode.PlayerOocName:
return queryable.Include(replay => replay.RoundEndPlayers).AsEnumerable().Where(x => (x.RoundEndPlayers ?? new List<Player> { }).Any(y => y.PlayerOocName.Contains(query, StringComparison.CurrentCultureIgnoreCase))).ToList();
players = Context.Players
.Where(p => p.PlayerOocName.ToLower().Contains(query.ToLower()))
.Include(p => p.Replay);
replayIds = players.Select(p => p.ReplayId).Distinct();
return Context.Replays.Where(r => replayIds.Contains(r.Id)).ToList();
case SearchMode.RoundEndText:
return queryable.Where(x => x.RoundEndText != null && x.RoundEndText.ToLower().Contains(query.ToLower())).ToList();
case SearchMode.ServerName:
Expand Down

0 comments on commit fc58f9c

Please sign in to comment.