Skip to content

Commit

Permalink
Adds functionality for "Find Traits" and "Find Names"
Browse files Browse the repository at this point in the history
(Only currently works with English text.)
  • Loading branch information
ronelm2000 committed Oct 21, 2024
1 parent 26ef3a9 commit 4d0b4f1
Show file tree
Hide file tree
Showing 4 changed files with 101 additions and 3 deletions.
34 changes: 34 additions & 0 deletions Montage.Weiss.Tools.GUI/ViewModels/CardEntryViewModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
using Avalonia.Threading;
using JasperFx.Core;
using Montage.Card.API.Entities;
using System.Diagnostics.CodeAnalysis;


namespace Montage.Weiss.Tools.GUI.ViewModels;
Expand Down Expand Up @@ -81,11 +82,14 @@ public partial class CardEntryViewModel : ViewModelBase
public IObservable<bool> IsCharacterOrEvent { get; private set; }

public ReactiveCommand<Unit, Unit> FindClimaxCombosCommand { get; private set; }
public ReactiveCommand<Unit, Unit> FindTraitsCommand { get; private set; }
public ReactiveCommand<Unit, Unit> FindNamesCommand { get; private set; }

public CardEntryViewModel(Uri imageUri, MultiLanguageString name, List<MultiLanguageString> traits)
{
DeclareObservables();

Card = new WeissSchwarzCard();
Name = name.AsNonEmptyString();
Traits = traits;
Serial = "XXX/WS01-100";
Expand Down Expand Up @@ -120,6 +124,14 @@ public CardEntryViewModel(WeissSchwarzCard card)
Effects = card.Effect.ToList();
}

[MemberNotNull(
nameof(IsCharacter),
nameof(IsCharacterOrEvent),
nameof(FindClimaxCombosCommand),
nameof(FindClimaxCombosCommand),
nameof(FindTraitsCommand),
nameof(FindNamesCommand)
)]
private void DeclareObservables()
{
IsCharacter = this.WhenPropertyChanged(c => c.CardType)
Expand All @@ -131,8 +143,30 @@ private void DeclareObservables()
.AsObservable();

FindClimaxCombosCommand = ReactiveCommand.CreateFromTask(FindClimaxCombos);
FindTraitsCommand = ReactiveCommand.CreateFromTask(FindTraits);
FindNamesCommand = ReactiveCommand.CreateFromTask(FindNames);

FindClimaxCombosCommand.ThrownExceptions.Subscribe(ReportException);
FindTraitsCommand.ThrownExceptions.Subscribe(ReportException);
FindNamesCommand.ThrownExceptions.Subscribe(ReportException);
}

private async Task FindNames()
{
if (Parent is null)
throw new NotImplementedException();

var newQuery = new NameQueryViewModel(Card);
await Dispatcher.UIThread.InvokeAsync(() => Parent.SearchQueries.Add(newQuery));
}

private async Task FindTraits()
{
if (Parent is null)
throw new NotImplementedException();

var newQuery = new TraitQueryViewModel(Card);
await Dispatcher.UIThread.InvokeAsync(() => Parent.SearchQueries.Add(newQuery));
}

private async Task FindClimaxCombos()
Expand Down
45 changes: 45 additions & 0 deletions Montage.Weiss.Tools.GUI/ViewModels/Query/NameQueryViewModel.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
using Montage.Card.API.Utilities;
using Montage.Weiss.Tools.Entities;
using Montage.Weiss.Tools.Utilities;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading.Tasks;

namespace Montage.Weiss.Tools.GUI.ViewModels.Query;
internal partial class NameQueryViewModel : CardSearchQueryViewModel
{
private List<string> _names;
private static readonly Regex _nameMatcher = NameMatcher();

public NameQueryViewModel(WeissSchwarzCard card)
{
_names = [
.. card.Effect.SelectMany(e => _nameMatcher.Matches(e).SelectMany(TranslateNameMatch))
];


var climaxesToSearchString = _names.ConcatAsString(", ");

Type = QueryType.ClimaxCombo;
DisplayText = climaxesToSearchString.Limit(10);
ToolTip = $"Finds any cards with names containing any of the following: {climaxesToSearchString}";
}

private IEnumerable<string> TranslateNameMatch(Match match)
{
yield return match.Groups[2].Value;
if (match.Groups[4].Success)
yield return match.Groups[4].Value;
}

public override Func<WeissSchwarzCard, bool> ToPredicate()
{
return c => _names.Any(n => (c.Name.EN?.Contains(n) ?? false) || (c.Name.JP?.Contains(n) ?? false));
}

[GeneratedRegex(@"([""])((?:(?=(?:\\)*)\\.|.)*?)(\((.*)\))?\1")]
private static partial Regex NameMatcher();
}
21 changes: 20 additions & 1 deletion Montage.Weiss.Tools.GUI/ViewModels/Query/TraitQueryViewModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,14 @@
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading.Tasks;

namespace Montage.Weiss.Tools.GUI.ViewModels.Query;
public class TraitQueryViewModel : CardSearchQueryViewModel
public partial class TraitQueryViewModel : CardSearchQueryViewModel
{
private List<string> _traits;
private static readonly Regex _traitMatcher = TraitMatcher();

public TraitQueryViewModel(IEnumerable<string> traits) : base()
{
Expand All @@ -23,8 +25,25 @@ public TraitQueryViewModel(IEnumerable<string> traits) : base()
ToolTip = $"Finds any cards with any of the following traits: {traitListString}";
}

public TraitQueryViewModel(WeissSchwarzCard card) : base()
{
_traits =
[
.. card.Effect.SelectMany(e => _traitMatcher.Matches(e).Select(m => m.Groups[1].Value)).Distinct()
];

var traitListString = _traits.ConcatAsString(", ");

Type = QueryType.Trait;
DisplayText = traitListString.Limit(10);
ToolTip = $"Finds any cards with any of the following traits: {traitListString}";
}

public override Func<WeissSchwarzCard, bool> ToPredicate()
{
return c => c.Traits.Any(t => (t.EN is not null && _traits.Contains(t.EN)) || (t.JP is not null && _traits.Contains(t.JP)));
}

[GeneratedRegex(@"(?:\:\:|«|<<|<)(.*?)(?:::|»|>>|>)")]
private static partial Regex TraitMatcher();
}
4 changes: 2 additions & 2 deletions Montage.Weiss.Tools.GUI/Views/MainView.axaml
Original file line number Diff line number Diff line change
Expand Up @@ -176,8 +176,8 @@
<MenuItem Header="Add"/>
<Separator />
<MenuItem Header="Find CXC" Command="{Binding FindClimaxCombosCommand}" />
<MenuItem Header="Find Traits" />
<MenuItem Header="Find Names" />
<MenuItem Header="Find Traits" Command="{Binding FindTraitsCommand}" />
<MenuItem Header="Find Names" Command="{Binding FindNamesCommand}" />
</ContextMenu>
</Border.ContextMenu>
<vuc:DatabaseCardViewPanel Click="DatabaseCardViewPanel_Click" />
Expand Down

0 comments on commit 4d0b4f1

Please sign in to comment.