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

Commit

Permalink
Merge pull request #87 from MaaAssistantArknights/feat/qol
Browse files Browse the repository at this point in the history
修复和优化
  • Loading branch information
AlisaAkiron authored Aug 20, 2022
2 parents 6a5e48b + df22d64 commit b8625bc
Show file tree
Hide file tree
Showing 4 changed files with 97 additions and 40 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -21,15 +21,24 @@ public class GetDataVersionQueryHandler : IRequestHandler<GetDataVersionQuery, M
private static readonly ServerStatusDto s_syncNoError = new(string.Empty);
private static readonly ServerStatusDto s_syncDisaster = new(SystemConstants.ARK_ASSET_CACHE_ERROR_DISASTER);

private static readonly Func<string, ServerStatusDto> s_buildSyncErrorDto = (str) =>
private static readonly Func<string, ServerStatusDto> s_buildSyncErrorDto = str =>
{
var languages = str.Split(";");

var dto = new ServerStatusDto(string.Empty);
if (string.IsNullOrEmpty(str))
{
return dto;
}

var languages = str.Split(";");

foreach (var language in languages)
{
var lang = Enum.Parse<ArkServerLanguage>(language);
var canParse = Enum.TryParse<ArkServerLanguage>(language, out var lang);
if (canParse is false)
{
continue;
}

switch (lang)
{
case ArkServerLanguage.ChineseSimplified:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -80,9 +80,11 @@ public async Task<MaaApiResponse> Handle(TRequest request, CancellationToken can
_ => LoggingType.FailedRequest
};

var doResponseLog = loggingType != LoggingType.Request;

_logger.Log(level,
"MaaCopilotServer: Type -> {LoggingType}; Name -> {Name}; Time -> {ElapsedTime}; Status -> {StatusCode}, User -> {UserId}; Request -> {@Request}",
loggingType, requestName, elapsedMilliseconds, statusCode, userId, request);
"MaaCopilotServer: Type -> {LoggingType}; Name -> {Name}; Time -> {ElapsedTime}; Status -> {StatusCode}, User -> {UserId}; Request -> {@Request}; Response -> {@Response}",
loggingType, requestName, elapsedMilliseconds, statusCode, userId, request, doResponseLog ? response : "Skipped");
return response;
}
}
30 changes: 30 additions & 0 deletions src/MaaCopilotServer.Application/Common/Enum/LoggingType.cs
Original file line number Diff line number Diff line change
Expand Up @@ -61,4 +61,34 @@ public override string ToString()
{
return _value;
}

/// <inheritdoc/>
public override bool Equals(object? obj)
{
if (obj is LoggingType other)
{
return _value == other._value;
}

return false;
}

/// <summary>
/// Check if two <see cref="LoggingType" /> are equal.
/// </summary>
/// <param name="other"></param>
/// <returns></returns>
public bool Equals(LoggingType other)
{
return _value == other._value;
}

/// <inheritdoc/>
public override int GetHashCode()
{
return _value.GetHashCode();
}

public static bool operator ==(LoggingType? a, LoggingType? b) => a.Equals(b);
public static bool operator !=(LoggingType? a, LoggingType? b) => !a.Equals(b);
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

using System.Text.Json;
using Json.Schema;
using System.Text.Json.Serialization;
using MaaCopilotServer.Application.Common.Interfaces;
using MaaCopilotServer.Application.Common.Models;
using MaaCopilotServer.Application.Common.Operation.Model;
Expand All @@ -12,6 +13,7 @@
using MaaCopilotServer.Resources;
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.Options;
using Action = MaaCopilotServer.Application.Common.Operation.Model.Action;

namespace MaaCopilotServer.Infrastructure.Services;

Expand All @@ -22,6 +24,11 @@ public class OperationProcessService : IOperationProcessService
private readonly JsonSchema _schema;
private readonly ValidationOptions _validationOptions;

private static readonly JsonSerializerOptions s_failedSerializerOptions = new()
{
DefaultIgnoreCondition = JsonIgnoreCondition.WhenWritingNull
};

public OperationProcessService(
IMaaCopilotDbContext dbContext,
ValidationErrorMessage validationErrorMessage,
Expand Down Expand Up @@ -88,8 +95,6 @@ public async Task<OperationValidationResult> Validate(string? operation)
};
}

var failed = false;

if (operationObj.Actions is null)
{
return new OperationValidationResult
Expand All @@ -101,46 +106,30 @@ public async Task<OperationValidationResult> Validate(string? operation)
};
}

foreach (var action in operationObj.Actions)
{
var type = GetTypeUnifiedString(action.Type);
var failedArea = operationObj.Actions
.Select(FailedCheck)
.ToList();

failed = type switch
{
// When type is "Deploy", operator name, deploy location and deploy direction could not be null.
"deploy" => action.Name is null || action.Location is null || DirectionIsValid(action.Direction) is false,
// When type is "Skill", operator name could not be null.
"skill" => action.Name is null,
// When type is "Retreat", operator name and deploy location could not be null at the same time.
"retreat" => action.Name is null && action.Location is null,
// When type is "Skill Usage", skill_usage could not be null.
"speedup" => failed,
"bullettime" => failed,
"skillusage" => action.SkillUsage is null,
"output" => action.Doc is null,
"skilldaemon" => failed,
_ => true
};
failedArea.RemoveAll(x => x is null);

if (failed)
if (failedArea.Count == 0)
{
return new OperationValidationResult
{
return new OperationValidationResult
{
IsValid = false,
Operation = null,
ErrorMessages = _validationErrorMessage.CopilotOperationJsonIsInvalid!,
ArkLevel = null
};
}
IsValid = true, Operation = operationObj, ErrorMessages = string.Empty, ArkLevel = level
};
}

var failedAreaMessage = $"{_validationErrorMessage.CopilotOperationJsonIsInvalid}\n{string.Join("\n", failedArea)}";

return new OperationValidationResult
{
IsValid = true,
Operation = operationObj,
ErrorMessages = string.Empty,
ArkLevel = level
IsValid = false,
Operation = null,
ErrorMessages = failedAreaMessage,
ArkLevel = null
};

}

private static string GetTypeUnifiedString(string? type) => type?.ToLower() switch
Expand All @@ -162,4 +151,31 @@ public async Task<OperationValidationResult> Validate(string? operation)
"左" or "右" or "上" or "下" or "无" => true,
_ => false
};

private static string? FailedCheck(Action action)
{
var type = GetTypeUnifiedString(action.Type);

// false => It's OK; true => Error;
var validationResult = type switch
{
// When type is "Deploy", operator name, deploy location and deploy direction could not be null.
"deploy" => action.Name is null || action.Location is null || DirectionIsValid(action.Direction) is false,
// When type is "Skill", operator name could not be null.
"skill" => action.Name is null,
// When type is "Retreat", operator name and deploy location could not be null at the same time.
"retreat" => action.Name is null && action.Location is null,
// When type is "Skill Usage", skill_usage could not be null.
"speedup" => false,
"bullettime" => false,
"skillusage" => action.SkillUsage is null,
"output" => action.Doc is null,
"skilldaemon" => false,
_ => true
};

return validationResult is false
? null
: JsonSerializer.Serialize(action, s_failedSerializerOptions);
}
}

0 comments on commit b8625bc

Please sign in to comment.