Commit 8ae277a8 authored by Ayush's avatar Ayush

Add compilation execution to arena

parent c339c587
...@@ -57,4 +57,4 @@ if ($ret_stat != 0) { ...@@ -57,4 +57,4 @@ if ($ret_stat != 0) {
unlink($in_fname); unlink($in_fname);
unlink($out_fname); unlink($out_fname);
?> ?>
\ No newline at end of file
...@@ -18,7 +18,7 @@ if(isset($postData) && !empty($postData)){ ...@@ -18,7 +18,7 @@ if(isset($postData) && !empty($postData)){
$sql = "SELECT out1 FROM questions WHERE title='$title'"; $sql = "SELECT out1 FROM questions WHERE title='$title'";
if($result = mysqli_query($mysqli,$sql)->fetch_all(MYSQLI_ASSOC)){ if($result = mysqli_query($mysqli,$sql)->fetch_all(MYSQLI_ASSOC)){
$bool = 0; $bool = 0;
if(result['out1'] === $out){ if($result[0]['out1'] === $out){
$bool=1; $bool=1;
} }
echo json_encode($bool); echo json_encode($bool);
...@@ -30,13 +30,15 @@ if(isset($postData) && !empty($postData)){ ...@@ -30,13 +30,15 @@ if(isset($postData) && !empty($postData)){
else{ else{
$sql = "SELECT out2 FROM questions WHERE title='$title'"; $sql = "SELECT out2 FROM questions WHERE title='$title'";
if($result = mysqli_query($mysqli,$sql)->fetch_all(MYSQLI_ASSOC)){ if($result = mysqli_query($mysqli,$sql)->fetch_all(MYSQLI_ASSOC)){
// echo json_encode($result);
// exit(0);
$bool = 0; $bool = 0;
if(result['out2'] === $out){ if($result[0]['out2'] === $out){
$bool=1; $bool=1;
} }
echo json_encode($bool); echo json_encode($bool);
} }
else{ else{
http_response_code(404); http_response_code(404);
} }
...@@ -44,4 +46,4 @@ if(isset($postData) && !empty($postData)){ ...@@ -44,4 +46,4 @@ if(isset($postData) && !empty($postData)){
} }
?> ?>
\ No newline at end of file
...@@ -13,4 +13,4 @@ ...@@ -13,4 +13,4 @@
<textarea id="editor" name="editor"></textarea> <textarea id="editor" name="editor"></textarea>
</div> </div>
<app-input (valueEmit)="customInput = $event"></app-input> <app-input (valueEmit)="customInput = $event"></app-input>
<app-submit-try-code [problem]="question"></app-submit-try-code> <app-submit-try-code [question]="question"></app-submit-try-code>
...@@ -3,13 +3,14 @@ import {Router} from '@angular/router'; ...@@ -3,13 +3,14 @@ import {Router} from '@angular/router';
// import {Problem} from '../problem'; // import {Problem} from '../problem';
// import {ProblemService} from '../problem.service'; // import {ProblemService} from '../problem.service';
import {ApiService} from '../api.service'; import {ApiService} from '../api.service';
import {InputComponent} from '../input/input.component'; import {InputComponent} from '../input/input.component';
import {SubmitTryCodeComponent} from '../submit-try-code/submit-try-code.component'; import {SubmitTryCodeComponent} from '../submit-try-code/submit-try-code.component';
import {RunCodeService} from '../run-code.service'; import {RunCodeService} from '../run-code.service';
import { Question } from '../question'; import {Question} from '../question';
import { QuestionService } from '../question.service'; import {QuestionService} from '../question.service';
import {FileService} from '../file.service';
import {File} from '../file';
declare const CodeMirror: any; declare const CodeMirror: any;
...@@ -22,6 +23,7 @@ export class ArenaComponent implements OnInit { ...@@ -22,6 +23,7 @@ export class ArenaComponent implements OnInit {
title: string; title: string;
question: Question; question: Question;
language: string;
code: string[] = [`#include <iostream> code: string[] = [`#include <iostream>
using namespace std; using namespace std;
...@@ -43,7 +45,8 @@ export class ArenaComponent implements OnInit { ...@@ -43,7 +45,8 @@ export class ArenaComponent implements OnInit {
constructor(public router: Router, private apiService: ApiService, constructor(public router: Router, private apiService: ApiService,
private runCodeService: RunCodeService, private runCodeService: RunCodeService,
private questionService: QuestionService) { private questionService: QuestionService,
private fileService: FileService) {
} }
ngOnInit(): void { ngOnInit(): void {
...@@ -73,23 +76,14 @@ export class ArenaComponent implements OnInit { ...@@ -73,23 +76,14 @@ export class ArenaComponent implements OnInit {
const extensions = ['.cpp', '.py', '.java']; const extensions = ['.cpp', '.py', '.java'];
const langsMime = ['text/x-c++src', 'text/x-python', 'text/x-java']; const langsMime = ['text/x-c++src', 'text/x-python', 'text/x-java'];
const code: string[] = [`#include <iostream> this.language = extensions[activeLang];
using namespace std;
int main() { const th = document.getElementById('toggle-lang') as HTMLDivElement;
cout << "Hello World!\\n"; th.onclick = () => {
return 0;
}`, `print("Hello World!")`, `class HelloWorld {
public static void main(String[] args) {
System.out.println("Hello, World!");
}
}`];
document.getElementById('toggle-lang').onclick = function(): void {
const th = this as HTMLDivElement;
activeLang = (activeLang + 1) % 3; activeLang = (activeLang + 1) % 3;
this.language = extensions[activeLang];
th.innerHTML = langs[activeLang]; th.innerHTML = langs[activeLang];
editor.setValue(code[activeLang]); editor.setValue(this.code[activeLang]);
editor.setOption('mode', langsMime[activeLang]); editor.setOption('mode', langsMime[activeLang]);
}; };
...@@ -107,12 +101,24 @@ export class ArenaComponent implements OnInit { ...@@ -107,12 +101,24 @@ export class ArenaComponent implements OnInit {
// } // }
tryCode(c): void { tryCode(c): void {
if (document.getElementById('try').classList.contains('disabled')) {return; } if (document.getElementById('try').classList.contains('disabled')) {
return;
}
this.isCompiling = true; this.isCompiling = true;
this.runCodeService.compileQuestionFile(this.question, this.code) const file: File = {
.subscribe(data => { username: JSON.parse(this.apiService.getToken()).username,
filename: this.question.title,
language: this.language,
text: this.code[(['.cpp', '.py', '.java']).indexOf(this.language)],
path: 'attempts/'
};
console.log(file);
this.fileService.upload(file).subscribe(data1 => {
console.log(data1);
this.runCodeService.compileFile(file).subscribe(data2 => {
console.log(data2);
this.submitField.submitting = c; this.submitField.submitting = c;
this.submitField.reset(); this.submitField.reset(file, [this.question.tc1, this.question.tc2]);
this.submitField.isActive = true; this.submitField.isActive = true;
this.isCompiling = false; this.isCompiling = false;
}, error => { }, error => {
...@@ -122,13 +128,20 @@ export class ArenaComponent implements OnInit { ...@@ -122,13 +128,20 @@ export class ArenaComponent implements OnInit {
this.compilationError = false; this.compilationError = false;
}, 3000); }, 3000);
}); });
}, error => {
this.isCompiling = false;
this.compilationError = true;
setTimeout(() => {
this.compilationError = false;
}, 3000);
});
} }
getQuestion(): void { getQuestion(): void {
this.questionService.getQues() this.questionService.getQues()
.subscribe(questions => { .subscribe(questions => {
this.question = questions.find(i => i.title === this.title); this.question = questions.find(i => i.title === this.title);
document.getElementById("p_s").innerHTML=this.question.statement; document.getElementById('p_s').innerHTML = this.question.statement;
}); });
} }
......
import {Injectable} from '@angular/core'; import {Injectable} from '@angular/core';
import {Problem} from './problem'; // import {Problem} from './problem';
import {Question} from './question'; import {Question} from './question';
import {File} from './file'; import {File} from './file';
import {Observable, of, throwError} from 'rxjs'; import {Observable, of, throwError} from 'rxjs';
...@@ -33,9 +33,8 @@ export class RunCodeService { ...@@ -33,9 +33,8 @@ export class RunCodeService {
// else {return throwError('error'); } // else {return throwError('error'); }
// } // }
tryTestcase(prob: Question, index: number): Observable<boolean> { verifyTestcase(title: string, out: string, ind: number): Observable<any> {
// return of(Math.floor(Math.random() * 2) === 1); return this.httpClient.post(this.baseUrl + 'questions/check_output.php', {title, out, ind});
} }
compileQuestionFile(ques: Question, code: string[]): Observable<any> { compileQuestionFile(ques: Question, code: string[]): Observable<any> {
......
...@@ -2,6 +2,9 @@ import {Component, Input, OnInit} from '@angular/core'; ...@@ -2,6 +2,9 @@ import {Component, Input, OnInit} from '@angular/core';
// import {Problem} from '../problem'; // import {Problem} from '../problem';
import {RunCodeService} from '../run-code.service'; import {RunCodeService} from '../run-code.service';
import {Question} from '../question'; import {Question} from '../question';
import {FileService} from '../file.service';
import {ApiService} from '../api.service';
import {File} from '../file';
@Component({ @Component({
selector: 'app-submit-try-code', selector: 'app-submit-try-code',
...@@ -13,21 +16,26 @@ export class SubmitTryCodeComponent implements OnInit { ...@@ -13,21 +16,26 @@ export class SubmitTryCodeComponent implements OnInit {
isActive = false; isActive = false;
nts: number[]; nts: number[];
status: number[]; status: number[];
@Input() problem: Question; @Input() question: Question;
submitting = 0; submitting = 0;
constructor(public runCodeService: RunCodeService) { } constructor(public runCodeService: RunCodeService, private fileService: FileService, private apiService: ApiService) { }
ngOnInit(): void { } ngOnInit(): void { }
reset(): void { reset(file: File, testcases: string[]): void {
this.nts = [0, 1]; //Array(this.problem.n_testcases[this.submitting]).fill(1).map((x, i) => i); console.log(file, testcases);
this.nts = [0, 1]; // Array(this.problem.n_testcases[this.submitting]).fill(1).map((x, i) => i);
this.status = [0, 0]; // Array(this.problem.n_testcases[this.submitting]).fill(0); this.status = [0, 0]; // Array(this.problem.n_testcases[this.submitting]).fill(0);
for (const i of this.nts) { for (const i of this.nts) {
this.runCodeService.tryTestcase(this.problem, i) this.runCodeService.executeFile(file, testcases[i])
.subscribe(ret => { .subscribe(ret => {
this.status[i] = ret ? 1 : -1; console.log(ret);
this.runCodeService.verifyTestcase(file.filename, ret, i)
.subscribe(data => {
this.status[i] = data === 1 ? 1 : -1;
});
}); });
} }
} }
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment