Skip to content

Commit

Permalink
Update LilyPad
Browse files Browse the repository at this point in the history
  • Loading branch information
Dougy Lee committed May 5, 2024
1 parent a52bf32 commit 735770b
Show file tree
Hide file tree
Showing 6 changed files with 293 additions and 21 deletions.
40 changes: 20 additions & 20 deletions client/src/Home.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,26 +25,26 @@ export default function Home() {
price: 100,
training: 0,
},
{
tokenId: 2,
name: "Machina",
imageUrl:
"https://ipfs.io/ipfs/QmVCYBq5Sasjp99MD6YMKp7xJwSxpxXjTCf85ekKaReFng",
brain: "https://www.google.com",
description: "An upgraded bot",
price: 100,
training: 500,
},
{
tokenId: 3,
name: "Beast",
imageUrl:
"https://ipfs.io/ipfs/QmSjfLd4h7PzJQ9RARUpr4z5918qZ4ziK9RFUnNGLefsR1",
brain: "https://www.google.com",
description: "A battle hardened bot",
price: 100,
training: 1000,
},
// {
// tokenId: 2,
// name: "Machina",
// imageUrl:
// "https://ipfs.io/ipfs/QmVCYBq5Sasjp99MD6YMKp7xJwSxpxXjTCf85ekKaReFng",
// brain: "https://www.google.com",
// description: "An upgraded bot",
// price: 100,
// training: 500,
// },
// {
// tokenId: 3,
// name: "Beast",
// imageUrl:
// "https://ipfs.io/ipfs/QmSjfLd4h7PzJQ9RARUpr4z5918qZ4ziK9RFUnNGLefsR1",
// brain: "https://www.google.com",
// description: "A battle hardened bot",
// price: 100,
// training: 1000,
// },
];

const [nftData, setNftData] = useState(defaultNftData);
Expand Down
144 changes: 144 additions & 0 deletions client/src/Home2.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,144 @@
import NftList from "./NftList";
import Scaffold from "./Scaffold";
import { HttpProvider, Web3 } from "web3";
import abi from "./abi/sarsa.json";
import { useState } from "react";

export default function Home2() {
const [connectedAccount, setConnectedAccount] = useState(null);

const provider = new HttpProvider(
"https://sepolia.infura.io/v3/d6d31fd77d604b18a171665fc35d45d6"
);
const web3 = new Web3(window.ethereum);

const contractAddress = "0x4B9E83Fa8f5C5C4402F49F2AbBBD61DDBA75c9b7";

const defaultNftData = [
{
tokenId: 1,
name: "Botson",
imageUrl:
"https://ipfs.io/ipfs/QmaG3Bqcy8mu1QwPmnp8gMEoN2N9fN99e2UGFKPgg7JLPv",
brain: "https://www.google.com",
description: "A bot that can do anything it sets it mind to",
price: 100,
training: 0,
},
{
tokenId: 2,
name: "Machina",
imageUrl:
"https://ipfs.io/ipfs/QmVCYBq5Sasjp99MD6YMKp7xJwSxpxXjTCf85ekKaReFng",
brain: "https://www.google.com",
description: "An upgraded bot",
price: 100,
training: 500,
},
{
tokenId: 3,
name: "Beast",
imageUrl:
"https://ipfs.io/ipfs/QmSjfLd4h7PzJQ9RARUpr4z5918qZ4ziK9RFUnNGLefsR1",
brain: "https://www.google.com",
description: "A battle hardened bot",
price: 100,
training: 1000,
},
];

const [nftData, setNftData] = useState(defaultNftData);
const miner = null;

const mintRobot = async () => {
console.log("Minting a new robot");

const contract = new web3.eth.Contract(abi, contractAddress);
contract.setProvider(provider);
const symbol = await contract.methods.symbol().call();
console.log(symbol);

const x = await contract.methods.x().call();
console.log(x);

await mintNFT(contract, "image-uri", "brain-uri");
};

async function mintNFT(contract, imageURI, brainURI) {
try {
const transaction = contract.methods.createBot(imageURI, brainURI);
const value = web3.utils.toWei("0.01", "ether");
const options = {
from: connectedAccount,
value: value,
};

const receipt = await transaction.send(options);
console.log("Transaction receipt:", receipt);
} catch (error) {
console.error("Error minting NFT:", error);
}
}

async function connectMetamask() {
//check metamask is installed
if (window.ethereum) {
// instantiate Web3 with the injected provider
const web3 = new Web3(window.ethereum);

//request user to connect accounts (Metamask will prompt)
await window.ethereum.request({ method: "eth_requestAccounts" });

//get the connected accounts
const accounts = await web3.eth.getAccounts();

//show the first connected account in the react page
setConnectedAccount(accounts[0]);
} else {
alert("Please download metamask");
}
}

function onTrain() {
setNftData([
{
tokenId: 1,
name: "Botson",
imageUrl:
"https://ipfs.io/ipfs/QmaG3Bqcy8mu1QwPmnp8gMEoN2N9fN99e2UGFKPgg7JLPv",
brain: "https://www.google.com",
description: "A bot that can do anything it sets it mind to",
price: 100,
training: 100,
},
]);
}

return (
<Scaffold>
{!connectedAccount && (
<button
type="button"
className="m-4 rounded-md bg-indigo-600 px-3.5 py-2.5 text-sm font-semibold text-white shadow-sm hover:bg-indigo-500 focus-visible:outline focus-visible:outline-2 focus-visible:outline-offset-2 focus-visible:outline-indigo-600"
onClick={() => connectMetamask()}
>
Connect to Metamask
</button>
)}

{connectedAccount && <h2>{connectedAccount}</h2>}
<button
type="button"
className="rounded-md bg-indigo-600 px-3.5 py-2.5 text-sm font-semibold text-white shadow-sm hover:bg-indigo-500 focus-visible:outline focus-visible:outline-2 focus-visible:outline-offset-2 focus-visible:outline-indigo-600"
onClick={mintRobot}
>
Mint a new Robot
</button>
<NftList
nftData={nftData}
onTrain={onTrain}
mineLink={nftData[0].training === 0 ? "/mine1" : "/mine2"}
/>
</Scaffold>
);
}
46 changes: 46 additions & 0 deletions client/src/NftList.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,43 @@ import {
WrenchScrewdriverIcon,
ArrowTrendingUpIcon,
} from "@heroicons/react/20/solid";
import React, { useState } from "react";

