-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.js
161 lines (143 loc) · 4.12 KB
/
app.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
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
async function main() {
const jsConfetti = new JSConfetti();
const loginSection = document.getElementById("login");
const controlsSection = document.getElementById("controls");
const gameSection = document.getElementById("game");
const lostSection = document.getElementById("lost");
const loaderSection = document.getElementById("loader");
const userSection = document.getElementById("user");
const winnerSection = document.getElementById("winner");
const loginButton = document.getElementById("login-button");
const buyCardButton = document.getElementById("buy-card");
const callBingoButton = document.getElementById("callbingo");
const resetButton = document.getElementById("reset");
const withdrawButton = document.getElementById("withdraw");
const numbersElements = document.querySelectorAll(`[data-type="number"]`);
const account = document.getElementById("account");
const installMetamask = document.getElementById("install-metamask");
//TODO sono connesso alla rete giusta?
const NUMBERS_REQUIRED = 6;
let numbersIHave = 0;
dapp
.isConnected()
.then((yes) => {
if (yes) {
hide(loginSection);
show(controlsSection);
} else {
show(loginButton);
show(loginSection);
}
})
.catch((err) => {
console.log(err);
show(installMetamask);
})
.finally(() => {
hide(loader);
});
function loginHandler() {
dapp.login().then((user) => {
if (user) {
hide(loginSection);
show(controlsSection);
show(userSection);
account.textContent = user;
}
});
}
function buyBingoCardHandler() {
show(loaderSection);
dapp
.buyBingoCard()
.then((card) => {
const { numbers } = card;
numbersElements.forEach((element, index) => {
element.textContent = numbers[index];
});
hide(controlsSection);
show(gameSection);
})
.finally(() => {
hide(loaderSection);
});
}
function numberClickedHandler(event) {
const element = this;
element.classList.add("is-success");
launchConfetti();
numbersIHave++;
if (numbersIHave === NUMBERS_REQUIRED) {
displayWinButton();
}
}
function displayWinButton() {
show(callBingoButton, { mode: "inline-block" });
}
function callBingoButtonHandler() {
show(loaderSection);
dapp
.callBingo()
.then((won) => {
if (won) {
launchConfetti(true);
show(winnerSection);
document.getElementById("dialog-default").showModal();
} else {
cheater();
}
})
.finally(() => {
hide(loaderSection);
});
}
function withdrawListener() {
dapp
.withdraw()
.then(() => {
launchConfetti(true);
})
.catch((e) => {
console.log("cannot withdraw", e);
});
}
function reset() {
numbersIHave = 0;
hide(callBingoButton);
hide(lostSection);
numbersElements.forEach((element) => {
element.classList.remove("is-success");
});
}
function hide(element) {
element.classList.remove("block");
element.classList.remove("inline-block");
element.classList.add("hidden");
}
function show(element, options = { mode: "block" }) {
element.classList.remove("hidden");
element.classList.add(options.mode);
}
function launchConfetti(big = false) {
jsConfetti.addConfetti();
if (big) {
jsConfetti.addConfetti({
emojis: ["🌈", "⚡️", "💥", "✨", "💫", "🌸"],
confettiNumber: 100,
});
}
}
function cheater() {
show(lostSection);
document.getElementById("lost-dialog").showModal();
}
loginButton.addEventListener("click", loginHandler);
buyCardButton.addEventListener("click", buyBingoCardHandler);
numbersElements.forEach((element) =>
element.addEventListener("click", numberClickedHandler)
);
callBingoButton.addEventListener("click", callBingoButtonHandler);
resetButton.addEventListener("click", reset);
withdrawButton.addEventListener("click", withdrawListener);
}
addEventListener("DOMContentLoaded", main);