2022-07-16 21:00:02 +00:00
|
|
|
let purchaseQuantity = 1;
|
2023-03-10 03:21:02 +00:00
|
|
|
const lotteryOnReady = function () {
|
2022-09-04 23:15:37 +00:00
|
|
|
checkLotteryStats();
|
2022-07-16 21:00:02 +00:00
|
|
|
|
2022-09-04 23:15:37 +00:00
|
|
|
// Show ticket being pulled.
|
|
|
|
const ticketPulled = document.getElementById("lotteryTicketPulled");
|
|
|
|
const purchaseTicket = document.getElementById("purchaseTicket");
|
2022-07-16 21:00:02 +00:00
|
|
|
|
2022-09-04 23:15:37 +00:00
|
|
|
purchaseTicket.addEventListener("click", () => {
|
2022-07-16 21:00:02 +00:00
|
|
|
ticketPulled.style.display = "block";
|
|
|
|
|
|
|
|
setTimeout(() => {
|
2022-09-04 23:15:37 +00:00
|
|
|
ticketPulled.style.display = "none";
|
|
|
|
ticketPulled.src =
|
2023-06-29 20:14:30 +00:00
|
|
|
"/i/rDrama/lottery_active.webp?x=6&t=" +
|
2022-07-16 21:00:02 +00:00
|
|
|
new Date().getTime();
|
2022-09-04 23:15:37 +00:00
|
|
|
purchaseTicket.disabled = false;
|
2022-07-16 21:00:02 +00:00
|
|
|
}, 1780);
|
2022-09-04 23:15:37 +00:00
|
|
|
});
|
2022-07-16 21:00:02 +00:00
|
|
|
|
2022-09-04 23:15:37 +00:00
|
|
|
// Update the quantity field
|
|
|
|
const purchaseQuantityField = document.getElementById(
|
2022-07-16 21:00:02 +00:00
|
|
|
"totalQuantityOfTickets"
|
2022-09-04 23:15:37 +00:00
|
|
|
);
|
|
|
|
const purchaseTotalCostField = document.getElementById("totalCostOfTickets");
|
|
|
|
const ticketPurchaseQuantityInput = document.getElementById(
|
2022-07-16 21:00:02 +00:00
|
|
|
"ticketPurchaseQuantity"
|
2022-09-04 23:15:37 +00:00
|
|
|
);
|
2022-07-16 21:00:02 +00:00
|
|
|
|
2022-09-04 23:15:37 +00:00
|
|
|
ticketPurchaseQuantityInput.addEventListener("change", (event) => {
|
2022-07-16 21:00:02 +00:00
|
|
|
const value = Math.max(1, parseInt(event.target.value))
|
|
|
|
purchaseQuantity = value
|
2023-08-14 14:29:06 +00:00
|
|
|
purchaseQuantityField.textContent = value
|
|
|
|
purchaseTotalCostField.textContent = value * 12
|
2022-09-04 23:15:37 +00:00
|
|
|
});
|
2022-07-16 21:00:02 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
function purchaseLotteryTicket() {
|
2022-09-04 23:15:37 +00:00
|
|
|
return handleLotteryRequest("buy", "POST");
|
2022-07-16 21:00:02 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
function checkLotteryStats() {
|
2022-09-04 23:15:37 +00:00
|
|
|
return handleLotteryRequest("active", "GET");
|
2022-07-16 21:00:02 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Admin
|
|
|
|
function ensureIntent() {
|
2022-09-04 23:15:37 +00:00
|
|
|
return window.confirm("Are you sure you want to end the current lottery?");
|
2022-07-16 21:00:02 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Composed
|
|
|
|
function handleLotteryRequest(uri, method, callback = () => {}) {
|
2022-09-04 23:15:37 +00:00
|
|
|
const form = new FormData();
|
|
|
|
form.append("formkey", formkey());
|
2022-10-14 12:43:23 +00:00
|
|
|
form.append("quantity", purchaseQuantity);
|
2023-08-13 14:06:32 +00:00
|
|
|
const xhr = createXhrWithFormKey(`/lottery/${uri}`, form, method);
|
2022-10-14 12:43:23 +00:00
|
|
|
xhr[0].onload = handleLotteryResponse.bind(null, xhr[0], method, callback);
|
|
|
|
xhr[0].send(xhr[1]);
|
2022-07-16 21:00:02 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
function handleLotteryResponse(xhr, method, callback) {
|
2022-09-04 23:15:37 +00:00
|
|
|
let response;
|
2022-07-16 21:00:02 +00:00
|
|
|
|
2022-09-04 23:15:37 +00:00
|
|
|
try {
|
2022-07-16 21:00:02 +00:00
|
|
|
response = JSON.parse(xhr.response);
|
2022-09-04 23:15:37 +00:00
|
|
|
} catch (error) {
|
2022-07-16 21:00:02 +00:00
|
|
|
console.error(error);
|
2022-09-04 23:15:37 +00:00
|
|
|
}
|
2022-07-16 21:00:02 +00:00
|
|
|
|
2022-09-04 23:15:37 +00:00
|
|
|
if (method === "POST") {
|
2022-07-16 21:00:02 +00:00
|
|
|
const succeeded =
|
2022-09-04 23:15:37 +00:00
|
|
|
xhr.status >= 200 && xhr.status < 300 && response && response.message;
|
2022-07-16 21:00:02 +00:00
|
|
|
|
|
|
|
if (succeeded) {
|
2022-09-04 23:15:37 +00:00
|
|
|
// Display success.
|
|
|
|
const toast = document.getElementById("lottery-post-success");
|
|
|
|
const toastMessage = document.getElementById("lottery-post-success-text");
|
2022-07-16 21:00:02 +00:00
|
|
|
|
2023-08-14 14:29:06 +00:00
|
|
|
toastMessage.textContent = response.message;
|
2022-07-16 21:00:02 +00:00
|
|
|
|
2022-09-04 23:15:37 +00:00
|
|
|
bootstrap.Toast.getOrCreateInstance(toast).show();
|
2022-07-16 21:00:02 +00:00
|
|
|
|
2022-09-04 23:15:37 +00:00
|
|
|
callback();
|
2022-07-16 21:00:02 +00:00
|
|
|
} else {
|
2022-09-04 23:15:37 +00:00
|
|
|
// Display error.
|
|
|
|
const toast = document.getElementById("lottery-post-error");
|
|
|
|
const toastMessage = document.getElementById("lottery-post-error-text");
|
2022-07-16 21:00:02 +00:00
|
|
|
|
2023-08-14 14:29:06 +00:00
|
|
|
toastMessage.textContent =
|
2023-08-08 17:06:03 +00:00
|
|
|
(response && response.details) || "Error, please try again later.";
|
2022-07-16 21:00:02 +00:00
|
|
|
|
2022-09-04 23:15:37 +00:00
|
|
|
bootstrap.Toast.getOrCreateInstance(toast).show();
|
|
|
|
}
|
2022-07-16 21:00:02 +00:00
|
|
|
}
|
|
|
|
|
2022-09-04 23:15:37 +00:00
|
|
|
if (response && response.stats) {
|
2022-07-16 21:00:02 +00:00
|
|
|
lastStats = response.stats;
|
|
|
|
|
|
|
|
const { user, lottery, participants } = response.stats;
|
|
|
|
const [
|
2022-09-04 23:15:37 +00:00
|
|
|
prizeImage,
|
|
|
|
prizeField,
|
|
|
|
timeLeftField,
|
|
|
|
ticketsSoldThisSessionField,
|
|
|
|
participantsThisSessionField,
|
|
|
|
ticketsHeldCurrentField,
|
|
|
|
ticketsHeldTotalField,
|
|
|
|
winningsField,
|
|
|
|
purchaseTicketButton,
|
2022-07-16 21:00:02 +00:00
|
|
|
] = [
|
2022-09-04 23:15:37 +00:00
|
|
|
"prize-image",
|
|
|
|
"prize",
|
|
|
|
"timeLeft",
|
|
|
|
"ticketsSoldThisSession",
|
|
|
|
"participantsThisSession",
|
|
|
|
"ticketsHeldCurrent",
|
|
|
|
"ticketsHeldTotal",
|
|
|
|
"winnings",
|
|
|
|
"purchaseTicket",
|
2022-07-16 21:00:02 +00:00
|
|
|
].map((id) => document.getElementById(id));
|
|
|
|
|
|
|
|
if (lottery) {
|
2022-09-04 23:15:37 +00:00
|
|
|
prizeImage.style.display = "inline";
|
|
|
|
prizeField.textContent = lottery.prize;
|
|
|
|
timeLeftField.textContent = formatTimeLeft(lottery.timeLeft);
|
2022-07-16 21:00:02 +00:00
|
|
|
|
2022-09-04 23:15:37 +00:00
|
|
|
if (participants) {
|
2022-07-16 21:00:02 +00:00
|
|
|
participantsThisSessionField.textContent = participants;
|
2022-09-04 23:15:37 +00:00
|
|
|
}
|
2022-07-16 21:00:02 +00:00
|
|
|
|
2022-09-04 23:15:37 +00:00
|
|
|
ticketsSoldThisSessionField.textContent = lottery.ticketsSoldThisSession;
|
|
|
|
ticketsHeldCurrentField.textContent = user.ticketsHeld.current;
|
2022-07-16 21:00:02 +00:00
|
|
|
} else {
|
2022-09-04 23:15:37 +00:00
|
|
|
prizeImage.style.display = "none";
|
|
|
|
[
|
2022-07-16 21:00:02 +00:00
|
|
|
prizeField,
|
|
|
|
timeLeftField,
|
|
|
|
ticketsSoldThisSessionField,
|
|
|
|
participantsThisSessionField,
|
|
|
|
ticketsHeldCurrentField,
|
2022-09-04 23:15:37 +00:00
|
|
|
].forEach((e) => (e.textContent = "-"));
|
|
|
|
purchaseTicketButton.disabled = true;
|
2022-07-16 21:00:02 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
ticketsHeldTotalField.textContent = user.ticketsHeld.total;
|
|
|
|
winningsField.textContent = user.winnings;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
function formatTimeLeft(secondsLeft) {
|
2022-09-04 23:15:37 +00:00
|
|
|
const minutesLeft = Math.floor(secondsLeft / 60);
|
|
|
|
const seconds = secondsLeft % 60;
|
|
|
|
const minutes = minutesLeft % 60;
|
|
|
|
const hours = Math.floor(minutesLeft / 60);
|
2022-07-16 21:00:02 +00:00
|
|
|
|
2022-09-04 23:15:37 +00:00
|
|
|
return `${hours}h, ${minutes}m, ${seconds}s`;
|
2022-07-16 21:00:02 +00:00
|
|
|
}
|
2022-12-29 14:20:27 +00:00
|
|
|
|
|
|
|
lotteryOnReady();
|