/
Problem.go
232 lines (185 loc) · 5.1 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
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
package main
import (
"net/http" // Server logic
"log" // Logs
"time" // Timing
"net" // Server logic
"strings" // Strings split
"strconv" // String convertations
"fmt" // I/O formatting
"path/filepath" // Filepath join
"io/ioutil" // Directory scan
// Database
"context"
"go.mongodb.org/mongo-driver/bson"
)
// Solution represents data for a single solution
type Solution struct {
Number int
Problem int
User string
Posting time.Time
Time float64
Memory int64
Status string
Score int
}
// ProblemPage holds information for page generating
type ProblemPage struct {
Problem Problem
Text string
Input string
Output string
TimeLimit float64
MemoryLimit int
Examples []IOExample
Solved bool
Solutions []Solution
}
// IOExample holds examples for problem solution
type IOExample struct {
Number int
Input string
Output string
}
// SingleProblem handles GET request for problem page
func SingleProblem(w http.ResponseWriter, r *http.Request) {
// Checking credentials
// If user is not logged in, redirect to start page
ip, _, err := net.SplitHostPort(r.RemoteAddr)
if err != nil {
log.Println("/problem/: Cannot discover user's IP")
ErrorHandler(w, r, http.StatusInternalServerError)
return
}
userCredential := Credential {
UserIP: net.ParseIP(ip),
}
if userCredential.UserIP == nil {
log.Println("/problem/: Cannot discover user's IP")
ErrorHandler(w, r, http.StatusInternalServerError)
return
}
credentialsCollection := client.Database("Judex").Collection("credentials")
filter := bson.D{{Key: "userip", Value: userCredential.UserIP}}
err = credentialsCollection.FindOne(context.TODO(), filter).Decode(&userCredential)
if err == nil {
if time.Now().After(userCredential.EndTime) {
http.Redirect(w, r, "/", http.StatusSeeOther)
credentialsCollection.DeleteMany(context.TODO(), filter)
return
}
} else {
http.Redirect(w, r, "/", http.StatusSeeOther)
}
// Checking problem existence
problemNumber := strings.Split(r.URL.Path, "/")[2]
if problemNumber == "" {
http.Redirect(w, r, "/problems", http.StatusSeeOther)
}
problemsCollection := client.Database("Judex").Collection("problems")
filter = bson.D{}
var problem Problem
found := false
cur, err := problemsCollection.Find(context.TODO(), filter)
if err != nil && err.Error() != "mongo: no documents in result" {
log.Println(err)
return
}
for cur.Next(context.TODO()) {
var elem Problem
err := cur.Decode(&elem)
if i, err := strconv.Atoi(problemNumber); err == nil {
if i == elem.Number {
problem = elem
found = true
break
}
} else {
log.Println(err)
}
if err != nil {
log.Println(err)
return
}
}
if err := cur.Err(); err != nil {
log.Println(err)
return
}
cur.Close(context.TODO())
if !found {
ErrorHandler(w, r, http.StatusNotFound)
return
}
// Looking for solutions
solutionsCollection := client.Database("Judex").Collection("solutions")
filter = bson.D {
{Key: "problem", Value: problem.Number},
{Key: "user", Value: userCredential.Username},
}
var solutions []Solution
cur, err = solutionsCollection.Find(context.TODO(), filter)
if err != nil && err.Error() != "mongo: no documents in result" {
log.Println(err)
return
}
for cur.Next(context.TODO()) {
var elem Solution
err := cur.Decode(&elem)
if err != nil {
log.Println(err)
return
}
solutions = append(solutions, elem)
}
if err := cur.Err(); err != nil {
log.Println(err)
return
}
cur.Close(context.TODO())
// Preparing the template
var page ProblemPage
if len(solutions) == 0 {
page.Solved = false
} else {
page.Solved = true
}
page.Problem = problem
page.Solutions = solutions
page.Text = FileReader(filepath.Join(problemsPath, strconv.Itoa(problem.Number), "text.txt"))
page.Input = FileReader(filepath.Join(problemsPath, strconv.Itoa(problem.Number), "input.txt"))
page.Output = FileReader(filepath.Join(problemsPath, strconv.Itoa(problem.Number), "output.txt"))
files, err := ioutil.ReadDir(filepath.Join(problemsPath, strconv.Itoa(problem.Number), "examples"))
if err != nil {
log.Println(err)
}
maxExample := 0
for _, v := range files {
number, _ := strconv.Atoi(strings.Split(v.Name(), ".")[0])
if number > maxExample {
maxExample = number
}
}
for i := 1; i <= maxExample; i++ {
var example IOExample
input := FileReader(filepath.Join(problemsPath, strconv.Itoa(problem.Number), "examples", strconv.Itoa(i) + ".in"))
output := FileReader(filepath.Join(problemsPath, strconv.Itoa(problem.Number), "examples", strconv.Itoa(i) + ".out"))
if input == "" || output == "" {
break
}
example.Input = input
example.Output = output
example.Number = i
page.Examples = append(page.Examples, example)
}
limits := FileReader(filepath.Join(problemsPath, strconv.Itoa(problem.Number), "limits.lim"))
if limits != "" {
fmt.Sscan(strings.Split(limits, "=")[1], &page.TimeLimit)
fmt.Sscan(strings.Split(limits, "=")[2], &page.MemoryLimit)
}
// Executing template
if err := templates.ExecuteTemplate(w, "problem.html", page); err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
}
}