-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutils.js
287 lines (260 loc) · 7.65 KB
/
utils.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
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
// Token
const initData = (values) => {
wheelId.value = values.wheelId;
token.value = values.access_token;
refreshToken.value = values.refresh_token;
API_URL.value = `${values.customerUrl}api/v1`;
};
const getApiUrl = () => {
return API_URL.value;
};
const setToken = (value) => {
token.value = value;
};
const getToken = () => {
return token.value;
};
const getRefreshToken = () => {
return refreshToken.value;
};
const getWheelId = () => {
return wheelId.value;
};
const setStartDate = (start_date) => {
startDate.value = start_date;
};
const getStartDate = () => {
return startDate.value;
};
// Loading
const offLoadingError = () => {
loading.style.display = "none";
loadingImg.style.display = "none";
loadingError.style.display = "none";
};
const showError = (errMsg = "Lỗi tải dữ liệu", errorCode = "ERROR") => {
loading.style.display = "block";
loadingImg.style.display = "none";
loadingError.style.display = "flex";
loadingError.children[0].innerText = errMsg;
btnLoadingError.dataset.errorCode = errorCode;
};
const showLoading = () => {
loading.style.display = "block";
loadingImg.style.display = "block";
loadingError.style.display = "none";
};
const offLoading = () => {
loading.style.display = "none";
loadingImg.style.display = "none";
loadingError.style.display = "none";
};
// Wheel
const randomInRange = (start, end) => {
const totalItems = end - start + 1;
return Math.floor(Math.random() * totalItems + start);
};
const getGiftName = (gift) => {
if (gift.type === "voucher") {
return gift.name;
}
if (gift.type === "bonus_turns") {
return `Thêm lượt`;
}
return "";
};
const renderWheel = (data) => {
gifts = data;
gifts.forEach((gift, index) => {
gift.degree = ((360 * (gifts.length - index) + 180) / gifts.length) % 360;
});
sectionsSortedByDegree = JSON.parse(JSON.stringify(gifts)).sort(
(preSection, nextSection) => preSection.degree - nextSection.degree
);
wheel.innerHTML = gifts
.map((gift, index) => {
let sectionHTML = `<span
class="gift"
style="
--i:${index};
--bgColor:${index % 2 ? "#ED1C24" : "#fff"};
--color:${index % 2 ? "#fff" : "#333"};
--number-of-gifts:${gifts.length}
"
>
<div class="gift-content">
<span class="gift-name">${getGiftName(gift)}</span>
<img onload="setImageFlag(${index + 1})" src="${
gift.image.thumbnail ?? gift.image.link
}" alt="Gift" class="gift-image" />
</div>
</span>
`;
return sectionHTML;
})
.join("");
};
const renderPlayCount = (count) => {
playCount.innerText = count;
};
const renderMission = (mission) => {
if (mission) {
const missionDate = new Date(mission.last_time_gain);
const currentDate = new Date();
if (currentDate.getDate() === missionDate.getDate()) {
countShare.innerHTML = "1/1";
} else {
countShare.innerHTML = `0/${mission.bonus_turns}`;
}
} else {
countShare.innerHTML = "0/1";
}
};
const renderBackground = (imgLink) => {
bgBody.src = imgLink;
};
const setImageFlag = (index) => {
imageLoadedFlags[index] = true;
};
const waitForImageLoaded = () => {
let counter = 0;
const intervalId = setInterval(() => {
++counter;
if (counter > TIME_OUT_RENDER) {
clearInterval(intervalId);
showError("Lỗi tải dữ liệu");
} else if (imageLoadedFlags.every((f) => f)) {
clearInterval(intervalId);
loading.style.display = "none";
}
}, 1000);
};
const renderGame = (data) => {
waitForImageLoaded();
renderBackground(data.spin_game.background.link);
renderWheel(data.spin_game.items);
renderPlayCount(data.remain_turn_count);
renderMission(data.missions[0]);
setStartDate(data.spin_game.start_date);
};
const changeTurns = (num) => {
playCount.innerText = Number(playCount.innerText) + num;
};
const getTurns = () => {
return Number(playCount.innerText);
};
// Modal
const showSuccessModal = (gift) => {
modal.style.display = "block";
imgSuccess.style.display = "inline-block";
btnContinueSpin.style.display = "inline-block";
imgSpin.style.display = "none";
btnUnderstand.style.display = "none";
textSuccess.innerHTML = gift.award_value;
changeTurns(-1);
};
const showMoreTurnModal = (gift) => {
modal.style.display = "block";
imgSuccess.style.display = "none";
btnContinueSpin.style.display = "inline-block";
imgSpin.style.display = "inline-block";
btnUnderstand.style.display = "none";
textSuccess.innerHTML = `
<div class="more-turns-title">Bạn đã nhận được</div>
<div class="more-turns-content">THÊM LƯỢT QUAY</div>
`;
changeTurns(gift.bonus_turns - 1);
};
const showLuckyModal = (gift) => {
modal.style.display = "block";
imgSuccess.style.display = "none";
btnContinueSpin.style.display = "none";
imgSpin.style.display = "inline-block";
btnUnderstand.style.display = "inline-block";
textSuccess.innerHTML = gift.name;
changeTurns(-1);
};
const showNoMoreTurnsModal = () => {
modal.style.display = "block";
imgSuccess.style.display = "none";
btnContinueSpin.style.display = "none";
imgSpin.style.display = "inline-block";
btnUnderstand.style.display = "inline-block";
textSuccess.innerHTML = `
<span style="font-size: 14px; font-weight:700; line-height:20px; color: #EA580C">
Bạn đã hết lượt quay. Hãy quay lại chơi vào ngày mai nhé!
</span>
`;
};
const showCommonErrorModal = (msg) => {
modal.style.display = "block";
imgSuccess.style.display = "none";
btnContinueSpin.style.display = "none";
imgSpin.style.display = "inline-block";
btnUnderstand.style.display = "inline-block";
textSuccess.innerHTML = `
<span style="font-size: 14px; font-weight:700; line-height:20px; color: #EA580C">
${msg}
</span>
`;
};
const hideModal = () => {
modal.style.display = "none";
};
const showNotSupportModal = () => {
document.body.style.overflow = "hidden";
document.body.style.pointerEvents = "none";
preventModal.style.display = "flex";
};
// Common
const checkRedirectUrl = () => {
const redirectUrl = window.location.href.split("?redirectUrl=")[1];
const cleanRedirectUrl = decodeURIComponent(redirectUrl).split("&fbclid=")[0];
if (redirectUrl) {
window.location.replace(cleanRedirectUrl);
return true;
} else {
return false;
}
};
const isWebview = (ua) => {
const rules = [
// if it says it's a webview, let's go with that
"WebView",
// iOS webview will be the same as safari but missing "Safari"
"(iPhone|iPod|iPad)(?!.*Safari)",
// Android Lollipop and Above: webview will be the same as native but it will contain "wv"
// Android KitKat to Lollipop webview will put Version/X.X Chrome/{version}.0.0.0
"Android.*(;\\s+wv|Version/\\d.\\d\\s+Chrome/\\d+(\\.0){3})",
// old chrome android webview agent
"Linux; U; Android",
];
const webviewRegExp = new RegExp("(" + rules.join("|") + ")", "ig");
return !!ua.match(webviewRegExp);
};
const isAndroid = () => {
const ua = navigator.userAgent.toLowerCase();
return ua.indexOf("android") > -1;
};
const handleShareSuccess = () => {
doMissionAPI()
.then(() => {
changeTurns(1);
countShare.innerHTML = "1/1";
window.ReactNativeWebView.postMessage("FACEBOOK_SHARE_SUCCESS");
})
.catch(() => {
window.ReactNativeWebView.postMessage("FACEBOOK_SHARE_FAIL");
});
};
const showEndGame = () => {
endGame.style.display = "block";
endGame.style.marginTop = "-20px";
loading.style.display = "none";
bgBody.src = "./images/background.png";
// Content
gameTitle.style.display = "none";
wheelWrapper.style.display = "none";
turns.style.display = "none";
missions.style.display = "none";
};