-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathraffle.html
More file actions
74 lines (68 loc) · 2.47 KB
/
raffle.html
File metadata and controls
74 lines (68 loc) · 2.47 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
<!DOCTYPE html><html lang="en">
<head>
<title>Rocket Fuel Contributors Raffle</title>
<style>
html { height: 100% }
body { background-color: #fca265; background-image: url("https://rocketpool.net/assets/header-bg-wide-3cd4678c.webp"); height: 90% }
h1 { text-align: center }
#main { margin: 1em; height: 60% }
textarea { font-size: 20px; margin: auto 5em; vertical-align: middle }
input { font-size: 30px; padding: 0.1em 1em }
div { text-align: center }
</style>
</head>
<body>
<h1>Rocket Fuel Contributors Raffle!</h1>
<div id="main">
<textarea id="contestants" rows="20" cols="30" placeholder="Paste contributors and contribution counts (two columns) here."></textarea>
<textarea id="winners" rows="8" cols="30" style="text-align: center; padding: 0.2em 5em"></textarea>
</div>
<div><input id="go" type="button" value="Go!" onclick="pick()"></div>
<script>
const WINNERS = 10
async function pick() {
document.getElementById("go").disabled = true
let contestants = document.getElementById("contestants").value
// Clean up names by getting rid of repeated spaces
contestants = contestants.trim().replaceAll(/ +/g, " ")
// Create tickets
let tickets = []
for (const contestant of contestants.split('\n')) {
const c = contestant.split('\t')
const name = c[0].replace(/#.*/, "").trim()
const numEntries = +c[1]
if (isNaN(numEntries) || c.length != 2) {
alert("Invalid formatting for this line: " + contestant)
tickets = []
break
}
if (tickets.includes(name)) {
alert(name + " appears twice in the list of contestants")
tickets = []
break
}
tickets = tickets.concat(new Array(numEntries).fill(name))
}
console.log(tickets)
// Decide winners!
document.getElementById("winners").value = ""
for (let rank = 1; rank <= WINNERS && tickets.length > 0; rank++) {
const winner = tickets[Math.floor(Math.random() * tickets.length)]
console.log(winner)
tickets = tickets.filter(c => c != winner) // Make sure each contestant can only win once
const winner_text = rank + ". " + winner
document.getElementById("go").value = rank
if (rank > 1)
document.getElementById("winners").value += "\n"
for (c of winner_text) {
document.getElementById("winners").value += c
await sleep(30)
}
await sleep(1000)
}
document.getElementById("go").disabled = false
document.getElementById("go").value = "Go!"
}
const sleep = ms => new Promise(resolve => setTimeout(resolve, ms))
</script>
</body>