-
Notifications
You must be signed in to change notification settings - Fork 0
/
general.h
executable file
·349 lines (306 loc) · 11.2 KB
/
general.h
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
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
/*
* This file is part of community judge client project developed by Shervin Kh.
* Copyright (C) 2014 Shervin Kh.
* License: GPLv3 Or Later
* Full license could be found in License file shipped with program or at http://www.gnu.org/licenses/
*/
#ifndef GENERAL_H
#define GENERAL_H
#include <QDataStream>
#include <QString>
#include <QColor>
#include <QSharedPointer>
#include <QMap>
#include <QVariant>
#include <QDateTime>
#if QT_VERSION < 0x050000
#ifndef NULL
#define NULL 0
#endif
#define Q_NULLPTR NULL
#endif
enum class StatCode : int
{
RegisterationRequest, LoginRequest, SubmissionRequest, ResultRequest, CompilerLogRequest, ChangePasswordRequest,
ViewCodeRequest,
InternalDatabaseError,
UserOK, NoSuchUser, UserNotApproved, InvalidPassword, AdminOK,
OperationSuccessful, NotAuthorized, IncompatibleVersion,
AlreadyExists,
NoSuchProblem, SlowDown, InternalFileOperationError,
Disabled, InQueue, Running, ServerError, ResourceLimit, RunError, WrongAnswer, Correct, CompileError, CompleteResult,
ProblemManagementQuery, ProblemManagementDetails, ProblemManagementEdit, ProblemManagementRemove,
ProblemManagementRejudge, ProblemNotEnoughData,
ScorePlanQuery, ScorePlanDetails, ScorePlanEdit, ScorePlanRemove,
UserQuery, UserChangeAdmin, UserChangeActivation, RenameUser, UserDelete, UserChangeDescription, NotTheOnlyAdmin,
ChangePublicState, DeleteSubmission, ViewProfile, GetConfig, SetConfig,
NotAvailable, SubmissionPurge, PurgeFiles, GiveDescription, GetNews, EditNews, RemoveNews,
CannotViewSubmission, ContestUserQuery, ContestListQuery, ContestDetails, CurrentContests, RegisterContest, ContestEdit, ContestRemove,
AlreadyRegistered, CannotProcessAtThisTime, ScoreboardConfig, ScoreboardEdit, ScoreboardMetaData, Scoreboard, UserRecord
};
enum class Operation {None, Remove, Add, Edit, Request, Other};
const QString timeUnits[] = {"ms", "s"};
const QString memUnits[] = {"KB", "MB", "GB"};
const QString statusString[] = {"In Queue", "Running", "Server Error", "Resource Limit", "Runtime Error", "Wrong Answer",
"Correct", "Compilation Error", "Complete Result"};
const QColor backgroundColors[] = {Qt::gray, Qt::white, QColor(102, 102, 255), Qt::yellow, QColor(255, 140, 0),
QColor(255, 51, 51), Qt::green, QColor(205, 92, 92), Qt::cyan};
const QString viewCodePermissions[] = {"Nobody", "Only users with correct submission", "Everybody"};
const QString scoreboardTypes[] = {"None", "Score", "#Corrects", "Score | #Corrects"};
const QString DATE_TIME_FORMAT = "yyyy/MM/dd hh:mm:ss";
class SubmissionResult
{
private:
qint64 sid;
QString uname;
QString pname;
qint64 dateTime;
StatCode status;
int numTest;
int totTest;
qint64 maxTime;
qint64 maxMem;
QList<qint8> fullResult;
bool isPub;
qreal scoreDff;
public:
SubmissionResult() {}
SubmissionResult(qint64 subid, const QString &un, const QString &pn, qint64 dt, StatCode stat,
int nt, int tt, qint64 mt, qint64 mm, const QList<qint8> &ls, bool pub)
: sid(subid), uname(un), pname(pn), dateTime(dt), status(stat), numTest(nt), totTest(tt), maxTime(mt),
maxMem(mm), fullResult(ls), isPub(pub) {}
qint64 submitID() const {return sid;}
QString username() const {return uname;}
QString probName() const {return pname;}
qint64 date() const {return dateTime;}
StatCode judgeStatus() const {return status;}
int numTests() const {return numTest;}
int totalTests() const {return totTest;}
qint64 maximumTime() const {return maxTime;}
qint64 maximumMemory() const {return maxMem;}
const QList<qint8> &completeResult() const {return fullResult;}
bool isPublic() const {return isPub;}
qreal scoreDiff() const {return scoreDff;}
SubmissionResult invertedPublicState() const
{
SubmissionResult ret(*this);
ret.isPub = !ret.isPub;
return ret;
}
SubmissionResult setInQueue() const
{
SubmissionResult ret(*this);
ret.status = StatCode::InQueue;
return ret;
}
friend QDataStream &operator>>(QDataStream &DS, SubmissionResult &in)
{
int tmp;
DS >> in.sid >> in.uname >> in.pname >> in.dateTime >> tmp >> in.numTest >> in.totTest
>> in.maxTime >> in.maxMem >> in.isPub >> in.scoreDff;
in.status = static_cast<StatCode>(tmp);
if (in.status == StatCode::CompleteResult)
DS >> in.fullResult;
return DS;
}
friend QDataStream & operator<<(QDataStream &DS, const SubmissionResult &in)
{
DS << in.sid << in.uname << in.pname << in.dateTime << static_cast<int>(in.status) << in.numTest
<< in.totTest << in.maxTime << in.maxMem << in.isPub << in.scoreDff;
if (in.status == StatCode::CompleteResult)
DS << in.fullResult;
return DS;
}
};
typedef QMap<QString, QVariant> DataTable;
typedef QSharedPointer<QDataStream> DataPointer;
class Problem
{
private:
qint64 id;
QString nam;
QString dsc;
int nTest;
qint64 TLimit;
qint64 MLimit;
bool isCompl;
bool enabled;
QString scoreP;
QString contst;
QString descFile;
QString foldr;
int pubsubs;
int othrssubs;
public:
Problem() {}
Problem(qint64 _id, const QString &nm, const QString &dc, int nt, qint64 tl, qint64 ml, bool comple,
bool enbl, const QString &sc, const QString &cont, const QString &df, const QString &fldr, int ps, int os)
: id(_id), nam(nm), dsc(dc), nTest(nt), TLimit(tl), MLimit(ml), isCompl(comple), enabled(enbl)
, scoreP(sc), contst(cont), descFile(df), foldr(fldr), pubsubs(ps), othrssubs(os) {}
qint64 ID() const {return id;}
QString name() const {return nam;}
QString description() const {return dsc;}
int numTests() const {return nTest;}
qint64 timeLimit() const {return TLimit;}
qint64 memoryLimit() const {return MLimit;}
bool isComplete() const {return isCompl;}
bool isEnabled() const {return enabled;}
QString scorePlan() const {return scoreP;}
QString contest() const {return contst;}
QString descriptionFile() const {return descFile;}
QString folder() const {return foldr;}
int publicSubmissions() const {return pubsubs;}
int othersSubmissions() const {return othrssubs;}
friend QDataStream & operator>>(QDataStream &DS, Problem &in)
{
DS >> in.id >> in.nam >> in.dsc >> in.nTest >> in.TLimit >> in.MLimit >> in.isCompl
>> in.enabled >> in.scoreP >> in.contst >> in.descFile >> in.foldr >> in.pubsubs >> in.othrssubs;
return DS;
}
friend QDataStream & operator<<(QDataStream &DS, const Problem &in)
{
DS << in.id << in.nam << in.dsc << in.nTest << in.TLimit << in.MLimit << in.isCompl
<< in.enabled << in.scoreP << in.contst << in.descFile << in.foldr << in.pubsubs << in.othrssubs;
return DS;
}
};
class ScorePlan
{
private:
qint64 id;
QString nam;
qreal p1;
qreal p2;
qreal convTo;
qreal mul;
qreal give(qreal in)
{
in = qMin(in, 10000.0);
qreal num = convTo * in + 2 * (1 - convTo);
return num / in;
}
public:
ScorePlan() {}
ScorePlan(qint64 _id, QString _nam, qreal _p1, qreal _p2, qreal _convTo, qreal _mul) :
id(_id), nam(_nam), p1(_p1), p2(_p2), convTo(_convTo), mul(_mul) {}
void init(qreal _penalty1, qreal _penalty2, qreal cTo, qreal mult)
{
p1 = _penalty1;
p2 = _penalty2;
convTo = cTo;
mul = mult;
}
qint64 ID() const {return id;}
QString name() const {return nam;}
qreal penalty1() const {return p1;}
qreal penalty2() const {return p2;}
qreal convergeTo() const {return convTo;}
qreal multiplier() const {return mul;}
qreal query(qreal val1, qreal val2)
{
return give(pow(val1, p1) + pow(val2, p2)) * mul;
}
friend QDataStream & operator<<(QDataStream &DS, const ScorePlan &in)
{
DS << in.id << in.nam << in.p1 << in.p2 << in.convTo << in.mul;
return DS;
}
friend QDataStream & operator>>(QDataStream &DS, ScorePlan &in)
{
DS >> in.id >> in.nam >> in.p1 >> in.p2 >> in.convTo >> in.mul;
return DS;
}
};
class User
{
private:
QString nam;
QString desc;
QDateTime joind;
bool enabld;
qint64 subs;
qint64 cors;
bool isAdm;
qreal scre;
public:
User() {}
User(const QString &_nam, const QString &_desc, const QDateTime &_joind, bool _enabld, qint64 _subs, qint64 _cors, bool _isAdm, qreal _scre)
: nam(_nam), desc(_desc), joind(_joind), enabld(_enabld), subs(_subs), cors(_cors), isAdm(_isAdm), scre(_scre) {}
QString name() const {return nam;}
QString description() const {return desc;}
bool enabled() const {return enabld;}
qint64 numSubmits() const {return subs;}
qint64 numCorrects() const {return cors;}
bool isAdmin() const {return isAdm;}
qreal score() const {return scre;}
QDateTime joinDate() const {return joind;}
void changeAdmin()
{
isAdm = !isAdm;
}
void changeActivation()
{
enabld = !enabld;
}
void changeDesc(const QString &str)
{
desc = str;
}
void changeName(const QString &str)
{
nam = str;
}
friend QDataStream & operator<<(QDataStream &DS, const User &in)
{
DS << in.nam << in.desc << in.joind << in.enabld << in.subs << in.cors << in.isAdm << in.scre;
return DS;
}
friend QDataStream & operator>>(QDataStream &DS, User &in)
{
DS >> in.nam >> in.desc >> in.joind >> in.enabld >> in.subs >> in.cors >> in.isAdm >> in.scre;
return DS;
}
};
class Contest
{
private:
qint64 id;
QString nam;
QString dsc;
bool enbld;
QDateTime regStart;
QDateTime regEnd;
QDateTime conStart;
QDateTime conEnd;
bool aftView;
bool aftSub;
public:
Contest() {}
Contest(qint64 _id, const QString &nm, const QString &dc, bool enab, const QDateTime &rs,
const QDateTime &re, const QDateTime &cs, const QDateTime &ce, bool av, bool as)
: id(_id), nam(nm), dsc(dc), enbld(enab), regStart(rs), regEnd(re), conStart(cs),
conEnd(ce), aftView(av), aftSub(as) {}
qint64 ID() const {return id;}
QString name() const {return nam;}
QString description() const {return dsc;}
bool enabled() const {return enbld;}
QDateTime registerStart() const {return regStart;}
QDateTime registerEnd() const {return regEnd;}
QDateTime contestStart() const {return conStart;}
QDateTime contestEnd() const {return conEnd;}
bool afterContestView() const {return aftView;}
bool afterContestSubmit() const {return aftSub;}
friend QDataStream & operator>>(QDataStream &DS, Contest &in)
{
DS >> in.id >> in.nam >> in.dsc >> in.enbld >> in.regStart >> in.regEnd
>> in.conStart >> in.conEnd >> in.aftView >> in.aftSub;
return DS;
}
friend QDataStream & operator<<(QDataStream &DS, const Contest &in)
{
DS << in.id << in.nam << in.dsc << in.enbld << in.regStart << in.regEnd
<< in.conStart << in.conEnd << in.aftView << in.aftSub;
return DS;
}
};
#endif // GENERAL_H