Skip to content

Commit

Permalink
chore: tidy up
Browse files Browse the repository at this point in the history
  • Loading branch information
GZTimeWalker committed Feb 11, 2024
1 parent 1667cf7 commit 490a162
Show file tree
Hide file tree
Showing 31 changed files with 122 additions and 93 deletions.
27 changes: 15 additions & 12 deletions src/GZCTF/Controllers/GameController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -921,7 +921,8 @@ public async Task<IActionResult> CreateContainer([FromRoute] int id, [FromRoute]

if (DateTimeOffset.UtcNow - instance.LastContainerOperation < TimeSpan.FromSeconds(10))
return new JsonResult(new RequestResponse(localizer[nameof(Resources.Program.Game_OperationTooFrequent)],
StatusCodes.Status429TooManyRequests)) { StatusCode = StatusCodes.Status429TooManyRequests };
StatusCodes.Status429TooManyRequests))
{ StatusCode = StatusCodes.Status429TooManyRequests };

if (instance.Container is not null)
{
Expand All @@ -934,15 +935,15 @@ public async Task<IActionResult> CreateContainer([FromRoute] int id, [FromRoute]

return await gameInstanceRepository.CreateContainer(instance, context.Participation!.Team, context.User!,
context.Game!.ContainerCountLimit, token) switch
{
null or (TaskStatus.Failed, null) => BadRequest(
new RequestResponse(localizer[nameof(Resources.Program.Game_ContainerCreationFailed)])),
(TaskStatus.Denied, null) => BadRequest(
new RequestResponse(localizer[nameof(Resources.Program.Game_ContainerNumberLimitExceeded),
context.Game.ContainerCountLimit])),
(TaskStatus.Success, var x) => Ok(ContainerInfoModel.FromContainer(x!)),
_ => throw new UnreachableException()
};
{
null or (TaskStatus.Failed, null) => BadRequest(
new RequestResponse(localizer[nameof(Resources.Program.Game_ContainerCreationFailed)])),
(TaskStatus.Denied, null) => BadRequest(
new RequestResponse(localizer[nameof(Resources.Program.Game_ContainerNumberLimitExceeded),
context.Game.ContainerCountLimit])),
(TaskStatus.Success, var x) => Ok(ContainerInfoModel.FromContainer(x!)),
_ => throw new UnreachableException()
};
}

/// <summary>
Expand Down Expand Up @@ -1035,7 +1036,8 @@ public async Task<IActionResult> DeleteContainer([FromRoute] int id, [FromRoute]

if (DateTimeOffset.UtcNow - instance.LastContainerOperation < TimeSpan.FromSeconds(10))
return new JsonResult(new RequestResponse(localizer[nameof(Resources.Program.Game_OperationTooFrequent)],
StatusCodes.Status429TooManyRequests)) { StatusCode = StatusCodes.Status429TooManyRequests };
StatusCodes.Status429TooManyRequests))
{ StatusCode = StatusCodes.Status429TooManyRequests };

var destroyId = instance.Container.ContainerId;

Expand Down Expand Up @@ -1068,7 +1070,8 @@ async Task<ContextInfo> GetContextInfo(int id, int challengeId = 0, bool withFla
{
ContextInfo res = new()
{
User = await userManager.GetUserAsync(User), Game = await gameRepository.GetGameById(id, token)
User = await userManager.GetUserAsync(User),
Game = await gameRepository.GetGameById(id, token)
};

if (res.Game is null)
Expand Down
3 changes: 2 additions & 1 deletion src/GZCTF/Controllers/ProxyController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -173,7 +173,8 @@ async Task<IActionResult> DoContainerProxy(Guid id, IPEndPoint client, IPEndPoin
TaskStatus.Failed, LogLevel.Debug);
return new JsonResult(new RequestResponse(
localizer[nameof(Resources.Program.Proxy_ContainerConnectionFailed), e.SocketErrorCode],
StatusCodes.Status418ImATeapot)) { StatusCode = StatusCodes.Status418ImATeapot };
StatusCodes.Status418ImATeapot))
{ StatusCode = StatusCodes.Status418ImATeapot };
}

using WebSocket ws = await HttpContext.WebSockets.AcceptWebSocketAsync();
Expand Down
21 changes: 14 additions & 7 deletions src/GZCTF/Controllers/TeamController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -144,7 +144,8 @@ public async Task<IActionResult> UpdateTeam([FromRoute] int id, [FromBody] TeamU

if (team.CaptainId != user!.Id)
return new JsonResult(new RequestResponse(localizer[nameof(Resources.Program.Auth_AccessForbidden)],
StatusCodes.Status403Forbidden)) { StatusCode = StatusCodes.Status403Forbidden };
StatusCodes.Status403Forbidden))
{ StatusCode = StatusCodes.Status403Forbidden };

