Commit 8ae277a8 authored by Ayush's avatar Ayush

Add compilation execution to arena

parent c339c587
......@@ -18,7 +18,7 @@ if(isset($postData) && !empty($postData)){
$sql = "SELECT out1 FROM questions WHERE title='$title'";
if($result = mysqli_query($mysqli,$sql)->fetch_all(MYSQLI_ASSOC)){
$bool = 0;
if(result['out1'] === $out){
if($result[0]['out1'] === $out){
$bool=1;
}
echo json_encode($bool);
......@@ -30,8 +30,10 @@ if(isset($postData) && !empty($postData)){
else{
$sql = "SELECT out2 FROM questions WHERE title='$title'";
if($result = mysqli_query($mysqli,$sql)->fetch_all(MYSQLI_ASSOC)){
// echo json_encode($result);
// exit(0);
$bool = 0;
if(result['out2'] === $out){
if($result[0]['out2'] === $out){
$bool=1;
}
......
......@@ -13,4 +13,4 @@
<textarea id="editor" name="editor"></textarea>
</div>
<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';
// import {Problem} from '../problem';
// import {ProblemService} from '../problem.service';
import {ApiService} from '../api.service';
import {InputComponent} from '../input/input.component';
import {SubmitTryCodeComponent} from '../submit-try-code/submit-try-code.component';
import {RunCodeService} from '../run-code.service';
import { Question } from '../question';
import { QuestionService } from '../question.service';
import {Question} from '../question';
import {QuestionService} from '../question.service';
import {FileService} from '../file.service';
import {File} from '../file';
declare const CodeMirror: any;
......@@ -22,6 +23,7 @@ export class ArenaComponent implements OnInit {
title: string;
question: Question;
language: string;
code: string[] = [`#include <iostream>
using namespace std;
......@@ -43,7 +45,8 @@ export class ArenaComponent implements OnInit {
constructor(public router: Router, private apiService: ApiService,
private runCodeService: RunCodeService,
private questionService: QuestionService) {
private questionService: QuestionService,
private fileService: FileService) {
}
ngOnInit(): void {
......@@ -73,23 +76,14 @@ export class ArenaComponent implements OnInit {
const extensions = ['.cpp', '.py', '.java'];
const langsMime = ['text/x-c++src', 'text/x-python', 'text/x-java'];
const code: string[] = [`#include <iostream>
using namespace std;
int main() {
cout << "Hello World!\\n";
return 0;
}`, `print("Hello World!")`, `class HelloWorld {
public static void main(String[] args) {
System.out.println("Hello, World!");
}
}`];
this.language = extensions[activeLang];
document.getElementById('toggle-lang').onclick = function(): void {
const th = this as HTMLDivElement;
const th = document.getElementById('toggle-lang') as HTMLDivElement;
th.onclick = () => {
activeLang = (activeLang + 1) % 3;
this.language = extensions[activeLang];
th.innerHTML = langs[activeLang];
editor.setValue(code[activeLang]);
editor.setValue(this.code[activeLang]);
editor.setOption('mode', langsMime[activeLang]);
};
......@@ -107,12 +101,24 @@ export class ArenaComponent implements OnInit {
// }
tryCode(c): void {
if (document.getElementById('try').classList.contains('disabled')) {return; }
if (document.getElementById('try').classList.contains('disabled')) {
return;
}
this.isCompiling = true;
this.runCodeService.compileQuestionFile(this.question, this.code)
.subscribe(data => {
const file: File = {
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.reset();
this.submitField.reset(file, [this.question.tc1, this.question.tc2]);
this.submitField.isActive = true;
this.isCompiling = false;
}, error => {
......@@ -122,13 +128,20 @@ export class ArenaComponent implements OnInit {
this.compilationError = false;
}, 3000);
});
}, error => {
this.isCompiling = false;
this.compilationError = true;
setTimeout(() => {
this.compilationError = false;
}, 3000);
});
}
getQuestion(): void {
this.questionService.getQues()
.subscribe(questions => {
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 {Problem} from './problem';
// import {Problem} from './problem';
import {Question} from './question';
import {File} from './file';
import {Observable, of, throwError} from 'rxjs';
......@@ -33,9 +33,8 @@ export class RunCodeService {
// else {return throwError('error'); }
// }
tryTestcase(prob: Question, index: number): Observable<boolean> {
// return of(Math.floor(Math.random() * 2) === 1);
verifyTestcase(title: string, out: string, ind: number): Observable<any> {
return this.httpClient.post(this.baseUrl + 'questions/check_output.php', {title, out, ind});
}
compileQuestionFile(ques: Question, code: string[]): Observable<any> {
......
......@@ -2,6 +2,9 @@ import {Component, Input, OnInit} from '@angular/core';
// import {Problem} from '../problem';
import {RunCodeService} from '../run-code.service';
import {Question} from '../question';
import {FileService} from '../file.service';
import {ApiService} from '../api.service';
import {File} from '../file';
@Component({
selector: 'app-submit-try-code',
......@@ -13,21 +16,26 @@ export class SubmitTryCodeComponent implements OnInit {
isActive = false;
nts: number[];
status: number[];
@Input() problem: Question;
@Input() question: Question;
submitting = 0;
constructor(public runCodeService: RunCodeService) { }
constructor(public runCodeService: RunCodeService, private fileService: FileService, private apiService: ApiService) { }
ngOnInit(): void { }
reset(): void {
this.nts = [0, 1]; //Array(this.problem.n_testcases[this.submitting]).fill(1).map((x, i) => i);
reset(file: File, testcases: string[]): void {
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);
for (const i of this.nts) {
this.runCodeService.tryTestcase(this.problem, i)
this.runCodeService.executeFile(file, testcases[i])
.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