Skip to content

Commit

Permalink
Merge pull request #88 from NovaDrake76/dev
Browse files Browse the repository at this point in the history
Dev
  • Loading branch information
NovaDrake76 authored Jun 12, 2024
2 parents ba58ecc + 11d1c85 commit 901293d
Show file tree
Hide file tree
Showing 4 changed files with 74 additions and 66 deletions.
9 changes: 8 additions & 1 deletion backend/games/crash.js
Original file line number Diff line number Diff line change
Expand Up @@ -148,7 +148,14 @@ const crashGame = (io) => {
const calculateCrashPoint = () => {
const e = 2 ** 32
const h = crypto.getRandomValues(new Uint32Array(1))[0]
return Math.floor((100 * e - h) / (e - h)) / 100
let crashPoint = Math.floor((100 * e - h) / (e - h)) / 100

// 3% of chance to crash instantly because there's a guy named "cat" that is using an autoclicker
if (Math.random() < 0.03) {
crashPoint = 1.00;
}

return crashPoint;
};

startGame();
Expand Down
20 changes: 10 additions & 10 deletions src/pages/Crash/Crash.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ const CrashGame = () => {
const [userMultiplier, setUserMultiplier] = useState(0);
const [animationSrc, setAnimationSrc] = useState(idle);
const [userCashedOut, setUserCashedOut] = useState(false);
const [disableButton, setDisableButton] = useState(false);
const [gameState, setGameState] = useState<any>({
gameBets: {},
gamePlayers: {},
Expand All @@ -50,14 +51,16 @@ const CrashGame = () => {
fixedItem: userData?.fixedItem,
payout: null,
walletBalance: userData?.walletBalance
}]
}];
setUserGambled(true);

socket.emit("crash:bet", user[0], bet);
setUserCashedOut(false);
};

