-
Notifications
You must be signed in to change notification settings - Fork 0
/
controller.js
95 lines (73 loc) · 2.34 KB
/
controller.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
let model, view;
const initalise = evt => {
model = new Model();
view = new View();
//definition of editor, codemirror library use
let editor = CodeMirror.fromTextArea(
document.getElementById("codeEditor"),
{
smartIndent: false,
lineNumbers: true,
electricChars: false,
indentWithTabs: false,
indentUnit: 4,
extraKeys: {Tab: "indentMore"},
mode: "text/x-python",
theme: "neat",
}
);
//fetch problem from php
model.fetchProblemObject(editor, view.setCode, view.setProblemStatement, view.displayProblemText, view.changeActiveButton);
view.setUpProblemEvaluationHandler(() => {
view.removeSubmitButton();
view.deactivateRunButton();
//we clear the output for loading animation
view.setCodeOutputBox("");
view.showLoadingAnimation();
let code = view.getCode(editor);
let solutionObject = model.getProblemObject();
model.sendCodeRequest(code, view.setOutputBox, view.activateSubmitButton, view.activateRunButton, view.removeLoadingAnimation, solutionObject.testCaseStr);
});
view.setUpSubmitHandler(() => {
view.showLoadingAnimation();
let solutionCases = model.getBatchedSolutions();
let actualCases = model.getBatchedTries();
let outputCode = model.getProblemOutput();
let len = Object.keys(solutionCases).length;
let outputStr = "";
let areEqual = true;
for(let i = 0; i < len; i++) {
if(actualCases[i] != solutionCases[i]) {
areEqual = false;
}
outputStr += `${i+1}:Output = ${actualCases[i]}${i+1}:Expected = ${solutionCases[i]}\n`;
}
if(outputCode == undefined) {
view.setOutputText("Before you submit your code make sure to run it.");
} else {
if (areEqual) {
view.setOutputText("Correct\n" + outputStr);
view.showNextButton();
} else {
view.setOutputText(outputStr);
}
}
view.activateSubmitButton();
view.activateRunButton();
view.removeLoadingAnimation();
});
view.setUpNextProblemHandler(() => {
location.reload();
});
view.setProblemTabHandler(() => {
let solutionObject = model.getProblemObject();
view.setProblemStatement(solutionObject.statement);
view.changeActiveButton("Problem");
view.displayProblemText();
});
view.setSolutionHandler(() => {
let solutionObject = model.getProblemObject();
view.showSolution(solutionObject.solution, view.changeActiveButton);
});
}
window.addEventListener("load", initalise);