From aa8dd1e8a19e80e500e5fa1e5415112205137fd9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=9F=83=E6=8B=89?= Date: Sun, 9 Jun 2024 13:34:46 +0800 Subject: [PATCH] wip: remove submission_qty field --- api/docs.go | 18 +++++------ api/swagger.json | 18 +++++------ api/swagger.yaml | 13 ++++---- internal/model/challenge.go | 3 +- internal/model/request/challenge_request.go | 1 - .../model/request/game_challenge_request.go | 9 +++--- internal/repository/game_challenge.go | 6 ++++ internal/service/challenge.go | 7 +++-- internal/service/game_challenge.go | 30 +++++-------------- web/src/components/modals/ChallengeModal.tsx | 15 ++++------ web/src/components/widgets/ChallengeCard.tsx | 2 +- web/src/pages/challenges/index.tsx | 1 - web/src/pages/games/[id]/scoreboard.tsx | 1 - web/src/types/challenge.ts | 3 +- web/src/types/game.ts | 1 - 15 files changed, 55 insertions(+), 73 deletions(-) diff --git a/api/docs.go b/api/docs.go index 645fe938..4716d1b5 100644 --- a/api/docs.go +++ b/api/docs.go @@ -209,11 +209,6 @@ const docTemplate = `{ "name": "sort_order", "in": "query" }, - { - "type": "integer", - "name": "submission_qty", - "in": "query" - }, { "type": "integer", "name": "team_id", @@ -1885,6 +1880,12 @@ const docTemplate = `{ } ] }, + "bloods": { + "type": "array", + "items": { + "$ref": "#/definitions/model.Submission" + } + }, "category": { "description": "The challenge's category.", "allOf": [ @@ -1962,11 +1963,8 @@ const docTemplate = `{ "solved": { "$ref": "#/definitions/model.Submission" }, - "submissions": { - "type": "array", - "items": { - "$ref": "#/definitions/model.Submission" - } + "solved_times": { + "type": "integer" }, "title": { "description": "The challenge's title.", diff --git a/api/swagger.json b/api/swagger.json index 71dc9d55..e4203532 100644 --- a/api/swagger.json +++ b/api/swagger.json @@ -200,11 +200,6 @@ "name": "sort_order", "in": "query" }, - { - "type": "integer", - "name": "submission_qty", - "in": "query" - }, { "type": "integer", "name": "team_id", @@ -1876,6 +1871,12 @@ } ] }, + "bloods": { + "type": "array", + "items": { + "$ref": "#/definitions/model.Submission" + } + }, "category": { "description": "The challenge's category.", "allOf": [ @@ -1953,11 +1954,8 @@ "solved": { "$ref": "#/definitions/model.Submission" }, - "submissions": { - "type": "array", - "items": { - "$ref": "#/definitions/model.Submission" - } + "solved_times": { + "type": "integer" }, "title": { "description": "The challenge's title.", diff --git a/api/swagger.yaml b/api/swagger.yaml index 870980d8..f6a97526 100644 --- a/api/swagger.yaml +++ b/api/swagger.yaml @@ -32,6 +32,10 @@ definitions: allOf: - $ref: '#/definitions/model.File' description: The challenge's attachment. + bloods: + items: + $ref: '#/definitions/model.Submission' + type: array category: allOf: - $ref: '#/definitions/model.Category' @@ -88,10 +92,8 @@ definitions: type: integer solved: $ref: '#/definitions/model.Submission' - submissions: - items: - $ref: '#/definitions/model.Submission' - type: array + solved_times: + type: integer title: description: The challenge's title. type: string @@ -918,9 +920,6 @@ paths: - in: query name: sort_order type: string - - in: query - name: submission_qty - type: integer - in: query name: team_id type: integer diff --git a/internal/model/challenge.go b/internal/model/challenge.go index 3150a8cd..49e1268c 100644 --- a/internal/model/challenge.go +++ b/internal/model/challenge.go @@ -29,7 +29,8 @@ type Challenge struct { Envs []*Env `json:"envs,omitempty"` Solved *Submission `json:"solved,omitempty"` SolvedTimes int `gorm:"-" json:"solved_times"` - Submissions []*Submission `json:"submissions,omitempty"` + Submissions []*Submission `json:"-"` + Bloods []*Submission `gorm:"-" json:"bloods,omitempty"` CreatedAt int64 `gorm:"autoUpdateTime:milli" json:"created_at,omitempty"` // The challenge's creation time. UpdatedAt int64 `gorm:"autoUpdateTime:milli" json:"updated_at,omitempty"` // The challenge's last update time. } diff --git a/internal/model/request/challenge_request.go b/internal/model/request/challenge_request.go index 3f0df798..d980c3e1 100644 --- a/internal/model/request/challenge_request.go +++ b/internal/model/request/challenge_request.go @@ -56,7 +56,6 @@ type ChallengeFindRequest struct { GameID *uint `json:"game_id" form:"game_id"` TeamID *uint `json:"team_id" form:"team_id"` IsDetailed *bool `json:"is_detailed" form:"is_detailed"` - SubmissionQty int `json:"submission_qty" form:"submission_qty"` Page int `json:"page" form:"page"` Size int `json:"size" form:"size"` SortKey string `json:"sort_key" form:"sort_key"` diff --git a/internal/model/request/game_challenge_request.go b/internal/model/request/game_challenge_request.go index 88d1b412..48b8f2bd 100644 --- a/internal/model/request/game_challenge_request.go +++ b/internal/model/request/game_challenge_request.go @@ -1,11 +1,10 @@ package request type GameChallengeFindRequest struct { - GameID uint `json:"game_id" form:"game_id"` - ChallengeID uint `json:"challenge_id" form:"challenge_id"` - TeamID uint `json:"team_id" form:"team_id"` - IsEnabled *bool `json:"is_enabled" form:"is_enabled"` - SubmissionQty int `json:"submission_qty" form:"submission_qty"` + GameID uint `json:"game_id" form:"game_id"` + ChallengeID uint `json:"challenge_id" form:"challenge_id"` + TeamID uint `json:"team_id" form:"team_id"` + IsEnabled *bool `json:"is_enabled" form:"is_enabled"` } type GameChallengeCreateRequest struct { diff --git a/internal/repository/game_challenge.go b/internal/repository/game_challenge.go index 606e172e..ee5bdb01 100644 --- a/internal/repository/game_challenge.go +++ b/internal/repository/game_challenge.go @@ -55,6 +55,12 @@ func (t *GameChallengeRepository) Find(req request.GameChallengeFindRequest) ([] Where("game_id = ?", req.GameID). Omit("flag") }). + Preload("Solved", func(db *gorm.DB) *gorm.DB { + return db. + Where("status = ?", 2). + Where("team_id = ?", req.TeamID). + Omit("flag") + }). Omit("flags", "images", "is_practicable", "practice_pts", "created_at", "updated_at") }). Preload("Game"). diff --git a/internal/service/challenge.go b/internal/service/challenge.go index bbad4ef3..944909d2 100644 --- a/internal/service/challenge.go +++ b/internal/service/challenge.go @@ -69,10 +69,13 @@ func (t *ChallengeService) Find(req request.ChallengeFindRequest) ([]model.Chall if !*(req.IsDetailed) { challenge.Simplify() } + + // Calculate the solved times and bloods. challenge.SolvedTimes = len(challenge.Submissions) - if req.SubmissionQty != 0 && challenge.Submissions != nil { - challenge.Submissions = challenge.Submissions[:min(req.SubmissionQty, len(challenge.Submissions))] + if challenge.Submissions != nil { + challenge.Bloods = challenge.Submissions[:min(3, len(challenge.Submissions))] } + challenges[index] = challenge } diff --git a/internal/service/game_challenge.go b/internal/service/game_challenge.go index d0e5e03e..dd3cdc53 100644 --- a/internal/service/game_challenge.go +++ b/internal/service/game_challenge.go @@ -43,7 +43,8 @@ func (g *GameChallengeService) Find(req request.GameChallengeFindRequest) ([]mod game := games[0] gameChallenges, err := g.gameChallengeRepository.Find(req) for i, gameChallenge := range gameChallenges { - pts := calculate.GameChallengePts( + // Calculate the challenge pts. + gameChallenge.Pts = calculate.GameChallengePts( gameChallenge.MaxPts, gameChallenge.MinPts, gameChallenge.Challenge.Difficulty, @@ -53,28 +54,13 @@ func (g *GameChallengeService) Find(req request.GameChallengeFindRequest) ([]mod game.SecondBloodRewardRatio, game.ThirdBloodRewardRatio, ) - gameChallenge.Pts = pts - for index, submission := range gameChallenge.Challenge.Submissions { - submission.Pts = calculate.GameChallengePts( - gameChallenge.MaxPts, - gameChallenge.MinPts, - gameChallenge.Challenge.Difficulty, - int64(len(gameChallenge.Challenge.Submissions)), - int64(int(submission.Rank-1)), - game.FirstBloodRewardRatio, - game.SecondBloodRewardRatio, - game.ThirdBloodRewardRatio, - ) - if req.TeamID != 0 && submission.TeamID != nil && *(submission.TeamID) == req.TeamID { - sub := submission - gameChallenge.Challenge.Solved = sub - break - } - gameChallenge.Challenge.Submissions[index] = submission - } - if req.SubmissionQty > 0 { - gameChallenge.Challenge.Submissions = gameChallenge.Challenge.Submissions[:min(req.SubmissionQty, len(gameChallenge.Challenge.Submissions))] + + // Calculate the solved times and bloods. + gameChallenge.Challenge.SolvedTimes = len(gameChallenge.Challenge.Submissions) + if gameChallenge.Challenge.Submissions != nil { + gameChallenge.Challenge.Bloods = gameChallenge.Challenge.Submissions[:min(3, len(gameChallenge.Challenge.Submissions))] } + gameChallenges[i] = gameChallenge } return gameChallenges, err diff --git a/web/src/components/modals/ChallengeModal.tsx b/web/src/components/modals/ChallengeModal.tsx index 470d02dd..c788b19d 100644 --- a/web/src/components/modals/ChallengeModal.tsx +++ b/web/src/components/modals/ChallengeModal.tsx @@ -263,10 +263,9 @@ export default function ChallengeModal(props: ChallengeModalProps) { {challenge?.title} - {(challenge?.submissions?.length as number) > - 0 && ( + {(challenge?.bloods?.length as number) > 0 && ( @@ -274,10 +273,9 @@ export default function ChallengeModal(props: ChallengeModalProps) { )} - {(challenge?.submissions?.length as number) > - 1 && ( + {(challenge?.bloods?.length as number) > 1 && ( )} - {(challenge?.submissions?.length as number) > - 2 && ( + {(challenge?.bloods?.length as number) > 2 && ( - {challenge?.submissions + {challenge?.bloods ?.slice(0, 3) ?.map((submission, index) => ( { const r = res.data; diff --git a/web/src/types/challenge.ts b/web/src/types/challenge.ts index f48dd75f..4dd3499d 100644 --- a/web/src/types/challenge.ts +++ b/web/src/types/challenge.ts @@ -28,7 +28,7 @@ export interface Challenge { hints?: Array; solved?: Submission | boolean; solved_times?: number; - submissions?: Array; + bloods?: Array; is_enabled?: boolean; min_pts?: number; max_pts?: number; @@ -45,7 +45,6 @@ export interface ChallengeFindRequest { difficulty?: number; page?: number; size?: number; - submission_qty?: number; sort_key?: string; sort_order?: string; } diff --git a/web/src/types/game.ts b/web/src/types/game.ts index 37caba38..86d55b19 100644 --- a/web/src/types/game.ts +++ b/web/src/types/game.ts @@ -36,7 +36,6 @@ export interface GameChallengeFindRequest { game_id?: number; is_enabled?: boolean; team_id?: number; - submission_qty?: number; } export interface GameCreateRequest {