team.UpdateInfo(model);

Expand Down Expand Up @@ -183,7 +184,8 @@ public async Task<IActionResult> Transfer([FromRoute] int id, [FromBody] TeamTra

if (team.CaptainId != user!.Id)
return new JsonResult(new RequestResponse(localizer[nameof(Resources.Program.Auth_AccessForbidden)],
StatusCodes.Status403Forbidden)) { StatusCode = StatusCodes.Status403Forbidden };
StatusCodes.Status403Forbidden))
{ StatusCode = StatusCodes.Status403Forbidden };

if (team.Locked && await teamRepository.AnyActiveGame(team, token))
return BadRequest(new RequestResponse(localizer[nameof(Resources.Program.Team_Locked)]));
Expand Down Expand Up @@ -231,7 +233,8 @@ public async Task<IActionResult> InviteCode([FromRoute] int id, CancellationToke

if (team.CaptainId != user!.Id)
return new JsonResult(new RequestResponse(localizer[nameof(Resources.Program.Auth_AccessForbidden)],
StatusCodes.Status403Forbidden)) { StatusCode = StatusCodes.Status403Forbidden };
StatusCodes.Status403Forbidden))
{ StatusCode = StatusCodes.Status403Forbidden };

return Ok(team.InviteCode);
}
Expand Down Expand Up @@ -264,7 +267,8 @@ public async Task<IActionResult> UpdateInviteToken([FromRoute] int id, Cancellat

if (team.CaptainId != user!.Id)
return new JsonResult(new RequestResponse(localizer[nameof(Resources.Program.Auth_AccessForbidden)],
StatusCodes.Status403Forbidden)) { StatusCode = StatusCodes.Status403Forbidden };
StatusCodes.Status403Forbidden))
{ StatusCode = StatusCodes.Status403Forbidden };

team.UpdateInviteToken();

Expand Down Expand Up @@ -302,7 +306,8 @@ public async Task<IActionResult> KickUser([FromRoute] int id, [FromRoute] Guid u

if (team.CaptainId != user!.Id)
return new JsonResult(new RequestResponse(localizer[nameof(Resources.Program.Auth_AccessForbidden)],
StatusCodes.Status403Forbidden)) { StatusCode = StatusCodes.Status403Forbidden };
StatusCodes.Status403Forbidden))
{ StatusCode = StatusCodes.Status403Forbidden };

IDbContextTransaction trans = await teamRepository.BeginTransactionAsync(token);

Expand Down Expand Up @@ -488,7 +493,8 @@ public async Task<IActionResult> Avatar([FromRoute] int id, IFormFile file, Canc

if (team.CaptainId != user!.Id)
return new JsonResult(new RequestResponse(localizer[nameof(Resources.Program.Auth_AccessForbidden)],
StatusCodes.Status403Forbidden)) { StatusCode = StatusCodes.Status403Forbidden };
StatusCodes.Status403Forbidden))
{ StatusCode = StatusCodes.Status403Forbidden };

if (file.Length == 0)
return BadRequest(new RequestResponse(localizer[nameof(Resources.Program.File_SizeZero)]));
Expand Down Expand Up @@ -540,7 +546,8 @@ public async Task<IActionResult> DeleteTeam(int id, CancellationToken token)

if (team.CaptainId != user!.Id)
return new JsonResult(new RequestResponse(localizer[nameof(Resources.Program.Auth_AccessForbidden)],
StatusCodes.Status403Forbidden)) { StatusCode = StatusCodes.Status403Forbidden };
StatusCodes.Status403Forbidden))
{ StatusCode = StatusCodes.Status403Forbidden };

if (team.Locked && await teamRepository.AnyActiveGame(team, token))
return BadRequest(new RequestResponse(localizer[nameof(Resources.Program.Team_Locked)]));
Expand Down
4 changes: 3 additions & 1 deletion src/GZCTF/Extensions/CaptchaExtension.cs
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,9 @@ public override async Task<bool> VerifyAsync(ModelWithCaptcha model, HttpContext

TurnstileRequestModel req = new()
{
Secret = Config.SecretKey, Response = model.Challenge, RemoteIp = ip.ToString()
Secret = Config.SecretKey,
Response = model.Challenge,
RemoteIp = ip.ToString()
};

const string api = "https://challenges.cloudflare.com/turnstile/v0/siteverify";
Expand Down
4 changes: 2 additions & 2 deletions src/GZCTF/Models/AppDbContext.cs
Original file line number Diff line number Diff line change
Expand Up @@ -317,7 +317,7 @@ protected override void OnModelCreating(ModelBuilder builder)

entity.Navigation(e => e.LocalFile).AutoInclude();
});

