bitburner-src/src/Casino/SlotMachine.tsx

313 lines
7.8 KiB
TypeScript
Raw Normal View History

2021-10-01 07:00:50 +02:00
import React, { useState, useEffect } from "react";
2021-09-05 01:09:30 +02:00
import { IPlayer } from "../PersonObjects/IPlayer";
import { Money } from "../ui/React/Money";
import { WHRNG } from "./RNG";
2021-10-01 07:00:50 +02:00
import { win, reachedLimit } from "./Game";
2021-09-05 01:09:30 +02:00
import { trusted } from "./utils";
2021-10-01 07:00:50 +02:00
import Typography from "@mui/material/Typography";
import TextField from "@mui/material/TextField";
import Button from "@mui/material/Button";
type IProps = {
2021-09-05 01:09:30 +02:00
p: IPlayer;
};
// statically shuffled array of symbols.
2021-09-05 01:09:30 +02:00
const symbols = [
"D",
"C",
"$",
"?",
"♥",
"A",
"C",
"B",
"C",
"E",
"B",
"E",
"C",
"*",
"D",
"♥",
"B",
"A",
"A",
"A",
"C",
"A",
"D",
"B",
"E",
"?",
"D",
"*",
"@",
"♥",
"B",
"E",
"?",
];
function getPayout(s: string, n: number): number {
2021-09-05 01:09:30 +02:00
switch (s) {
case "$":
return [20, 200, 1000][n];
case "@":
return [8, 80, 400][n];
case "♥":
case "?":
return [6, 20, 150][n];
case "D":
case "E":
return [1, 8, 30][n];
default:
return [1, 5, 20][n];
}
}
const payLines = [
2021-09-05 01:09:30 +02:00
// lines
[
[0, 0],
[0, 1],
[0, 2],
[0, 3],
[0, 4],
],
[
[1, 0],
[1, 1],
[1, 2],
[1, 3],
[1, 4],
],
[
[2, 0],
[2, 1],
[2, 2],
[2, 3],
[2, 4],
],
// Vs
[
[2, 0],
[1, 1],
[0, 2],
[1, 3],
[2, 4],
],
[
[0, 0],
[1, 1],
[2, 2],
[1, 3],
[0, 4],
],
// rest
[
[0, 0],
[1, 1],
[1, 2],
[1, 3],
[0, 4],
],
[
[2, 0],
[1, 1],
[1, 2],
[1, 3],
[2, 4],
],
[
[1, 0],
[0, 1],
[0, 2],
[0, 3],
[1, 4],
],
[
[1, 0],
[2, 1],
[2, 2],
[2, 3],
[1, 4],
],
];
const minPlay = 0;
const maxPlay = 1e6;
2021-10-01 07:00:50 +02:00
export function SlotMachine(props: IProps): React.ReactElement {
const [rng] = useState(new WHRNG(props.p.totalPlaytime));
const [index, setIndex] = useState<number[]>([0, 0, 0, 0, 0]);
const [locks, setLocks] = useState<number[]>([0, 0, 0, 0, 0]);
const [investment, setInvestment] = useState(1000);
const [canPlay, setCanPlay] = useState(true);
const [status, setStatus] = useState<string | JSX.Element>("waiting");
2021-09-05 01:09:30 +02:00
2021-10-01 07:00:50 +02:00
useEffect(() => {
const i = window.setInterval(step, 50);
return () => clearInterval(i);
});
2021-09-05 01:09:30 +02:00
2021-10-01 07:00:50 +02:00
function step(): void {
2021-09-05 01:09:30 +02:00
let stoppedOne = false;
2021-10-01 07:00:50 +02:00
const copy = index.slice();
2022-01-16 01:45:03 +01:00
for (let i = 0; i < copy.length; i++) {
2021-10-01 07:00:50 +02:00
if (copy[i] === locks[i] && !stoppedOne) continue;
copy[i] = (copy[i] + 1) % symbols.length;
2021-09-05 01:09:30 +02:00
stoppedOne = true;
}
2021-10-01 07:00:50 +02:00
setIndex(copy);
2021-10-01 07:00:50 +02:00
if (stoppedOne && copy.every((e, i) => e === locks[i])) {
checkWinnings();
}
2021-09-05 01:09:30 +02:00
}
2021-10-01 07:00:50 +02:00
function getTable(): string[][] {
2021-09-05 01:09:30 +02:00
return [
[
2021-10-01 07:00:50 +02:00
symbols[(index[0] + symbols.length - 1) % symbols.length],
symbols[(index[1] + symbols.length - 1) % symbols.length],
symbols[(index[2] + symbols.length - 1) % symbols.length],
symbols[(index[3] + symbols.length - 1) % symbols.length],
symbols[(index[4] + symbols.length - 1) % symbols.length],
2021-09-05 01:09:30 +02:00
],
2021-10-01 07:00:50 +02:00
[symbols[index[0]], symbols[index[1]], symbols[index[2]], symbols[index[3]], symbols[index[4]]],
2021-09-05 01:09:30 +02:00
[
2021-10-01 07:00:50 +02:00
symbols[(index[0] + 1) % symbols.length],
symbols[(index[1] + 1) % symbols.length],
symbols[(index[2] + 1) % symbols.length],
symbols[(index[3] + 1) % symbols.length],
symbols[(index[4] + 1) % symbols.length],
2021-09-05 01:09:30 +02:00
],
];
}
2021-10-01 07:00:50 +02:00
function play(): void {
if (reachedLimit(props.p)) return;
setStatus("playing");
win(props.p, -investment);
if (!canPlay) return;
unlock();
setTimeout(lock, rng.random() * 2000 + 1000);
2021-09-05 01:09:30 +02:00
}
2021-10-01 07:00:50 +02:00
function lock(): void {
setLocks([
Math.floor(rng.random() * symbols.length),
Math.floor(rng.random() * symbols.length),
Math.floor(rng.random() * symbols.length),
Math.floor(rng.random() * symbols.length),
Math.floor(rng.random() * symbols.length),
]);
2021-09-05 01:09:30 +02:00
}
2021-10-01 07:00:50 +02:00
function checkWinnings(): void {
const t = getTable();
2021-09-05 01:09:30 +02:00
const getPaylineData = function (payline: number[][]): string[] {
const data = [];
for (const point of payline) {
data.push(t[point[0]][point[1]]);
}
return data;
};
const countSequence = function (data: string[]): number {
let count = 1;
for (let i = 1; i < data.length; i++) {
if (data[i] !== data[i - 1]) break;
count++;
}
return count;
};
2021-10-01 07:00:50 +02:00
let gains = -investment;
2021-09-05 01:09:30 +02:00
for (const payline of payLines) {
const data = getPaylineData(payline);
const count = countSequence(data);
if (count < 3) continue;
const payout = getPayout(data[0], count - 3);
2021-10-01 07:00:50 +02:00
gains += investment * payout;
win(props.p, investment * payout);
}
2021-10-01 07:00:50 +02:00
setStatus(
<>
{gains > 0 ? "gained" : "lost"} <Money money={Math.abs(gains)} />
</>,
);
setCanPlay(true);
if (reachedLimit(props.p)) return;
2021-09-05 01:09:30 +02:00
}
2021-10-01 07:00:50 +02:00
function unlock(): void {
setLocks([-1, -1, -1, -1, -1]);
setCanPlay(false);
2021-09-05 01:09:30 +02:00
}
2021-10-01 07:00:50 +02:00
function updateInvestment(e: React.ChangeEvent<HTMLInputElement>): void {
2021-09-05 01:09:30 +02:00
let investment: number = parseInt(e.currentTarget.value);
if (isNaN(investment)) {
investment = minPlay;
}
2021-09-05 01:09:30 +02:00
if (investment > maxPlay) {
investment = maxPlay;
}
2021-09-05 01:09:30 +02:00
if (investment < minPlay) {
investment = minPlay;
}
2021-10-01 07:00:50 +02:00
setInvestment(investment);
2021-09-05 01:09:30 +02:00
}
2021-10-01 07:00:50 +02:00
const t = getTable();
// prettier-ignore
return (
2021-09-05 01:09:30 +02:00
<>
2021-10-01 07:00:50 +02:00
<Typography sx={{ lineHeight: "1em", whiteSpace: "pre" }}>++</Typography>
<Typography sx={{ lineHeight: "1em", whiteSpace: "pre" }}>| | {t[0][0]} | {t[0][1]} | {t[0][2]} | {t[0][3]} | {t[0][4]} | |</Typography>
<Typography sx={{ lineHeight: "1em", whiteSpace: "pre" }}>| | | | | | | |</Typography>
<Typography sx={{ lineHeight: "1em", whiteSpace: "pre" }}>| | {symbols[index[0]]} | {symbols[index[1]]} | {symbols[index[2]]} | {symbols[index[3]]} | {symbols[index[4]]} | |</Typography>
<Typography sx={{ lineHeight: "1em", whiteSpace: "pre" }}>| | | | | | | |</Typography>
<Typography sx={{ lineHeight: "1em", whiteSpace: "pre" }}>| | {symbols[(index[0]+1)%symbols.length]} | {symbols[(index[1]+1)%symbols.length]} | {symbols[(index[2]+1)%symbols.length]} | {symbols[(index[3]+1)%symbols.length]} | {symbols[(index[4]+1)%symbols.length]} | |</Typography>
<Typography sx={{ lineHeight: "1em", whiteSpace: "pre" }}>++</Typography>
<TextField
2021-09-05 01:09:30 +02:00
type="number"
2021-10-01 07:00:50 +02:00
onChange={updateInvestment}
2021-09-05 01:09:30 +02:00
placeholder={"Amount to play"}
2021-10-01 07:00:50 +02:00
disabled={!canPlay}
InputProps={{endAdornment:(<Button
onClick={trusted(play)}
disabled={!canPlay}
>Spin!</Button>)}}
2021-09-05 01:09:30 +02:00
/>
2021-10-01 07:00:50 +02:00
<Typography variant="h4">{status}</Typography>
<Typography>Pay lines</Typography>
2021-10-01 07:00:50 +02:00
<Typography sx={{ lineHeight: "1em", whiteSpace: "pre" }}>----- ····· ·····</Typography>
<Typography sx={{ lineHeight: "1em", whiteSpace: "pre" }}>····· ----- ·····</Typography>
<Typography sx={{ lineHeight: "1em", whiteSpace: "pre" }}>····· ····· -----</Typography>
<br />
2021-10-01 07:00:50 +02:00
<Typography sx={{ lineHeight: "1em", whiteSpace: "pre" }}>··^·· \···/ \···/</Typography>
<Typography sx={{ lineHeight: "1em", whiteSpace: "pre" }}>·/·\· ·\·/· ·---·</Typography>
<Typography sx={{ lineHeight: "1em", whiteSpace: "pre" }}>/···\ ··v·· ·····</Typography>
<br />
2021-10-01 07:00:50 +02:00
<Typography sx={{ lineHeight: "1em", whiteSpace: "pre" }}>····· ·---· ·····</Typography>
<Typography sx={{ lineHeight: "1em", whiteSpace: "pre" }}>·---· /···\ \···/</Typography>
<Typography sx={{ lineHeight: "1em", whiteSpace: "pre" }}>/···\ ····· ·---·</Typography>
</>
2021-09-05 01:09:30 +02:00
);
}
2021-09-05 01:09:30 +02:00
// https://felgo.com/doc/how-to-make-a-slot-game-tutorial/