-
Notifications
You must be signed in to change notification settings - Fork 0
/
scripts.js
92 lines (83 loc) · 2.59 KB
/
scripts.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
// Init
const mainTag = isAndroid() ? document : window;
mainTag.addEventListener("message", (message) => {
const data = JSON.parse(message.data);
if (data.wheelId) {
initData(data);
} else if (data.share_facebook_success) {
handleShareSuccess();
}
});
const onDocumentLoad = () => {
if (checkRedirectUrl()) return;
if (!isWebview(navigator.userAgent)) {
return showNotSupportModal();
}
getWheelDetailsAPI();
};
// Header
btnBack.addEventListener("click", () => {
window.ReactNativeWebView.postMessage("BACK");
});
// Footer
btnGuide.addEventListener("click", () => {
window.ReactNativeWebView.postMessage("GAME_GUIDE");
});
btnGift.addEventListener("click", () => {
window.ReactNativeWebView.postMessage("MY_GIFTS");
});
// Modal
iconClose.addEventListener("click", hideModal);
btnContinueSpin.addEventListener("click", hideModal);
btnUnderstand.addEventListener("click", hideModal);
btnViewGift.addEventListener("click", () => {
window.ReactNativeWebView.postMessage("MY_GIFTS");
});
btnLoadingError.addEventListener("click", () => {
window.ReactNativeWebView.postMessage(btnLoadingError.dataset.errorCode);
});
// Wheel
btnShare.addEventListener("click", () => {
window.ReactNativeWebView.postMessage("SHARE");
});
btnSpin.addEventListener("click", () => {
// Check turns
if (getTurns() <= 0) {
return showNoMoreTurnsModal();
}
// Check start date
const currDate = new Date();
const startDate = new Date(getStartDate());
if (currDate.getTime() < startDate.getTime()) {
return showCommonErrorModal(
`Game vòng quay may mắn sẽ bắt đầu từ ${startDate.getHours()}h${startDate.getMinutes()}' ngày ${startDate.getDate()}-${
startDate.getMonth() + 1
}-${startDate.getFullYear()}`
);
}
// Spin
if (isWheelStopped) {
isWheelStopped = false;
getSpinResultAPI()
.then((res) => {
const selectedGift = gifts.find(({ id }) => res.id === id);
const surplus = totalDegree % 360;
const spinDegree = (selectedGift.degree - surplus + 360) % 360;
totalDegree += randomInRange(spinDegree - 40, spinDegree - 5) + 2880;
wheel.style.transform = `rotate(${totalDegree}deg)`;
setTimeout(() => {
isWheelStopped = true;
if (selectedGift.type === "voucher") {
showSuccessModal(selectedGift);
} else if (selectedGift.type === "bonus_turns") {
showMoreTurnModal(selectedGift);
} else {
showLuckyModal(selectedGift);
}
}, 8000);
})
.catch(() => {
isWheelStopped = true;
});
}
});