Skip to content

Commit c532800

Browse files
authored
Merge pull request #49 from Starrah/import-chart-targetlevel
refactor&fix: 重构将maidata中的lv映射为游戏中的难度的逻辑。
2 parents 393fcf5 + 4f76275 commit c532800

2 files changed

Lines changed: 79 additions & 67 deletions

File tree

MaiChartManager/Controllers/Charts/ImportChartController.cs

Lines changed: 31 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -43,60 +43,52 @@ public ImportChartCheckResult ImportChartCheck(IFormFile file, [FromForm] bool i
4343
fatal = true;
4444
}
4545

46-
var levels = new bool[5];
4746
var allChartText = new Dictionary<int, string>();
48-
49-
for (var i = 0; i < 5; i++)
47+
for (var i = 0; i < 9; i++)
5048
{
51-
// maidata 里 2 是绿谱,6 是白谱
52-
if (!string.IsNullOrWhiteSpace(maiData.GetValueOrDefault($"inote_{i + 2}")))
49+
if (i == 1) continue; // maidata 中 inote_1 无对应游戏难度,与 ImportMaidata 保持一致
50+
if (!string.IsNullOrWhiteSpace(maiData.GetValueOrDefault($"inote_{i}")))
5351
{
54-
levels[i] = true;
55-
allChartText.Add(i + 2, maiData.GetValueOrDefault($"inote_{i + 2}"));
52+
allChartText.Add(i, maiData.GetValueOrDefault($"inote_{i}"));
5653
}
5754
}
55+
var targetLevelMap = MaidataImportService.MapMaidataLevelToGame(allChartText.Keys.ToList());
5856

