refund on timeout

This commit is contained in:
Sewmina 2025-04-10 11:56:53 +05:30
parent 71cf1f90d5
commit b01e31f146
4 changed files with 30 additions and 77 deletions

File diff suppressed because one or more lines are too long

View File

@ -18,6 +18,7 @@ export default function HeroSection() {
const [solWallet, setSolWallet] = useState<ConnectedSolanaWallet>(); const [solWallet, setSolWallet] = useState<ConnectedSolanaWallet>();
const [myActiveBet, setMyActiveBet] = useState<Bet>(); const [myActiveBet, setMyActiveBet] = useState<Bet>();
const [rematch, setRematch] = useState(false); const [rematch, setRematch] = useState(false);
const [refund, setRefund] = useState(false);
const [lastActiveBet, setLastActiveBet] = useState<Bet>(); const [lastActiveBet, setLastActiveBet] = useState<Bet>();
const [rematchInProgress, setRematchInProgress] = useState(false); const [rematchInProgress, setRematchInProgress] = useState(false);
const [rematchTxError, setRematchTxError] = useState(false); const [rematchTxError, setRematchTxError] = useState(false);
@ -44,8 +45,10 @@ export default function HeroSection() {
},[rematch]) },[rematch])
const game_close_signal = (status: number) => { const game_close_signal = (status: number) => {
setRematch(status == 1); setRematch(status == 1);
setRefund(status == -1);
setMyActiveBet(undefined); setMyActiveBet(undefined);
}; };
@ -62,11 +65,16 @@ export default function HeroSection() {
const filledBets = fetchedBets.filter((bet) => bet.owner_id && bet.joiner_id); const filledBets = fetchedBets.filter((bet) => bet.owner_id && bet.joiner_id);
const activeBet = filledBets.find((bet) => bet.owner_id === user?.id || bet.joiner_id === user?.id); const activeBet = filledBets.find((bet) => bet.owner_id === user?.id || bet.joiner_id === user?.id);
if(!activeBet && refund){ // if user got rid of the bet and still on a refund, set refund to false
setRefund(false);
}
if(rematch){ if(rematch){
setMyActiveBet(undefined); setMyActiveBet(undefined);
return; return;
} }
setBets(filteredBets); setBets(filteredBets);
if (!myActiveBet && lastActiveBet?.address !== activeBet?.address) { if (!myActiveBet && lastActiveBet?.address !== activeBet?.address) {
setMyActiveBet(activeBet); setMyActiveBet(activeBet);
setLastActiveBet(activeBet); setLastActiveBet(activeBet);
@ -192,68 +200,6 @@ export default function HeroSection() {
} }
}; };
// const handleJoinRematch = async () => {
// console.log('rejoining rematch');
// if (!solWallet || !lastActiveBet?.owner_id || !lastActiveBet?.wager) return;
// setRematchInProgress(true);
// setRematchTxError(false);
// const maxAttempts = 50;
// let attempts = 0;
// const pollForMatchingBet = async (): Promise<Bet | null> => {
// try {
// const openBets = await fetchOpenBets(solWallet);
// const match = openBets.find(
// (bet) =>
// bet.owner_id === lastActiveBet.owner_id &&
// bet.wager === lastActiveBet.wager &&
// bet.address !== lastActiveBet.address // to ensure it's a new one
// );
// return match ?? null;
// } catch (err) {
// console.error("Error fetching bets:", err);
// return null;
// }
// };
// const tryJoin = async () => {
// while (attempts < maxAttempts) {
// const matchingBet = await pollForMatchingBet();
// if (matchingBet) {
// try {
// setRematchBetAddress(matchingBet.address);
// const tx = await joinBet(
// solWallet,
// user?.id ?? "",
// lastActiveBet.id ?? "tetris",
// matchingBet.address
// );
// console.log("Joined rematch:", tx);
// setRematch(false);
// return;
// } catch (err) {
// console.error("Join rematch failed:", err);
// setRematchTxError(true);
// return;
// } finally {
// setRematchInProgress(false);
// }
// }
// attempts++;
// await new Promise((res) => setTimeout(res, 5000));
// }
// // Max attempts reached
// setRematchTxError(true);
// setRematchInProgress(false);
// };
// tryJoin();
// };
useEffect(() => { useEffect(() => {
@ -285,20 +231,27 @@ export default function HeroSection() {
return ( return (
<> <>
{myActiveBet && !rematch ? ( {myActiveBet ? (
(myActiveBet.owner_id && myActiveBet.joiner_id) ? ( rematch ? (
<div className="w-full h-screen flex justify-center items-center bg-black"> <div className="w-full h-screen flex justify-center items-center bg-black">
<iframe <label>Loading...</label>
ref={iframeRef} </div>
src={`/UnityBuild/${myActiveBet.id}/index.html?betId=${myActiveBet.id}&owner=${myActiveBet.owner_id}&joiner=${myActiveBet.joiner_id}&address=${myActiveBet.address}&uid=${user?.id}&pubkey=${solWallet?.address}&wager=${myActiveBet.wager * 1e8}`} ) : (
className="w-full h-full" myActiveBet.owner_id && myActiveBet.joiner_id ? (
allowFullScreen <div className="w-full h-screen flex justify-center items-center bg-black">
/> <iframe
</div> ref={iframeRef}
) : <> src={`/UnityBuild/${myActiveBet.id}/index.html?betId=${myActiveBet.id}&owner=${myActiveBet.owner_id}&joiner=${myActiveBet.joiner_id}&address=${myActiveBet.address}&uid=${user?.id}&pubkey=${solWallet?.address}&wager=${myActiveBet.wager * 1e8}`}
<div className="w-full h-screen flex justify-center items-center bg-black"> className="w-full h-full"
<label>Loading...</label> allowFullScreen
</div></> />
</div>
) : (
<div className="w-full h-screen flex justify-center items-center bg-black">
<label>Loading...</label>
</div>
)
)
) : ( ) : (
<section className="flex flex-col items-center text-center py-16"> <section className="flex flex-col items-center text-center py-16">
<Image <Image