-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathseinoscope.js
100 lines (92 loc) · 2.57 KB
/
seinoscope.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
const fs = require('fs');
const csv = require('csv-parser');
let data;
let episodes = [];
const monthMap = [
'January',
'February',
'March',
'April',
'May',
'June',
'July',
'August',
'September',
'October',
'November',
'December',
];
// Reads the data.json file.
function getData() {
return new Promise((resolve, reject) => {
fs.readFile('./data.json', 'utf8', (err, data) => {
if (err) {
reject(err);
} else {
const json = JSON.parse(data);
resolve(json);
}
});
});
}
function getEpisodeData() {
return new Promise((resolve, reject) => {
fs.createReadStream('episodes.csv')
.pipe(csv())
.on('data', (data) => episodes.push(data))
.on('end', () => {
resolve(episodes);
});
// todo: error handling
});
}
// Parses through data.json to retrieve seinoscope values
async function getSign(birthDate) {
const { day, month, year } = birthDate;
data = await getData();
await getEpisodeData();
const episode = getEpisode(month, day, year);
const mainCharacter = getMainCharacter(day);
const secondaryCharacter = getSecondaryCharacter(year, day);
const quote = getQuote(mainCharacter.mainCharacter, month, day);
return { ...mainCharacter, ...secondaryCharacter, quote, episode };
}
function getMainCharacter(day) {
const length = data.characters.main.length;
const characterIndex =
Math.floor(new Date().getDate() ^ (parseInt(day) * 1.67)) % length;
const mainCharacter = data.characters.main[characterIndex];
return {
mainCharacter: mainCharacter,
mainCharacterImage: data.characters.imageUrl[mainCharacter],
};
}
// Tested and verified randomness
function getSecondaryCharacter(year, day) {
const length = data.characters.secondary.length;
const characterIndex =
((new Date().getDate() ^ 1.67) *
((parseInt(year) * parseInt(day)) ^ 1.67)) %
length;
const secondaryCharacter = data.characters.secondary[characterIndex];
return {
secondaryCharacter: secondaryCharacter,
secondaryCharacterImage: data.characters.imageUrl[secondaryCharacter],
};
}
function getQuote(mainCharacter, month, day) {
const quoteList = data.quotes[mainCharacter];
const length = quoteList.length;
const quoteIndex =
(parseInt(monthMap.indexOf(month) + 1) * parseInt(day)) % length;
return quoteList[quoteIndex];
}
function getEpisode(month, day, year) {
const length = episodes.length;
const episodeIndex =
(parseInt(monthMap.indexOf(month) + 1) * year * parseInt(day)) % length;
return episodes[episodeIndex];
}
module.exports = {
getSign,
};