Skip to content

Commit

Permalink
Fix to use IntegerParser for th16.5
Browse files Browse the repository at this point in the history
  • Loading branch information
y-iihoshi committed Jan 1, 2025
1 parent 6f9b7e1 commit 4efabb8
Show file tree
Hide file tree
Showing 7 changed files with 30 additions and 15 deletions.
8 changes: 5 additions & 3 deletions ThScoreFileConverter/Models/Th165/CardReplacer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,21 +11,23 @@
using System.Linq;
using System.Text.RegularExpressions;
using ThScoreFileConverter.Core.Extensions;
using ThScoreFileConverter.Core.Models;
using ThScoreFileConverter.Helpers;

namespace ThScoreFileConverter.Models.Th165;

// %T165CARD[xx][y][z]
internal sealed class CardReplacer(IReadOnlyList<IScore> scores, bool hideUntriedCards) : IStringReplaceable
{
private static readonly IntegerParser TypeParser = new(@"[12]");
private static readonly string Pattern = StringHelper.Create(
$"{Definitions.FormatPrefix}CARD({Parsers.DayParser.Pattern})([1-7])([12])");
$"{Definitions.FormatPrefix}CARD({Parsers.DayParser.Pattern})({Parsers.SceneParser.Pattern})({TypeParser.Pattern})");

