Skip to content
Open
Show file tree
Hide file tree
Changes from 1 commit
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
15 changes: 8 additions & 7 deletions TPP.ArgsParsing/TypeParsers/ShinyParser.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,14 @@
using System.Linq;
using System.Threading.Tasks;
using TPP.Common;
using TPP.ArgsParsing.Types;

namespace TPP.ArgsParsing.TypeParsers
{
/// <summary>
/// A parser that finds a badge form by name.
/// A parser that determines if something is indicated to be shiny or not.
/// </summary>
public class ShinyParser : BaseArgumentParser<bool>
public class ShinyParser : BaseArgumentParser<Shiny>
{
string[] shinyWords =
{
Expand All @@ -22,21 +23,21 @@ public class ShinyParser : BaseArgumentParser<bool>
"regular",
"shiny:false"
};
public override Task<ArgsParseResult<bool>> Parse(IImmutableList<string> args, Type[] genericTypes)
public override Task<ArgsParseResult<Shiny>> Parse(IImmutableList<string> args, Type[] genericTypes)
{
string s = args[0];
ArgsParseResult<bool> result;
ArgsParseResult<Shiny> result;
if (shinyWords.Contains(s))
{
result = ArgsParseResult<bool>.Success(true, args.Skip(1).ToImmutableList());
result = ArgsParseResult<Shiny>.Success(new Shiny { Value = true }, args.Skip(1).ToImmutableList());
}
else if (plainWords.Contains(s))
{
result = ArgsParseResult<bool>.Success(false, args.Skip(1).ToImmutableList());
result = ArgsParseResult<Shiny>.Success(new Shiny { Value = false }, args.Skip(1).ToImmutableList());
}
else
{
result = ArgsParseResult<bool>.Failure("The argument couldn't be understood as shiny or not", ErrorRelevanceConfidence.Unlikely);
result = ArgsParseResult<Shiny>.Failure("The argument couldn't be understood as shiny or not", ErrorRelevanceConfidence.Unlikely);
}
return Task.FromResult(result);
}
Expand Down
19 changes: 19 additions & 0 deletions TPP.ArgsParsing/Types/ImplicitBoolean.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace TPP.ArgsParsing.Types
{
public class ImplicitBoolean
{
public bool Value { get; internal init; }
public static implicit operator bool(ImplicitBoolean b) => b.Value;
public override string ToString() => Value.ToString();
}

public class Shiny : ImplicitBoolean
{
}
}
6 changes: 3 additions & 3 deletions TPP.Core/Commands/Definitions/OperatorCommands.cs
Original file line number Diff line number Diff line change
Expand Up @@ -184,11 +184,11 @@ await _messageSender.SendWhisper(recipient, amount > 1

public async Task<CommandResult> CreateBadge(CommandContext context)
{
(User recipient, PkmnSpecies species, Optional<PositiveInt> amountOpt, Optional<string> formOpt, Optional<bool> shinyOpt) =
await context.ParseArgs<AnyOrder<User, PkmnSpecies, Optional<PositiveInt>, Optional<string>, Optional<bool>>>();
(User recipient, PkmnSpecies species, Optional<PositiveInt> amountOpt, Optional<string> formOpt, Optional<Shiny> shinyOpt) =
await context.ParseArgs<AnyOrder<User, PkmnSpecies, Optional<PositiveInt>, Optional<string>, Optional<Shiny>>>();
int amount = amountOpt.Map(i => i.Number).OrElse(1);
int form = PkmnForms.pokemonHasForms(species) ? 0 : 1; // default to the first listed form if form is unspecified
bool shiny = shinyOpt.IsPresent ? shinyOpt.Value : false;
bool shiny = shinyOpt.Value ?? false;
if (formOpt.IsPresent)
{
string formName = formOpt.Value;
Expand Down
6 changes: 4 additions & 2 deletions TPP.Persistence.MongoDB/Repos/BadgeRepo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,8 @@ static BadgeRepo()
cm.MapProperty(b => b.SellingSince).SetElementName("selling_since")
.SetIgnoreIfNull(true);
cm.MapProperty(b => b.Shiny).SetElementName("shiny")
.SetDefaultValue(false);
.SetDefaultValue(false)
.SetIgnoreIfDefault(true);
});
}

Expand Down Expand Up @@ -89,7 +90,7 @@ public async Task<List<Badge>> FindByUser(string? userId) =>
public async Task<List<Badge>> FindByUserAndSpecies(string? userId, PkmnSpecies species) =>
await Collection.Find(b => b.UserId == userId && b.Species == species).ToListAsync();

public async Task<List<Badge>> FindAllByCustom(string? userId = null, PkmnSpecies? species = null, int? form = null, Badge.BadgeSource? source = null)
public async Task<List<Badge>> FindAllByCustom(string? userId = null, PkmnSpecies? species = null, int? form = null, Badge.BadgeSource? source = null, bool? shiny = false)
{
FilterDefinition<Badge> filter = Builders<Badge>.Filter.Empty;
if (userId != null)
Expand All @@ -102,6 +103,7 @@ public async Task<List<Badge>> FindAllByCustom(string? userId = null, PkmnSpecie
filter &= Builders<Badge>.Filter.Eq(b => b.Form, form);
if (source != null)
filter &= Builders<Badge>.Filter.Eq(b => b.Source, source);
filter &= Builders<Badge>.Filter.Eq(b => b.Shiny, shiny); // always assigned, defaults to false

return await Collection.Find(filter).ToListAsync();
}
Expand Down
2 changes: 1 addition & 1 deletion TPP.Persistence/Repos/IBadgeRepo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ public Task<Badge> AddBadge(
string? userId, PkmnSpecies species, Badge.BadgeSource source, int form, bool shiny, Instant? createdAt = null);
public Task<List<Badge>> FindByUser(string? userId);
public Task<List<Badge>> FindByUserAndSpecies(string? userId, PkmnSpecies species);
public Task<List<Badge>> FindAllByCustom(string? userId = null, PkmnSpecies? species = null, int? form = null, Badge.BadgeSource? source = null);
public Task<List<Badge>> FindAllByCustom(string? userId = null, PkmnSpecies? species = null, int? form = null, Badge.BadgeSource? source = null, bool? shiny = false);
public Task<long> CountByUserAndSpecies(string? userId, PkmnSpecies species);
public Task<ImmutableSortedDictionary<PkmnSpecies, int>> CountByUserPerSpecies(string? userId);
public Task<bool> HasUserBadge(string? userId, PkmnSpecies species);
Expand Down