builder.Entity<GameNotice>(entity =>
{
entity.Property(e => e.Values)
Expand All @@ -332,7 +332,7 @@ protected override void OnModelCreating(ModelBuilder builder)
.HasConversion(listConverter)
.Metadata
.SetValueComparer(listComparer);

entity.HasOne(e => e.Team)
.WithMany()
.HasForeignKey(e => e.TeamId);
Expand Down
7 changes: 2 additions & 5 deletions src/GZCTF/Models/Data/Challenge.cs
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,7 @@ public class Challenge
/// <summary>
/// 题目对应的 Flag 列表
/// </summary>
public List<FlagContext> Flags { get; set; } = new();
public List<FlagContext> Flags { get; set; } = [];

/// <summary>
/// 为参赛对象生成动态 Flag
Expand Down Expand Up @@ -191,10 +191,7 @@ internal string GenerateTestFlag()
if (FlagTemplate.Contains("[GUID]"))
return FlagTemplate.Replace("[GUID]", Guid.NewGuid().ToString("D"));

if (FlagTemplate.StartsWith("[LEET]"))
return Codec.Leet.LeetFlag(FlagTemplate[6..]);

return Codec.Leet.LeetFlag(FlagTemplate);
return Codec.Leet.LeetFlag(FlagTemplate.StartsWith("[LEET]") ? FlagTemplate[6..] : FlagTemplate);
}

#endregion
Expand Down
4 changes: 2 additions & 2 deletions src/GZCTF/Models/Data/ExerciseChallenge.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,14 +15,14 @@ public class ExerciseChallenge : Challenge
/// <summary>
/// 练习题目附加标签
/// </summary>
public List<string>? Tags { get; set; } = new();
public List<string>? Tags { get; set; } = [];

#region Db Relationship

/// <summary>
/// 依赖的练习题目
/// </summary>
public List<ExerciseChallenge> Dependencies { get; set; } = new();
public List<ExerciseChallenge> Dependencies { get; set; } = [];

#endregion
}
10 changes: 5 additions & 5 deletions src/GZCTF/Models/Data/Game.cs
Original file line number Diff line number Diff line change
Expand Up @@ -195,31 +195,31 @@ internal Game Update(GameInfoModel model)
/// 比赛事件
/// </summary>
[JsonIgnore]
public List<GameEvent> GameEvents { get; set; } = new();
public List<GameEvent> GameEvents { get; set; } = [];

/// <summary>
/// 比赛通知
/// </summary>
[JsonIgnore]
public List<GameNotice> GameNotices { get; set; } = new();
public List<GameNotice> GameNotices { get; set; } = [];

/// <summary>
/// 比赛题目
/// </summary>
[JsonIgnore]
public List<GameChallenge> Challenges { get; set; } = new();
public List<GameChallenge> Challenges { get; set; } = [];

/// <summary>
/// 比赛提交
/// </summary>
[JsonIgnore]
public List<Submission> Submissions { get; set; } = new();
public List<Submission> Submissions { get; set; } = [];

/// <summary>
/// 比赛队伍参赛对象
/// </summary>
[JsonIgnore]
public HashSet<Participation> Participations { get; set; } = new();
public HashSet<Participation> Participations { get; set; } = [];

/// <summary>
/// 比赛队伍
Expand Down
6 changes: 3 additions & 3 deletions src/GZCTF/Models/Data/GameChallenge.cs
Original file line number Diff line number Diff line change
Expand Up @@ -72,17 +72,17 @@ internal void Update(ChallengeUpdateModel model)
/// <summary>
/// 提交
/// </summary>
public List<Submission> Submissions { get; set; } = new();
public List<Submission> Submissions { get; set; } = [];

/// <summary>
/// 赛题实例
/// </summary>
public List<GameInstance> Instances { get; set; } = new();
public List<GameInstance> Instances { get; set; } = [];

/// <summary>
/// 激活赛题的队伍
/// </summary>
public HashSet<Participation> Teams { get; set; } = new();
public HashSet<Participation> Teams { get; set; } = [];

/// <summary>
/// 比赛 Id
Expand Down
2 changes: 1 addition & 1 deletion src/GZCTF/Models/Data/LocalFile.cs
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ public class LocalFile
/// 文件大小
/// </summary>
[JsonIgnore]
public long FileSize { get; set; } = 0;
public long FileSize { get; set; }

