-
Notifications
You must be signed in to change notification settings - Fork 0
/
voteAlgo.js
110 lines (102 loc) · 3.48 KB
/
voteAlgo.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
/**
* Created by Aleksandar on 2014-08-05.
*/
module.exports={
getUserPoint:function(user,timeStarted,answers) { // @TODO SHOULD BE ASYNCHRONOUS
var amountOfPoints = 0;
var amountOfCorrect = 0;
if (!user) {
return {
"amountOfPoints": amountOfPoints,
"amountOfCorrect": amountOfCorrect
};
}
for (var key in user.votes) {
var obj = user.votes[key];
//TODO
amountOfPoints += 90000 - (obj.answered.time-timeStarted);
if (module.exports.checkIfCorrect(obj.answered.name,key,answers)) {
amountOfCorrect++;
};
};
user.totalPoints = user.totalPoints || 0;
user.lastPoints = 0;
if (amountOfCorrect==3) {
user.totalPoints += amountOfPoints;
user.lastPoints = amountOfPoints;
};
return {
"amountOfPoints": amountOfPoints,
"amountOfCorrect": amountOfCorrect
};
},
checkIfCorrect:function(answer,key,answers) {
var correctAnswer = module.exports.getCorrectAnswer(key,answers);
return answer == correctAnswer;
},
getCorrectAnswer: function(key,answers) {
return answers[key];
},
loopThrough:function(connecteds,socketIdsOfPersonsInRoom,timeStarted,answers) {
var results=[];
for (var key in socketIdsOfPersonsInRoom) {
console.log("connecteds",connecteds);
console.log("socketIds",socketIdsOfPersonsInRoom);
if (!connecteds) {
console.log("connectds not set");
return [];
}
console.log("connectedsKey",connecteds[key]);
try {
var userResult = module.exports.getUserPoint(connecteds[key], timeStarted, answers);
var result = {
"amountOfPoints": userResult.amountOfPoints,
"amountOfCorrect": userResult.amountOfCorrect,
socket: connecteds[key]
};
} catch (err) {
var result = {
"amountOfPoints": 0,
"amountOfCorrect": 0,
socket: connecteds[key]
};
}
results.push(result);
}
return results;
},
getMinimum:function(results) {
var lowest = 0;
if (results.length==0) {
console.log("results empty");
return null;
}
for (var i = 1; i < results.length; i++ ) {
var lowestObj = results[lowest];
var currentObj = results[i];
if (currentObj.amountOfCorrect > lowestObj.amountOfCorrect) {
lowest=i;
} else if (currentObj.amountOfCorrect == lowestObj.amountOfCorrect) {
if (currentObj.amountOfPoints>lowestObj.amountOfPoints) {
lowest=i;
}
}
}
console.log("results",results)
return results[lowest];
},"getHillData":function(connecteds,socketIdsOfPersonsInRoom) {
var results=[];
for (var key in socketIdsOfPersonsInRoom) {
if (!connecteds) {
return [];
}
var socket = connecteds[key];
var result = {
"username":socket.username,
"points":socket.totalPoints
}
results.push(result);
}
return results;
}
};