Skip to content
This repository was archived by the owner on Dec 21, 2022. It is now read-only.

Commit c086dbb

Browse files
committed
perf: 优化 JSON 校验失败的返回值
1 parent 39477ff commit c086dbb

File tree

1 file changed

+51
-35
lines changed

1 file changed

+51
-35
lines changed

src/MaaCopilotServer.Infrastructure/Services/OperationProcessService.cs

Lines changed: 51 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
// Licensed under the AGPL-3.0 license.
44

55
using System.Text.Json;
6+
using System.Text.Json.Serialization;
67
using MaaCopilotServer.Application.Common.Interfaces;
78
using MaaCopilotServer.Application.Common.Models;
89
using MaaCopilotServer.Application.Common.Operation.Model;
@@ -12,6 +13,7 @@
1213
using Microsoft.EntityFrameworkCore;
1314
using Microsoft.Extensions.Options;
1415
using NJsonSchema;
16+
using Action = MaaCopilotServer.Application.Common.Operation.Model.Action;
1517

1618
namespace MaaCopilotServer.Infrastructure.Services;
1719

@@ -21,6 +23,11 @@ public class OperationProcessService : IOperationProcessService
2123
private readonly ValidationErrorMessage _validationErrorMessage;
2224
private readonly JsonSchema _schema;
2325

26+
private static readonly JsonSerializerOptions s_failedSerializerOptions = new()
27+
{
28+
DefaultIgnoreCondition = JsonIgnoreCondition.WhenWritingNull
29+
};
30+
2431
public OperationProcessService(
2532
IMaaCopilotDbContext dbContext,
2633
ValidationErrorMessage validationErrorMessage,
@@ -73,7 +80,7 @@ public async Task<OperationValidationResult> Validate(string? operation)
7380
};
7481
}
7582

76-
var levelId = operationObj!.StageName;
83+
var levelId = operationObj.StageName;
7784
var level = await _dbContext.ArkLevelData.FirstOrDefaultAsync(x => x.LevelId == levelId);
7885
if (level is null)
7986
{
@@ -86,8 +93,6 @@ public async Task<OperationValidationResult> Validate(string? operation)
8693
};
8794
}
8895

89-
var failed = false;
90-
9196
if (operationObj.Actions is null)
9297
{
9398
return new OperationValidationResult
@@ -99,46 +104,30 @@ public async Task<OperationValidationResult> Validate(string? operation)
99104
};
100105
}
101106

102-
foreach (var action in operationObj.Actions)
103-
{
104-
var type = GetTypeUnifiedString(action.Type);
107+
var failedArea = operationObj.Actions
108+
.Select(FailedCheck)
109+
.ToList();
105110

106-
failed = type switch
107-
{
108-
// When type is "Deploy", operator name, deploy location and deploy direction could not be null.
109-
"deploy" => action.Name is null || action.Location is null || DirectionIsValid(action.Direction) is false,
110-
// When type is "Skill", operator name could not be null.
111-
"skill" => action.Name is null,
112-
// When type is "Retreat", operator name and deploy location could not be null at the same time.
113-
"retreat" => action.Name is null && action.Location is null,
114-
// When type is "Skill Usage", skill_usage could not be null.
115-
"speedup" => failed,
116-
"bullettime" => failed,
117-
"skillusage" => action.SkillUsage is null,
118-
"output" => action.Doc is null,
119-
"skilldaemon" => failed,
120-
_ => true
121-
};
111+
failedArea.RemoveAll(x => x is null);
122112

123-
if (failed)
113+
if (failedArea.Count == 0)
114+
{
115+
return new OperationValidationResult
124116
{
125-
return new OperationValidationResult
126-
{
127-
IsValid = false,
128-
Operation = null,
129-
ErrorMessages = _validationErrorMessage.CopilotOperationJsonIsInvalid!,
130-
ArkLevel = null
131-
};
132-
}
117+
IsValid = true, Operation = operationObj, ErrorMessages = string.Empty, ArkLevel = level
118+
};
133119
}
134120

121+
var failedAreaMessage = $"{_validationErrorMessage.CopilotOperationJsonIsInvalid}\n{string.Join("\n", failedArea)}";
122+
135123
return new OperationValidationResult
136124
{
137-
IsValid = true,
138-
Operation = operationObj,
139-
ErrorMessages = string.Empty,
140-
ArkLevel = level
125+
IsValid = false,
126+
Operation = null,
127+
ErrorMessages = failedAreaMessage,
128+
ArkLevel = null
141129
};
130+
142131
}
143132

144133
private static string GetTypeUnifiedString(string? type) => type?.ToLower() switch
@@ -160,4 +149,31 @@ public async Task<OperationValidationResult> Validate(string? operation)
160149
"左" or "右" or "上" or "下" or "无" => true,
161150
_ => false
162151
};
152+
153+
private static string? FailedCheck(Action action)
154+
{
155+
var type = GetTypeUnifiedString(action.Type);
156+
157+
// false => It's OK; true => Error;
158+
var validationResult = type switch
159+
{
160+
// When type is "Deploy", operator name, deploy location and deploy direction could not be null.
161+
"deploy" => action.Name is null || action.Location is null || DirectionIsValid(action.Direction) is false,
162+
// When type is "Skill", operator name could not be null.
163+
"skill" => action.Name is null,
164+
// When type is "Retreat", operator name and deploy location could not be null at the same time.
165+
"retreat" => action.Name is null && action.Location is null,
166+
// When type is "Skill Usage", skill_usage could not be null.
167+
"speedup" => false,
168+
"bullettime" => false,
169+
"skillusage" => action.SkillUsage is null,
170+
"output" => action.Doc is null,
171+
"skilldaemon" => false,
172+
_ => true
173+
};
174+
175+
return validationResult is false
176+
? null
177+
: JsonSerializer.Serialize(action, s_failedSerializerOptions);
178+
}
163179
}

0 commit comments

Comments
 (0)