/// <summary>
/// 文件名
Expand Down
8 changes: 4 additions & 4 deletions src/GZCTF/Models/Data/Participation.cs
Original file line number Diff line number Diff line change
Expand Up @@ -43,22 +43,22 @@ public class Participation
/// <summary>
/// 队伍参与的队员
/// </summary>
public HashSet<UserParticipation> Members { get; set; } = new();
public HashSet<UserParticipation> Members { get; set; } = [];

/// <summary>
/// 队伍激活的题目
/// </summary>
public HashSet<GameChallenge> Challenges { get; set; } = new();
public HashSet<GameChallenge> Challenges { get; set; } = [];

/// <summary>
/// 赛题实例
/// </summary>
public List<GameInstance> Instances { get; set; } = new();
public List<GameInstance> Instances { get; set; } = [];

/// <summary>
/// 提交
/// </summary>
public List<Submission> Submissions { get; set; } = new();
public List<Submission> Submissions { get; set; } = [];

[Required]
public int GameId { get; set; }
Expand Down
2 changes: 1 addition & 1 deletion src/GZCTF/Models/Data/Post.cs
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ public partial class Post
/// <summary>
/// 文章标签
/// </summary>
public List<string>? Tags { get; set; } = new();
public List<string>? Tags { get; set; } = [];

/// <summary>
/// 作者信息
Expand Down
4 changes: 2 additions & 2 deletions src/GZCTF/Models/Data/Team.cs
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ internal void UpdateInfo(AdminTeamModel model)
/// <summary>
/// 比赛参与对象
/// </summary>
public List<Participation> Participations { get; set; } = new();
public List<Participation> Participations { get; set; } = [];

/// <summary>
/// 比赛对象
Expand All @@ -89,7 +89,7 @@ internal void UpdateInfo(AdminTeamModel model)
/// <summary>
/// 队员
/// </summary>
public HashSet<UserInfo> Members { get; set; } = new();
public HashSet<UserInfo> Members { get; set; } = [];

#endregion Db Relationship
}
4 changes: 2 additions & 2 deletions src/GZCTF/Models/Data/UserInfo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -125,13 +125,13 @@ internal void UpdateUserInfo(ProfileUpdateModel model)
/// 个人提交记录
/// </summary>
[MemoryPackIgnore]
public List<Submission> Submissions { get; set; } = new();
public List<Submission> Submissions { get; set; } = [];

/// <summary>
/// 参与的队伍
/// </summary>
[MemoryPackIgnore]
public List<Team> Teams { get; set; } = new();
public List<Team> Teams { get; set; } = [];

#endregion
}
5 changes: 3 additions & 2 deletions src/GZCTF/Models/Internal/CaptchaModel.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using System.Text.Json.Serialization;

// ReSharper disable UnusedMember.Global
// ReSharper disable ClassNeverInstantiated.Global

namespace GZCTF.Models.Internal;
Expand All @@ -19,7 +20,7 @@ public class RecaptchaResponseModel
public string Action { get; set; } = string.Empty;

[JsonPropertyName("error-codes")]
public List<string> ErrorCodes { get; set; } = new();
public List<string> ErrorCodes { get; set; } = [];
}

/// <summary>
Expand Down Expand Up @@ -59,5 +60,5 @@ public class TurnstileResponseModel
public string Action { get; set; } = string.Empty;

[JsonPropertyName("error-codes")]
public List<string> ErrorCodes { get; set; } = new();
public List<string> ErrorCodes { get; set; } = [];
}
2 changes: 1 addition & 1 deletion src/GZCTF/Models/Internal/FormattableData.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ public abstract class FormattableData<T> where T : Enum
/// </summary>
[Required]
public T Type { get; set; } = default!;

/// <summary>
/// 格式化值列表
/// </summary>
Expand Down
6 changes: 4 additions & 2 deletions src/GZCTF/Models/Request/Account/AccountVerifyModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,14 @@ public class AccountVerifyModel
/// <summary>
/// 邮箱接收到的Base64格式Token
/// </summary>
[Required(ErrorMessage = "Token是必需的")]
[Required(ErrorMessageResourceName = nameof(Resources.Program.Model_TokenRequired),
ErrorMessageResourceType = typeof(Resources.Program))]
public string? Token { get; set; }

/// <summary>
/// 用户邮箱的Base64格式
/// </summary>
[Required(ErrorMessage = "邮箱是必需的")]
[Required(ErrorMessageResourceName = nameof(Resources.Program.Model_EmailRequired),
ErrorMessageResourceType = typeof(Resources.Program))]
public string? Email { get; set; }
}
Loading

0 comments on commit 490a162

Please sign in to comment.