Skip to content

Commit

Permalink
Add cf-tools for submitting code to codeforces
Browse files Browse the repository at this point in the history
  • Loading branch information
coder3101 committed Jan 10, 2020
1 parent a9aaf62 commit 2261643
Show file tree
Hide file tree
Showing 5 changed files with 84 additions and 16 deletions.
2 changes: 1 addition & 1 deletion include/Core.hpp
Expand Up @@ -33,7 +33,7 @@ class Compiler : public QObject, private Core::Base::Files
Compiler(QString compileCpp, QString compileJava, int index, MessageLogger *log);

void compile(QCodeEditor *editor, QString lang = "Cpp");

void syncToBuffer(QCodeEditor *editor);
void updateCommandCpp(QString newCommand);
void updateCommandJava(QString newCommand);
void updateLanguage(QString newLang);
Expand Down
11 changes: 9 additions & 2 deletions include/cftools.hpp
Expand Up @@ -3,21 +3,28 @@

#include <QProcess>
#include <BaseFiles.hpp>
#include <MessageLogger.hpp>
namespace Network
{
class CFTools : private Core::Base::Files
class CFTools : public QObject, private Core::Base::Files
{
Q_OBJECT

public:
CFTools(int index);
CFTools(int index, MessageLogger* logger);

void submit(QString url, QString lang);
void killProcess();

static bool check();
~CFTools();

private slots:
void onReadReady();

private:
QProcess *cftool = nullptr;
MessageLogger* log;
int index;
};
} // namespace Network
Expand Down
24 changes: 14 additions & 10 deletions src/Core.cpp
Expand Up @@ -78,15 +78,8 @@ Compiler::~Compiler()
delete compilationProcess;
}

void Compiler::compile(QCodeEditor *editor, QString lang)
void Compiler::syncToBuffer(QCodeEditor *editor)
{
if (compilationProcess != nullptr)
{
compilationProcess->kill();
delete compilationProcess; // calls sigkill
compilationProcess = nullptr;
}

if (!file->isOpen())
file->open(QIODevice::ReadWrite | QFile::Text);

Expand All @@ -96,8 +89,6 @@ void Compiler::compile(QCodeEditor *editor, QString lang)
if (!javaFile->isOpen())
javaFile->open(QIODevice::ReadWrite | QFile::Text);

log->info("Compiler", "Compilation requested");

file->resize(0);
file->write(editor->toPlainText().toUtf8().toStdString().c_str());
file->close();
Expand All @@ -109,6 +100,19 @@ void Compiler::compile(QCodeEditor *editor, QString lang)
javaFile->resize(0);
javaFile->write(editor->toPlainText().toUtf8().toStdString().c_str());
javaFile->close();
}

void Compiler::compile(QCodeEditor *editor, QString lang)
{
if (compilationProcess != nullptr)
{
compilationProcess->kill();
delete compilationProcess; // calls sigkill
compilationProcess = nullptr;
}

syncToBuffer(editor);
log->info("Compiler", "Compilation requested");

if (lang == "Python")
{
Expand Down
56 changes: 55 additions & 1 deletion src/cftools.cpp
@@ -1,10 +1,20 @@
#include "cftools.hpp"
#include <QDebug>
#include <QUrl>
namespace Network
{

CFTools::CFTools(int index) : Core::Base::Files(index)
CFTools::CFTools(int index, MessageLogger *logger) : Core::Base::Files(index)
{
log = logger;
}

CFTools::~CFTools()
{
if (cftool != nullptr)
{
delete cftool;
}
}

void CFTools::killProcess()
Expand All @@ -13,6 +23,12 @@ void CFTools::killProcess()

void CFTools::submit(QString url, QString language)
{
if (cftool != nullptr)
{
delete cftool;
cftool = nullptr;
}

QString filePath;
if (language == "Cpp")
{
Expand All @@ -30,6 +46,25 @@ void CFTools::submit(QString url, QString language)
{
return;
}

auto problemCode = url.right(url.size() - 1 - url.lastIndexOf('/'));
auto splitRes = url.split('/');
QString problemContestId;
for (auto e : splitRes)
{
if (!e.isEmpty() && e.toInt())
{
problemContestId = e;
break;
}
}

cftool = new QProcess();
cftool->setProgram("cf");
cftool->setArguments({"submit", problemContestId, problemCode, filePath});
connect(cftool, SIGNAL(readyReadStandardOutput()), this, SLOT(onReadReady()));
cftool->start();

// Todo : Invoke cf-tools here.
}

Expand All @@ -45,4 +80,23 @@ bool CFTools::check()
return started;
}

void CFTools::onReadReady()
{
QString newResponse = cftool->readAll();
auto shortStatus = newResponse.right(newResponse.size() - newResponse.indexOf("status:") - 8).toStdString();
if (newResponse.contains("status: Happy New Year") || newResponse.contains("status: Accepted"))
{
// Warnings are green so its a hack to look green
log->warn("CFTool", shortStatus);
}
else if (newResponse.contains("status: Running on"))
{
log->info("CFTool", shortStatus);
}
else
{
log->error("CFTool", newResponse.toStdString(), newResponse.contains("status:"));
}
}

} // namespace Network
7 changes: 5 additions & 2 deletions src/mainwindow.cc
Expand Up @@ -282,17 +282,20 @@ void MainWindow::setCFToolsUI()
if (submitToCodeforces == nullptr)
{
submitToCodeforces = new QPushButton("Submit Solution", this);
cftools = new Network::CFTools(windowIndex);
cftools = new Network::CFTools(windowIndex, &log);
ui->horizontalLayout_9->addWidget(submitToCodeforces);
connect(submitToCodeforces, &QPushButton::clicked, this, [this] {
auto response = QMessageBox::warning(
this, "Sure to submit",
"Are you sure you want to submit this solution to codeforces?\nContest URL: " + companionData.url +
"Are you sure you want to submit this solution to codeforces?\n\n URL: " + companionData.url +
"\n Language : " + language,
QMessageBox::Yes | QMessageBox::No);

if (response == QMessageBox::Yes)
{
compiler->syncToBuffer(editor);
cftools->submit(companionData.url, language);
}
});

}
Expand Down

0 comments on commit 2261643

Please sign in to comment.