This repository has been archived by the owner on Apr 5, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
r.js
86 lines (75 loc) · 1.77 KB
/
r.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
"use strict"
/*
[
{
"name": "ro",
"moments": [
{
"local": "2019-01-13T08:10",
"utc": "2019-01-13T13:10",
"preference": 0.8
},
{
"local": "2019-01-12T08:10",
"utc": "2019-01-12T13:10",
"preference": 0.5
}
]
}
]
*/
const nFirstMoments = 3
const nParticipants = 10
const pNewMoment = 0.2
const step = 900
const earliest = Date.parse("2019-01-10T13:10Z")
const latest = Date.parse("2019-01-14T13:10Z")
const covers = latest - earliest
const generateMoment = () =>
new Date(
Math.floor((Math.random() * covers + earliest) / (step * 1000)) *
(step * 1000),
).toISOString()
const generateFirst = () => {
const moments = []
let r
for (r = 0; r < nFirstMoments; ++r) {
moments.push(generateMoment())
}
return moments.sort()
}
const first = generateFirst()
// const x = generateFirst()
// console.log(x)
const userVotes = () => {
const votes = []
let r
const m = Math.floor(Math.random() * 5 + 1)
for (r = 0; r < m; ++r) {
let mom
if (Math.random() < pNewMoment) {
mom = generateMoment()
if (!first.includes(mom)) first.push(mom)
} else {
mom = first[Math.floor(Math.random() * first.length)]
}
const preference = Math.random() * 0.5 + 0.5
if (!votes.find((el) => el.local === mom))
votes.push({ local: mom, preference })
}
return votes.sort((a, b) => {
if (a.preference > b.preference) return 1
if (a.preference < b.preference) return -1
return 0
})
}
const allUserVotes = () => {
const votes = []
let r
const m = Math.floor(Math.random() * 5 + 1)
for (r = 0; r < nParticipants; ++r) {
votes.push({ name: `name-${r + 1}`, moments: userVotes() })
}
return votes
}
console.log(JSON.stringify(allUserVotes()))