private readonly MatchEvaluator evaluator = new(match =>
{
var day = Parsers.DayParser.Parse(match.Groups[1]);
var scene = IntegerHelper.Parse(match.Groups[2].Value);
var type = IntegerHelper.Parse(match.Groups[3].Value);
var scene = Parsers.SceneParser.Parse(match.Groups[2]);
var type = TypeParser.Parse(match.Groups[3]);

var key = (day, scene);
if (!Definitions.SpellCards.TryGetValue(key, out var enemyCardPair))
Expand Down
6 changes: 4 additions & 2 deletions ThScoreFileConverter/Models/Th165/NicknameReplacer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,18 +9,20 @@

using System.Linq;
using System.Text.RegularExpressions;
using ThScoreFileConverter.Core.Models;
using ThScoreFileConverter.Helpers;

namespace ThScoreFileConverter.Models.Th165;

// %T165NICK[xx]
internal sealed class NicknameReplacer(IStatus status) : IStringReplaceable
{
private static readonly string Pattern = StringHelper.Create($@"{Definitions.FormatPrefix}NICK(\d{{2}})");
private static readonly IntegerParser NumberParser = new(@"\d{2}");
private static readonly string Pattern = StringHelper.Create($@"{Definitions.FormatPrefix}NICK({NumberParser.Pattern})");

private readonly MatchEvaluator evaluator = new(match =>
{
var number = IntegerHelper.Parse(match.Groups[1].Value);
var number = NumberParser.Parse(match.Groups[1]);

if ((number > 0) && (number <= Definitions.Nicknames.Count))
{
Expand Down
5 changes: 5 additions & 0 deletions ThScoreFileConverter/Models/Th165/Parsers.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,4 +18,9 @@ internal static class Parsers
/// Gets the parser of <see cref="Day"/>.
/// </summary>
public static Core.Models.EnumPatternParser<Day> DayParser { get; } = new();

/// <summary>
/// Gets the parser of scenes.
/// </summary>
public static Core.Models.IntegerParser SceneParser { get; } = new(@"[1-7]");
}
8 changes: 5 additions & 3 deletions ThScoreFileConverter/Models/Th165/ScoreReplacer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,21 +10,23 @@
using System.Collections.Generic;
using System.Linq;
using System.Text.RegularExpressions;
using ThScoreFileConverter.Core.Models;
using ThScoreFileConverter.Helpers;

namespace ThScoreFileConverter.Models.Th165;

// %T165SCR[xx][y][z]
internal sealed class ScoreReplacer(IReadOnlyList<IScore> scores, INumberFormatter formatter) : IStringReplaceable
{
private static readonly IntegerParser TypeParser = new(@"[1-4]");
private static readonly string Pattern = StringHelper.Create(
$"{Definitions.FormatPrefix}SCR({Parsers.DayParser.Pattern})([1-7])([1-4])");
$"{Definitions.FormatPrefix}SCR({Parsers.DayParser.Pattern})({Parsers.SceneParser.Pattern})({TypeParser.Pattern})");

private readonly MatchEvaluator evaluator = new(match =>
{
var day = Parsers.DayParser.Parse(match.Groups[1]);
var scene = IntegerHelper.Parse(match.Groups[2].Value);
var type = IntegerHelper.Parse(match.Groups[3].Value);
var scene = Parsers.SceneParser.Parse(match.Groups[2]);
var type = TypeParser.Parse(match.Groups[3]);

var key = (day, scene);
if (!Definitions.SpellCards.ContainsKey(key))
Expand Down
6 changes: 4 additions & 2 deletions ThScoreFileConverter/Models/Th165/ScoreTotalReplacer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
using System.Collections.Generic;
using System.Linq;
using System.Text.RegularExpressions;
using ThScoreFileConverter.Core.Models;
using ThScoreFileConverter.Helpers;

namespace ThScoreFileConverter.Models.Th165;
Expand All @@ -18,11 +19,12 @@ namespace ThScoreFileConverter.Models.Th165;
internal sealed class ScoreTotalReplacer(IReadOnlyList<IScore> scores, IStatus status, INumberFormatter formatter)
: IStringReplaceable
{
private static readonly string Pattern = StringHelper.Create($"{Definitions.FormatPrefix}SCRTL([1-6])");
private static readonly IntegerParser TypeParser = new(@"[1-6]");
private static readonly string Pattern = StringHelper.Create($"{Definitions.FormatPrefix}SCRTL({TypeParser.Pattern})");

private readonly MatchEvaluator evaluator = new(match =>
{
var type = IntegerHelper.Parse(match.Groups[1].Value);
var type = TypeParser.Parse(match.Groups[1]);

return type switch
{
Expand Down
8 changes: 5 additions & 3 deletions ThScoreFileConverter/Models/Th165/ShotExReplacer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
using System.Globalization;
using System.Linq;
using System.Text.RegularExpressions;
using ThScoreFileConverter.Core.Models;
using ThScoreFileConverter.Core.Models.Th165;
using ThScoreFileConverter.Helpers;

Expand All @@ -24,8 +25,9 @@ internal sealed class ShotExReplacer(
string outputFilePath)
: IStringReplaceable
{
private static readonly IntegerParser TypeParser = new(@"[1-9]");
private static readonly string Pattern = StringHelper.Create(
$"{Definitions.FormatPrefix}SHOTEX({Parsers.DayParser.Pattern})([1-7])([1-9])");
$"{Definitions.FormatPrefix}SHOTEX({Parsers.DayParser.Pattern})({Parsers.SceneParser.Pattern})({TypeParser.Pattern})");

private static readonly Func<IBestShotHeader, IReadOnlyList<Hashtag>> HashtagList =
header =>
Expand Down Expand Up @@ -96,8 +98,8 @@ internal sealed class ShotExReplacer(
private readonly MatchEvaluator evaluator = new(match =>
{
var day = Parsers.DayParser.Parse(match.Groups[1]);
var scene = IntegerHelper.Parse(match.Groups[2].Value);
var type = IntegerHelper.Parse(match.Groups[3].Value);
var scene = Parsers.SceneParser.Parse(match.Groups[2]);
var type = TypeParser.Parse(match.Groups[3]);

var key = (day, scene);
if (!Definitions.SpellCards.ContainsKey(key))
Expand Down
4 changes: 2 additions & 2 deletions ThScoreFileConverter/Models/Th165/ShotReplacer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,12 +20,12 @@ internal sealed class ShotReplacer(
: IStringReplaceable
{
private static readonly string Pattern = StringHelper.Create(
$"{Definitions.FormatPrefix}SHOT({Parsers.DayParser.Pattern})([1-7])");
$"{Definitions.FormatPrefix}SHOT({Parsers.DayParser.Pattern})({Parsers.SceneParser.Pattern})");

private readonly MatchEvaluator evaluator = new(match =>
{
var day = Parsers.DayParser.Parse(match.Groups[1]);
var scene = IntegerHelper.Parse(match.Groups[2].Value);
var scene = Parsers.SceneParser.Parse(match.Groups[2]);

var key = (day, scene);
if (!Definitions.SpellCards.TryGetValue(key, out var enemyCardPair))
Expand Down

0 comments on commit 4efabb8

Please sign in to comment.