diff --git a/packages/client/src/pages/home/index.tsx b/packages/client/src/pages/home/index.tsx
index 5d6b577d..1a91140b 100644
--- a/packages/client/src/pages/home/index.tsx
+++ b/packages/client/src/pages/home/index.tsx
@@ -15,14 +15,20 @@ import { useMUD } from '@/mud/MUDContext';
import { useEntityQuery } from "@latticexyz/react";
import { Has, getComponentValue } from '@latticexyz/recs';
import { ethers } from 'ethers';
+import { min } from 'rxjs';
-const abi = [{"inputs":[{"internalType":"uint256","name":"_waitBlockCount","type":"uint256"},{"internalType":"string","name":"_symbol","type":"string"},{"internalType":"string","name":"_name","type":"string"},{"internalType":"string","name":"_notRevealedInfo","type":"string"},{"internalType":"string","name":"_revealedDesc","type":"string"}],"stateMutability":"nonpayable","type":"constructor"},{"inputs":[{"internalType":"address","name":"sender","type":"address"},{"internalType":"uint256","name":"tokenId","type":"uint256"},{"internalType":"address","name":"owner","type":"address"}],"name":"ERC721IncorrectOwner","type":"error"},{"inputs":[{"internalType":"address","name":"operator","type":"address"},{"internalType":"uint256","name":"tokenId","type":"uint256"}],"name":"ERC721InsufficientApproval","type":"error"},{"inputs":[{"internalType":"address","name":"approver","type":"address"}],"name":"ERC721InvalidApprover","type":"error"},{"inputs":[{"internalType":"address","name":"operator","type":"address"}],"name":"ERC721InvalidOperator","type":"error"},{"inputs":[{"internalType":"address","name":"owner","type":"address"}],"name":"ERC721InvalidOwner","type":"error"},{"inputs":[{"internalType":"address","name":"receiver","type":"address"}],"name":"ERC721InvalidReceiver","type":"error"},{"inputs":[{"internalType":"address","name":"sender","type":"address"}],"name":"ERC721InvalidSender","type":"error"},{"inputs":[{"internalType":"uint256","name":"tokenId","type":"uint256"}],"name":"ERC721NonexistentToken","type":"error"},{"anonymous":false,"inputs":[{"indexed":true,"internalType":"address","name":"owner","type":"address"},{"indexed":true,"internalType":"address","name":"approved","type":"address"},{"indexed":true,"internalType":"uint256","name":"tokenId","type":"uint256"}],"name":"Approval","type":"event"},{"anonymous":false,"inputs":[{"indexed":true,"internalType":"address","name":"owner","type":"address"},{"indexed":true,"internalType":"address","name":"operator","type":"address"},{"indexed":false,"internalType":"bool","name":"approved","type":"bool"}],"name":"ApprovalForAll","type":"event"},{"anonymous":false,"inputs":[{"indexed":false,"internalType":"uint256","name":"randomId","type":"uint256"},{"indexed":false,"internalType":"address","name":"author","type":"address"}],"name":"NewRandom","type":"event"},{"anonymous":false,"inputs":[{"indexed":true,"internalType":"address","name":"from","type":"address"},{"indexed":true,"internalType":"address","name":"to","type":"address"},{"indexed":true,"internalType":"uint256","name":"tokenId","type":"uint256"}],"name":"Transfer","type":"event"},{"inputs":[{"internalType":"address","name":"to","type":"address"},{"internalType":"uint256","name":"tokenId","type":"uint256"}],"name":"approve","outputs":[],"stateMutability":"nonpayable","type":"function"},{"inputs":[{"internalType":"address","name":"owner","type":"address"}],"name":"balanceOf","outputs":[{"internalType":"uint256","name":"","type":"uint256"}],"stateMutability":"view","type":"function"},{"inputs":[{"internalType":"uint256","name":"tokenId","type":"uint256"}],"name":"getApproved","outputs":[{"internalType":"address","name":"","type":"address"}],"stateMutability":"view","type":"function"},{"inputs":[{"internalType":"uint256","name":"_tokenId","type":"uint256"}],"name":"getStructInfo","outputs":[{"internalType":"uint256","name":"","type":"uint256"},{"internalType":"uint256","name":"","type":"uint256"},{"internalType":"uint256","name":"","type":"uint256"},{"internalType":"uint256","name":"","type":"uint256"},{"internalType":"uint256","name":"","type":"uint256"},{"internalType":"uint256","name":"","type":"uint256"}],"stateMutability":"view","type":"function"},{"inputs":[],"name":"getUserTokenIdList","outputs":[{"internalType":"uint256[]","name":"","type":"uint256[]"}],"stateMutability":"view","type":"function"},{"inputs":[{"internalType":"address","name":"owner","type":"address"},{"internalType":"address","name":"operator","type":"address"}],"name":"isApprovedForAll","outputs":[{"internalType":"bool","name":"","type":"bool"}],"stateMutability":"view","type":"function"},{"inputs":[],"name":"mint","outputs":[],"stateMutability":"nonpayable","type":"function"},{"inputs":[],"name":"name","outputs":[{"internalType":"string","name":"","type":"string"}],"stateMutability":"view","type":"function"},{"inputs":[],"name":"owner","outputs":[{"internalType":"address","name":"","type":"address"}],"stateMutability":"view","type":"function"},{"inputs":[{"internalType":"uint256","name":"tokenId","type":"uint256"}],"name":"ownerOf","outputs":[{"internalType":"address","name":"","type":"address"}],"stateMutability":"view","type":"function"},{"inputs":[],"name":"randomId","outputs":[{"internalType":"uint256","name":"","type":"uint256"}],"stateMutability":"view","type":"function"},{"inputs":[{"internalType":"uint256","name":"","type":"uint256"}],"name":"randomList","outputs":[{"internalType":"uint256","name":"blockNumber","type":"uint256"},{"internalType":"address","name":"author","type":"address"}],"stateMutability":"view","type":"function"},{"inputs":[{"internalType":"uint256","name":"_tokenId","type":"uint256"}],"name":"revealNFT","outputs":[],"stateMutability":"nonpayable","type":"function"},{"inputs":[{"internalType":"address","name":"from","type":"address"},{"internalType":"address","name":"to","type":"address"},{"internalType":"uint256","name":"tokenId","type":"uint256"}],"name":"safeTransferFrom","outputs":[],"stateMutability":"nonpayable","type":"function"},{"inputs":[{"internalType":"address","name":"from","type":"address"},{"internalType":"address","name":"to","type":"address"},{"internalType":"uint256","name":"tokenId","type":"uint256"},{"internalType":"bytes","name":"data","type":"bytes"}],"name":"safeTransferFrom","outputs":[],"stateMutability":"nonpayable","type":"function"},{"inputs":[{"internalType":"address","name":"operator","type":"address"},{"internalType":"bool","name":"approved","type":"bool"}],"name":"setApprovalForAll","outputs":[],"stateMutability":"nonpayable","type":"function"},{"inputs":[{"internalType":"bytes4","name":"interfaceId","type":"bytes4"}],"name":"supportsInterface","outputs":[{"internalType":"bool","name":"","type":"bool"}],"stateMutability":"view","type":"function"},{"inputs":[],"name":"symbol","outputs":[{"internalType":"string","name":"","type":"string"}],"stateMutability":"view","type":"function"},{"inputs":[],"name":"tokenId","outputs":[{"internalType":"uint256","name":"","type":"uint256"}],"stateMutability":"view","type":"function"},{"inputs":[{"internalType":"uint256","name":"_tokenId","type":"uint256"}],"name":"tokenURI","outputs":[{"internalType":"string","name":"","type":"string"}],"stateMutability":"view","type":"function"},{"inputs":[{"internalType":"address","name":"from","type":"address"},{"internalType":"address","name":"to","type":"address"},{"internalType":"uint256","name":"tokenId","type":"uint256"}],"name":"transferFrom","outputs":[],"stateMutability":"nonpayable","type":"function"},{"inputs":[{"internalType":"uint256","name":"","type":"uint256"}],"name":"userList","outputs":[{"internalType":"uint256","name":"randomId","type":"uint256"},{"internalType":"address","name":"owner","type":"address"},{"internalType":"uint256","name":"HP","type":"uint256"},{"internalType":"uint256","name":"Attack","type":"uint256"},{"internalType":"uint256","name":"AttackRange","type":"uint256"},{"internalType":"uint256","name":"Speed","type":"uint256"},{"internalType":"uint256","name":"Strength","type":"uint256"},{"internalType":"uint256","name":"Space","type":"uint256"},{"internalType":"enum MRandom.RandomState","name":"state","type":"uint8"}],"stateMutability":"view","type":"function"},{"inputs":[],"name":"waitBlockCount","outputs":[{"internalType":"uint256","name":"","type":"uint256"}],"stateMutability":"view","type":"function"}]
-let userContract
+import lootAbi from '../../../../contracts/out/Loot.sol/MLoot.abi.json'
+import userAbi from '../../../../contracts/out/User.sol/MUser.abi.json'
+
+console.log(lootAbi, 'lootAbi')
+
+let userContract: any
+let lootContract: any
const Home = () => {
const {
components: { GlobalConfig },
- systemCalls,
+ systemCalls: { selectUserNft, joinBattlefield },
network
} = useMUD();
@@ -50,19 +56,30 @@ const Home = () => {
const [handheld, setHandheld] = useState
();
const [head, setHead] = useState();
const [username, setUsername] = useState();
+ const [userUrl, setUserUrl] = useState();
+ const [lootUrl, setLootUrl] = useState();
const GlobalConfigData = useEntityQuery([Has(GlobalConfig)]).map((entity) => getComponentValue(GlobalConfig, entity));
console.log(GlobalConfigData, 'GlobalConfigData')
- // if (GlobalConfigData.length && GlobalConfigData[0].userContract) {
+ if (GlobalConfigData.length && GlobalConfigData[0].userContract) {
let privateKey = network.privateKey
let rpc = network.walletClient?.chain?.rpcUrls?.default?.http[0] || 'http://127.0.0.1:8545'
let provider = new ethers.providers.JsonRpcProvider(rpc)
let wallet = new ethers.Wallet(privateKey, provider)
console.log(wallet)
- let userContractAddress = '0x5FbDB2315678afecb367f032d93F642f64180aa3' // GlobalConfigData[0].userContract
- userContract = new ethers.Contract(userContractAddress, abi, wallet)
- // }
+ let userContractAddress = GlobalConfigData[0].userContract
+ userContract = new ethers.Contract(userContractAddress, userAbi, wallet)
+ }
+
+ if (GlobalConfigData.length && GlobalConfigData[0].lootContract && !lootContract) {
+ let privateKey = network.privateKey
+ let rpc = network.walletClient?.chain?.rpcUrls?.default?.http[0] || 'http://127.0.0.1:8545'
+ let provider = new ethers.providers.JsonRpcProvider(rpc)
+ let wallet = new ethers.Wallet(privateKey, provider)
+ let lootContractAddress = GlobalConfigData[0].lootContract
+ lootContract = new ethers.Contract(lootContractAddress, lootAbi, wallet)
+ }
const createWallet = () => {
setContent(
@@ -102,44 +119,111 @@ const Home = () => {
setStep('mint');
}
- const mintAndGo = async () => {
- const clothes = Duck.Clothes[~~(Math.random() * Duck.Clothes.length)];
- const handheld = Duck.HandHeld[~~(Math.random() * Duck.HandHeld.length)];
- const head = Duck.Head[~~(Math.random() * Duck.Head.length)];
-
- setMinting(true);
+ const mintLoot = async () => {
+ return new Promise(async (resolve, reject) => {
+ try {
+ let tx = await lootContract.mint()
+ await tx.wait()
+ console.log(tx, 'tx')
+ message.success('Mint Loot Success');
+ let tokenIds = await lootContract.getUserTokenIdList()
+ let tokenId = tokenIds[tokenIds.length - 1].toString()
+ // 获取当前getBlockNumber
+ let blockNumber = await network.publicClient.getBlockNumber()
+ // 每隔1s获取一次getBlockNumber
+ let interval = setInterval(async () => {
+ let currentBlockNumber = await network.publicClient.getBlockNumber()
+ if (currentBlockNumber - blockNumber >= 2) {
+ clearInterval(interval)
+ let t = await lootContract.revealNFT(tokenId)
+ await t.wait()
+ message.success('reveal Loot Success');
+ resolve('success')
+ }
+ }, 1000)
+ } catch (error) {
+ console.log(error)
+ reject(error)
+ }
+ })
+ }
- if (userContract) {
- console.log(userContract, 'userContract')
+ const mintUser = async () => {
+ return new Promise(async (resolve, reject) => {
try {
let tx = await userContract.mint()
await tx.wait()
console.log(tx, 'tx')
- // let tokenIds = await userContract.getUserTokenIdList()
- // console.log(tokenIds, 'tokenIds')
- let tokenId = '0'
- let t = await userContract.revealNFT(tokenId)
- await t.wait()
- console.log(t, 't')
- delay(100).then(() => {
- setClothes(clothes);
- setHandheld(handheld);
- setHead(head);
- }).delay(3000).then(() => {
- navigate('/game', {
- state: {
- username,
- clothes,
- handheld,
- head,
- }
- });
- })
+ message.success('Mint User Success');
+ let tokenIds = await userContract.getUserTokenIdList()
+ let tokenId = tokenIds[tokenIds.length - 1].toString()
+ // 获取当前getBlockNumber
+ let blockNumber = await network.publicClient.getBlockNumber()
+ // 每隔1s获取一次getBlockNumber
+ let interval = setInterval(async () => {
+ let currentBlockNumber = await network.publicClient.getBlockNumber()
+ if (currentBlockNumber - blockNumber >= 2) {
+ clearInterval(interval)
+ let t = await userContract.revealNFT(tokenId)
+ await t.wait()
+ console.log(t, 't')
+ message.success('reveal User Success');
+ resolve('success')
+ }
+ }, 1000)
} catch (error) {
console.log(error)
+ reject(error)
}
- }
+ })
+ }
+
+ const atobUrl = (url) => {
+ url = url.replace('data:application/json;base64,', '')
+ url = atob(url)
+ url = JSON.parse(url)
+ return url
+ }
+ const mintAndGo = async () => {
+ const clothes = Duck.Clothes[~~(Math.random() * Duck.Clothes.length)];
+ const handheld = Duck.HandHeld[~~(Math.random() * Duck.HandHeld.length)];
+ const head = Duck.Head[~~(Math.random() * Duck.Head.length)];
+
+ setMinting(true);
+ await mintUser();
+ await mintLoot()
+
+ let tokenIds = await userContract.getUserTokenIdList()
+ let tokenId = tokenIds[0].toString()
+ let lootTokenIds = await lootContract.getUserTokenIdList()
+ let lootTokenId = lootTokenIds[0].toString()
+ let url = await userContract.tokenURI(tokenId)
+ let lootUrl = await lootContract.tokenURI(lootTokenId)
+
+ url = atobUrl(url)
+ lootUrl = atobUrl(lootUrl)
+ setUserUrl(url.image)
+ setLootUrl(lootUrl.image)
+
+ await selectUserNft(tokenId)
+ await joinBattlefield()
+
+ delay(100).then(() => {
+ setClothes(clothes);
+ setHandheld(handheld);
+ setHead(head);
+ }).delay(3000).then(() => {
+ setMinting(false);
+ navigate('/game', {
+ state: {
+ username,
+ clothes,
+ handheld,
+ head,
+ }
+ });
+ })
}
const play = () => {
@@ -177,7 +261,7 @@ const Home = () => {
HOME
-
+
diff --git a/packages/client/src/pages/test/index.jsx b/packages/client/src/pages/test/index.jsx
index a8c3a327..d0f0c34f 100644
--- a/packages/client/src/pages/test/index.jsx
+++ b/packages/client/src/pages/test/index.jsx
@@ -1,4 +1,4 @@
-import React, { useEffect, useRef, useState } from 'react';
+import React, { useEffect, useMemo, useState } from 'react';
import { useComponentValue, useEntityQuery } from "@latticexyz/react";
import { decodeEntity } from "@latticexyz/store-sync/recs";
import { Has, getComponentValue } from '@latticexyz/recs';
@@ -9,9 +9,16 @@ import { ethers } from 'ethers';
import { solidityKeccak256 } from 'ethers/lib/utils';
import { getRandomStr } from '../../utils/utils';
import './index.scss';
+import { bfs, simplifyMapData } from '@/utils/map';
+import useMerkel from '@/hooks/useMerkel';
+import { loadMapData } from "@/utils";
+
+import lootAbi from '../../../../contracts/out/Loot.sol/MLoot.abi.json'
+import userAbi from '../../../../contracts/out/User.sol/MUser.abi.json'
-const abi = [{"inputs":[{"internalType":"uint256","name":"_waitBlockCount","type":"uint256"},{"internalType":"string","name":"_symbol","type":"string"},{"internalType":"string","name":"_name","type":"string"},{"internalType":"string","name":"_notRevealedInfo","type":"string"},{"internalType":"string","name":"_revealedDesc","type":"string"}],"stateMutability":"nonpayable","type":"constructor"},{"inputs":[{"internalType":"address","name":"sender","type":"address"},{"internalType":"uint256","name":"tokenId","type":"uint256"},{"internalType":"address","name":"owner","type":"address"}],"name":"ERC721IncorrectOwner","type":"error"},{"inputs":[{"internalType":"address","name":"operator","type":"address"},{"internalType":"uint256","name":"tokenId","type":"uint256"}],"name":"ERC721InsufficientApproval","type":"error"},{"inputs":[{"internalType":"address","name":"approver","type":"address"}],"name":"ERC721InvalidApprover","type":"error"},{"inputs":[{"internalType":"address","name":"operator","type":"address"}],"name":"ERC721InvalidOperator","type":"error"},{"inputs":[{"internalType":"address","name":"owner","type":"address"}],"name":"ERC721InvalidOwner","type":"error"},{"inputs":[{"internalType":"address","name":"receiver","type":"address"}],"name":"ERC721InvalidReceiver","type":"error"},{"inputs":[{"internalType":"address","name":"sender","type":"address"}],"name":"ERC721InvalidSender","type":"error"},{"inputs":[{"internalType":"uint256","name":"tokenId","type":"uint256"}],"name":"ERC721NonexistentToken","type":"error"},{"anonymous":false,"inputs":[{"indexed":true,"internalType":"address","name":"owner","type":"address"},{"indexed":true,"internalType":"address","name":"approved","type":"address"},{"indexed":true,"internalType":"uint256","name":"tokenId","type":"uint256"}],"name":"Approval","type":"event"},{"anonymous":false,"inputs":[{"indexed":true,"internalType":"address","name":"owner","type":"address"},{"indexed":true,"internalType":"address","name":"operator","type":"address"},{"indexed":false,"internalType":"bool","name":"approved","type":"bool"}],"name":"ApprovalForAll","type":"event"},{"anonymous":false,"inputs":[{"indexed":false,"internalType":"uint256","name":"randomId","type":"uint256"},{"indexed":false,"internalType":"address","name":"author","type":"address"}],"name":"NewRandom","type":"event"},{"anonymous":false,"inputs":[{"indexed":true,"internalType":"address","name":"from","type":"address"},{"indexed":true,"internalType":"address","name":"to","type":"address"},{"indexed":true,"internalType":"uint256","name":"tokenId","type":"uint256"}],"name":"Transfer","type":"event"},{"inputs":[{"internalType":"address","name":"to","type":"address"},{"internalType":"uint256","name":"tokenId","type":"uint256"}],"name":"approve","outputs":[],"stateMutability":"nonpayable","type":"function"},{"inputs":[{"internalType":"address","name":"owner","type":"address"}],"name":"balanceOf","outputs":[{"internalType":"uint256","name":"","type":"uint256"}],"stateMutability":"view","type":"function"},{"inputs":[{"internalType":"uint256","name":"tokenId","type":"uint256"}],"name":"getApproved","outputs":[{"internalType":"address","name":"","type":"address"}],"stateMutability":"view","type":"function"},{"inputs":[{"internalType":"uint256","name":"_tokenId","type":"uint256"}],"name":"getStructInfo","outputs":[{"internalType":"uint256","name":"","type":"uint256"},{"internalType":"uint256","name":"","type":"uint256"},{"internalType":"uint256","name":"","type":"uint256"},{"internalType":"uint256","name":"","type":"uint256"},{"internalType":"uint256","name":"","type":"uint256"},{"internalType":"uint256","name":"","type":"uint256"}],"stateMutability":"view","type":"function"},{"inputs":[],"name":"getUserTokenIdList","outputs":[{"internalType":"uint256[]","name":"","type":"uint256[]"}],"stateMutability":"view","type":"function"},{"inputs":[{"internalType":"address","name":"owner","type":"address"},{"internalType":"address","name":"operator","type":"address"}],"name":"isApprovedForAll","outputs":[{"internalType":"bool","name":"","type":"bool"}],"stateMutability":"view","type":"function"},{"inputs":[],"name":"mint","outputs":[],"stateMutability":"nonpayable","type":"function"},{"inputs":[],"name":"name","outputs":[{"internalType":"string","name":"","type":"string"}],"stateMutability":"view","type":"function"},{"inputs":[],"name":"owner","outputs":[{"internalType":"address","name":"","type":"address"}],"stateMutability":"view","type":"function"},{"inputs":[{"internalType":"uint256","name":"tokenId","type":"uint256"}],"name":"ownerOf","outputs":[{"internalType":"address","name":"","type":"address"}],"stateMutability":"view","type":"function"},{"inputs":[],"name":"randomId","outputs":[{"internalType":"uint256","name":"","type":"uint256"}],"stateMutability":"view","type":"function"},{"inputs":[{"internalType":"uint256","name":"","type":"uint256"}],"name":"randomList","outputs":[{"internalType":"uint256","name":"blockNumber","type":"uint256"},{"internalType":"address","name":"author","type":"address"}],"stateMutability":"view","type":"function"},{"inputs":[{"internalType":"uint256","name":"_tokenId","type":"uint256"}],"name":"revealNFT","outputs":[],"stateMutability":"nonpayable","type":"function"},{"inputs":[{"internalType":"address","name":"from","type":"address"},{"internalType":"address","name":"to","type":"address"},{"internalType":"uint256","name":"tokenId","type":"uint256"}],"name":"safeTransferFrom","outputs":[],"stateMutability":"nonpayable","type":"function"},{"inputs":[{"internalType":"address","name":"from","type":"address"},{"internalType":"address","name":"to","type":"address"},{"internalType":"uint256","name":"tokenId","type":"uint256"},{"internalType":"bytes","name":"data","type":"bytes"}],"name":"safeTransferFrom","outputs":[],"stateMutability":"nonpayable","type":"function"},{"inputs":[{"internalType":"address","name":"operator","type":"address"},{"internalType":"bool","name":"approved","type":"bool"}],"name":"setApprovalForAll","outputs":[],"stateMutability":"nonpayable","type":"function"},{"inputs":[{"internalType":"bytes4","name":"interfaceId","type":"bytes4"}],"name":"supportsInterface","outputs":[{"internalType":"bool","name":"","type":"bool"}],"stateMutability":"view","type":"function"},{"inputs":[],"name":"symbol","outputs":[{"internalType":"string","name":"","type":"string"}],"stateMutability":"view","type":"function"},{"inputs":[],"name":"tokenId","outputs":[{"internalType":"uint256","name":"","type":"uint256"}],"stateMutability":"view","type":"function"},{"inputs":[{"internalType":"uint256","name":"_tokenId","type":"uint256"}],"name":"tokenURI","outputs":[{"internalType":"string","name":"","type":"string"}],"stateMutability":"view","type":"function"},{"inputs":[{"internalType":"address","name":"from","type":"address"},{"internalType":"address","name":"to","type":"address"},{"internalType":"uint256","name":"tokenId","type":"uint256"}],"name":"transferFrom","outputs":[],"stateMutability":"nonpayable","type":"function"},{"inputs":[{"internalType":"uint256","name":"","type":"uint256"}],"name":"userList","outputs":[{"internalType":"uint256","name":"randomId","type":"uint256"},{"internalType":"address","name":"owner","type":"address"},{"internalType":"uint256","name":"HP","type":"uint256"},{"internalType":"uint256","name":"Attack","type":"uint256"},{"internalType":"uint256","name":"AttackRange","type":"uint256"},{"internalType":"uint256","name":"Speed","type":"uint256"},{"internalType":"uint256","name":"Strength","type":"uint256"},{"internalType":"uint256","name":"Space","type":"uint256"},{"internalType":"enum MRandom.RandomState","name":"state","type":"uint8"}],"stateMutability":"view","type":"function"},{"inputs":[],"name":"waitBlockCount","outputs":[{"internalType":"uint256","name":"","type":"uint256"}],"stateMutability":"view","type":"function"}]
let userContract
+let lootContract
+let transfering = false
const Test = () => {
const [stepData, setStepData] = useState([]);
@@ -21,8 +28,10 @@ const Test = () => {
const [battlesData, setBattlesData] = useState([]);
const [boxData, setBoxData] = useState([]);
const [boxId, setBoxId] = useState([]);
- const [revealNFTData, setRevealNFTData] = useState([]);
+ const [revealNFTData, setRevealNFTData] = useState('');
const [nftListData, setNftListData] = useState([]);
+ const [walletBalance, setWalletBalance] = useState('');
+ const [renderMapData, setRenderMapData] = useState([]);
const {
components: { Player, GameConfig, BattleList, BoxList, GlobalConfig },
@@ -31,20 +40,57 @@ const Test = () => {
} = useMUD();
const { account } = network;
- console.log(network, 'account')
-
- // PRIVATE_KEY=0xac0974bec39a17e36ba4a6b4d238ff944bacb478cbed5efcae784d7bf4f2ff80 转账
- // let PRIVATE_KEY = '0xac0974bec39a17e36ba4a6b4d238ff944bacb478cbed5efcae784d7bf4f2ff80'
- // let provider = new ethers.providers.JsonRpcProvider('http://127.0.0.1:8545')
- // let wallet = new ethers.Wallet(PRIVATE_KEY, provider)
- // console.log(wallet, 'wallet')
- // // 转账到0x6B5A3EF0cEdDE6f8266eCcb7971a6dbdE9D93D44
- // wallet.sendTransaction({
- // to: '0x74F750d72009B2a70aAe2F934B0F0C1F4015A037',
- // value: ethers.utils.parseEther('1')
- // }).then(res => {
- // console.log(res, 'res')
- // })
+
+ const simpleMapData = useMemo(() => {
+ return simplifyMapData(renderMapData);
+ }, [renderMapData]);
+
+ const formatMovePath = useMerkel(simpleMapData);
+
+ useEffect(() => {
+ loadMapData().then((csv) => {
+ setRenderMapData(csv);
+ mapDataRef.current = csv;
+ });
+ }, []);
+
+
+ // 转账函数
+ const transferFun = async (to) => {
+ if (transfering) return
+ transfering = true
+ let PRIVATE_KEY = '0xac0974bec39a17e36ba4a6b4d238ff944bacb478cbed5efcae784d7bf4f2ff80'
+ let rpc = network.walletClient?.chain?.rpcUrls?.default?.http[0] || 'http://127.0.0.1:8545'
+ let provider = new ethers.providers.JsonRpcProvider(rpc)
+ let wallet = new ethers.Wallet(PRIVATE_KEY, provider)
+ console.log(wallet, 'wallet')
+ wallet.sendTransaction({
+ to,
+ value: ethers.utils.parseEther('1')
+ }).then(res => {
+ console.log(res, 'res')
+ transfering = false
+ getBalance()
+ }).catch(err => {
+ console.log(err)
+ })
+ }
+
+ const getBalance = () => {
+ network.publicClient.getBalance({
+ address: network.walletClient.account.address
+ }).then(balance => {
+ if (balance.toString() == 0) {
+ transferFun(network.walletClient.account.address)
+ } else {
+ let walletBalance = (+ethers.utils.formatEther(balance.toString())).toFixed(2)
+ console.log(walletBalance)
+ setWalletBalance(walletBalance);
+ }
+ })
+ }
+
+ getBalance()
const GameData = useEntityQuery([Has(GameConfig)]).map((entity) => getComponentValue(GameConfig, entity));
console.log(GameData, 'GameData')
@@ -52,13 +98,24 @@ const Test = () => {
const GlobalConfigData = useEntityQuery([Has(GlobalConfig)]).map((entity) => getComponentValue(GlobalConfig, entity));
console.log(GlobalConfigData, 'GlobalConfigData')
- if (GlobalConfigData.length && GlobalConfigData[0].userContract) {
+ if (GlobalConfigData.length && GlobalConfigData[0].userContract && !userContract) {
+ console.log(userAbi, 'userAbi')
let privateKey = network.privateKey
let rpc = network.walletClient?.chain?.rpcUrls?.default?.http[0] || 'http://127.0.0.1:8545'
let provider = new ethers.providers.JsonRpcProvider(rpc)
let wallet = new ethers.Wallet(privateKey, provider)
let userContractAddress = GlobalConfigData[0].userContract
- userContract = new ethers.Contract(userContractAddress, abi, wallet)
+ userContract = new ethers.Contract(userContractAddress, userAbi, wallet)
+ }
+
+ if (GlobalConfigData.length && GlobalConfigData[0].lootContract && !lootContract) {
+ console.log(lootAbi, 'lootAbi')
+ let privateKey = network.privateKey
+ let rpc = network.walletClient?.chain?.rpcUrls?.default?.http[0] || 'http://127.0.0.1:8545'
+ let provider = new ethers.providers.JsonRpcProvider(rpc)
+ let wallet = new ethers.Wallet(privateKey, provider)
+ let lootContractAddress = GlobalConfigData[0].lootContract
+ lootContract = new ethers.Contract(lootContractAddress, lootAbi, wallet)
}
const battles = useEntityQuery([Has(BattleList)]).map((entity) => {
@@ -67,14 +124,8 @@ const Test = () => {
console.log(battle, 'battle', id, entity)
battle.id = id.battleId.toString()
return battle;
- });
+ }).filter(e => !e.isEnd)
console.log(battles, 'battles')
- battles.forEach(async item => {
- if (item.attackerState == 2 && item.defenderState == 2) {
- let hp = await getBattlePlayerHp(item.id, account)
- console.log(hp, 'hp')
- }
- })
const boxs = useEntityQuery([Has(BoxList)]).map((entity) => {
let id = decodeEntity({ boxId: "uint256" }, entity);
@@ -112,7 +163,6 @@ const Test = () => {
}
const mintFun = () => {
- console.log(1, userContract)
userContract.mint().then(async res => {
await res.wait()
console.log(res)
@@ -191,8 +241,8 @@ const Test = () => {
let player = players.find(item => item.isMe);
let from = {x: player.x, y: player.y}
let to = {x: stepData[0], y: stepData[1]}
- let merkelData = main(from, to);
- move(merkelData);
+ const paths = bfs(simpleMapData, from, to).slice(1);
+ move(formatMovePath(paths));
}
const transferPlayer = () => {
@@ -208,7 +258,8 @@ const Test = () => {
let player = players.find(item => item.isMe);
let from = {x: player.x, y: player.y}
let to = {x: battleData[0], y: battleData[1]}
- let merkelData = main(from, to);
+ const paths = bfs(simpleMapData, from, to).slice(1);
+ let merkelData = formatMovePath(paths)
battleInvitation(tragePlayer.addr, merkelData);
}
}
@@ -280,11 +331,15 @@ const Test = () => {
)
}
+ // useEffect(() => {
+ // getBalance()
+ // });
+
return (
测试面板
-
当前用户地址:{account}
+
当前用户地址:{account} | {walletBalance} ETH
{
@@ -302,7 +357,7 @@ const Test = () => {
{
- boxs.map((item, index) => (
+ boxs.map((item, index) => (
宝箱信息
id: {item.id}
opened: {item.opened.toString()}
diff --git a/packages/client/src/pages/test/index.scss b/packages/client/src/pages/test/index.scss
index 1179d66a..c5326003 100644
--- a/packages/client/src/pages/test/index.scss
+++ b/packages/client/src/pages/test/index.scss
@@ -22,6 +22,7 @@
align-items: flex-start;
justify-content: space-between;
min-height: 67px;
+ flex-wrap: wrap;
}
.main {
diff --git a/packages/client/src/utils/createMerkelTree.js b/packages/client/src/utils/createMerkelTree.js
index ab060f60..55cadc31 100644
--- a/packages/client/src/utils/createMerkelTree.js
+++ b/packages/client/src/utils/createMerkelTree.js
@@ -70,7 +70,7 @@ export const main = (from, to) => {
console.log("总移动步数", steps_arr.length);
let steps_list = move(steps_arr);
console.log("生成的传入文件", steps_list);
- return steps_list
+ return {merkelData: steps_list, paths: steps}
}
diff --git a/packages/contracts/package.json b/packages/contracts/package.json
index ee80c137..1b8f398f 100644
--- a/packages/contracts/package.json
+++ b/packages/contracts/package.json
@@ -10,7 +10,7 @@
"build:typechain": "rimraf types && typechain --target=ethers-v5 out/IWorld.sol/IWorld.json",
"deploy:local": "pnpm run build && mud deploy",
"deploy:testnet": "pnpm run build && mud deploy --profile=lattice-testnet",
- "dev": "pnpm mud dev-contracts",
+ "dev": "pnpm mud dev-contracts --rpc http://127.0.0.1:8545",
"dev-old": "pnpm mud dev-contracts --rpc http://127.0.0.1:8545",
"lint": "pnpm run prettier && pnpm run solhint",
"prettier": "prettier --write 'src/**/*.sol'",
diff --git a/packages/contracts/script/GameConfigInit.sol b/packages/contracts/script/GameConfigInit.sol
index 2888bfe3..66ca1608 100644
--- a/packages/contracts/script/GameConfigInit.sol
+++ b/packages/contracts/script/GameConfigInit.sol
@@ -9,8 +9,8 @@ import { GAME_CONFIG_KEY } from "../src/Constants.sol";
library GameConfigInit {
function initGameConfig(IWorld _world) internal {
console.log(" ========= initGameConfig");
- // bytes32 merkleRoot = 0x5df91eca63323dbb115087ef262075c5bcea99b8eaf95f520efb8d48ff447499;
- bytes32 merkleRoot = 0xa969691ad8c2e97e3d516e08f5b10ee4decd5f278a5f03ac4fa3532be181c854;
+ bytes32 merkleRoot = 0x5df91eca63323dbb115087ef262075c5bcea99b8eaf95f520efb8d48ff447499;
+ // bytes32 merkleRoot = 0xa969691ad8c2e97e3d516e08f5b10ee4decd5f278a5f03ac4fa3532be181c854;
GameConfig.set(
_world,
GAME_CONFIG_KEY, //key
diff --git a/packages/contracts/src/other/Loot.sol b/packages/contracts/src/other/Loot.sol
index 848240b7..ab5aaa76 100644
--- a/packages/contracts/src/other/Loot.sol
+++ b/packages/contracts/src/other/Loot.sol
@@ -252,8 +252,22 @@ contract MLoot is Suit, ERC721,MRandom {
loot.Ring
);
}
-
- function getUserTokenIdList() view external returns(uint256[] memory){
+ function getStructIndexInfo(uint256 _tokenId) external view returns(uint256,uint256,uint256,uint256,uint256,uint256,uint256,uint256){
+ Loot memory loot = lootList[_tokenId];
+ require(loot.state == RandomState.Confirmed,"User not exists");
+ return(
+ indexOf(weapons,loot.Weapon),
+ indexOf(chestArmor,loot.Chest),
+ indexOf(headArmor,loot.Head),
+ indexOf(waistArmor,loot.Waist),
+ indexOf(footArmor,loot.Foot),
+ indexOf(handArmor,loot.Hand),
+ indexOf(necklaces,loot.Neck),
+ indexOf(rings,loot.Ring)
+ );
+ }
+
+ function getUserTokenIdList() view external returns(uint256[] memory){
uint256 balance = balanceOf(msg.sender);
uint256[] memory tokenIds = new uint256[](balance);
uint256 index;
@@ -268,4 +282,16 @@ contract MLoot is Suit, ERC721,MRandom {
}
return tokenIds;
}
+
+ function indexOf(string[] memory _list,string memory _name) internal pure returns(uint256){
+ uint256 r;
+ require(_list.length > 0,"list is empty");
+ for(uint256 i;i<_list.length;i++){
+ if(keccak256(abi.encodePacked(_list[i])) == keccak256(abi.encodePacked(_name))){
+ r = i;
+ break;
+ }
+ }
+ return r;
+ }
}
diff --git a/packages/contracts/worlds.json b/packages/contracts/worlds.json
new file mode 100644
index 00000000..678ac95d
--- /dev/null
+++ b/packages/contracts/worlds.json
@@ -0,0 +1,17 @@
+{
+ "4242": {
+ "address": "0x41dEc9c4BfB2162811790C9cFc83c486bc51fE73",
+ "blockNumber": 27950347
+ },
+ "31337": {
+ "address": "0x2C0Cb6183720432832b4aAC01553eCfc476c1D27"
+ },
+ "421613": {
+ "address": "0x2Bc1034975c3df48D6f3026802f372677844b85d",
+ "blockNumber": 46508363
+ },
+ "11155111": {
+ "address": "0xec7F8CF3B3640b1C5E8fD3c208776aad056D4fc3",
+ "blockNumber": 4450305
+ }
+}
\ No newline at end of file