44
55using System . Text . Json ;
66using Json . Schema ;
7+ using System . Text . Json . Serialization ;
78using MaaCopilotServer . Application . Common . Interfaces ;
89using MaaCopilotServer . Application . Common . Models ;
910using MaaCopilotServer . Application . Common . Operation . Model ;
1213using MaaCopilotServer . Resources ;
1314using Microsoft . EntityFrameworkCore ;
1415using Microsoft . Extensions . Options ;
16+ using Action = MaaCopilotServer . Application . Common . Operation . Model . Action ;
1517
1618namespace MaaCopilotServer . Infrastructure . Services ;
1719
@@ -22,6 +24,11 @@ public class OperationProcessService : IOperationProcessService
2224 private readonly JsonSchema _schema ;
2325 private readonly ValidationOptions _validationOptions ;
2426
27+ private static readonly JsonSerializerOptions s_failedSerializerOptions = new ( )
28+ {
29+ DefaultIgnoreCondition = JsonIgnoreCondition . WhenWritingNull
30+ } ;
31+
2532 public OperationProcessService (
2633 IMaaCopilotDbContext dbContext ,
2734 ValidationErrorMessage validationErrorMessage ,
@@ -88,8 +95,6 @@ public async Task<OperationValidationResult> Validate(string? operation)
8895 } ;
8996 }
9097
91- var failed = false ;
92-
9398 if ( operationObj . Actions is null )
9499 {
95100 return new OperationValidationResult
@@ -101,46 +106,30 @@ public async Task<OperationValidationResult> Validate(string? operation)
101106 } ;
102107 }
103108
104- foreach ( var action in operationObj . Actions )
105- {
106- var type = GetTypeUnifiedString ( action . Type ) ;
109+ var failedArea = operationObj . Actions
110+ . Select ( FailedCheck )
111+ . ToList ( ) ;
107112
108- failed = type switch
109- {
110- // When type is "Deploy", operator name, deploy location and deploy direction could not be null.
111- "deploy" => action . Name is null || action . Location is null || DirectionIsValid ( action . Direction ) is false ,
112- // When type is "Skill", operator name could not be null.
113- "skill" => action . Name is null ,
114- // When type is "Retreat", operator name and deploy location could not be null at the same time.
115- "retreat" => action . Name is null && action . Location is null ,
116- // When type is "Skill Usage", skill_usage could not be null.
117- "speedup" => failed ,
118- "bullettime" => failed ,
119- "skillusage" => action . SkillUsage is null ,
120- "output" => action . Doc is null ,
121- "skilldaemon" => failed ,
122- _ => true
123- } ;
113+ failedArea . RemoveAll ( x => x is null ) ;
124114
125- if ( failed )
115+ if ( failedArea . Count == 0 )
116+ {
117+ return new OperationValidationResult
126118 {
127- return new OperationValidationResult
128- {
129- IsValid = false ,
130- Operation = null ,
131- ErrorMessages = _validationErrorMessage . CopilotOperationJsonIsInvalid ! ,
132- ArkLevel = null
133- } ;
134- }
119+ IsValid = true , Operation = operationObj , ErrorMessages = string . Empty , ArkLevel = level
120+ } ;
135121 }
136122
123+ var failedAreaMessage = $ "{ _validationErrorMessage . CopilotOperationJsonIsInvalid } \n { string . Join ( "\n " , failedArea ) } ";
124+
137125 return new OperationValidationResult
138126 {
139- IsValid = true ,
140- Operation = operationObj ,
141- ErrorMessages = string . Empty ,
142- ArkLevel = level
127+ IsValid = false ,
128+ Operation = null ,
129+ ErrorMessages = failedAreaMessage ,
130+ ArkLevel = null
143131 } ;
132+
144133 }
145134
146135 private static string GetTypeUnifiedString ( string ? type ) => type ? . ToLower ( ) switch
@@ -162,4 +151,31 @@ public async Task<OperationValidationResult> Validate(string? operation)
162151 "左" or "右" or "上" or "下" or "无" => true ,
163152 _ => false
164153 } ;
154+
155+ private static string ? FailedCheck ( Action action )
156+ {
157+ var type = GetTypeUnifiedString ( action . Type ) ;
158+
159+ // false => It's OK; true => Error;
160+ var validationResult = type switch
161+ {
162+ // When type is "Deploy", operator name, deploy location and deploy direction could not be null.
163+ "deploy" => action . Name is null || action . Location is null || DirectionIsValid ( action . Direction ) is false ,
164+ // When type is "Skill", operator name could not be null.
165+ "skill" => action . Name is null ,
166+ // When type is "Retreat", operator name and deploy location could not be null at the same time.
167+ "retreat" => action . Name is null && action . Location is null ,
168+ // When type is "Skill Usage", skill_usage could not be null.
169+ "speedup" => false ,
170+ "bullettime" => false ,
171+ "skillusage" => action . SkillUsage is null ,
172+ "output" => action . Doc is null ,
173+ "skilldaemon" => false ,
174+ _ => true
175+ } ;
176+
177+ return validationResult is false
178+ ? null
179+ : JsonSerializer . Serialize ( action , s_failedSerializerOptions ) ;
180+ }
165181}
0 commit comments