Skip to content

Commit

Permalink
Fix to use IntegerParser for th10
Browse files Browse the repository at this point in the history
  • Loading branch information
y-iihoshi committed Jan 1, 2025
1 parent 84bceef commit 8d2192b
Show file tree
Hide file tree
Showing 6 changed files with 27 additions and 14 deletions.
5 changes: 3 additions & 2 deletions ThScoreFileConverter/Models/Th10/CardReplacerBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -39,11 +39,12 @@ protected CardReplacerBase(
Func<TLevel, string> levelToString)
{
var numDigits = IntegerHelper.GetNumDigits(cardTable.Count);
var cardNumberParser = new IntegerParser($@"\d{{{numDigits}}}");

this.pattern = StringHelper.Create($@"{formatPrefix}CARD(\d{{{numDigits}}})([NR])");
this.pattern = StringHelper.Create($@"{formatPrefix}CARD({cardNumberParser.Pattern})([NR])");
this.evaluator = new MatchEvaluator(match =>
{
var number = IntegerHelper.Parse(match.Groups[1].Value);
var number = cardNumberParser.Parse(match.Groups[1]);
var type = match.Groups[2].Value.ToUpperInvariant();

if (cardTable.TryGetValue(number, out var cardInfo))
Expand Down
9 changes: 6 additions & 3 deletions ThScoreFileConverter/Models/Th10/CareerReplacerBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,9 @@ internal class CareerReplacerBase<TCharaWithTotal, TStage, TLevel> : IStringRepl
where TStage : struct, Enum
where TLevel : struct, Enum
{
private static readonly IntegerParser CardNumberParser = new(@"\d{3}");
private static readonly IntegerParser TypeParser = new(@"[12]");

private readonly string pattern;
private readonly MatchEvaluator evaluator;

Expand All @@ -32,12 +35,12 @@ protected CareerReplacerBase(
IReadOnlyDictionary<int, SpellCardInfo<TStage, TLevel>> cardTable,
INumberFormatter formatter)
{
this.pattern = StringHelper.Create($@"{formatPrefix}C(\d{{3}})({charaWithTotalParser.Pattern})([12])");
this.pattern = StringHelper.Create($@"{formatPrefix}C({CardNumberParser.Pattern})({charaWithTotalParser.Pattern})({TypeParser.Pattern})");
this.evaluator = new MatchEvaluator(match =>
{
var number = IntegerHelper.Parse(match.Groups[1].Value);
var number = CardNumberParser.Parse(match.Groups[1]);
var chara = charaWithTotalParser.Parse(match.Groups[2]);
var type = IntegerHelper.Parse(match.Groups[3].Value);
var type = TypeParser.Parse(match.Groups[3]);

Func<ISpellCard<Level>, int> getCount = type switch
{
Expand Down
6 changes: 4 additions & 2 deletions ThScoreFileConverter/Models/Th10/CharaExReplacerBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ namespace ThScoreFileConverter.Models.Th10;
internal class CharaExReplacerBase<TCharaWithTotal> : IStringReplaceable
where TCharaWithTotal : struct, Enum
{
private static readonly IntegerParser TypeParser = new(@"[1-3]");

private readonly string pattern;
private readonly MatchEvaluator evaluator;

Expand All @@ -32,12 +34,12 @@ protected CharaExReplacerBase(
INumberFormatter formatter)
{
this.pattern = StringHelper.Create(
$"{formatPrefix}CHARAEX({levelWithTotalParser.Pattern})({charaWithTotalParser.Pattern})([1-3])");
$"{formatPrefix}CHARAEX({levelWithTotalParser.Pattern})({charaWithTotalParser.Pattern})({TypeParser.Pattern})");
this.evaluator = new MatchEvaluator(match =>
{
var level = levelWithTotalParser.Parse(match.Groups[1]);
var chara = charaWithTotalParser.Parse(match.Groups[2]);
var type = IntegerHelper.Parse(match.Groups[3].Value);
var type = TypeParser.Parse(match.Groups[3]);

Func<IClearData<TCharaWithTotal>, long> getValueByType = (level, type) switch
{
Expand Down
6 changes: 4 additions & 2 deletions ThScoreFileConverter/Models/Th10/CharaReplacerBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ namespace ThScoreFileConverter.Models.Th10;
internal class CharaReplacerBase<TCharaWithTotal> : IStringReplaceable
where TCharaWithTotal : struct, Enum
{
private static readonly IntegerParser TypeParser = new(@"[1-3]");

private readonly string pattern;
private readonly MatchEvaluator evaluator;

Expand All @@ -29,11 +31,11 @@ protected CharaReplacerBase(
IReadOnlyDictionary<TCharaWithTotal, IClearData<TCharaWithTotal>> clearDataDictionary,
INumberFormatter formatter)
{
this.pattern = StringHelper.Create($"{formatPrefix}CHARA({charaWithTotalParser.Pattern})([1-3])");
this.pattern = StringHelper.Create($"{formatPrefix}CHARA({charaWithTotalParser.Pattern})({TypeParser.Pattern})");
this.evaluator = new MatchEvaluator(match =>
{
var chara = charaWithTotalParser.Parse(match.Groups[1]);
var type = IntegerHelper.Parse(match.Groups[2].Value);
var type = TypeParser.Parse(match.Groups[2]);

Func<IClearData<TCharaWithTotal>, long> getValueByType = type switch
{
Expand Down
6 changes: 4 additions & 2 deletions ThScoreFileConverter/Models/Th10/CollectRateReplacerBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ namespace ThScoreFileConverter.Models.Th10;
internal class CollectRateReplacerBase<TCharaWithTotal> : IStringReplaceable
where TCharaWithTotal : struct, Enum
{
private static readonly IntegerParser TypeParser = new(@"[12]");

private readonly string pattern;
private readonly MatchEvaluator evaluator;

Expand All @@ -32,13 +34,13 @@ protected CollectRateReplacerBase(
INumberFormatter formatter)
{
this.pattern = StringHelper.Create(
$"{formatPrefix}CRG({levelWithTotalParser.Pattern})({charaWithTotalParser.Pattern})({stageWithTotalParser.Pattern})([12])");
$"{formatPrefix}CRG({levelWithTotalParser.Pattern})({charaWithTotalParser.Pattern})({stageWithTotalParser.Pattern})({TypeParser.Pattern})");
this.evaluator = new MatchEvaluator(match =>
{
var level = levelWithTotalParser.Parse(match.Groups[1]);
var chara = charaWithTotalParser.Parse(match.Groups[2]);
var stage = stageWithTotalParser.Parse(match.Groups[3]);
var type = IntegerHelper.Parse(match.Groups[4].Value);
var type = TypeParser.Parse(match.Groups[4]);

if (stage == StageWithTotal.Extra)
return match.ToString();
Expand Down
9 changes: 6 additions & 3 deletions ThScoreFileConverter/Models/Th10/ScoreReplacerBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,9 @@ namespace ThScoreFileConverter.Models.Th10;
internal class ScoreReplacerBase<TChara> : IStringReplaceable
where TChara : struct, Enum
{
private static readonly IntegerParser RankParser = new(@"\d");
private static readonly IntegerParser TypeParser = new(@"[1-5]");

private readonly string pattern;
private readonly MatchEvaluator evaluator;

Expand All @@ -30,13 +33,13 @@ protected ScoreReplacerBase(
INumberFormatter formatter)
{
this.pattern = StringHelper.Create(
$@"{formatPrefix}SCR({levelParser.Pattern})({charaParser.Pattern})(\d)([1-5])");
$@"{formatPrefix}SCR({levelParser.Pattern})({charaParser.Pattern})({RankParser.Pattern})({TypeParser.Pattern})");
this.evaluator = new MatchEvaluator(match =>
{
var level = levelParser.Parse(match.Groups[1]);
var chara = charaParser.Parse(match.Groups[2]);
var rank = IntegerHelper.ToZeroBased(IntegerHelper.Parse(match.Groups[3].Value));
var type = IntegerHelper.Parse(match.Groups[4].Value);
var rank = IntegerHelper.ToZeroBased(RankParser.Parse(match.Groups[3]));
var type = TypeParser.Parse(match.Groups[4]);

var score = getScore(level, chara, rank);

Expand Down

0 comments on commit 8d2192b

Please sign in to comment.