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

Commit b8625bc

Browse files
authored
Merge pull request #87 from MaaAssistantArknights/feat/qol
修复和优化
2 parents 6a5e48b + df22d64 commit b8625bc

File tree

4 files changed

+97
-40
lines changed

4 files changed

+97
-40
lines changed

src/MaaCopilotServer.Application/Arknights/GetDataVersion/GetDataVersionQuery.cs

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -21,15 +21,24 @@ public class GetDataVersionQueryHandler : IRequestHandler<GetDataVersionQuery, M
2121
private static readonly ServerStatusDto s_syncNoError = new(string.Empty);
2222
private static readonly ServerStatusDto s_syncDisaster = new(SystemConstants.ARK_ASSET_CACHE_ERROR_DISASTER);
2323

24-
private static readonly Func<string, ServerStatusDto> s_buildSyncErrorDto = (str) =>
24+
private static readonly Func<string, ServerStatusDto> s_buildSyncErrorDto = str =>
2525
{
26-
var languages = str.Split(";");
27-
2826
var dto = new ServerStatusDto(string.Empty);
27+
if (string.IsNullOrEmpty(str))
28+
{
29+
return dto;
30+
}
31+
32+
var languages = str.Split(";");
2933

3034
foreach (var language in languages)
3135
{
32-
var lang = Enum.Parse<ArkServerLanguage>(language);
36+
var canParse = Enum.TryParse<ArkServerLanguage>(language, out var lang);
37+
if (canParse is false)
38+
{
39+
continue;
40+
}
41+
3342
switch (lang)
3443
{
3544
case ArkServerLanguage.ChineseSimplified:

src/MaaCopilotServer.Application/Common/Behaviours/PerformanceBehaviour.cs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -80,9 +80,11 @@ public async Task<MaaApiResponse> Handle(TRequest request, CancellationToken can
8080
_ => LoggingType.FailedRequest
8181
};
8282

83+
var doResponseLog = loggingType != LoggingType.Request;
84+
8385
_logger.Log(level,
84-
"MaaCopilotServer: Type -> {LoggingType}; Name -> {Name}; Time -> {ElapsedTime}; Status -> {StatusCode}, User -> {UserId}; Request -> {@Request}",
85-
loggingType, requestName, elapsedMilliseconds, statusCode, userId, request);
86+
"MaaCopilotServer: Type -> {LoggingType}; Name -> {Name}; Time -> {ElapsedTime}; Status -> {StatusCode}, User -> {UserId}; Request -> {@Request}; Response -> {@Response}",
87+
loggingType, requestName, elapsedMilliseconds, statusCode, userId, request, doResponseLog ? response : "Skipped");
8688
return response;
8789
}
8890
}

src/MaaCopilotServer.Application/Common/Enum/LoggingType.cs

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,4 +61,34 @@ public override string ToString()
6161
{
6262
return _value;
6363
}
64+
65+
/// <inheritdoc/>
66+
public override bool Equals(object? obj)
67+
{
68+
if (obj is LoggingType other)
69+
{
70+
return _value == other._value;
71+
}
72+
73+
return false;
74+
}
75+
76+
/// <summary>
77+
/// Check if two <see cref="LoggingType" /> are equal.
78+
/// </summary>
79+
/// <param name="other"></param>
80+
/// <returns></returns>
81+
public bool Equals(LoggingType other)
82+
{
83+
return _value == other._value;
84+
}
85+
86+
/// <inheritdoc/>
87+
public override int GetHashCode()
88+
{
89+
return _value.GetHashCode();
90+
}
91+
92+
public static bool operator ==(LoggingType? a, LoggingType? b) => a.Equals(b);
93+
public static bool operator !=(LoggingType? a, LoggingType? b) => !a.Equals(b);
6494
}

src/MaaCopilotServer.Infrastructure/Services/OperationProcessService.cs

Lines changed: 50 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

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

1618
namespace 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

Comments
 (0)