websocket quee developed
This commit is contained in:
parent
8b751e752f
commit
14f8b02662
|
|
@ -1,144 +1,175 @@
|
||||||
import { useState, useEffect } from 'react';
|
import { useState, useEffect } from "react";
|
||||||
import { useLotteryAuth } from '@/store/useLotteryAuth';
|
import { useLotteryAuth } from "@/store/useLotteryAuth";
|
||||||
import { LotteryWinnerDataSimplified } from '@/typings/lottery/lottery.types';
|
import { LotteryWinnerDataSimplified } from "@/typings/lottery/lottery.types";
|
||||||
import LotteryWinnersList from './winners/LotteryWinnersList';
|
import LotteryWinnersList from "./winners/LotteryWinnersList";
|
||||||
import LotterySlotCounter from './slotCounter/LotterySlotCounter';
|
import LotterySlotCounter from "./slotCounter/LotterySlotCounter";
|
||||||
import ReactConfetti from 'react-confetti';
|
import ReactConfetti from "react-confetti";
|
||||||
import { useWindowSize } from 'react-use';
|
import { useWindowSize } from "react-use";
|
||||||
import AnimatedText from '@/components/common/AnimatedText';
|
import AnimatedText from "@/components/common/AnimatedText";
|
||||||
import { useWebsocketLottery } from '@/hooks/useWebSocketLottery';
|
import { useWebsocketLottery } from "@/hooks/useWebSocketLottery";
|
||||||
import Confetti from '../common/Confetti';
|
import Confetti from "../common/Confetti";
|
||||||
import { AnimatePresence, motion } from 'framer-motion';
|
import { AnimatePresence, motion } from "framer-motion";
|
||||||
|
|
||||||
const WEBSOCKET_URL = 'wss://sms.turkmentv.gov.tm/ws/lottery?dst=0506';
|
const WEBSOCKET_URL = "wss://sms.turkmentv.gov.tm/ws/lottery?dst=0506";
|
||||||
const SLOT_COUNTER_DURATION = 30000;
|
const SLOT_COUNTER_DURATION = 30000;
|
||||||
|
|
||||||
const LotteryWinnersSection = ({ lotteryStatus }: { lotteryStatus: string }) => {
|
const LotteryWinnersSection = ({
|
||||||
|
lotteryStatus,
|
||||||
|
}: {
|
||||||
|
lotteryStatus: string;
|
||||||
|
}) => {
|
||||||
const [winners, setWinners] = useState<LotteryWinnerDataSimplified[]>([]);
|
const [winners, setWinners] = useState<LotteryWinnerDataSimplified[]>([]);
|
||||||
const [currentNumber, setCurrentNumber] = useState<string>('00-00-00-00-00');
|
const [currentNumber, setCurrentNumber] = useState<string>("00-00-00-00-00");
|
||||||
const [startNumber, setStartNumber] = useState<string>('00-00-00-00-00');
|
|
||||||
const [isConfettiActive, setIsConfettiActive] = useState(false);
|
const [isConfettiActive, setIsConfettiActive] = useState(false);
|
||||||
|
|
||||||
const { lotteryData } = useLotteryAuth();
|
const { lotteryData } = useLotteryAuth();
|
||||||
const [winnerSelectingStatus, setWinnerSelectingStatus] = useState<
|
const [winnerSelectingStatus, setWinnerSelectingStatus] = useState<
|
||||||
'not-selected' | 'is-selecting' | 'selected'
|
"not-selected" | "is-selecting" | "selected"
|
||||||
>('not-selected');
|
>("not-selected");
|
||||||
const [pendingWinner, setPendingWinner] = useState<LotteryWinnerDataSimplified | null>(null);
|
const [pendingWinner, setPendingWinner] =
|
||||||
const [topText, setTopText] = useState<string>('Bije az wagtdan başlaýar');
|
useState<LotteryWinnerDataSimplified | null>(null);
|
||||||
const [bottomText, setBottomText] = useState<string>('');
|
const [topText, setTopText] = useState<string>("Bije az wagtdan başlaýar");
|
||||||
|
const [bottomText, setBottomText] = useState<string>("");
|
||||||
|
|
||||||
|
const [messageQueue, setMessageQueue] = useState<
|
||||||
|
LotteryWinnerDataSimplified[]
|
||||||
|
>([]); // Queue for incoming WebSocket messages
|
||||||
|
const [isProcessing, setIsProcessing] = useState<boolean>(false); // Track if a message is being processed
|
||||||
|
|
||||||
// WebSocket Hook
|
|
||||||
const { wsStatus, subscribeToMessages } = useWebsocketLottery(WEBSOCKET_URL);
|
const { wsStatus, subscribeToMessages } = useWebsocketLottery(WEBSOCKET_URL);
|
||||||
|
|
||||||
|
// Simulate WebSocket messages for testing
|
||||||
|
const simulateMessage = () => {
|
||||||
|
const dummyWinner: LotteryWinnerDataSimplified = {
|
||||||
|
client: `9936${Math.floor(10000000 + Math.random() * 90000000)}`,
|
||||||
|
winner_no: winners.length + 1,
|
||||||
|
ticket: `${Math.floor(Math.random() * 99)
|
||||||
|
.toString()
|
||||||
|
.padStart(2, "0")}-${Math.floor(Math.random() * 99)
|
||||||
|
.toString()
|
||||||
|
.padStart(2, "0")}-${Math.floor(Math.random() * 99)
|
||||||
|
.toString()
|
||||||
|
.padStart(2, "0")}-${Math.floor(Math.random() * 99)
|
||||||
|
.toString()
|
||||||
|
.padStart(2, "0")}-${Math.floor(Math.random() * 99)
|
||||||
|
.toString()
|
||||||
|
.padStart(2, "0")}`,
|
||||||
|
};
|
||||||
|
|
||||||
|
setMessageQueue((prevQueue) => [...prevQueue, dummyWinner]);
|
||||||
|
};
|
||||||
|
|
||||||
|
// Initialize winners from lottery data
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
if (lotteryData) {
|
if (lotteryData && lotteryData.data.winners.length > 0) {
|
||||||
if (lotteryData?.data.winners.length > 0) {
|
const simplifiedWinners = lotteryData.data.winners.map((winner) => ({
|
||||||
const simplifiedWinners = lotteryData.data.winners.map((winner) => ({
|
client: winner.client,
|
||||||
client: winner.client,
|
winner_no: winner.winner_no,
|
||||||
winner_no: winner.winner_no,
|
ticket: winner.ticket,
|
||||||
ticket: winner.ticket,
|
}));
|
||||||
}));
|
setWinners(simplifiedWinners);
|
||||||
setWinners(simplifiedWinners);
|
|
||||||
// setCurrentNumber(lotteryData.data.winners.at(-1)?.ticket || '00-00-00-00-00');
|
const lastWinner = simplifiedWinners[simplifiedWinners.length - 1];
|
||||||
setWinnerSelectingStatus('selected');
|
setWinnerSelectingStatus("selected");
|
||||||
setTopText(`${lotteryData.data.winners.at(-1)?.winner_no}-nji ýeňiji`);
|
setTopText(`${lastWinner.winner_no}-nji ýeňiji`);
|
||||||
setBottomText(lotteryData.data.winners.at(-1)?.client || '');
|
setBottomText(lastWinner.client);
|
||||||
setIsConfettiActive(true);
|
setIsConfettiActive(true);
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}, [lotteryData]);
|
}, [lotteryData]);
|
||||||
|
|
||||||
|
// Subscribe to WebSocket messages
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
const unsubscribe = subscribeToMessages((event) => {
|
const unsubscribe = subscribeToMessages((event) => {
|
||||||
const newWinner = JSON.parse(event.data);
|
try {
|
||||||
if (!newWinner || !newWinner.phone || !newWinner.winner_no || !newWinner.ticket) {
|
const newWinner: LotteryWinnerDataSimplified = JSON.parse(event.data);
|
||||||
console.error('❌ Invalid data format received');
|
|
||||||
return;
|
// Add new message to the queue
|
||||||
|
setMessageQueue((prevQueue) => [...prevQueue, newWinner]);
|
||||||
|
} catch (error) {
|
||||||
|
console.error("Error parsing WebSocket message:", error);
|
||||||
}
|
}
|
||||||
|
|
||||||
const winnerData = {
|
|
||||||
client: newWinner.phone,
|
|
||||||
winner_no: newWinner.winner_no,
|
|
||||||
ticket: newWinner.ticket,
|
|
||||||
};
|
|
||||||
|
|
||||||
setIsConfettiActive(false);
|
|
||||||
setTopText(`${winnerData.winner_no}-nji ýeňiji saýlanýar`);
|
|
||||||
setBottomText('...');
|
|
||||||
setWinnerSelectingStatus('is-selecting');
|
|
||||||
setPendingWinner(winnerData);
|
|
||||||
setCurrentNumber(winnerData.ticket);
|
|
||||||
|
|
||||||
setTimeout(() => {
|
|
||||||
setTopText(`${winnerData.winner_no}-nji ýeňiji`);
|
|
||||||
setBottomText(winnerData.client);
|
|
||||||
setWinnerSelectingStatus('selected');
|
|
||||||
setIsConfettiActive(true);
|
|
||||||
setWinners((prev) => [...prev, winnerData]);
|
|
||||||
}, SLOT_COUNTER_DURATION);
|
|
||||||
});
|
});
|
||||||
|
|
||||||
return () => {
|
return () => unsubscribe();
|
||||||
unsubscribe();
|
|
||||||
};
|
|
||||||
}, [subscribeToMessages]);
|
}, [subscribeToMessages]);
|
||||||
|
|
||||||
// useEffect(() => {
|
// Process queue when a new message is added
|
||||||
// setTimeout(() => {
|
useEffect(() => {
|
||||||
// setIsConfettiActive(false);
|
if (!isProcessing && messageQueue.length > 0) {
|
||||||
// setTopText(`${1}-nji ýeňiji saýlanýar`);
|
processQueue();
|
||||||
// setBottomText('...');
|
}
|
||||||
// setWinnerSelectingStatus('is-selecting');
|
}, [messageQueue, isProcessing]);
|
||||||
// // setPendingWinner(winnerData);
|
|
||||||
// setCurrentNumber('55-44-33-22-11');
|
|
||||||
|
|
||||||
// setTimeout(() => {
|
// Process a single message from the queue
|
||||||
// setTopText('Ýeniji');
|
const processQueue = async () => {
|
||||||
// setBottomText('99361245555');
|
if (isProcessing || messageQueue.length === 0) return;
|
||||||
// setWinnerSelectingStatus('selected');
|
|
||||||
// setIsConfettiActive(true);
|
|
||||||
// // setWinners((prev) => [...prev, winnerData]);
|
|
||||||
// }, SLOT_COUNTER_DURATION);
|
|
||||||
// }, 10000);
|
|
||||||
|
|
||||||
// setTimeout(() => {
|
setIsProcessing(true); // Lock processing
|
||||||
// setIsConfettiActive(false);
|
const message = messageQueue[0]; // Get the first message in the queue
|
||||||
// setTopText(`${1}-nji ýeňiji saýlanýar`);
|
|
||||||
// setBottomText('...');
|
|
||||||
// setWinnerSelectingStatus('is-selecting');
|
|
||||||
// // setPendingWinner(winnerData);
|
|
||||||
// setCurrentNumber('11-22-33-44-55');
|
|
||||||
|
|
||||||
// setTimeout(() => {
|
try {
|
||||||
// setTopText('Ýeniji');
|
await handleMessage(message); // Process the message
|
||||||
// setBottomText('99361245555');
|
} catch (error) {
|
||||||
// setWinnerSelectingStatus('selected');
|
console.error("Error processing message:", error);
|
||||||
// setIsConfettiActive(true);
|
}
|
||||||
// // setWinners((prev) => [...prev, winnerData]);
|
|
||||||
// }, SLOT_COUNTER_DURATION);
|
setMessageQueue((prevQueue) => prevQueue.slice(1)); // Remove the processed message from the queue
|
||||||
// }, SLOT_COUNTER_DURATION + 20000);
|
setIsProcessing(false); // Unlock processing
|
||||||
// }, []);
|
};
|
||||||
|
|
||||||
|
// Handle the logic for processing a single WebSocket message
|
||||||
|
const handleMessage = async (winner: LotteryWinnerDataSimplified) => {
|
||||||
|
setIsConfettiActive(false);
|
||||||
|
setTopText(`${winner.winner_no}-nji ýeňiji saýlanýar`);
|
||||||
|
setBottomText("...");
|
||||||
|
setWinnerSelectingStatus("is-selecting");
|
||||||
|
setPendingWinner(winner);
|
||||||
|
setCurrentNumber(winner.ticket);
|
||||||
|
|
||||||
|
// Wait for the animation duration
|
||||||
|
await new Promise((resolve) => setTimeout(resolve, SLOT_COUNTER_DURATION));
|
||||||
|
|
||||||
|
// Finalize winner selection
|
||||||
|
setTopText(`${winner.winner_no}-nji ýeňiji`);
|
||||||
|
setBottomText(winner.client);
|
||||||
|
setWinnerSelectingStatus("selected");
|
||||||
|
setIsConfettiActive(true);
|
||||||
|
|
||||||
|
// Add the winner to the list
|
||||||
|
setWinners((prevWinners) => [...prevWinners, winner]);
|
||||||
|
// Wait for the animation duration
|
||||||
|
await new Promise((resolve) => setTimeout(resolve, 5000));
|
||||||
|
};
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<section>
|
<section>
|
||||||
{wsStatus === 'error' && (
|
{wsStatus === "error" && (
|
||||||
<div className="text-red-500 text-center mb-2">Websocket connection error.</div>
|
<div className="text-red-500 text-center mb-2">
|
||||||
|
Websocket connection error.
|
||||||
|
</div>
|
||||||
)}
|
)}
|
||||||
<Confetti showConfetti={isConfettiActive} numberOfPieces={300} />{' '}
|
<Confetti showConfetti={isConfettiActive} numberOfPieces={300} />
|
||||||
|
|
||||||
|
{/* Simmulation Button */}
|
||||||
|
{/* <div className="w-full flex justify-center py-4">
|
||||||
|
<button
|
||||||
|
onClick={simulateMessage}
|
||||||
|
className="px-4 py-2 bg-blue-500 text-white rounded"
|
||||||
|
>
|
||||||
|
Simulate Message
|
||||||
|
</button>
|
||||||
|
</div> */}
|
||||||
|
|
||||||
<div className="container">
|
<div className="container">
|
||||||
<div
|
<div
|
||||||
className="flex flex-col items-center rounded-[32px] gap-[40px]"
|
className="flex flex-col items-center rounded-[32px] gap-[40px]"
|
||||||
style={{
|
style={{
|
||||||
background: 'linear-gradient(180deg, #F0ECF4 0%, #E1E0FF 43.5%)',
|
background: "linear-gradient(180deg, #F0ECF4 0%, #E1E0FF 43.5%)",
|
||||||
}}>
|
}}
|
||||||
|
>
|
||||||
<AnimatePresence>
|
<AnimatePresence>
|
||||||
<div className="flex items-center justify-center w-full sm:min-h-[240px] pt-6">
|
<div className="flex items-center justify-center w-full sm:min-h-[240px] pt-6">
|
||||||
{winnerSelectingStatus === 'not-selected' ? (
|
{winnerSelectingStatus === "not-selected" ? (
|
||||||
// <TextMaskReveal
|
|
||||||
// text={topText}
|
|
||||||
// className="text-center flex items-center justify-center text-[100px] leading-[108px] text-[#E65E19]"
|
|
||||||
// duration={0.5}
|
|
||||||
// />
|
|
||||||
<AnimatedText
|
<AnimatedText
|
||||||
key={topText}
|
key={topText}
|
||||||
text={topText}
|
text={topText}
|
||||||
|
|
@ -158,12 +189,8 @@ const LotteryWinnersSection = ({ lotteryStatus }: { lotteryStatus: string }) =>
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
}}
|
}}
|
||||||
className="flex flex-col items-center justify-center">
|
className="flex flex-col items-center justify-center"
|
||||||
{/* <TextMaskReveal
|
>
|
||||||
text={topText}
|
|
||||||
className="text-center text-[56px] leading-[64px] text-[#E65E19]"
|
|
||||||
duration={0.4}
|
|
||||||
/> */}
|
|
||||||
<AnimatedText
|
<AnimatedText
|
||||||
key={topText}
|
key={topText}
|
||||||
text={topText}
|
text={topText}
|
||||||
|
|
@ -171,18 +198,11 @@ const LotteryWinnersSection = ({ lotteryStatus }: { lotteryStatus: string }) =>
|
||||||
duration={0.4}
|
duration={0.4}
|
||||||
/>
|
/>
|
||||||
{bottomText && (
|
{bottomText && (
|
||||||
// <TextMaskReveal
|
|
||||||
// text={bottomText}
|
|
||||||
// className="text-center text-[80px] leading-[88px] text-[#E65E19]"
|
|
||||||
// duration={0.4}
|
|
||||||
// startDelay={3}
|
|
||||||
// />
|
|
||||||
<AnimatedText
|
<AnimatedText
|
||||||
key={bottomText}
|
key={bottomText}
|
||||||
text={bottomText}
|
text={bottomText}
|
||||||
className="text-center sm:text-[80px] text-[32px] leading-[120%] text-[#E65E19]"
|
className="text-center sm:text-[80px] text-[32px] leading-[120%] text-[#E65E19]"
|
||||||
duration={0.4}
|
duration={0.4}
|
||||||
// characterDelay={2}
|
|
||||||
/>
|
/>
|
||||||
)}
|
)}
|
||||||
</motion.div>
|
</motion.div>
|
||||||
|
|
@ -191,7 +211,9 @@ const LotteryWinnersSection = ({ lotteryStatus }: { lotteryStatus: string }) =>
|
||||||
</AnimatePresence>
|
</AnimatePresence>
|
||||||
|
|
||||||
<div className="z-10">
|
<div className="z-10">
|
||||||
{currentNumber && <LotterySlotCounter numberString={currentNumber} />}
|
{currentNumber && (
|
||||||
|
<LotterySlotCounter numberString={currentNumber} />
|
||||||
|
)}
|
||||||
</div>
|
</div>
|
||||||
<div className="flex gap-6 rounded-[12px] flex-1 w-full items-center justify-center sm:pb-[62px] pb-[32px] px-4">
|
<div className="flex gap-6 rounded-[12px] flex-1 w-full items-center justify-center sm:pb-[62px] pb-[32px] px-4">
|
||||||
{winners.length > 0 && <LotteryWinnersList winners={winners} />}
|
{winners.length > 0 && <LotteryWinnersList winners={winners} />}
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue