This repository has been archived by the owner on Dec 21, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #95 from MaaAssistantArknights/feat/custom-level
- Loading branch information
Showing
22 changed files
with
1,520 additions
and
32 deletions.
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
154 changes: 154 additions & 0 deletions
154
...lotServer.Application/Arknights/AddOrUpdateCustomLevels/AddOrUpdateCustomLevelsCommand.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,154 @@ | ||
// This file is a part of MaaCopilotServer project. | ||
// MaaCopilotServer belongs to the MAA organization. | ||
// Licensed under the AGPL-3.0 license. | ||
|
||
using System.Text.Json.Serialization; | ||
using MaaCopilotServer.Application.Common.Helpers; | ||
using MaaCopilotServer.Domain.Entities; | ||
using MaaCopilotServer.Domain.Enums; | ||
using MaaCopilotServer.GameData; | ||
using Microsoft.EntityFrameworkCore; | ||
using ArkI18N = MaaCopilotServer.Domain.Entities.ArkI18N; | ||
|
||
namespace MaaCopilotServer.Application.Arknights.AddOrUpdateCustomLevels; | ||
|
||
public record AddOrUpdateCustomLevelsCommand | ||
{ | ||
/// <summary> | ||
/// Arknights level name. | ||
/// </summary> | ||
[JsonPropertyName("name")] | ||
public ArkI18NDto Name { get; set; } = new(); | ||
|
||
/// <summary> | ||
/// Arknights level category one. Typically zone name. | ||
/// </summary> | ||
[JsonPropertyName("cat_one")] | ||
public ArkI18NDto CatOne { get; set; } = new(); | ||
|
||
/// <summary> | ||
/// Arknights level category two. Typically chapter name. | ||
/// </summary> | ||
[JsonPropertyName("cat_two")] | ||
public ArkI18NDto CatTwo { get; set; } = new(); | ||
|
||
/// <summary> | ||
/// Arknights level category three. Typically level code. | ||
/// </summary> | ||
[JsonPropertyName("cat_three")] | ||
public ArkI18NDto CatThree { get; set; } = new(); | ||
|
||
/// <summary> | ||
/// Arknights level id. Custom levels will have `copilot-custom/` prefix added to their id automatically. | ||
/// </summary> | ||
[JsonPropertyName("level_id")] | ||
public string LevelId { get; set; } = string.Empty; | ||
|
||
/// <summary> | ||
/// Map width, defaults to 0. | ||
/// </summary> | ||
[JsonPropertyName("width")] | ||
public int Width { get; set; } = 0; | ||
|
||
/// <summary> | ||
/// Map height, defaults to 0. | ||
/// </summary> | ||
[JsonPropertyName("height")] | ||
public int Height { get; set; } = 0; | ||
} | ||
|
||
[Authorized(UserRole.Admin)] | ||
public record AddOrUpdateCustomLevelsCommandBatch : IRequest<MaaApiResponse> | ||
{ | ||
/// <summary> | ||
/// Custom levels to add or update. | ||
/// </summary> | ||
[JsonPropertyName("custom_levels")] | ||
// ReSharper disable once CollectionNeverUpdated.Global | ||
public List<AddOrUpdateCustomLevelsCommand> Commands { get; set; } = new(); | ||
} | ||
|
||
public class AddOrUpdateCustomLevelsCommandBatchHandler : IRequestHandler<AddOrUpdateCustomLevelsCommandBatch, MaaApiResponse> | ||
{ | ||
private readonly IMaaCopilotDbContext _dbContext; | ||
private readonly ICurrentUserService _currentUserService; | ||
|
||
public AddOrUpdateCustomLevelsCommandBatchHandler(IMaaCopilotDbContext dbContext, ICurrentUserService currentUserService) | ||
{ | ||
_dbContext = dbContext; | ||
_currentUserService = currentUserService; | ||
} | ||
|
||
public async Task<MaaApiResponse> Handle(AddOrUpdateCustomLevelsCommandBatch request, CancellationToken cancellationToken) | ||
{ | ||
var uid = _currentUserService.GetUserIdentity(); | ||
if (uid is null) | ||
{ | ||
return MaaApiResponseHelper.InternalError(); | ||
} | ||
|
||
var dbE = _dbContext.ArkLevelData | ||
.Include(x => x.Name) | ||
.Include(x => x.CatOne) | ||
.Include(x => x.CatTwo) | ||
.Include(x => x.CatThree) | ||
.Where(x => x.Custom == true) | ||
.ToList(); | ||
|
||
var pending = request.Commands.Select(x => | ||
new ArkLevelData(uid.Value) | ||
{ | ||
Name = x.Name.ToDomainEntityFromDto(), | ||
CatOne = x.CatOne.ToDomainEntityFromDto(), | ||
CatTwo = x.CatTwo.ToDomainEntityFromDto(), | ||
CatThree = x.CatThree.ToDomainEntityFromDto(), | ||
Height = x.Height, | ||
Width = x.Width, | ||
Custom = true, | ||
LevelId = $"copilot-custom/{x.LevelId}", | ||
Keyword = Helpers.BuildKeyword( | ||
x.Name.ToGameDataEntityFromDto(), | ||
x.CatOne.ToGameDataEntityFromDto(), | ||
x.CatTwo.ToGameDataEntityFromDto(), | ||
x.CatThree.ToGameDataEntityFromDto()) | ||
.ToDomainEntityFromGameDataEntity() | ||
}) | ||
.ToList(); | ||
|
||
var added = new List<string>(); | ||
var updated = new List<string>(); | ||
|
||
foreach (var command in pending) | ||
{ | ||
var exist = dbE.FirstOrDefault(x => x.LevelId == command.LevelId); | ||
if (exist is null) | ||
{ | ||
await _dbContext.ArkLevelData.AddAsync(command, cancellationToken); | ||
added.Add(command.LevelId); | ||
continue; | ||
} | ||
|
||
exist.Name.Update(command.Name.ToGameDataEntityFromDomainEntity()); | ||
exist.CatOne.Update(command.CatOne.ToGameDataEntityFromDomainEntity()); | ||
exist.CatTwo.Update(command.CatTwo.ToGameDataEntityFromDomainEntity()); | ||
exist.CatThree.Update(command.CatThree.ToGameDataEntityFromDomainEntity()); | ||
|
||
exist.Keyword ??= new ArkI18N(); | ||
exist.Keyword.Update(command.Keyword!.ToGameDataEntityFromDomainEntity()); | ||
|
||
exist.LevelId = command.LevelId; | ||
exist.Width = command.Width; | ||
exist.Height = command.Height; | ||
|
||
_dbContext.ArkLevelData.Update(exist); | ||
updated.Add(command.LevelId); | ||
} | ||
|
||
var changes = await _dbContext.SaveChangesAsync(cancellationToken); | ||
|
||
return MaaApiResponseHelper.Ok(new AddOrUpdateCustomLevelsDto | ||
{ | ||
DbContextChanges = changes, Added = added, Updated = updated | ||
}); | ||
} | ||
} |
28 changes: 28 additions & 0 deletions
28
...CopilotServer.Application/Arknights/AddOrUpdateCustomLevels/AddOrUpdateCustomLevelsDto.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
// This file is a part of MaaCopilotServer project. | ||
// MaaCopilotServer belongs to the MAA organization. | ||
// Licensed under the AGPL-3.0 license. | ||
|
||
using System.Text.Json.Serialization; | ||
|
||
namespace MaaCopilotServer.Application.Arknights.AddOrUpdateCustomLevels; | ||
|
||
public record AddOrUpdateCustomLevelsDto | ||
{ | ||
/// <summary> | ||
/// Total number of database changes. | ||
/// </summary> | ||
[JsonPropertyName("db_context_changes")] | ||
public int DbContextChanges { get; set; } | ||
|
||
/// <summary> | ||
/// Added levels. | ||
/// </summary> | ||
[JsonPropertyName("added")] | ||
public List<string> Added { get; set; } = new(); | ||
|
||
/// <summary> | ||
/// Updated levels. | ||
/// </summary> | ||
[JsonPropertyName("updated")] | ||
public List<string> Updated { get; set; } = new(); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
66 changes: 66 additions & 0 deletions
66
src/MaaCopilotServer.Application/Arknights/RemoveCustomLevels/RemoveCustomLevelsCommand.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
// This file is a part of MaaCopilotServer project. | ||
// MaaCopilotServer belongs to the MAA organization. | ||
// Licensed under the AGPL-3.0 license. | ||
|
||
using System.Text.Json.Serialization; | ||
using MaaCopilotServer.Application.Common.Helpers; | ||
using MaaCopilotServer.Domain.Enums; | ||
using Microsoft.EntityFrameworkCore; | ||
|
||
namespace MaaCopilotServer.Application.Arknights.RemoveCustomLevels; | ||
|
||
[Authorized(UserRole.SuperAdmin)] | ||
public record RemoveCustomLevelsCommand : IRequest<MaaApiResponse> | ||
{ | ||
/// <summary> | ||
/// A list of level ids that is pending to be removed. | ||
/// </summary> | ||
[JsonPropertyName("level_ids")] | ||
public List<string> LevelIds { get; set; } = new(); | ||
} | ||
|
||
public class RemoveCustomLevelsCommandHandler : IRequestHandler<RemoveCustomLevelsCommand, MaaApiResponse> | ||
{ | ||
private readonly IMaaCopilotDbContext _dbContext; | ||
private readonly ICurrentUserService _currentUserService; | ||
|
||
public RemoveCustomLevelsCommandHandler(IMaaCopilotDbContext dbContext, ICurrentUserService currentUserService) | ||
{ | ||
_dbContext = dbContext; | ||
_currentUserService = currentUserService; | ||
} | ||
|
||
public async Task<MaaApiResponse> Handle(RemoveCustomLevelsCommand request, CancellationToken cancellationToken) | ||
{ | ||
var uid = _currentUserService.GetUserIdentity(); | ||
if (uid is null) | ||
{ | ||
return MaaApiResponseHelper.InternalError(); | ||
} | ||
|
||
var removed = new List<string>(); | ||
|
||
foreach (var levelId in request.LevelIds) | ||
{ | ||
var exist = await _dbContext.ArkLevelData | ||
.Where(x => x.Custom) | ||
.FirstOrDefaultAsync(x => x.LevelId == $"copilot-custom/{levelId}", | ||
cancellationToken: cancellationToken); | ||
|
||
if (exist is null) | ||
{ | ||
continue; | ||
} | ||
|
||
exist.Delete(uid.Value); | ||
_dbContext.ArkLevelData.Remove(exist); | ||
removed.Add(exist.LevelId); | ||
} | ||
|
||
var changes = await _dbContext.SaveChangesAsync(cancellationToken); | ||
return MaaApiResponseHelper.Ok(new RemoveCustomLevelsDto | ||
{ | ||
DbContextChanges = changes, Removed = removed | ||
}); | ||
} | ||
} |
Oops, something went wrong.