Skip to content

Commit

Permalink
Merge pull request #32 from Killerrekt/master
Browse files Browse the repository at this point in the history
Implemented the group scoring system, error will be shown for invalid question_id,test_id,request errors etc and added the multiplier
  • Loading branch information
Mr-Emerald-Wolf authored Sep 6, 2023
2 parents ef7378f + 566387b commit 78ddfb5
Showing 1 changed file with 107 additions and 28 deletions.
135 changes: 107 additions & 28 deletions api/controllers/submission.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ const testdb = require("../models/testCasesModel.js");
const axios = require("axios");
const jwt = require("jsonwebtoken");
const User = require("../models/User.js");
const { get } = require("../routes/submission.js");
const ObjectId = require('mongoose').Types.ObjectId


class submission{
Expand All @@ -31,22 +31,28 @@ class submission{
const {question_id,code,language_id} = req.body;
if(!question_id){
res.status(400).json({
message : "Question ID was not given"
Error : "Question ID was not given"
})
return;
}
if(!code){
res.status(400).json({
message : "Code was not given"
Error : "Code was not given"
})
return;
}
if(!language_id){
res.status(400).json({
message : "Language ID not given"
Error : "Language ID not given"
})
return;
}
if(!ObjectId.isValid(question_id)){
res.status(400).json({
Error : "Question ID is not an ObjectID"
});
return;
}
const check = await submission_db.findOne({regNo : reg_no,question_id : question_id},"code score");
if(check && check.code == code){
await this.create_score(reg_no);
Expand All @@ -57,20 +63,74 @@ class submission{
return;
}

const testcase = await questiondb.findById(question_id,'testCases');
let multipler;
switch(parseInt(language_id)){
case 50://for C
case 54://for C++
case 63://for Node JS/Javascript
case 60://for Go
case 73://for Rust
multipler = 1;
break;
case 51://for C#
case 62://for java
multipler = 2;
break;
case 68://for PHP
multipler = 3;
break;
case 71://for Python
multipler = 5;
break;
default:
res.status(400).json({
Error : "Invalid language ID"
})
return;
}
const testcase = await questiondb.findById(question_id,'testCases')
if(testcase == null){
res.status(400).json({
Error : "Question ID doesn't exist"
})
return;
}
const testcases = testcase.testCases;
let tests = [];
let grp = {};
for(let i in testcases){
const current = await testdb.findById(testcases[i]);
if(current == null){
res.status(400).json({
Error : "Testcase ID "+testcases[i]+" doesn't exist"
})
return;
}
tests.push({
source_code : btoa(code),
source_code : Buffer.from(code,'binary').toString('base64'),
language_id : language_id,
stdin : btoa(current.input),
expected_output : btoa(current.expectedOutput),
cpu_time_limit : current.time,
memory_limit : current.memory,
stdin : Buffer.from(current.input,'binary').toString('base64'),
expected_output : Buffer.from(current.expectedOutput,'binary').toString('base64'),
cpu_time_limit : (current.time * multipler<15)?(current.time*multipler):15,
memory_limit : (current.memory * multipler),
redirect_stderr_to_stdout : true
})
const group = current.group;
if(group in grp){
let data = grp[group];
data.push(parseInt(i));
console.log(data);
grp[group] = data;
}
else{
grp[group] = [parseInt(i)];
}
}

//console.log(grp)

//console.log(tests);

const tokens = await axios.post("http://139.59.4.43:2358/submissions/batch?base64_encoded=true",
{
"submissions" : tests
Expand All @@ -80,13 +140,21 @@ class submission{
'Content-Type' : "application/JSON"
}
}).then(response => response.data)

.catch((err) => {
res.status(400).json({
Error : err.code
})
return;
})
if (!tokens){
return;
}
let str = [];
tokens.forEach(element => {
str.push(element.token);
});
const url = "http://139.59.4.43:2358/submissions/batch?tokens="+str.toString()+
"&base64_encoded=false&fields=status_id,stdout,stderr,expected_output,stdin";
"&base64_encoded=false&fields=status_id,stdout,expected_output,stdin";
console.log(url);
let completion = false;
let data_sent_back = {
Expand All @@ -100,53 +168,69 @@ class submission{
while(!completion){
let score = 0;
completion = true;
let failed = [];
const result = await axios.get(url).then(response => response.data.submissions);
result.forEach(element => {
for(let i in result){
const element = result[i];
switch(element.status_id){
case 1:
case 2:
completion = false;
break;
case 3:
score += 1;
break;
case 4:
if(element.expected_output+"\n" == element.stdout) score +=1;
if(element.expected_output+"\n" == element.stdout) continue;
else {
data_sent_back.error = element.stderr;
failed.push(i);
if(data_sent_back.input != ""){continue;}
data_sent_back.input = element.stdin;
data_sent_back.expectedOutput = element.expected_output;
data_sent_back.outputReceived = element.stdout;
completion = true;
}
break;
case 5:
data_sent_back.error = element.stderr;
failed.push(i);
if(data_sent_back.input != ""){continue;}
data_sent_back.error = "Time Limit Exceeded";
data_sent_back.input = element.stdin;
data_sent_back.expectedOutput = element.expected_output;
data_sent_back.outputReceived = element.stdout;
break;
case 6:
data_sent_back.error = element.stderr;
failed.push(i);
data_sent_back.error = "Complilation error";
data_sent_back.input = element.stdin;
data_sent_back.expectedOutput = element.expected_output;
data_sent_back.outputReceived = element.stdout;
break;
case 13:
failed.push(i);
data_sent_back.error = "Server side error please contact the nearest admin";
break;
default:
data_sent_back.error = element.stderr;
failed.push(i);
data_sent_back.error = element.status_id;
data_sent_back.input = element.stdin;
data_sent_back.expectedOutput = element.expected_output;
data_sent_back.outputReceived = element.stdout;
break;
}
})
}
if(completion){
data_sent_back.Sub_db = await this.create(req,reg_no,score,result.length);
//console.log(failed);
//console.log(grp);
Object.keys(grp).forEach(element =>{
let check = true;
const hell = grp[element];
for(let i in failed){
if(check && hell.includes(parseInt(failed[i]))){check = false;}
}
if(check){score += 1;}
});
data_sent_back.Sub_db = await this.create(req,reg_no,score,Object.keys(grp).length);
await this.create_score(reg_no);
data_sent_back.Score = await this.findscore(reg_no,question_id);
data_sent_back.Score = score;
res.status(201).json(data_sent_back);
break;
}
Expand All @@ -158,11 +242,6 @@ class submission{
res.status(200).json(all);
}

async findscore(user, question_id){
const details = await submission_db.findOne({regNo : user,question_id : question_id});
return details.score;
}

async create_score(user){
const ele = await submission_db.aggregate([
{
Expand Down

0 comments on commit 78ddfb5

Please sign in to comment.