59-
if (levels.Any(it => it))
57+
# region 向前端返回,关于导入谱面的inote_映射到游戏中的难度的提示信息
58+
string[] levelNames = [Locale.DifficultyBasic, Locale.DifficultyAdvanced, Locale.DifficultyExpert, Locale.DifficultyMaster, Locale.DifficultyReMaster];
59+
string[] importAsMessages = [Locale.DifficultyImportedAsBasic, null, null, Locale.DifficultyImportedAsMaster, Locale.DifficultyImportedAsReMaster];
60+
61+
string generalImportMessage = ""; // “将导入以下难度:” 的默认信息
62+
var extraImportMessages = new List<string>(); // “有一个难度为 {0} 的谱面,将导入为XX谱 ” 的信息
63+
foreach (var (lv, _) in allChartText)
6064
{
61-
string[] levelNames = [Locale.DifficultyBasic, Locale.DifficultyAdvanced, Locale.DifficultyExpert, Locale.DifficultyMaster, Locale.DifficultyReMaster];
62-
var message = Locale.ImportingDifficulties;
63-
for (var i = 0; i < 5; i++)
64-
{
65-
if (levels[i])
66-
{
67-
message += levelNames[i] + " ";
68-
}
65+
if (!targetLevelMap.TryGetValue(lv, out var targetLevel))
66+
{ // 根据targetLevelMap返回的结果,该谱面应被忽略
67+
extraImportMessages.Add(string.Format(Locale.DifficultyIgnored, lv));
68+
continue;
6969
}
70-
71-
errors.Add(new ImportChartMessage(message, MessageLevel.Info));
72-
}
73-
74-
foreach (var i in (int[])[7, 8, 0])
75-
{
76-
if (string.IsNullOrWhiteSpace(maiData.GetValueOrDefault($"inote_{i}"))) continue;
77-
allChartText.Add(i, maiData.GetValueOrDefault($"inote_{i}"));
78-
if (!levels[3])
70+
if (2 <= lv && lv <= 6)
7971
{
80-
levels[3] = true;
81-
errors.Add(new ImportChartMessage(string.Format(Locale.DifficultyImportedAsMaster, i), MessageLevel.Warning));
82-
}
83-
else if (!levels[4])
84-
{
85-
levels[4] = true;
86-
errors.Add(new ImportChartMessage(string.Format(Locale.DifficultyImportedAsReMaster, i), MessageLevel.Warning));
87-
}
88-
else if (!levels[0])
89-
{
90-
levels[0] = true;
91-
errors.Add(new ImportChartMessage(string.Format(Locale.DifficultyImportedAsBasic, i), MessageLevel.Warning));
72+
generalImportMessage += levelNames[targetLevel] + " ";
9273
}
9374
else
9475
{
95-
errors.Add(new ImportChartMessage(string.Format(Locale.DifficultyIgnored, i), MessageLevel.Warning));
76+
extraImportMessages.Add(string.Format(importAsMessages[targetLevel], lv));
9677
}
9778
}
79+
80+
if (!string.IsNullOrEmpty(generalImportMessage))
81+
{
82+
errors.Add(new ImportChartMessage(Locale.ImportingDifficulties + generalImportMessage, MessageLevel.Info));
83+
}
84+
85+
foreach (var message in extraImportMessages)
86+
{
87+
errors.Add(new ImportChartMessage(message, MessageLevel.Warning));
88+
}
89+
# endregion
9890

99-
if (!levels.Any(it => it))
91+
if (targetLevelMap.Count == 0) // 没有能够被映射的谱面
10092
{
10193
errors.Add(new ImportChartMessage(Locale.MusicNoCharts, MessageLevel.Fatal));
10294
fatal = true;

MaiChartManager/Services/MaidataImportService.cs

Lines changed: 48 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -270,6 +270,47 @@ private static string ApplyClockCountToFirstMetLine(string ma2Content, int clock
270270
return ma2Content;
271271
}
272272

273+
/** 根据maidata中定义的所有难度,将其映射到游戏中的难度。 **/
274+
public static Dictionary<int, int> MapMaidataLevelToGame(List<int> maidataLevels)
275+
{
276+
var result = new Dictionary<int, int>();
277+
var gameLevels = new bool[5];
278+
279+
// 先映射标准难度谱面 绿红黄紫白
280+
for (int lv = 2; lv <= 6; lv++)
281+
{
282+
if (!maidataLevels.Contains(lv)) continue;
283+
var targetLevel = lv - 2;
284+
result.Add(lv, targetLevel);
285+
gameLevels[targetLevel] = true;
286+
}
287+
288+
// 再映射非标准难度
289+
var nonStandardMappings = new[]
290+
{
291+
new { Levels = new[] { 7, 8 }, Targets = new[] { 3, 4, 0 } }, // lv7和8的匹配顺序:紫,白,绿
292+
new { Levels = new[] { 0 }, Targets = new[] { 0, 3, 4 } } // lv0的匹配顺序:绿,紫,白
293+
};
294+
foreach (var mapping in nonStandardMappings)
295+
{
296+
foreach (var lv in mapping.Levels)
297+
{
298+
if (!maidataLevels.Contains(lv)) continue;
299+
foreach (var targetLevel in mapping.Targets)
300+
{
301+
if (!gameLevels[targetLevel])
302+
{
303+
result.Add(lv, targetLevel);
304+
gameLevels[targetLevel] = true;
305+
break;
306+
}
307+
}
308+
}
309+
}
310+
311+
return result;
312+
}
313+
273314
public ImportChartResult ImportMaidata(
274315
MusicXml music,
275316
IFormFile file,
@@ -326,6 +367,7 @@ public ImportChartResult ImportMaidata(
326367
}
327368

328369
float bpm = 0f;
370+
var targetLevelMap = MapMaidataLevelToGame(allCharts.Keys.ToList());
329371
foreach (var (level, chart) in allCharts)
330372
{
331373
// 宴会场只导入第一个谱面
@@ -336,41 +378,19 @@ public ImportChartResult ImportMaidata(
336378
// 一个小节多少秒
337379
var bar = 60 / bpm * 4;
338380

339-
# region 设定 targetLevel
340-
341-
var targetLevel = level - 2;
342-
343-
// 处理非标准难度
344-
if (level is > 6 or < 1)
345-
{
346-
// 分给 3 4 0
347-
if (!music.Charts[3].Enable)
348-
{
349-
targetLevel = 3;
350-
}
351-
else if (!music.Charts[4].Enable)
352-
{
353-
targetLevel = 4;
354-
}
355-
else if (!music.Charts[0].Enable)
356-
{
357-
targetLevel = 0;
358-
}
359-
else
360-
{
361-
continue;
362-
}
363-
}
364-
381+
if (!targetLevelMap.TryGetValue(level, out var targetLevel)) continue; // 字典里没查到、说明这个难度是“被忽略的难度”
365382
if (isUtage) targetLevel = 0;
366383

367-
# endregion
368-
369384
var targetChart = music.Charts[targetLevel];
370385
targetChart.Path = $"{id:000000}_0{targetLevel}.ma2";
371386
var levelNumStr = maiData.GetValueOrDefault($"lv_{level}");
372387
if (!string.IsNullOrWhiteSpace(levelNumStr))
373388
{
389+
if (isUtage && !char.IsDigit(levelNumStr[0]))
390+
{
391+
music.UtageKanji = levelNumStr.Substring(0, 1);
392+
levelNumStr = levelNumStr.Substring(1).Replace("?", ""); // 为了处理类似“奏13+?”这种情况,留下13+给后面的逻辑处理
393+
}
374394
levelNumStr = levelNumStr.Replace("+", ".7");
375395
}
376396

0 commit comments

Comments
 (0)