Skip to content

Commit

Permalink
wip: remove submission_qty field
Browse files Browse the repository at this point in the history
  • Loading branch information
ElaBosak233 committed Jun 9, 2024
1 parent 6c518cb commit aa8dd1e
Show file tree
Hide file tree
Showing 15 changed files with 55 additions and 73 deletions.
18 changes: 8 additions & 10 deletions api/docs.go
Original file line number Diff line number Diff line change
Expand Up @@ -209,11 +209,6 @@ const docTemplate = `{
"name": "sort_order",
"in": "query"
},
{
"type": "integer",
"name": "submission_qty",
"in": "query"
},
{
"type": "integer",
"name": "team_id",
Expand Down Expand Up @@ -1885,6 +1880,12 @@ const docTemplate = `{
}
]
},
"bloods": {
"type": "array",
"items": {
"$ref": "#/definitions/model.Submission"
}
},
"category": {
"description": "The challenge's category.",
"allOf": [
Expand Down Expand Up @@ -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.",
Expand Down
18 changes: 8 additions & 10 deletions api/swagger.json
Original file line number Diff line number Diff line change
Expand Up @@ -200,11 +200,6 @@
"name": "sort_order",
"in": "query"
},
{
"type": "integer",
"name": "submission_qty",
"in": "query"
},
{
"type": "integer",
"name": "team_id",
Expand Down Expand Up @@ -1876,6 +1871,12 @@
}
]
},
"bloods": {
"type": "array",
"items": {
"$ref": "#/definitions/model.Submission"
}
},
"category": {
"description": "The challenge's category.",
"allOf": [
Expand Down Expand Up @@ -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.",
Expand Down
13 changes: 6 additions & 7 deletions api/swagger.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
3 changes: 2 additions & 1 deletion internal/model/challenge.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.
}
Expand Down
1 change: 0 additions & 1 deletion internal/model/request/challenge_request.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"`
Expand Down
9 changes: 4 additions & 5 deletions internal/model/request/game_challenge_request.go
Original file line number Diff line number Diff line change
@@ -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 {
Expand Down
6 changes: 6 additions & 0 deletions internal/repository/game_challenge.go
Original file line number Diff line number Diff line change
Expand Up @@ -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").
Expand Down
7 changes: 5 additions & 2 deletions internal/service/challenge.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

Expand Down
30 changes: 8 additions & 22 deletions internal/service/game_challenge.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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
Expand Down
15 changes: 6 additions & 9 deletions web/src/components/modals/ChallengeModal.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -263,21 +263,19 @@ export default function ChallengeModal(props: ChallengeModalProps) {
<Text fw={700}>{challenge?.title}</Text>
</Group>
<Group gap={0}>
{(challenge?.submissions?.length as number) >
0 && (
{(challenge?.bloods?.length as number) > 0 && (
<Tooltip
label={`一血 ${challenge?.submissions?.[0]?.team?.name || challenge?.submissions?.[0]?.user?.nickname}`}
label={`一血 ${challenge?.bloods?.[0]?.team?.name || challenge?.bloods?.[0]?.user?.nickname}`}
position={"top"}
>
<ThemeIcon variant="transparent">
<FirstBloodIcon />
</ThemeIcon>
</Tooltip>
)}
{(challenge?.submissions?.length as number) >
1 && (
{(challenge?.bloods?.length as number) > 1 && (
<Tooltip
label={`二血 ${challenge?.submissions?.[1]?.team?.name || challenge?.submissions?.[1]?.user?.nickname}`}
label={`二血 ${challenge?.bloods?.[1]?.team?.name || challenge?.bloods?.[1]?.user?.nickname}`}
position={"top"}
>
<Box
Expand All @@ -292,10 +290,9 @@ export default function ChallengeModal(props: ChallengeModalProps) {
</Box>
</Tooltip>
)}
{(challenge?.submissions?.length as number) >
2 && (
{(challenge?.bloods?.length as number) > 2 && (
<Tooltip
label={`三血 ${challenge?.submissions?.[2]?.team?.name || challenge?.submissions?.[2]?.user?.nickname}`}
label={`三血 ${challenge?.bloods?.[2]?.team?.name || challenge?.bloods?.[2]?.user?.nickname}`}
position={"top"}
>
<Box
Expand Down
2 changes: 1 addition & 1 deletion web/src/components/widgets/ChallengeCard.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -169,7 +169,7 @@ export default function ChallengeCard({
</Text>
</Tooltip>
<Flex align={"center"}>
{challenge?.submissions
{challenge?.bloods
?.slice(0, 3)
?.map((submission, index) => (
<Tooltip
Expand Down
1 change: 0 additions & 1 deletion web/src/pages/challenges/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,6 @@ export default function Page() {
is_detailed: false,
page: page,
size: rowsPerPage,
submission_qty: 3,
title: search,
category_id:
selectedCategory === 0 ? undefined : selectedCategory,
Expand Down
1 change: 0 additions & 1 deletion web/src/pages/games/[id]/scoreboard.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,6 @@ function Page() {
.getGameChallenges({
game_id: Number(id),
is_enabled: true,
submission_qty: 0,
})
.then((res) => {
const r = res.data;
Expand Down
3 changes: 1 addition & 2 deletions web/src/types/challenge.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ export interface Challenge {
hints?: Array<Hint>;
solved?: Submission | boolean;
solved_times?: number;
submissions?: Array<Submission>;
bloods?: Array<Submission>;
is_enabled?: boolean;
min_pts?: number;
max_pts?: number;
Expand All @@ -45,7 +45,6 @@ export interface ChallengeFindRequest {
difficulty?: number;
page?: number;
size?: number;
submission_qty?: number;
sort_key?: string;
sort_order?: string;
}
Expand Down
1 change: 0 additions & 1 deletion web/src/types/game.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,6 @@ export interface GameChallengeFindRequest {
game_id?: number;
is_enabled?: boolean;
team_id?: number;
submission_qty?: number;
}

export interface GameCreateRequest {
Expand Down

0 comments on commit aa8dd1e

Please sign in to comment.