export default function NftList({ nftData, onTrain, mineLink }) {
const [showData, setShowData] = useState(false);

const Action = {
UP: 0,
DOWN: 1,
LEFT: 2,
RIGHT: 3,
};

function createQTable(rows, cols, numActions) {
// Initialize the Q-table as a 3D array
const qTable = new Array(rows)
.fill(null)
.map(() =>
new Array(cols)
.fill(null)
.map(() => new Array(numActions).fill(0))
);

// Set higher values for the DOWN action
for (let row = 0; row < rows; row++) {
for (let col = 0; col < cols; col++) {
qTable[row][col][Action.DOWN] = 1 + 0.1; // Higher values for DOWN
qTable[row][col][Action.UP] = 0.1;
qTable[row][col][Action.LEFT] = -1;
qTable[row][col][Action.RIGHT] = 0.42;
}
}

return qTable;
}

const data = JSON.stringify(createQTable(5, 5, 4));

return (
<ul
role="list"
Expand Down Expand Up @@ -72,6 +107,17 @@ export default function NftList({ nftData, onTrain, mineLink }) {
</div>
</li>
))}

{!showData && (
<button onClick={() => setShowData(!showData)}>
Show Data
</button>
)}
{showData && (
<li className="col-span-1 flex flex-col text-center rounded-lg bg-white shadow">
{data}
</li>
)}
</ul>
);
}
77 changes: 77 additions & 0 deletions client/src/SchoolList.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
import {
WrenchScrewdriverIcon,
ArrowTrendingUpIcon,
} from "@heroicons/react/20/solid";

export default function NftList({ nftData, onTrain, mineLink }) {
return (
<ul
role="list"
className="grid grid-cols-1 gap-6 sm:grid-cols-2 md:grid-cols-3 lg:grid-cols-4"
>
{nftData.map((nft) => (
<li
key={nft.tokenId}
className="col-span-1 flex flex-col divide-y divide-gray-200 rounded-lg bg-white text-center shadow"
>
<div className="flex flex-1 flex-col p-8">
<img
className="mx-auto h-32 w-32 flex-shrink-0 rounded-full"
src={nft.imageUrl}
alt=""
/>
<h3 className="mt-6 text-sm font-medium text-gray-900">
{nft.name}
</h3>
<dl className="mt-1 flex flex-grow flex-col justify-between">
<dt className="sr-only">Title</dt>
<dd className="text-sm text-gray-500">
{nft.description}
</dd>
<dt className="sr-only">Role</dt>
<dd className="mt-3">
<span className="inline-flex items-center rounded-full bg-green-50 px-2 py-1 text-xs font-medium text-green-700 ring-1 ring-inset ring-green-600/20">
{"Training Cycles: " + nft.training}
</span>
</dd>
</dl>
</div>
<div>
<div className="-mt-px flex divide-x divide-gray-200">
<div className="flex w-0 flex-1">
<a
href={mineLink}
className="relative -mr-px inline-flex w-0 flex-1 items-center justify-center gap-x-3 rounded-bl-lg border border-transparent py-4 text-sm font-semibold text-gray-900"
onClick={(e) => {
console.log(
"Mine Clicked",
nft.tokenId
);
}}
>
<WrenchScrewdriverIcon
className="h-5 w-5 text-gray-400"
aria-hidden="true"
/>
Mine
</a>
</div>
<div className="-ml-px flex w-0 flex-1">
<a
className="relative inline-flex w-0 flex-1 items-center justify-center gap-x-3 rounded-br-lg border border-transparent py-4 text-sm font-semibold text-gray-900"
onClick={onTrain}
>
<ArrowTrendingUpIcon
className="h-5 w-5 text-gray-400"
aria-hidden="true"
/>
Train
</a>
</div>
</div>
</div>
</li>
))}
</ul>
);
}
5 changes: 5 additions & 0 deletions client/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import "./index.css";
import Scaffold from "./Scaffold";
import Mine from "./Mine";
import Home from "./Home";
import Home2 from "./Home2";
import RunGameScreen1 from "./RunGameScreen1";
import RunGameScreen2 from "./RunGameScreen2";

Expand All @@ -13,6 +14,10 @@ const router = createBrowserRouter([
path: "/",
element: <Home />,
},
{
path: "/2",
element: <Home2 />,
},
{
path: "mine1",
element: (
Expand Down
2 changes: 1 addition & 1 deletion lilypad_module.json.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
"Entrypoint": [
"/bin/sh", "-c", "python lilypad_sarsa.py"
],
"Image": "zeath/sarsa:latest"
"Image": "zeath/sarsa:latest@sha256:9e3996b171ab8080425d40bf5c82f3c102c84d56c2fa9f355754f16d513f67f4"
},
"Engine": "Docker",
"Network": {
Expand Down

0 comments on commit 735770b

Please sign in to comment.