generated from scaffold-eth/scaffold-eth-2
-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Pk dice updates (#36) * Added a welcome roll section to home screen, set theme to light mode only, removed ai background & responsive UI updates
- Loading branch information
Showing
14 changed files
with
270 additions
and
149 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,143 @@ | ||
import React, { useEffect, useState } from "react"; | ||
import Image from "next/image"; | ||
import { Address, Balance } from "../scaffold-eth"; | ||
import { generatePrivateKey, privateKeyToAccount } from "viem/accounts"; | ||
import { generateRandomHex } from "~~/utils/diceDemo/gameUtils"; | ||
|
||
const WelcomeRoll = () => { | ||
const [isRolling, setIsRolling] = useState(false); | ||
const [isUnitRolling, setIsUnitRolling] = useState<boolean[]>(Array(64).fill(false)); | ||
const [rolled, setRolled] = useState(false); | ||
const [rolledResult, setRolledResult] = useState<string[]>([]); | ||
const [rolls, setRolls] = useState<string[]>([]); | ||
const [spinning, setSpinning] = useState(false); | ||
const [isHacked, setIsHacked] = useState(false); | ||
const [rolledAddress, setRolledAddress] = useState(""); | ||
|
||
const privateKey = generatePrivateKey(); | ||
const pk = privateKey.substring(2); | ||
|
||
const rollTheDice = () => { | ||
setRolledAddress(""); | ||
setIsRolling(true); | ||
setIsUnitRolling(Array.from({ length: isUnitRolling.length }, () => true)); | ||
if (!rolled) { | ||
setRolled(true); | ||
} | ||
setSpinning(true); | ||
const rolls: string[] = []; | ||
for (let index = 0; index < 64; index++) { | ||
rolls.push(generateRandomHex()); | ||
} | ||
setRolls(rolls); | ||
let iterations = 0; | ||
setTimeout(() => { | ||
for (let i = 0; i < isUnitRolling.length; i++) { | ||
setTimeout(() => { | ||
setIsUnitRolling(prevState => { | ||
const newState = [...prevState]; | ||
newState[i] = false; | ||
return newState; | ||
}); | ||
iterations++; | ||
if (iterations === isUnitRolling.length) { | ||
setIsRolling(false); | ||
setTimeout(() => { | ||
setSpinning(false); | ||
setRolledResult(rolls); | ||
}, 300); | ||
} | ||
}, i * 100); | ||
} | ||
}, 400); | ||
}; | ||
|
||
const compareResult = (rolledResult: string[], pkChars: string) => { | ||
return rolledResult.every((value, index) => value.toLowerCase() === pkChars[index].toLowerCase()); | ||
}; | ||
|
||
useEffect(() => { | ||
let isHiiddenChars; | ||
|
||
if (rolled && rolledResult.length > 0 && privateKey) { | ||
isHiiddenChars = compareResult(rolledResult, pk); | ||
const rolledPk: `0x${string}` = `0x${rolledResult.join("")}`; | ||
const account = privateKeyToAccount(rolledPk as `0x${string}`); | ||
setRolledAddress(account.address); | ||
} | ||
|
||
if (isHiiddenChars) { | ||
setIsRolling(false); | ||
setSpinning(false); | ||
setIsHacked(true); | ||
} | ||
// eslint-disable-next-line react-hooks/exhaustive-deps | ||
}, [rolledResult]); | ||
|
||
return ( | ||
<div className="flex justify-center flex-col gap-2"> | ||
<button | ||
className="btn btn-sm btn-primary mx-auto " | ||
onClick={rollTheDice} | ||
disabled={isRolling || spinning || isHacked} | ||
> | ||
{spinning && <span className="loading loading-spinner"></span>} | ||
Roll | ||
</button> | ||
<div className="h-10"> | ||
{rolledAddress != "" && ( | ||
<div className="flex justify-center items-center gap-2 mt-2 "> | ||
<span> | ||
<Address address={rolledAddress}></Address> | ||
</span> | ||
<span> | ||
<Balance address={rolledAddress}></Balance> | ||
</span> | ||
</div> | ||
)} | ||
</div> | ||
<div className="grid grid-cols-8 w-full mt-2 mx-auto opacity-100"> | ||
{Array.from({ length: 64 }).map((_, index) => | ||
rolled ? ( | ||
isUnitRolling[index] ? ( | ||
<div | ||
key={index} | ||
className="w-[95%] h-[95%] overflow-hidden rounded-lg mx-auto flex justify-center items-center" | ||
> | ||
<div className="scale-125"> | ||
<Image className="scale-150" src="/rolls-gif/Spin.gif" alt="spinning" width={400} height={400} /> | ||
</div> | ||
</div> | ||
) : ( | ||
<div | ||
key={index} | ||
className="w-[95%] h-[95%] overflow-hidden rounded-lg mx-auto flex justify-center items-center" | ||
> | ||
<div className="scale-125"> | ||
<Image | ||
className="scale-150" | ||
src={`/rolls-jpg/${rolls[index]}.jpg`} | ||
alt="rolled" | ||
width={400} | ||
height={400} | ||
/> | ||
</div> | ||
</div> | ||
) | ||
) : ( | ||
<div | ||
key={index} | ||
className="w-[95%] h-[95%] overflow-hidden rounded-lg mx-auto flex justify-center items-center" | ||
> | ||
<div className="scale-125"> | ||
<Image className=" scale-150" src={`/rolls-jpg/0.jpg`} alt="zero roll" width={400} height={400} /> | ||
</div> | ||
</div> | ||
), | ||
)} | ||
</div> | ||
</div> | ||
); | ||
}; | ||
|
||
export default WelcomeRoll; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.