const handleCashout = () => {
console.log("cashout")
setDisableButton(true); // Disable the button immediately
const user = {
id: userData?.id,
name: userData?.username,
Expand All @@ -67,13 +70,16 @@ const CrashGame = () => {
payout: null
};

socket.emit("crash:cashout", user);
socket.emit("crash:cashout", user, () => {
setDisableButton(false); // Re-enable the button after the server responds
});
};

useEffect(() => {
const cashoutSuccessListener = (data: any) => {
setUserMultiplier(data.multiplier);
setUserCashedOut(true);
setDisableButton(false); // Ensure the button is enabled after a successful cashout
};

socket.on("crash:cashoutSuccess", cashoutSuccessListener);
Expand All @@ -95,7 +101,6 @@ const CrashGame = () => {
};
}, []);


useEffect(() => {
const startListener = () => {
setAnimationSrc(up);
Expand Down Expand Up @@ -136,7 +141,6 @@ const CrashGame = () => {
timeoutId = setTimeout(() => setAnimationSrc(idle), 700);
};


socket.on("crash:start", startListener);
socket.on("crash:result", resultListener);

Expand All @@ -163,7 +167,6 @@ const CrashGame = () => {
};
}, []);


useEffect(() => {
if (countDown > 0.1 && !gameStarted) {
setTimeout(() => {
Expand All @@ -172,11 +175,11 @@ const CrashGame = () => {
}
}, [countDown]);


return (
<div className="w-screen flex flex-col items-center justify-center gap-12">
<div className="flex bg-[#212031] rounded flex-col lg:flex-row">
<SideMenu bet={bet} setBet={setBet} gameStarted={gameStarted} handleBet={handleBet} handleCashout={handleCashout} isLogged={isLogged} userGambled={userGambled} userCashedOut={userCashedOut} userData={userData} userMultiplier={userMultiplier} />
<SideMenu bet={bet} setBet={setBet} gameStarted={gameStarted} handleBet={handleBet} handleCashout={handleCashout}
isLogged={isLogged} userGambled={userGambled} userCashedOut={userCashedOut} userData={userData} userMultiplier={userMultiplier} disableButton={disableButton}/>
<GameContainer
crashPoint={crashPoint}
multiplier={multiplier}
Expand All @@ -189,10 +192,7 @@ const CrashGame = () => {
falling={falling}
history={history} />
</div>


<LiveBets gameState={gameState} />

</div >
);
};
Expand Down
110 changes: 55 additions & 55 deletions src/pages/Crash/SideMenu.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -11,64 +11,64 @@ interface SideMenuProps {
userCashedOut: boolean;
userData: User;
userMultiplier: number;
disableButton: boolean;
}

const SideMenu: React.FC<SideMenuProps> = ({ bet, setBet, gameStarted, handleBet, handleCashout, isLogged, userGambled, userCashedOut, userData, userMultiplier }) => {
const SideMenu: React.FC<SideMenuProps> = ({ bet, setBet, gameStarted, handleBet, handleCashout, isLogged, userGambled, userCashedOut, userData, userMultiplier, disableButton }) => {

const renderMessage = () => {
let message = "";

if (!isLogged) {
message = "Sign in to play";
} else if (userCashedOut) {
message = `Cashed Out at x${userMultiplier.toFixed(2)}`;
} else if (userGambled) {
message = gameStarted ? "Cash Out" : "You're in!";
} else if (gameStarted) {
message = "Wait for next round";
} else if (bet === 0 || !bet || bet < 1) {
message = "Place the bet value";
} else if (bet > 1000000) {
message = "Max bet is 1M";
} else if (userData.walletBalance < (bet ?? 0)) {
message = "Not enough money";
} else {
message = "Place Bet";
}
return message;
let message = "";
if (!isLogged) {
message = "Sign in to play";
} else if (userCashedOut) {
message = `Cashed Out at x${userMultiplier.toFixed(2)}`;
} else if (userGambled) {
message = gameStarted ? "Cash Out" : "You're in!";
} else if (gameStarted) {
message = "Wait for next round";
} else if (bet === 0 || !bet || bet < 1) {
message = "Place the bet value";
} else if (bet > 1000000) {
message = "Max bet is 1M";
} else if (userData.walletBalance < (bet ?? 0)) {
message = "Not enough money";
} else {
message = "Place Bet";
}
return message;
}

return (
<div className="lg:w-[340px] flex flex-col items-center gap-4 border-r border-gray-700 py-4 px-6">
<input
type="number"
value={bet || ""}
onKeyDown={(event) => {
if (!/[0-9]/.test(event.key) && event.key !== "Backspace") {
event.preventDefault();
}
}}
max={1000000}
onChange={(e) => {
const value = Number(e.target.value);
setBet(value < 0 ? 0 : value);
}}
className="p-2 border rounded w-1/2 lg:w-full"
/>
<button
onClick={gameStarted ? handleCashout : handleBet}
className="p-2 border rounded bg-indigo-600 hover:bg-indigo-700 w-full mt-4"
disabled={
(gameStarted && (!userGambled || userCashedOut)) ||
(!gameStarted && userGambled) || (!gameStarted && bet === 0 || !bet || bet > 1000000)
}
>
{renderMessage()}
</button>

</div>
)

}

export default SideMenu;
<div className="lg:w-[340px] flex flex-col items-center gap-4 border-r border-gray-700 py-4 px-6">
<input
type="number"
value={bet || ""}
onKeyDown={(event) => {
if (!/[0-9]/.test(event.key) && event.key !== "Backspace") {
event.preventDefault();
}
}}
max={1000000}
onChange={(e) => {
const value = Number(e.target.value);
setBet(value < 0 ? 0 : value);
}}
className="p-2 border rounded w-1/2 lg:w-full"
/>
<button
onClick={gameStarted ? handleCashout : handleBet}
className="p-2 border rounded bg-indigo-600 hover:bg-indigo-700 w-full mt-4"
disabled={
(gameStarted && (!userGambled || userCashedOut)) ||
(!gameStarted && userGambled) || (!gameStarted && bet === 0 || !bet || bet > 1000000) || disableButton
}
>
{renderMessage()}
</button>
</div>
);
}

export default SideMenu;

1 change: 1 addition & 0 deletions src/pages/Slot/Slot.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,7 @@ const Slots = () => {

if (userData?.walletBalance < betAmount) {
toast.error("Insufficient funds");
setIsSpinning(false)
return;
}

Expand Down

0 comments on commit 901293d

Please sign in to comment.