Skip to content

Commit c6c4b47

Browse files
committed
refactor: Introduce common models and refactor record conversion logic
- Add common model classes such as CommonChart, CommonSong, and Notes - Refactor record conversion logic in the DivingFish and Lxns modules using common models - Add cache dictionaries in SongData and Songs to improve query performance - Unify difficulty and rank mapping logic to eliminate duplicate code
1 parent a8d9331 commit c6c4b47

File tree

15 files changed

+213
-148
lines changed

15 files changed

+213
-148
lines changed

src/Prober/Common/CommonChart.cs

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
namespace Limekuma.Prober.Common;
2+
3+
public class CommonChart
4+
{
5+
public required CommonSong Song { get; init; }
6+
7+
public required CommonDifficulties Difficulty { get; init; }
8+
9+
public required int TotalDXScore { get; init; }
10+
11+
public required string Level { get; init; }
12+
13+
public required double LevelValue { get; init; }
14+
15+
public required Notes Notes { get; init; }
16+
}

src/Prober/Common/CommonRecord.cs

Lines changed: 2 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -2,20 +2,14 @@ namespace Limekuma.Prober.Common;
22

33
public record CommonRecord : IComparer<CommonRecord>
44
{
5-
public required int Id { get; init; }
6-
7-
public required string Title { get; init; }
8-
9-
public required CommonDifficulties Difficulty { get; init; }
5+
public required CommonChart Chart { get; init; }
106

117
public required ComboFlags ComboFlag { get; init; }
128

139
public required SyncFlags SyncFlag { get; init; }
1410

1511
public required Ranks Rank { get; init; }
1612

17-
public required CommonSongTypes Type { get; init; }
18-
1913
public required double Achievements { get; init; }
2014

2115
public required int DXScore { get; init; }
@@ -24,16 +18,6 @@ public record CommonRecord : IComparer<CommonRecord>
2418

2519
public required int DXRating { get; init; }
2620

27-
public required int TotalDXScore { get; init; }
28-
29-
public required double LevelValue { get; init; }
30-
31-
public required bool InCurrentVersion { get; init; }
32-
33-
public required string AudioUrl { get; init; }
34-
35-
public required string JacketUrl { get; init; }
36-
3721
public int Compare(CommonRecord? x, CommonRecord? y)
3822
{
3923
if (x is null && y is null)
@@ -57,7 +41,7 @@ public int Compare(CommonRecord? x, CommonRecord? y)
5741
return result;
5842
}
5943

60-
result = x.LevelValue.CompareTo(y.LevelValue);
44+
result = x.Chart.LevelValue.CompareTo(y.Chart.LevelValue);
6145
if (result is not 0)
6246
{
6347
return result;

src/Prober/Common/CommonSong.cs

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
namespace Limekuma.Prober.Common;
2+
3+
public class CommonSong
4+
{
5+
public required int Id { get; init; }
6+
7+
public required string Title { get; init; }
8+
9+
public required CommonSongTypes Type { get; init; }
10+
11+
public required string Genre { get; init; }
12+
13+
public required bool InCurrentGenre { get; init; }
14+
15+
public required string AudioUrl { get; init; }
16+
17+
public required string JacketUrl { get; init; }
18+
}
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
using System.Text.Json.Serialization;
22

3-
namespace Limekuma.Prober.Lxns.Models;
3+
namespace Limekuma.Prober.Common;
44

55
public record Notes
66
{
Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,23 @@
1+
using Limekuma.Prober.Common;
12
using System.Text.Json.Serialization;
23

34
namespace Limekuma.Prober.DivingFish.Models;
45

56
public record Chart
67
{
78
[JsonPropertyName("notes")]
8-
public required List<int> Notes { get; set; }
9+
public required List<int> NotesNumber { get; set; }
910

1011
[JsonPropertyName("charter")]
1112
public required string Charter { get; set; }
13+
14+
public Notes Notes => new()
15+
{
16+
Total = NotesNumber.Sum(),
17+
Tap = NotesNumber[0],
18+
Hold = NotesNumber[1],
19+
Slide = NotesNumber[2],
20+
Touch = NotesNumber.Count > 4 ? NotesNumber[3] : 0,
21+
Break = NotesNumber.Count < 5 ? NotesNumber[3] : NotesNumber[4],
22+
};
1223
}

src/Prober/DivingFish/Models/Record.cs

Lines changed: 53 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,17 @@ namespace Limekuma.Prober.DivingFish.Models;
77

88
public class Record
99
{
10+
private static CommonDifficulties MapDifficulty(Difficulties difficulty) => difficulty switch
11+
{
12+
Difficulties.Dummy => CommonDifficulties.Dummy,
13+
Difficulties.Basic => CommonDifficulties.Basic,
14+
Difficulties.Advanced => CommonDifficulties.Advanced,
15+
Difficulties.Expert => CommonDifficulties.Expert,
16+
Difficulties.Master => CommonDifficulties.Master,
17+
Difficulties.ReMaster => CommonDifficulties.ReMaster,
18+
_ => throw new InvalidDataException()
19+
};
20+
1021
[JsonPropertyName("achievements")]
1122
public required double Achievements { get; init; }
1223

@@ -53,69 +64,59 @@ public class Record
5364

5465
public string JacketUrl => $"https://maimai.diving-fish.com/covers/{Id}.png";
5566

56-
public Song Song
57-
{
58-
get
59-
{
60-
field ??= Songs.Shared.First(x => x.Id == Id.ToString());
61-
return field;
62-
}
63-
}
67+
private Lazy<Song>? _song;
6468

65-
public int TotalDXScore
66-
{
67-
get
68-
{
69-
if (field is 0)
70-
{
71-
field = Song.Charts[DifficultyIndex].Notes.Sum() * 3;
72-
}
69+
public Song Song => (_song ??= new(() => Songs.GetById(Id.ToString()))).Value;
7370

74-
return field;
75-
}
76-
}
71+
private Lazy<int>? _totalDXScore;
7772

78-
public int DXStar
79-
{
80-
get
81-
{
82-
if (field is not 0)
83-
{
84-
return field;
85-
}
73+
public int TotalDXScore => (_totalDXScore ??= new(() => Song.Charts[DifficultyIndex].Notes.Total * 3)).Value;
8674

87-
double dxScorePersent = (double)DXScore / TotalDXScore;
88-
field = dxScorePersent switch
89-
{
90-
< 0.9 => 1,
91-
< 0.93 => 2,
92-
< 0.95 => 3,
93-
< 0.97 => 4,
94-
<= 1 => 5,
95-
_ => throw new InvalidDataException()
96-
};
97-
return field;
98-
}
99-
}
75+
private Lazy<int>? _dxStar;
10076

101-
public static implicit operator CommonRecord(Record record) =>
102-
new()
77+
public int DXStar => (_dxStar ??= new(() => ((double)DXScore / TotalDXScore) switch
78+
{
79+
< 0.9 => 1,
80+
< 0.93 => 2,
81+
< 0.95 => 3,
82+
< 0.97 => 4,
83+
<= 1 => 5,
84+
_ => throw new InvalidDataException()
85+
})).Value;
86+
87+
public static implicit operator CommonRecord(Record record)
88+
{
89+
Song song = record.Song;
90+
Chart chart = song.Charts[record.DifficultyIndex];
91+
BasicInfo basicInfo = song.BasicInfo;
92+
93+
return new()
10394
{
104-
Id = record.Id,
105-
Title = record.Title,
106-
Difficulty = (CommonDifficulties)record.Difficulty,
95+
Chart = new()
96+
{
97+
Song = new()
98+
{
99+
Id = record.Id,
100+
Title = record.Title,
101+
Type = (CommonSongTypes)record.Type,
102+
Genre = basicInfo.Genre,
103+
InCurrentGenre = basicInfo.InCurrentVersion,
104+
AudioUrl = record.AudioUrl,
105+
JacketUrl = record.JacketUrl
106+
},
107+
Difficulty = MapDifficulty(record.Difficulty),
108+
TotalDXScore = record.TotalDXScore,
109+
Level = record.Level,
110+
LevelValue = record.LevelValue,
111+
Notes = chart.Notes
112+
},
107113
ComboFlag = record.ComboFlag,
108114
SyncFlag = record.SyncFlag,
109115
Rank = record.Rank,
110-
Type = (CommonSongTypes)record.Type,
111116
Achievements = record.Achievements,
112117
DXRating = record.DXRating,
113118
DXStar = record.DXStar,
114-
DXScore = record.DXScore,
115-
TotalDXScore = record.TotalDXScore,
116-
LevelValue = record.LevelValue,
117-
InCurrentVersion = record.Song.BasicInfo.InCurrentVersion,
118-
AudioUrl = record.AudioUrl,
119-
JacketUrl = record.JacketUrl
119+
DXScore = record.DXScore
120120
};
121+
}
121122
}

src/Prober/DivingFish/Models/Song.cs

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,14 +36,16 @@ internal record Songs
3636

3737
private readonly DateTimeOffset _pullTime;
3838
private readonly List<Song> _songs;
39+
private readonly Dictionary<string, Song> _songsById;
3940

4041
private Songs(List<Song> songs)
4142
{
4243
_pullTime = DateTimeOffset.Now;
4344
_songs = songs;
45+
_songsById = songs.ToDictionary(x => x.Id);
4446
}
4547

46-
public static List<Song> Shared
48+
private static Songs SharedSongs
4749
{
4850
get
4951
{
@@ -57,6 +59,11 @@ public static List<Song> Shared
5759
}
5860
}
5961

62+
public static List<Song> Shared => SharedSongs;
63+
64+
public static Song GetById(string id) =>
65+
SharedSongs._songsById.TryGetValue(id, out Song? song) ? song : throw new InvalidDataException();
66+
6067
public static implicit operator Songs(List<Song> songs) => new(songs);
6168

6269
public static implicit operator List<Song>(Songs a) => a._songs;

src/Prober/Lxns/Models/BuddyNotes.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
using Limekuma.Prober.Common;
12
using System.Text.Json.Serialization;
23

34
namespace Limekuma.Prober.Lxns.Models;

src/Prober/Lxns/Models/Chart.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
using Limekuma.Prober.Common;
12
using Limekuma.Prober.Lxns.Enums;
23
using System.Text.Json.Serialization;
34

src/Prober/Lxns/Models/Player.cs

Lines changed: 29 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,34 @@ namespace Limekuma.Prober.Lxns.Models;
66

77
public record Player
88
{
9+
private static CommonCourseRank MapCourseRank(CourseRank courseRank) => courseRank switch
10+
{
11+
CourseRank.Shoshinsha => CommonCourseRank.Shoshinsha,
12+
CourseRank.Shodan => CommonCourseRank.Shodan,
13+
CourseRank.Nidan => CommonCourseRank.Nidan,
14+
CourseRank.Sandan => CommonCourseRank.Sandan,
15+
CourseRank.Yondan => CommonCourseRank.Yondan,
16+
CourseRank.Godan => CommonCourseRank.Godan,
17+
CourseRank.Rokudan => CommonCourseRank.Rokudan,
18+
CourseRank.Shichidan => CommonCourseRank.Shichidan,
19+
CourseRank.Hachidan => CommonCourseRank.Hachidan,
20+
CourseRank.Kyudan => CommonCourseRank.Kyudan,
21+
CourseRank.Judan => CommonCourseRank.Judan,
22+
CourseRank.Shinshodan => CommonCourseRank.Shinshodan,
23+
CourseRank.Shinnidan => CommonCourseRank.Shinnidan,
24+
CourseRank.Shinsandan => CommonCourseRank.Shinsandan,
25+
CourseRank.Shinyondan => CommonCourseRank.Shinyondan,
26+
CourseRank.Shingodan => CommonCourseRank.Shingodan,
27+
CourseRank.Shinrokudan => CommonCourseRank.Shinrokudan,
28+
CourseRank.Shinshichidan => CommonCourseRank.Shinshichidan,
29+
CourseRank.Shinhachidan => CommonCourseRank.Shinhachidan,
30+
CourseRank.Shinkyudan => CommonCourseRank.Shinkyudan,
31+
CourseRank.Shinjudan => CommonCourseRank.Shinjudan,
32+
CourseRank.Shinkaiden => CommonCourseRank.Shinkaiden,
33+
CourseRank.Urakaiden => CommonCourseRank.Urakaiden,
34+
_ => throw new InvalidDataException()
35+
};
36+
937
public LxnsDeveloperClient? Client { get; internal set; }
1038

1139
[JsonPropertyName("name")]
@@ -64,9 +92,7 @@ public static implicit operator CommonUser(Player player) =>
6492
TrophyColor = player.Trophy?.Color ?? TrophyColor.Normal,
6593
TrophyText = player.Trophy?.Name ?? "なかよしmai友~!",
6694
ClassRank = player.ClassRank,
67-
CourseRank = (CommonCourseRank)(player.CourseRank < CourseRank.Shinshodan
68-
? player.CourseRank
69-
: player.CourseRank - 1),
95+
CourseRank = MapCourseRank(player.CourseRank),
7096
IconId = player.Icon?.Id ?? 458001,
7197
FrameId = player.Frame?.Id ?? 558001,
7298
PlateId = player.NamePlate?.Id ?? 458001

0 commit comments

Comments
 (0)