-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathVerificationProcess.js
More file actions
121 lines (101 loc) · 3.26 KB
/
VerificationProcess.js
File metadata and controls
121 lines (101 loc) · 3.26 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
119
120
class VerificationProcess {
constructor(songs) {
this.songs = songs;
this.currentSongs = [];
this.one = "";
this.two = "";
this.three = "";
this.four = "";
this.score = 0;
this.attempt = 0;
}
static extractSongData(song) {
return {
id: song.id,
name: song.name.replace(/\s?\(.+?\)\s?/g, " ").trim(),
previewURL: song.preview_url,
coverArt: song.album.images.reduce((smallest, next) => smallest.height > next.height ? next : smallest).url,
artists: song.artists.map(a => a.name).reduce((ns, n) => `${ns}, ${n}`)
};
}
static shuffle(a) {
for (let i = a.length - 1; i > 0; i--) {
const j = Math.floor(Math.random() * (i + 1));
[a[i], a[j]] = [a[j], a[i]];
}
return a;
}
getSongs() {
const groupSize = Math.floor(this.songs.length / 4);
this.currentSongs[0] = this.songs[Math.floor(groupSize * Math.random())];
this.currentSongs[1] = this.songs[Math.floor(groupSize * Math.random()) + groupSize];
this.currentSongs[2] = this.songs[Math.floor(groupSize * Math.random()) + (groupSize * 2)];
this.currentSongs[3] = this.songs[Math.floor(groupSize * Math.random()) + (groupSize * 3)];
this.one = this.currentSongs[0].id; // spotify ID for the track
this.two = this.currentSongs[1].id;
this.three = this.currentSongs[2].id;
this.four = this.currentSongs[3].id;
console.dir(this.currentSongs.map(s => s.name));
return VerificationProcess.shuffle(this.currentSongs.map(VerificationProcess.extractSongData));
}
updateScore(orderedSongs) {
// returns -1 < n < 1
// where n > 0.5 means the user is verified
this.attempt++;
const roundScore = this.getRoundScore(orderedSongs);
const multiplier = 1; //((roundScore < 0) ? 1.3 : 0.7) ** (this.attempt - 1);
this.score += roundScore * multiplier;
return this.score;
}
getRoundScore(orderedSongs) {
// returns a score for the round
// 0 = half correct
// +ve = mostly correct
// -ve = mostly wrong
return VerificationProcess.map[this.getOrders(orderedSongs).join("")];
}
getOrders(orderedSongs) {
return orderedSongs.map(orderedSong => this.getOrder(orderedSong));
}
getOrder(orderedSong) {
switch (orderedSong) {
case this.one:
return 1;
case this.two:
return 2;
case this.three:
return 3;
case this.four:
return 4;
default:
console.error('Invalid number: ' + orderedSong);
}
}
}
VerificationProcess.map = {
"1234": +0.40, // v v good
"1243": +0.25, // v good
"1324": +0.40, // v v good
"1342": +0.10, // good
"1423": +0.10, // good
"1432": -0.25, // v bad
"2134": +0.25, // v good
"2143": +0.10, // good
"2314": +0.10, // good
"2341": +0.10, // good
"2413": -0.25, // v bad
"2431": -0.10, // bad
"3214": -0.10, // bad
"3241": -0.10, // bad
"3124": +0.25, // v good
"3142": +0.10, // good
"3421": -0.25, // v bad
"3412": -0.10, // bad
"4231": -0.40, // v v bad
"4213": -0.10, // bad
"4321": -0.40, // v v bad
"4312": -0.25, // v bad
"4123": +0.25, // v good
"4132": -0.10 // bad
};
module.exports = VerificationProcess;