forked from CodewarsClone/Codewars
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtrainingCtrl.js
More file actions
118 lines (104 loc) · 3.61 KB
/
Copy pathtrainingCtrl.js
File metadata and controls
118 lines (104 loc) · 3.61 KB
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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
/**********TRAINING CONTROLLER************/
angular.module('app').controller('trainingCtrl', function ($scope, $state, mainService, $stateParams) {
mainService.checkAuth();
$scope.kataid = $stateParams.kataid;
$scope.passed = false;
$scope.showInstruction = true;
$scope.showOutput = false;
/** Create text areas **/
var textarea1 = document.getElementById('solution-input');
var solutionsCode = CodeMirror.fromTextArea(textarea1, {
lineNumbers: true,
theme: 'seti',
lineSeparator: '\\n',
});
var textarea2 = document.getElementById('example-input');
var examplesCode = CodeMirror.fromTextArea(textarea2, {
lineNumbers: true,
theme: 'seti',
});
$scope.languages = ['JavaScript', 'Python'];
$scope.versions = 'Node v6.6.0';
$scope.output = [];
//NG-SHOWS
$scope.showOutputShow = true;
$scope.showInstructionsShow = false;
$scope.showOutput = function () {
$scope.showOutputShow = false;
$scope.showInstructionsShow = true;
};
$scope.showInstructions = function () {
$scope.showOutputShow = true;
$scope.showInstructionsShow = false;
};
// GET KATA INFORMATION
$scope.getKataById = (kataid) => {
mainService.getKataById(kataid).then((response) => {
console.log(response.data);
$scope.name = response.data.name;
$scope.instructions = response.data.description.replace(/\\n/g, '\n');
$scope.kyu = response.data.kyu;
$scope.starter = response.data.starter_code;
$scope.examples = response.data.examples[0].test;
$scope.kataid = response.data.id;
}).then(() => {
solutionsCode.setValue($scope.starter);
examplesCode.setValue($scope.examples);
});
};
$scope.getKataById($scope.kataid);
//Examples should be an array of objects. Returned results will be an array with the different tests and their results.
$scope.testExamples = function () {
var solutions = solutionsCode.getValue();
var examples = examplesCode.getValue();
$scope.showOutput();
var t0 = performance.now();
solutions = solutions
.replace(/\s*\n*\r*\/\/.*\n*\r*/g, '')
.replace(/\n\s*\./g, `.`)
.replace(/\\n/g, " ")
.replace(/\s+/g, " ");
var examplesArr = [];
examples = examples
.replace(/\s*\n*\r*\/\/.*\n*\r*/g, '')
.replace(/\n\s*\./g, `.`)
.replace(/\n/g, ` `)
.replace(/\s+/g, ` `);
console.log(examples);
mainService.testExamples(solutions, examples).then((response) => {
var t1 = performance.now();
$scope.answer = response.data.nest;
console.log(response.data.nest[0]);
$scope.time = Math.round(t1 - t0) + " ms";
$scope.testPass = response.data.passCount;
$scope.testFail = response.data.testCount - response.data.passCount;
});
};
$scope.testSuite = function () {
var solutions = solutionsCode.getValue();
$scope.showOutput();
var t0 = performance.now();
solutions = solutions
.replace(/\s*\n*\r*\/\/.*\n*\r*/g, '')
.replace(/\n\s*\./g, `.`)
.replace(/\\n/g, " ")
.replace(/\s+/g, " ");
mainService.testSuite(solutions, $scope.kataid).then((response) => {
var t1 = performance.now();
$scope.answer = response.data.nest;
console.log(response.data.nest[0]);
$scope.time = Math.round(t1 - t0) + " ms";
$scope.testPass = response.data.passCount;
$scope.testFail = response.data.testCount - response.data.passCount;
$scope.submit = (response.data.testCount === response.data.passCount ? true : false);
});
};
$scope.addPointsToUser = (points, userid) => {
mainService.addPointsToUser(mainService.pointsCalculator($scope.kyu, mainServive.user.id), mainServive.user.id)
};
$scope.submitAnswer = (solution, kataid, userid) => {
if ($scope.passed) {
mainService.submitAnswer(solution, kataid, userid);
}
}
});