-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathUserData.js
346 lines (334 loc) · 13 KB
/
UserData.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
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
export default class UserData{
userName;
points;
gold;
level_up_points;
badges = [];
level;
just_leveled_up=false;
validation_completed;
answer_completed;
isNew;
full_positive;
full_negative;
my_images;
geolocationPermission = false;
powerUps=[];
constructor(userName,points,level_up_points,badges,level){
this.userName = userName;
this.points = points;
this.gold = 0;
this.level_up_points = level_up_points;
this.badges = badges;
this.level = level;
this.just_leveled_up = false;
this.validation_completed=0;
this.answer_completed=0;
this.full_positive=0;
this.full_negative=0;
this.my_images = [];
this.powerUps=[];
}
//create the userData and update it with the data of the gamification engine
//add the power up in the database
async createUser(userName,api_url){
this.userName = userName;
var my_user = userName;
//var my_user = "anothertrycomeone";
var my_url = api_url + "/posts/user/getUser/"+my_user;
try{
var my_request = {
method: "GET",
headers:{ "Content-Type":"application/json"},
}
const fetchdata = await fetch(my_url,my_request)
.then(response=>response.json())
.then(async (new_response_data)=>{
//if it doesn't exist then create it
if(JSON.stringify(new_response_data.customData)==='{}'){
console.log("NUOVO GIOCATORE")
this.updateUserInfo(new_response_data)
//TODO UPDATE USER POWERUPS!!!!!!
//this.createNewUser(my_user,api_url)
console.log(new_response_data)
this.isNew = true;
await (this.checkPowerUps(userName,api_url)).then(items=>{
console.log(items);
this.powerUps = items;
})
}else{
//if it does exist then update its data
console.log("GIOCATORE ESISTE Già")
this.updateUserInfo(new_response_data)
console.log(new_response_data)
this.isNew = false;
await (this.checkPowerUps(userName,api_url)).then(items=>{
console.log(items);
this.powerUps = items;
})
}
}).catch((err)=>console.log(err))
return fetchdata
}catch(e){
console.log(e)
}
}
//update the userData with the data received from the gamification engine
updateUserInfo(userdata){
console.log(userdata);
var validations = userdata.customData.validation_completed;
var answers = userdata.customData.answer_completed
var fullPositive = userdata.customData.full_positive_validation;
var fullNegative = userdata.customData.full_negative_validation;
var my_images = userdata.customData.myImages;
this.badges=new Array();
this.level = userdata.customData.level;
if(userdata.state.PointConcept != undefined ){
this.points = userdata.state.PointConcept[1].score;
this.gold = userdata.state.PointConcept[0].score;
console.log(userdata.state.PointConcept);
}else{
this.points = 0;
this.gold = 0;
}
//this.points = userdata.state.PointConcept[1].score;
this.level_up_points = userdata.customData.level_up_points;
this.validation_completed = validations;
this.answer_completed = answers;
this.full_positive = fullPositive;
this.full_negative = fullNegative;
this.my_images = my_images;
var badgeCollection = userdata.state.BadgeCollectionConcept;
if(badgeCollection == undefined){
console.log("Empty");
}else{
for(var i=0;i<badgeCollection.length;i++){
if(badgeCollection[i].badgeEarned.length==0){
//console.log("no badge...");
}else{
this.badges.push(badgeCollection[i].name);
}
}
}
console.log("QUESTA è la userData con custom data level up points: " + userdata.customData.level_up_points);
//console.log("checking level_up undefined....");
if(userdata.customData.level_up_points==undefined || isNaN(userdata.customData.level_up_points)){
this.level_up_points=0;
console.log("LEVEL UP POINTS WELL IT WAS UNDEFINED OR NAN, NOW 0!!!");
}
if(userdata.customData.level==undefined || isNaN(userdata.customData.level)){
//console.log("LEVEL WAS UNDEFINED OR NAN, NOW 0!!!");
this.level=0;
this.level_up_points=0;
}
if(userdata.state.PointConcept != undefined ){
if(userdata.state.PointConcept[1].score==undefined || isNaN(userdata.state.PointConcept[1].score)){
//console.log("SCORE WAS UNDEFINED OR NAN, NOW 0!!!");
this.points=0;
}
}
if(answers == undefined || isNaN(answers)){
//console.log("ANSWERS WAS UNDEFINED OR NAN, NOW 0!!!");
this.answer_completed = 0;
}
if(validations == undefined || isNaN(validations)){
//console.log("VALIDATIONS WAS UNDEFINED OR NAN, NOW 0!!!");
this.validation_completed = 0;
}
if(fullPositive == undefined || isNaN(fullPositive)){
//console.log("FULLPOSITIVE WAS UNDEFINED OR NAN, NOW 0!!!");
this.full_positive= 0;
}
if(fullNegative == undefined || isNaN(fullNegative)){
//console.log("FULLNegative WAS UNDEFINED OR NAN, NOW 0!!!");
this.full_negative = 0;
}
if(my_images == undefined){
this.my_images = [];
}
}
//get the power up of the userData with a call to the database
async checkPowerUps(userName,api_url){
console.log("USER SENT"+userName)
var my_body = {
"tableName" : userName,
}
try{
const my_url = api_url + "/managePowerUps/retrieveAll"
const requestSpatialite = {
method:"post",
headers:{"Content-Type":"application/json", 'pw_token':process.env.VUE_APP_REST_PASSWORD},
body: JSON.stringify(my_body),
};
//console.log(requestSpatialite);
const fetchdata = await fetch(my_url, requestSpatialite)
.then(response => response.json())
.then((new_response_data)=>{
return new_response_data;
}).catch((err)=>console.log(err))
return fetchdata
}catch(e){
alert("Error......");
}
}
//if a day has passed then deletes the power up
checkPowerUpsTime(){
var diffTime;
var diffDays;
var now = new Date();
var new_array = [];
var resultDate;
for(var i=0;i<this.powerUps.length;i++){
resultDate = new Date(this.powerUps[i].TIME);
diffTime = Math.abs(now - resultDate);
diffDays = diffTime / (1000*60*60*24);
if(diffDays<this.powerUps[i].LIFETIME){
new_array.push(this.powerUps[i])
}
}
this.powerUps = new_array;
}
//get the power up that are not expired
getPowerUps(){
this.checkPowerUpsTime();
var array_with_names = [];
console.log("getting powers...");
console.log(this.powerUps);
for(var i=0;i<this.powerUps.length;i++){
console.log(this.powerUps);
array_with_names.push(this.powerUps[i].POWERNAME);
}
return array_with_names;
}
async createNewUser(username,api_url){
console.log(username)
//call the game engine to create the user
var my_url = api_url + "/posts/user/createNewUser"
var my_body = {
"userid":username
}
try{
const my_request = {
method:"POST",
headers:{ "Content-Type":"application/json"},
body: JSON.stringify(my_body)
};
const fetchdata = await fetch(my_url,my_request)
.then(response => response.json())
.then((new_response_data)=>{
//update the data to show the profile
this.updateUserInfo(new_response_data)
console.log(new_response_data)
return new_response_data;
}).catch((err)=>{
console.log(err);
console.log(err.status)
})
return fetchdata
}catch(e){
alert("Errorrrr")
}
}
//Method used to update the user score inside the userData when a mission gets completed
missionComplete(type,score){
var medalArray = [];
console.log("this is missionComplete:" + type + score);
this.points = this.points + score;
this.gold = this.gold + score;
var my_level_up = parseInt(this.level_up_points);
my_level_up=my_level_up+score;
this.level_up_points = my_level_up;
console.log("sta funzionando"+this.level_up_points);
var currentLevel = parseInt(this.level);
var level_thresh = currentLevel * 10;
console.log("this is level_thresh: "+level_thresh)
console.log("this is level up:" + my_level_up);
if(my_level_up > level_thresh||my_level_up==level_thresh){
//Level Up
var new_level_up_points = my_level_up - level_thresh;
this.level = currentLevel + 1;
this.level_up_points = new_level_up_points;
this.just_leveled_up = true;
//notice the user
console.log("YOU LEVELED UP!")
medalArray = this.checkLevelMedals(medalArray);
//alert("YOU LEVELED UP!");
}
if(type=="validation"){
medalArray = this.checkValidationMedals(medalArray);
}else{
medalArray = this.checkOpenMedals(medalArray);
}
return medalArray;
}
//Method used to assign the medal to the userData when he levels up
checkLevelMedals(my_array){
console.log("EnteredCheckLevelMedals")
if(this.level==5 || this.level=="5"){
//get the beginner medal
my_array.push("beginner badge")
console.log("Congratulations level 5")
this.badges.push("beginner badge");
}
if(this.level==10 || this.level=="10"){
//get the expert medal
my_array.push("expert badge")
console.log("Congratulations level 10")
this.badges.push("expert badge");
}
if(this.level==20 || this.level=="20"){
//get the professional medal
my_array.push("professional badge");
console.log("Congratulations level 20")
this.badges.push("professional badge");
}
return my_array;
}
//Method used to assign the validation medals
checkValidationMedals(my_array){
console.log("EnteredCheckValidationMedals")
//increment by one
var my_validations = parseInt(this.validation_completed);
my_validations = my_validations + 1;
if(my_validations == 1){
//get the validator badge
my_array.push("validator badge");
console.log("Congratulation First Validation")
this.badges.push("validator badge");
}
if(my_validations == 20){
//get the expert validator badge
my_array.push("expert validator badge");
console.log("Congratulation 20 Validation")
this.badges.push("expert validator badge");
}
this.validation_completed = my_validations;
return my_array;
}
//method used to assign the questions badges
checkOpenMedals(my_array){
console.log("EnteredCheckOpenMedals")
//increment by 1
var my_answers = parseInt(this.answer_completed);
my_answers = my_answers + 1;
if(my_answers == 1){
my_array.push("first steps");
console.log("Congratulations on your first answer! FIRST STEPS MEDAL");
this.badges.push("first steps");
}
if(my_answers == 20){
//get the validator badge
my_array.push("cyclist");
console.log("Congratulation 20 Answers")
this.badges.push("cyclist");
}
if(my_answers == 50){
//get the expert validator badge
my_array.push("professional cyclist");
console.log("Congratulation 50 Answers")
this.badges.push("professional cyclist");
}
this.answer_completed = my_answers;
return my_array;
}
}