forked from php-pb/PHPinga
-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
94 lines (78 loc) · 2.35 KB
/
app.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
var app = angular.module('Pinga', []);
app.controller('MainController', ['$scope', '$http', function($scope, $http){
$scope.state = 'welcome';
$scope.perguntas = [];
$scope.respondidas = [];
$scope.contador = 0;
$scope.time = 0;
var timeout = 15
, timer;
$scope.letTheGameBegin = function() {
getPerguntas().then(function(response){
$scope.perguntas = response.data.perguntas;
$scope.contador = Math.floor(Math.random() * ($scope.perguntas.length - 0)) + 0;
mostraPergunta();
});
};
$scope.responder = function() {
if(angular.element('input[type="radio"]:checked').length === 0) return;
var resp = angular.element('input[type="radio"]:checked').val();
stopTimer();
if($scope.perguntas[$scope.contador].alternativas[resp].correta === true){
$scope.state = 'correct';
$scope.respondidas.push($scope.perguntas[$scope.contador]);
$scope.perguntas.splice($scope.contador, 1);
if ($scope.perguntas.length === 0) {
$scope.perguntas = $scope.respondidas;
$scope.respondidas = [];
$scope.state = 'end';
}
} else {
$scope.state = 'wrong';
}
$('input[type="radio"]').prop('checked', false);
};
$scope.proximo = function() {
$scope.contador = Math.floor(Math.random() * ($scope.perguntas.length - 0)) + 0;
$scope.perguntas[$scope.contador].alternativas = shuffleArray($scope.perguntas[$scope.contador].alternativas);
if($scope.contador == $scope.perguntas.length) {
$scope.letTheGameBegin();
} else {
mostraPergunta();
}
};
var startTimer = function() {
$scope.time = $scope.perguntas[$scope.contador].tempo || timeout;
timer = setInterval(function() {
$scope.time--;
if($scope.time == 0){
stopTimer();
$scope.state = 'wrong';
}
$scope.$apply();
}, 1000);
};
var stopTimer = function() {
clearInterval(timer);
};
var mostraPergunta = function() {
$scope.state = 'questions';
startTimer();
};
var getPerguntas = function() {
return $http({
method: 'GET',
url: location.href + '/data.json'
});
};
var shuffleArray = function(array) {
var m = array.length, t, i;
while (m) {
i = Math.floor(Math.random() * m--);
t = array[m];
array[m] = array[i];
array[i] = t;
}
return array;
};
}]);