import React, { useState, useEffect } from "react"; import { IPlayer } from "../PersonObjects/IPlayer"; import { Money } from "../ui/React/Money"; import { win, reachedLimit } from "./Game"; import { WHRNG } from "./RNG"; import { trusted } from "./utils"; import Typography from "@mui/material/Typography"; import Button from "@mui/material/Button"; import TextField from "@mui/material/TextField"; type IProps = { p: IPlayer; }; const minPlay = 0; const maxPlay = 1e7; function isRed(n: number): boolean { return [1, 3, 5, 7, 9, 12, 14, 16, 18, 19, 21, 23, 25, 27, 30, 32, 34, 36].includes(n); } type Strategy = { match: (n: number) => boolean; payout: number; }; const redNumbers: number[] = [1, 3, 5, 7, 9, 12, 14, 16, 18, 19, 21, 23, 25, 27, 30, 32, 34, 36]; const strategies: { Red: Strategy; Black: Strategy; Odd: Strategy; Even: Strategy; High: Strategy; Low: Strategy; Third1: Strategy; Third2: Strategy; Third3: Strategy; } = { Red: { match: (n: number): boolean => { if (n === 0) return false; return redNumbers.includes(n); }, payout: 1, }, Black: { match: (n: number): boolean => { return !redNumbers.includes(n); }, payout: 1, }, Odd: { match: (n: number): boolean => { if (n === 0) return false; return n % 2 === 1; }, payout: 1, }, Even: { match: (n: number): boolean => { if (n === 0) return false; return n % 2 === 0; }, payout: 1, }, High: { match: (n: number): boolean => { if (n === 0) return false; return n > 18; }, payout: 1, }, Low: { match: (n: number): boolean => { if (n === 0) return false; return n < 19; }, payout: 1, }, Third1: { match: (n: number): boolean => { if (n === 0) return false; return n <= 12; }, payout: 2, }, Third2: { match: (n: number): boolean => { if (n === 0) return false; return n >= 13 && n <= 24; }, payout: 2, }, Third3: { match: (n: number): boolean => { if (n === 0) return false; return n >= 25; }, payout: 2, }, }; function Single(s: number): Strategy { return { match: (n: number): boolean => { return s === n; }, payout: 36, }; } export function Roulette(props: IProps): React.ReactElement { const [rng] = useState(new WHRNG(new Date().getTime())); const [investment, setInvestment] = useState(1000); const [canPlay, setCanPlay] = useState(true); const [status, setStatus] = useState("waiting"); const [n, setN] = useState(0); const [lock, setLock] = useState(true); const [strategy, setStrategy] = useState({ payout: 0, match: (): boolean => { return false; }, }); useEffect(() => { const i = window.setInterval(step, 50); return () => clearInterval(i); }); function step(): void { if (!lock) { setN(Math.floor(Math.random() * 37)); } } function updateInvestment(e: React.ChangeEvent): void { let investment: number = parseInt(e.currentTarget.value); if (isNaN(investment)) { investment = minPlay; } if (investment > maxPlay) { investment = maxPlay; } if (investment < minPlay) { investment = minPlay; } setInvestment(investment); } function currentNumber(): string { if (n === 0) return "0"; const color = isRed(n) ? "R" : "B"; return `${n}${color}`; } function play(s: Strategy): void { if (reachedLimit(props.p)) return; setCanPlay(false); setLock(false); setStatus("playing"); setStrategy(s); setTimeout(() => { let n = Math.floor(rng.random() * 37); let status = <>; let gain = 0; let playerWin = strategy.match(n); // oh yeah, the house straight up cheats. Try finding the seed now! if (playerWin && Math.random() > 0.9) { playerWin = false; while (strategy.match(n)) { n = (n + 1) % 36; } } if (playerWin) { gain = investment * strategy.payout; status = ( <> won ); } else { gain = -investment; status = ( <> lost ); } win(props.p, gain); setCanPlay(true); setLock(true); setStatus(status); setN(n); reachedLimit(props.p); }, 1600); } return ( <> {currentNumber()} {status}
); }