This repository has been archived by the owner on Jun 11, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathvue.js
140 lines (139 loc) · 4.3 KB
/
vue.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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
var adress = "https://lp-miar-groupe01-cloned-paul604.c9users.io/quizz-movie-1.0.0";
var vue = new Vue({
el: '#vue',
data: {
body: "loginTemp",
precedentPage: "loginTemp",
score: 0,
totalScore: 0,
totalQuestion: 0,
login: "",
password: "",
token: "",
questionPicture: "",
question: "",
answer: "",
imageError: "",
textError: "",
},
computed: {
successRate: function() {
return (this.totalQuestion === 0) ? 0 : ((this.totalScore/this.totalQuestion)*100);
}
},
methods: {
toStats: function() {
this.httpRequestAsync("GET", adress+"/answers?token="+this.token, "", function(json){
var jsonObject = JSON.parse(json);
this.totalQuestion = parseInt(jsonObject.answers);
this.httpRequestAsync("GET", adress+"/goodanswers?token="+this.token, "", function(json){
var jsonObject = JSON.parse(json);
this.totalScore = parseInt(jsonObject.goodanswers);
this.body = "statsTemp";
}.bind(this));
}.bind(this));
},
loginToQuiz: function() {
this.connection(function(json) {
this.token = JSON.parse(json).token;
this.toQuiz();
}.bind(this));
},
loginToRegister: function() {
this.body = "registerTemp";
},
toLogin: function() {
this.resetConnection();
this.body = "loginTemp";
},
registerToQuiz: function() {
this.register(function(json) {
this.body = "loginTemp";
}.bind(this));
},
toRegister: function() {
this.disconnection();
this.body = "registerTemp";
},
toQuiz: function() {
this.getQuestion();
this.body = "quizTemp";
},
toLegal: function() {
if(this.body != "legalTemp") {
this.precedentPage = this.body;
this.body = "legalTemp";
}
},
toError: function(statusCode, statusText) {
if(this.body != "errorTemp") {
this.precedentPage = this.body;
this.imageError = "https://http.cat/"+statusCode;
this.textError = statusText;
this.body = "errorTemp";
}
},
toPrecedent: function() {
this.body = this.precedentPage
},
disconnection: function(){
this.httpRequestAsync("POST", adress+"/disconnect?token="+this.token, "", function(json) {
this.resetConnection();
this.body = "loginTemp";
}.bind(this));
},
resetConnection: function() {
this.login = "";
this.password = "";
this.token = "";
this.score = 0;
this.totalScore = 0;
this.totalQuestion = 0;
},
connection: function(callback) {
var params = "{\"login\":\""+this.login+"\", \"password\":\""+this.password+"\"}";
this.httpRequestAsync("POST", adress+"/login", params, callback);
},
register: function(callback) {
var params = "{\"login\":\""+this.login+"\", \"password\":\""+this.password+"\"}";
this.httpRequestAsync("POST", adress+"/register", params, callback);
},
answerQuestion: function() {
var params = "";
this.httpRequestAsync("POST", adress+"/response?token="+this.token, "{\"response\":\""+this.answer+"\"}", function(json) {
var jsonObject = JSON.parse(json);
if(jsonObject.result==="true") {
this.score ++;
this.totalScore ++;
}
this.getQuestion();
this.answer = "";
this.question = "";
this.questionPicture = "";
this.totalQuestion ++;
}.bind(this));
},
getQuestion: function() {
this.httpRequestAsync("GET", adress+"/question?token="+this.token, "", function(json) {
var jsonObject = JSON.parse(json);
this.question = "<b>Question: </b>"+jsonObject.question;
this.questionPicture = jsonObject.poster;
}.bind(this));
},
httpRequestAsync: function(type, theUrl, params, callback) {
var xmlHttp = new XMLHttpRequest();
xmlHttp.onreadystatechange = function() {
if (xmlHttp.readyState == 4) {
if(xmlHttp.status == 200) {
callback(xmlHttp.responseText);
} else if(xmlHttp.status != 0) {
this.toError(xmlHttp.status, xmlHttp.statusText);
}
}
}.bind(this)
xmlHttp.open(type, theUrl, true);
var send = type === "GET" ? null : params;
xmlHttp.send(send);
}
}
});