Skip to content

Commit ce58f50

Browse files
committed
refactor&fix: 重构将maidata中的lv映射为游戏中的难度的逻辑。
之前的逻辑是在ImportChartCheck和ImportChart中分别写的,甚至对同时标准难度和lv_8这种非标准难度的情况下,两边的处理甚至还不一致,造成ImportChartCheck返回给前端的信息和实际导入的结果不一致。
1 parent 10f63d1 commit ce58f50

2 files changed

Lines changed: 83 additions & 65 deletions

File tree

MaiChartManager/Controllers/Charts/ImportChartController.cs

Lines changed: 33 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -42,60 +42,56 @@ public ImportChartCheckResult ImportChartCheck(IFormFile file, [FromForm] bool i
4242
fatal = true;
4343
}
4444

45-
var levels = new bool[5];
4645
var allChartText = new Dictionary<int, string>();
47-
48-
for (var i = 0; i < 5; i++)
46+
for (var i = 2; i < 9; i++)
4947
{
50-
// maidata 里 2 是绿谱,6 是白谱
51-
if (!string.IsNullOrWhiteSpace(maiData.GetValueOrDefault($"inote_{i + 2}")))
48+
if (!string.IsNullOrWhiteSpace(maiData.GetValueOrDefault($"inote_{i}")))
5249
{
53-
levels[i] = true;
54-
allChartText.Add(i + 2, maiData.GetValueOrDefault($"inote_{i + 2}"));
50+
allChartText.Add(i, maiData.GetValueOrDefault($"inote_{i}"));
5551
}
5652
}
5753

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

98-
if (!levels.Any(it => it))
94+
if (targetLevelMap.Count == 0) // 没有能够被映射的谱面
9995
{
10096
errors.Add(new ImportChartMessage(Locale.MusicNoCharts, MessageLevel.Fatal));
10197
fatal = true;

MaiChartManager/Services/MaidataImportService.cs

Lines changed: 50 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -257,6 +257,54 @@ public static Dictionary<ShiftMethod, float> CalcChartPadding(List<MaiChart> cha
257257

258258
private record AllChartsEntry(string chartText, MaiChart simaiSharpChart);
259259

260+
/** 根据maidata中定义的所有难度,将其映射到游戏中的难度。 **/
261+
public Dictionary<int, int> mapMaidataLevelToGame(List<int> maidataLevels)
262+
{
263+
var result = new Dictionary<int, int>();
264+
var gameLevels = new bool[5];
265+
266+
// 先映射标准难度谱面 绿红黄紫白
267+
for (int lv = 2; lv <= 6; lv++)
268+
{
269+
if (!maidataLevels.Contains(lv)) continue;
270+
var targetLevel = lv - 2;
271+
result.Add(lv, targetLevel);
272+
gameLevels[targetLevel] = true;
273+
}
274+
275+
// 再映射78
276+
foreach (var lv in (int[])[7,8])
277+
{
278+
if (!maidataLevels.Contains(lv)) continue;
279+
foreach (var targetLevel in (int[])[3,4,0]) // 匹配顺序:紫,白,绿
280+
{
281+
if (!gameLevels[targetLevel])
282+
{
283+
result.Add(lv, targetLevel);
284+
gameLevels[targetLevel] = true;
285+
break;
286+
}
287+
}
288+
}
289+
290+
// 再映射0
291+
foreach (var lv in (int[])[0])
292+
{
293+
if (!maidataLevels.Contains(lv)) continue;
294+
foreach (var targetLevel in (int[])[0,3,4]) // 匹配顺序:绿,紫,白
295+
{
296+
if (!gameLevels[targetLevel])
297+
{
298+
result.Add(lv, targetLevel);
299+
gameLevels[targetLevel] = true;
300+
break;
301+
}
302+
}
303+
}
304+
305+
return result;
306+
}
307+
260308
public ImportChartResult ImportMaidata(
261309
MusicXml music,
262310
IFormFile file,
@@ -313,6 +361,7 @@ public ImportChartResult ImportMaidata(
313361
}
314362

315363
float bpm = 0f;
364+
var targetLevelMap = mapMaidataLevelToGame(allCharts.Keys.ToList());
316365
foreach (var (level, chart) in allCharts)
317366
{
318367
// 宴会场只导入第一个谱面
@@ -323,36 +372,9 @@ public ImportChartResult ImportMaidata(
323372
// 一个小节多少秒
324373
var bar = 60 / bpm * 4;
325374

326-
# region 设定 targetLevel
327-
328-
var targetLevel = level - 2;
329-
330-
// 处理非标准难度
331-
if (level is > 6 or < 1)
332-
{
333-
// 分给 3 4 0
334-
if (!music.Charts[3].Enable)
335-
{
336-
targetLevel = 3;
337-
}
338-
else if (!music.Charts[4].Enable)
339-
{
340-
targetLevel = 4;
341-
}
342-
else if (!music.Charts[0].Enable)
343-
{
344-
targetLevel = 0;
345-
}
346-
else
347-
{
348-
continue;
349-
}
350-
}
351-
375+
if (!targetLevelMap.TryGetValue(level, out var targetLevel)) continue; // 字典里没查到、说明这个难度是“被忽略的难度”
352376
if (isUtage) targetLevel = 0;
353377

354-
# endregion
355-
356378
var targetChart = music.Charts[targetLevel];
357379
targetChart.Path = $"{id:000000}_0{targetLevel}.ma2";
358380
var levelNumStr = maiData.GetValueOrDefault($"lv_{level}");

0 commit comments

Comments
 (0)