/
problem.go
183 lines (163 loc) · 4.58 KB
/
problem.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
package contests
import (
"bytes"
"database/sql"
"fmt"
"io"
"net/http"
"path/filepath"
"strconv"
"time"
"github.com/labstack/echo/v4"
"github.com/natsukagami/kjudge/db"
"github.com/natsukagami/kjudge/models"
"github.com/natsukagami/kjudge/server/httperr"
"github.com/pkg/errors"
)
// ProblemCtx is the context required to display a problem page.
type ProblemCtx struct {
*ContestCtx
Problem *models.Problem
Files map[string]*models.File
Submissions []*models.Submission
}
// Render renders the context.
func (p *ProblemCtx) Render(c echo.Context) error {
return c.Render(http.StatusOK, "contests/problem", p)
}
// Collect a problemCtx.
func getProblemCtx(db db.DBContext, c echo.Context) (*ProblemCtx, error) {
contest, err := getContestCtx(db, c)
if err != nil {
return nil, err
}
// If the contest has not started, throw
if contest.Contest.StartTime.After(time.Now()) {
return nil, httperr.BadRequestf("Contest has not started")
}
name := c.Param("problem")
problem, err := models.GetProblemByName(db, contest.Contest.ID, name)
if errors.Is(err, sql.ErrNoRows) {
return nil, httperr.NotFoundf("Problem not found: %s", name)
} else if err != nil {
return nil, err
}
files, err := models.GetProblemFilesMeta(db, problem.ID)
if err != nil {
return nil, err
}
fm := make(map[string]*models.File)
for _, f := range files {
if f.Public {
fm[f.Filename] = f
}
}
subs, err := models.GetUserProblemSubmissions(db, contest.Me.ID, problem.ID)
if err != nil {
return nil, err
}
return &ProblemCtx{
ContestCtx: contest,
Problem: problem,
Files: fm,
Submissions: subs,
}, nil
}
// ProblemGet implements GET /contest/:id/problems/:problem
func (g *Group) ProblemGet(c echo.Context) error {
ctx, err := getProblemCtx(g.db, c)
if err != nil {
return err
}
return ctx.Render(c)
}
// FileGet implements GET /contest/:id/problems/:problem/files/:file
func (g *Group) FileGet(c echo.Context) error {
ctx, err := getProblemCtx(g.db, c)
if err != nil {
return err
}
fileIDStr := c.Param("file")
fileID, err := strconv.Atoi(fileIDStr)
if err != nil {
return httperr.NotFoundf("File not found: %v", fileID)
}
file, err := models.GetFile(g.db, fileID)
if errors.Is(err, sql.ErrNoRows) {
return httperr.NotFoundf("File not found: %v", fileID)
} else if err != nil {
return err
}
if file.ProblemID != ctx.Problem.ID || !file.Public {
return httperr.NotFoundf("File not found: %v", fileID)
}
http.ServeContent(c.Response(), c.Request(), file.Filename, time.Now(), bytes.NewReader(file.Content))
return nil
}
// SubmitPost implements POST /contest/:id/problems/:problem/submit.
func (g *Group) SubmitPost(c echo.Context) error {
tx, err := g.db.Beginx()
if err != nil {
return errors.WithStack(err)
}
defer db.Rollback(tx)
now := time.Now()
ctx, err := getProblemCtx(tx, c)
if err != nil {
return err
}
if ctx.Contest.EndTime.Before(time.Now()) {
return httperr.BadRequestf("Contest has already ended")
}
// Check limits
if ctx.Problem.MaxSubmissionsCount > 0 && len(ctx.Submissions) >= ctx.Problem.MaxSubmissionsCount {
return httperr.Newf(http.StatusTooManyRequests, "You can only submit %d submissions for this problem", ctx.Problem.MaxSubmissionsCount)
}
if len(ctx.Submissions) > 0 {
lastSub := ctx.Submissions[0]
if now.Sub(lastSub.SubmittedAt) < time.Duration(ctx.Problem.SecondsBetweenSubmissions)*time.Second {
return httperr.Newf(http.StatusTooManyRequests, "Please wait %d seconds between submissions for this problem", ctx.Problem.SecondsBetweenSubmissions)
}
}
form, err := c.MultipartForm()
if err != nil {
return err
}
files, ok := form.File["file"]
if !ok || len(files) != 1 {
return httperr.BadRequestf("One file must be attached")
}
file := files[0]
lang, err := models.LanguageByExt(filepath.Ext(file.Filename))
if err != nil {
return httperr.BadRequestf("Cannot resolve language: %v", err)
}
fileContent, err := file.Open()
if err != nil {
return errors.WithStack(err)
}
defer fileContent.Close()
source, err := io.ReadAll(fileContent)
if err != nil {
return errors.WithStack(err)
}
sub := models.Submission{
ProblemID: ctx.Problem.ID,
UserID: ctx.Me.ID,
Source: source,
Language: lang,
SubmittedAt: now,
Verdict: "...",
}
if err := sub.Write(tx); err != nil {
return err
}
job := models.NewJobScore(sub.ID)
if err := job.Write(tx); err != nil {
return err
}
if err := tx.Commit(); err != nil {
return errors.WithStack(err)
}
return c.Redirect(http.StatusSeeOther, fmt.Sprintf("/contests/%d/problems/%s#submissions", ctx.Contest.ID, ctx.Problem.Name))
}