Skip to content

Commit

Permalink
feat: update scoreboard flush strategy
Browse files Browse the repository at this point in the history
  • Loading branch information
GZTimeWalker committed May 6, 2023
1 parent 826c130 commit 8dea6bb
Show file tree
Hide file tree
Showing 6 changed files with 20 additions and 11 deletions.
4 changes: 2 additions & 2 deletions src/GZCTF/Controllers/AssetsController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ public async Task<IActionResult> Upload(List<IFormFile> files, [FromQuery] strin
if (file.Length > 0)
{
var res = await fileRepository.CreateOrUpdateFile(file, filename, token);
logger.SystemLog($"更新文件 [{res.Hash[..8]}] {filename ?? file.FileName} - {file.Length} Bytes", TaskStatus.Success, LogLevel.Debug);
logger.SystemLog($"更新文件 [{res.Hash[..8]}] {filename ?? file.FileName} @ {file.Length} bytes", TaskStatus.Success, LogLevel.Debug);
results.Add(res);
}
}
Expand Down Expand Up @@ -136,4 +136,4 @@ public async Task<IActionResult> Delete(string hash, CancellationToken token)
_ => BadRequest(new RequestResponse("文件删除失败"))
};
}
}
}
9 changes: 5 additions & 4 deletions src/GZCTF/Repositories/InstanceRepository.cs
Original file line number Diff line number Diff line change
Expand Up @@ -225,7 +225,7 @@ public async Task<CheatCheckInfo> CheckCheat(Submission submission, Cancellation
return checkInfo;
}

public async Task<(SubmissionType, AnswerResult)> VerifyAnswer(Submission submission, CancellationToken token = default)
public async Task<VerifyResult> VerifyAnswer(Submission submission, CancellationToken token = default)
{
var trans = await context.Database.BeginTransactionAsync(token);

Expand All @@ -247,7 +247,7 @@ public async Task<CheatCheckInfo> CheckCheat(Submission submission, Cancellation
if (instance is null)
{
submission.Status = AnswerResult.NotFound;
return (SubmissionType.Unaccepted, AnswerResult.NotFound);
return new(SubmissionType.Unaccepted, AnswerResult.NotFound);
}

if (instance.FlagContext is null && submission.Challenge.Type.IsStatic())
Expand All @@ -266,8 +266,9 @@ public async Task<CheatCheckInfo> CheckCheat(Submission submission, Cancellation
}

bool firstTime = !instance.IsSolved && updateSub.Status == AnswerResult.Accepted;
bool beforeEnd = submission.Game.EndTimeUTC > submission.SubmitTimeUTC;

if (firstTime && submission.Game.EndTimeUTC > submission.SubmitTimeUTC)
if (firstTime && beforeEnd)
{
instance.IsSolved = true;
updateSub.Challenge.AcceptedCount++;
Expand All @@ -288,7 +289,7 @@ public async Task<CheatCheckInfo> CheckCheat(Submission submission, Cancellation
await SaveAsync(token);
await trans.CommitAsync(token);

return (ret, updateSub.Status);
return new(ret, updateSub.Status);
}
catch
{
Expand Down
2 changes: 1 addition & 1 deletion src/GZCTF/Repositories/Interface/IInstanceRepository.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ public interface IInstanceRepository : IRepository
/// <param name="submission">当前提交</param>
/// <param name="token"></param>
/// <returns></returns>
public Task<(SubmissionType, AnswerResult)> VerifyAnswer(Submission submission, CancellationToken token = default);
public Task<VerifyResult> VerifyAnswer(Submission submission, CancellationToken token = default);

/// <summary>
/// 获取题目实例
Expand Down
2 changes: 0 additions & 2 deletions src/GZCTF/Services/CacheMaker.cs
Original file line number Diff line number Diff line change
Expand Up @@ -80,8 +80,6 @@ private async Task Maker(CancellationToken token = default)

var updateLock = CacheKey.UpdateLock(key);

logger.SystemLog($"缓存更新线程开始处理更新请求:{key}", TaskStatus.Pending, LogLevel.Debug);

if (await cache.GetAsync(updateLock, token) is not null)
{
// only one GZCTF instance will never encounter this problem
Expand Down
7 changes: 5 additions & 2 deletions src/GZCTF/Services/FlagChecker.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System.Threading.Channels;
using CTFServer.Models;
using CTFServer.Repositories.Interface;
using CTFServer.Utils;
using Microsoft.EntityFrameworkCore;
Expand Down Expand Up @@ -54,6 +55,10 @@ private async Task Checker(int id, CancellationToken token = default)
logger.Log($"[提交正确] 队伍 [{item.Team.Name}] 提交题目 [{item.Challenge.Title}] 的答案 [{item.Answer}]", item.User!, TaskStatus.Success, LogLevel.Information);

await eventRepository.AddEvent(GameEvent.FromSubmission(item, type, ans), token);

// only flush the scoreboard if the contest is not ended and the submission is accepted
if (item.Game.EndTimeUTC > item.SubmitTimeUTC)
await gameRepository.FlushScoreboardCache(item.GameId, token);
}
else
{
Expand Down Expand Up @@ -85,8 +90,6 @@ await eventRepository.AddEvent(new()

item.Status = ans;
await submissionRepository.SendSubmission(item);

await gameRepository.FlushScoreboardCache(item.GameId, token);
}
catch (DbUpdateConcurrencyException)
{
Expand Down
7 changes: 7 additions & 0 deletions src/GZCTF/Utils/Shared.cs
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,13 @@ public record RequestResponse(string Title, int Status = 400);
/// <param name="Status">状态码</param>
public record RequestResponse<T>(string Title, T Data, int Status = 400);

/// <summary>
/// 答案校验结果
/// </summary>
/// <param name="SubType">提交类型</param>
/// <param name="AnsRes">提交 flag 判定结果</param>
public record VerifyResult(SubmissionType SubType, AnswerResult AnsRes);

/// <summary>
/// 列表响应
/// </summary>
Expand Down

0 comments on commit 8dea6bb

Please sign in to comment.