Skip to content

Commit 981b90f

Browse files
feat: add new contest file based on JSON read support (#113)
Co-authored-by: Coelacanthus <[email protected]>
1 parent 1a5baee commit 981b90f

17 files changed

+376
-109
lines changed

src/base/LemonConfig.cpp

Lines changed: 22 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -12,22 +12,22 @@
1212

1313
namespace Lemon::base::config {
1414

15-
void LemonConfigJudge::read(const QJsonObject &json) {
16-
READ_JSON_INT(defaultFullScore)
17-
READ_JSON_INT(defaultTimeLimit)
18-
READ_JSON_INT(defaultMemoryLimit)
19-
READ_JSON_INT(compileTimeLimit)
20-
READ_JSON_INT(specialJudgeTimeLimit)
21-
READ_JSON_INT(fileSizeLimit)
22-
READ_JSON_INT(rejudgeTimes)
23-
24-
READ_JSON_STR(defaultInputFileExtension)
25-
READ_JSON_STR(defaultOutputFileExtension)
26-
READ_JSON_STR(diffPath)
27-
28-
READ_JSON_STRLIST(inputFileExtensions)
29-
READ_JSON_STRLIST(outputFileExtensions)
30-
READ_JSON_STRLIST(recentContest)
15+
int LemonConfigJudge::read(const QJsonObject &json) {
16+
READ_JSON(json, defaultFullScore);
17+
READ_JSON(json, defaultTimeLimit);
18+
READ_JSON(json, defaultMemoryLimit);
19+
READ_JSON(json, compileTimeLimit);
20+
READ_JSON(json, specialJudgeTimeLimit);
21+
READ_JSON(json, fileSizeLimit);
22+
READ_JSON(json, rejudgeTimes);
23+
24+
READ_JSON(json, defaultInputFileExtension);
25+
READ_JSON(json, defaultOutputFileExtension);
26+
READ_JSON(json, diffPath);
27+
28+
READ_JSON(json, inputFileExtensions);
29+
READ_JSON(json, outputFileExtensions);
30+
READ_JSON(json, recentContest);
3131

3232
// CompilerList
3333
if (json.contains("compilerList") && json["compilerList"].isArray()) {
@@ -37,12 +37,16 @@ namespace Lemon::base::config {
3737
for (int compilerIndex = 0; compilerIndex < _compilerList.size(); ++compilerIndex) {
3838
QJsonObject compilerObject = _compilerList[compilerIndex].toObject();
3939
Compiler *compiler = new Compiler;
40-
compiler->read(compilerObject);
40+
if (compiler->read(compilerObject) == -1)
41+
return -1;
4142
compilerList.append(compiler);
4243
}
4344
}
45+
return 0;
4446
}
4547

46-
void LemonConfigJudge::write(QJsonObject &json) const {}
48+
void LemonConfigJudge::write(QJsonObject &json) const {
49+
// TODO: Write Config
50+
}
4751

4852
} // namespace Lemon::base::config

src/base/LemonConfig.hpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ namespace Lemon::base::config {
3434
QString diffPath;
3535

3636
public:
37-
void read(const QJsonObject &json);
37+
int read(const QJsonObject &json);
3838
void write(QJsonObject &json) const;
3939
};
4040

@@ -44,7 +44,7 @@ namespace Lemon::base::config {
4444
// Prepare for theme setting
4545
// QString theme = ;
4646
public:
47-
void read(const QJsonObject &json);
47+
int read(const QJsonObject &json);
4848
void write(QJsonObject &json) const;
4949
};
5050

src/base/LemonUtils.hpp

Lines changed: 99 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -6,33 +6,110 @@
66
*/
77

88
#pragma once
9-
//
9+
#include <LemonType.hpp>
1010
#include <QDir>
11+
#include <QJsonArray>
12+
#include <QJsonObject>
1113

12-
#define READ_JSON_INT(___x) \
13-
if (json.contains("___x") && json["___x"].isDouble()) \
14-
___x = json["___x"].toInt();
15-
#define READ_JSON_BOOL(___x) \
16-
if (json.contains("___x") && json["___x"].isBool()) \
17-
___x = json["___x"].toBool();
18-
#define READ_JSON_DOUBLE(___x) \
19-
if (json.contains("___x") && json["___x"].isDouble()) \
20-
___x = json["___x"].toDouble();
21-
#define READ_JSON_STR(___x) \
22-
if (json.contains("___x") && json["___x"].isString()) \
23-
___x = json["___x"].toString();
24-
#define READ_JSON_STRLIST(___x) \
25-
if (json.contains("___x") && json["___x"].isString()) \
26-
___x = json["___x"].toString().split(QLatin1Char(';'), Qt::SkipEmptyParts);
27-
28-
#define WRITE_JSON(___x) json["___x"] = ___x;
29-
#define WRITE_JSON_STRLIST(___x) json["___x"] = ___x.join(QLatin1Char(';'));
30-
14+
namespace Lemon::detail {
15+
inline int JsonReadHelper(QString &val, const QJsonValue &jval) {
16+
if (jval.isString()) {
17+
val = jval.toString();
18+
return 0;
19+
} else
20+
return -1;
21+
}
22+
inline int JsonReadHelper(int &val, const QJsonValue &jval) {
23+
if (jval.isDouble()) {
24+
val = jval.toInt();
25+
return 0;
26+
} else
27+
return -1;
28+
}
29+
inline int JsonReadHelper(bool &val, const QJsonValue &jval) {
30+
if (jval.isBool()) {
31+
val = jval.toBool();
32+
return 0;
33+
} else
34+
return -1;
35+
}
36+
inline int JsonReadHelper(double &val, const QJsonValue &jval) {
37+
if (jval.isDouble()) {
38+
val = jval.toDouble();
39+
return 0;
40+
} else
41+
return -1;
42+
}
43+
inline int JsonReadHelper(QJsonObject &val, const QJsonValue &jval) {
44+
if (jval.isObject()) {
45+
val = jval.toObject();
46+
return 0;
47+
} else
48+
return -1;
49+
}
50+
inline int JsonReadHelper(QJsonArray &val, const QJsonValue &jval) {
51+
if (jval.isArray()) {
52+
val = jval.toArray();
53+
QJsonArray arr;
54+
return 0;
55+
} else
56+
return -1;
57+
}
58+
inline int JsonReadHelper(ResultState &val, const QJsonValue &jval) {
59+
int x;
60+
if (JsonReadHelper(x, jval) == -1)
61+
return -1;
62+
val = ResultState(x);
63+
return 0;
64+
}
65+
inline int JsonReadHelper(CompileState &val, const QJsonValue &jval) {
66+
int x;
67+
if (JsonReadHelper(x, jval) == -1)
68+
return -1;
69+
val = CompileState(x);
70+
return 0;
71+
}
72+
template <typename T> int JsonReadHelper(QList<T> &val, const QJsonValue &jval) {
73+
QJsonArray arr;
74+
return JsonReadHelper(arr, jval) == -1 || JsonReadHelper(val, arr) == -1 ? -1 : 0;
75+
}
76+
inline int JsonReadHelper(QStringList &val, const QJsonArray &jval) {
77+
QList<QString> s;
78+
if (JsonReadHelper(s, jval) == -1)
79+
return -1;
80+
val = s;
81+
return 0;
82+
}
83+
template <typename T> int JsonReadHelper(QList<T> &val, const QJsonArray &jval) {
84+
val.clear();
85+
for (auto i : jval) {
86+
T x;
87+
if (JsonReadHelper(x, i) == -1)
88+
return -1;
89+
val.append(x);
90+
}
91+
return 0;
92+
}
93+
template <typename T> int JsonReadHelper(T &val, const QString &name, const QJsonObject &json);
94+
template <typename T> int JsonReadHelper(T &val, const QString &name, const QJsonObject &json) {
95+
if (json.contains(name))
96+
return JsonReadHelper(val, json[name]);
97+
else
98+
return -1;
99+
}
100+
} // namespace Lemon::detail
101+
#define WRITE_JSON(json, ___x) json[#___x] = ___x;
102+
#define WRITE_JSON_STRLIST(json, ___x) json[#___x] = ___x.join(QLatin1Char(';'));
103+
namespace Lemon {
104+
template <typename T> int readJson(T &x, const QString &name, const QJsonObject &json) {
105+
return detail::JsonReadHelper(x, name, json);
106+
}
107+
} // namespace Lemon
108+
#define READ_JSON(json, x) Lemon::readJson(x, #x, json)
31109
namespace Lemon::common {
32110

33111
QStringList GetFileList(const QDir &dir);
112+
34113
bool FileExistsIn(const QDir &dir, const QString &fileName);
35114

36115
} // namespace Lemon::common
37-
38-
using namespace Lemon::common;

src/base/compiler.cpp

Lines changed: 29 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -116,20 +116,20 @@ void Compiler::copyFrom(Compiler *other) {
116116
disableMemoryLimitCheck = other->getDisableMemoryLimitCheck();
117117
}
118118

119-
void Compiler::read(const QJsonObject &json) {
119+
int Compiler::read(const QJsonObject &json) {
120120

121121
if (json.contains("compilerType") && json["compilerType"].isDouble())
122122
compilerType = CompilerType(json["compilerType"].toInt());
123123

124-
READ_JSON_STR(compilerName)
125-
READ_JSON_STR(compilerLocation)
126-
READ_JSON_STR(interpreterLocation)
124+
READ_JSON(json, compilerName);
125+
READ_JSON(json, compilerLocation);
126+
READ_JSON(json, interpreterLocation);
127127

128-
READ_JSON_STRLIST(sourceExtensions)
129-
READ_JSON_STRLIST(bytecodeExtensions)
130-
READ_JSON_STRLIST(configurationNames)
131-
READ_JSON_STRLIST(compilerArguments)
132-
READ_JSON_STRLIST(interpreterArguments)
128+
READ_JSON(json, sourceExtensions);
129+
READ_JSON(json, bytecodeExtensions);
130+
READ_JSON(json, configurationNames);
131+
READ_JSON(json, compilerArguments);
132+
READ_JSON(json, interpreterArguments);
133133

134134
QStringList _environment;
135135
if (json.contains("environment") && json["environment"].isString()) {
@@ -143,26 +143,27 @@ void Compiler::read(const QJsonObject &json) {
143143
environment.insert(variable, value);
144144
}
145145

146-
READ_JSON_DOUBLE(timeLimitRatio)
147-
READ_JSON_DOUBLE(memoryLimitRatio)
148-
READ_JSON_BOOL(disableMemoryLimitCheck)
146+
READ_JSON(json, timeLimitRatio);
147+
READ_JSON(json, memoryLimitRatio);
148+
READ_JSON(json, disableMemoryLimitCheck);
149+
return 0;
149150
}
150151

151152
void Compiler::write(QJsonObject &json) const {
152-
WRITE_JSON(compilerType)
153-
WRITE_JSON(compilerName)
154-
WRITE_JSON(compilerLocation)
155-
WRITE_JSON(interpreterLocation)
156-
157-
WRITE_JSON_STRLIST(sourceExtensions)
158-
WRITE_JSON_STRLIST(bytecodeExtensions)
159-
WRITE_JSON_STRLIST(configurationNames)
160-
WRITE_JSON_STRLIST(compilerArguments)
161-
WRITE_JSON_STRLIST(interpreterArguments)
162-
163-
WRITE_JSON_STRLIST(environment.toStringList())
164-
165-
WRITE_JSON(timeLimitRatio) // double
166-
WRITE_JSON(memoryLimitRatio) // double
167-
WRITE_JSON(disableMemoryLimitCheck) // bool
153+
WRITE_JSON(json, compilerType)
154+
WRITE_JSON(json, compilerName)
155+
WRITE_JSON(json, compilerLocation)
156+
WRITE_JSON(json, interpreterLocation)
157+
158+
WRITE_JSON_STRLIST(json, sourceExtensions)
159+
WRITE_JSON_STRLIST(json, bytecodeExtensions)
160+
WRITE_JSON_STRLIST(json, configurationNames)
161+
WRITE_JSON_STRLIST(json, compilerArguments)
162+
WRITE_JSON_STRLIST(json, interpreterArguments)
163+
164+
WRITE_JSON_STRLIST(json, environment.toStringList())
165+
166+
WRITE_JSON(json, timeLimitRatio) // double
167+
WRITE_JSON(json, memoryLimitRatio) // double
168+
WRITE_JSON(json, disableMemoryLimitCheck) // bool
168169
}

src/base/compiler.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ class Compiler : public QObject {
5454

5555
void copyFrom(Compiler *);
5656

57-
void read(const QJsonObject &json);
57+
int read(const QJsonObject &json);
5858
void write(QJsonObject &json) const;
5959

6060
private:

src/component/exportutil/exportutil.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -129,7 +129,7 @@ auto ExportUtil::getContestantHtmlCode(Contest *contest, Contestant *contestant,
129129
QList<QStringList> message = contestant->getMessage(i);
130130
QList<QList<int>> timeUsed = contestant->getTimeUsed(i);
131131
QList<QList<int>> memoryUsed = contestant->getMemoryUsed(i);
132-
QList<QList<int>> score = contestant->getSocre(i);
132+
QList<QList<int>> score = contestant->getScore(i);
133133

134134
for (int j = 0; j < inputFiles.size(); j++) {
135135
for (int k = 0; k < inputFiles[j].size(); k++) {
@@ -490,7 +490,7 @@ auto ExportUtil::getSmallerContestantHtmlCode(Contest *contest, Contestant *cont
490490
QList<QStringList> message = contestant->getMessage(i);
491491
QList<QList<int>> timeUsed = contestant->getTimeUsed(i);
492492
QList<QList<int>> memoryUsed = contestant->getMemoryUsed(i);
493-
QList<QList<int>> score = contestant->getSocre(i);
493+
QList<QList<int>> score = contestant->getScore(i);
494494

495495
for (int j = 0; j < inputFiles.size(); j++) {
496496
for (int k = 0; k < inputFiles[j].size(); k++) {

src/core/contest.cpp

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99

1010
#include "contest.h"
1111
//
12+
#include "base/LemonUtils.hpp"
1213
#include "base/compiler.h"
1314
#include "base/settings.h"
1415
#include "core/assignmentthread.h"
@@ -353,7 +354,34 @@ void Contest::writeToStream(QDataStream &out) {
353354
i->writeToStream(out);
354355
}
355356
}
357+
int Contest::readFromJson(const QJsonObject &in) {
358+
READ_JSON(in, contestTitle);
356359

360+
QJsonArray tasks;
361+
READ_JSON(in, tasks);
362+
363+
taskList.clear();
364+
for (const auto &task : tasks) {
365+
Task *newTask = new Task(this);
366+
if (newTask->readFromJson(task.toObject()) == -1)
367+
return -1;
368+
taskList.append(newTask);
369+
}
370+
371+
QJsonArray contestants;
372+
READ_JSON(in, contestants);
373+
374+
contestantList.clear();
375+
for (const auto &contestant : contestants) {
376+
auto *newContestant = new Contestant(this);
377+
if (newContestant->readFromJson(contestant.toObject()) == -1)
378+
return -1;
379+
connect(this, &Contest::taskAddedForContestant, newContestant, &Contestant::addTask);
380+
connect(this, &Contest::taskDeletedForContestant, newContestant, &Contestant::deleteTask);
381+
contestantList.insert(newContestant->getContestantName(), newContestant);
382+
}
383+
return 0;
384+
}
357385
void Contest::readFromStream(QDataStream &in) {
358386
int count = 0;
359387
in >> contestTitle;

src/core/contest.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@ class Contest : public QObject {
4242
void deleteContestant(const QString &);
4343
void writeToStream(QDataStream &);
4444
void readFromStream(QDataStream &);
45+
int readFromJson(const QJsonObject &);
4546

4647
private:
4748
QString contestTitle;

0 commit comments

Comments
 (0)