-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathjudge.go
243 lines (190 loc) · 4.63 KB
/
judge.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
233
234
235
236
237
238
239
240
241
242
243
package main
import (
"GoOnlineJudge/model"
"RunServer/config"
"fmt"
"os"
"os/exec"
"strconv"
"time"
)
type solution struct {
Vid int
OJ string
SubmitTime time.Time
model.Solution
}
func (s *solution) GetResult() int {
return s.Judge
}
func (s *solution) SetResult(result int) {
s.Judge = result
}
func (s *solution) SetResource(time int, Mem int, length int) {
s.Time = time
s.Memory = Mem
s.Length = length
}
func (s *solution) SetErrorInfo(text string) {
s.Error = text
}
func (s *solution) GetSubmitTime() time.Time {
return s.SubmitTime
}
func (s *solution) SetSubmitTime(submitTime time.Time) {
s.SubmitTime = submitTime
}
func (s *solution) GetCode() string {
return s.Code
}
func (s *solution) GetOJ() string {
return s.OJ
}
func (s *solution) GetLang() int {
return s.Language
}
func (s *solution) GetVid() int {
return s.Vid
}
func (s *solution) GetSid() int {
return s.Sid
}
func (s *solution) GetPid() int {
return s.Pid
}
func (s *solution) Init(info Info) {
logger.Println(info)
solutionModel := model.SolutionModel{}
sol, err := solutionModel.Detail(info.Sid)
if err != nil {
logger.Println(err)
return
}
s.Solution = *sol
s.OJ = info.OJ
s.Vid = info.Pid
}
func (this *solution) UpdateSim() {
var sim, Sim_s_id int
if this.Judge == config.JudgeAC && this.Module == config.ModuleC { //当为练习或竞赛时检测
sim, Sim_s_id = this.get_sim(this.Sid, this.Language)
}
this.Sim = sim
this.Sim_s_id = Sim_s_id
this.UpdateSolution()
}
func (this *solution) UpdateRecord() {
if this.Module != config.ModuleP {
return
}
solutionModel := model.SolutionModel{}
qry := make(map[string]string)
qry["module"] = strconv.Itoa(config.ModuleP)
qry["action"] = "submit"
qry["pid"] = strconv.Itoa(this.Pid)
submit, _ := solutionModel.Count(qry)
qry["action"] = "solve"
solve, _ := solutionModel.Count(qry)
proModel := model.ProblemModel{}
err := proModel.Record(this.Pid, solve, submit)
if err != nil {
logger.Println(err)
}
qry["action"] = "submit"
qry["uid"] = this.Uid
delete(qry, "pid")
delete(qry, "module")
submit, _ = solutionModel.Count(qry)
solvelist, err := solutionModel.Achieve(this.Uid, config.ModuleP, config.ModuleP)
if err != nil {
logger.Println(err)
}
solve = len(solvelist)
userModel := model.UserModel{}
err = userModel.Record(this.Uid, solve, submit)
if err != nil {
logger.Println(err)
}
}
//get_sim 相似度检测,返回值为相似度和相似的id
func (this *solution) get_sim(Sid, Language int) (sim, Sim_s_id int) {
var extension string
if this.Language == config.LanguageC {
extension = "c"
} else if this.Language == config.LanguageCPP {
extension = "cc"
} else if this.Language == config.LanguageJAVA {
extension = "java"
}
qry := make(map[string]string)
qry["pid"] = strconv.Itoa(this.Pid)
qry["action"] = "solve"
solutionModel := model.SolutionModel{}
list, err := solutionModel.List(qry)
workdir := runPath + "/" + strconv.Itoa(this.Sid)
sim_test_dir := workdir + "/sim_test"
cmd := exec.Command("mkdir", "-p", sim_test_dir)
err = cmd.Run()
if err != nil {
logger.Println(err)
return
}
defer os.RemoveAll(workdir)
codefile, err := os.Create(sim_test_dir + "/../Main." + extension)
defer codefile.Close()
_, err = codefile.WriteString(this.Code)
if err != nil {
logger.Println("source code writing to file failed")
}
var count int
for i := range list {
sid := list[i].Sid
sol, err := solutionModel.Detail(sid)
if sid < this.Sid && err == nil {
filepath := sim_test_dir + "/" + strconv.Itoa(sid) + "." + extension
codefile, err := os.Create(filepath)
defer codefile.Close()
_, err = codefile.WriteString(sol.Code)
if err != nil {
logger.Println("source code writing to file failed")
}
count++
}
}
cmd = exec.Command("sim.sh", sim_test_dir, extension)
if err = cmd.Run(); err != nil {
return
}
defer os.Remove("./simfile")
if _, err := os.Stat("./simfile"); err == nil {
simfile, err := os.Open("./simfile")
if err != nil {
logger.Println("sim file open error")
return
}
defer simfile.Close()
fmt.Fscanf(simfile, "%d %d", &sim, &Sim_s_id)
}
return sim, Sim_s_id
}
//UpdateSolution 更新判题结果
func (this *solution) UpdateSolution() {
sid, err := strconv.Atoi(strconv.Itoa(this.Sid))
solutionModel := model.SolutionModel{}
ori, err := solutionModel.Detail(sid)
if err != nil {
logger.Println(err)
return
}
ori.Judge = this.Judge
ori.Time = this.Time
ori.Memory = this.Memory
ori.Sim = this.Sim
ori.Sim_s_id = this.Sim_s_id
ori.Error = this.Error
err = solutionModel.Update(sid, *ori)
if err != nil {
logger.Println(err)
return
}
}