forked from moonsama/lions-raffle
-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.js
86 lines (67 loc) · 1.98 KB
/
index.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
const seedrandom = require("seedrandom");
const fs = require("fs");
// Hash from block #11935000
const hash =
"0x899c47cccd7292f4b8d72cd8861fab24274fbb5c0ebe287d0a21f355ca851dd0";
// isolate all numeric values from hash. ouput: '08994772924872886124274502870213558510'
const seed = hash.replace(/[a-zA-Z]/g, "");
console.log("seed", seed);
const rng = seedrandom(seed);
const data = JSON.parse(fs.readFileSync("leaderboard.json"));
const leaders = data.leaderboards[0].leaderboard;
const list = leaders.filter((item) => item.amount > 0);
// sort by amount in descending order
list.sort((a, b) => b.amount - a.amount);
// get top 10 and remove them from the list
const topTen = list.splice(0, 10);
const ten = topTen.reduce((acc, { name }) => {
acc[`${name}`] = 1;
return acc;
}, {});
// extracting names and weights list
const names = [];
const weights = [];
list
.filter((item) => item["amount"] !== "0")
.map((item) => {
names.push(item["name"]);
weights.push(item["amount"]);
});
// draw 90 unique winners
const winnersList = [];
for (let round = 0; round < 90; round++) {
const rnd = rng();
let random = rnd * weights.reduce((a, b) => a + b, 0);
let i = 0;
for (i; i < weights.length; i++) {
if (random < weights.slice(0, i + 1).reduce((a, b) => a + b, 0)) {
const winner = names[i];
if (winnersList.includes(winner)) {
// continue drawing until unique winner is found
round--;
break;
} else {
winnersList.push(winner);
break;
}
}
}
}
// count winners
const winners = {};
winnersList.map((winner) => {
if (winners[winner]) {
winners[winner]++;
} else {
winners[winner] = 1;
}
});
// sort winner list from high to low
const sortedWinners = Object.entries(winners)
.sort((a, b) => b[1] - a[1])
.reduce((obj, [key, value]) => {
obj[key] = value;
return obj;
}, {});
const final = { ...ten, ...sortedWinners };
fs.writeFileSync("winners.json", JSON.stringify(final, null, 2));