Skip to content

Commit

Permalink
update: update some
Browse files Browse the repository at this point in the history
  • Loading branch information
TiyoSheng committed Oct 28, 2023
2 parents 5b27a01 + ba29bcd commit 3252bb1
Show file tree
Hide file tree
Showing 16 changed files with 1,018 additions and 80 deletions.
8 changes: 8 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,12 @@ node_modules
packages/contracts/src/codegen/
pnpm-lock.yaml
packages/contracts/worlds.json
.DS_Store
# worlds.json
packages/contracts/deploys/**/*.json
packages/contracts/cache/*
packages/contracts/out/*
packages/contracts/node_modules/*
packages/contracts/broadcast/*
packages/contracts/types/*
*.meta
3 changes: 2 additions & 1 deletion packages/client/src/components/Map/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,8 @@ const Map = (props: IProps) => {
const cellClassCache = useRef<ICellClassCache>({});

const onMoveTo = (coordinate) => {
const { x, y} = players.find((player) => player.id === curId);
console.log(players, coordinate, 'onMoveTo')
const { x, y} = players.find((player) => player.addr === curId);
const paths = bfs(simpleMapData, { x, y }, coordinate).slice(1);
onPlayerMove(paths, formatMovePath(paths));
}
Expand Down
3 changes: 2 additions & 1 deletion packages/client/src/components/Rank/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,8 @@ interface IProps {
const Rank = (props: IProps) => {

const { data, curId } = props;
const curIndex = data.findIndex(item => item.id === curId);
console.log(data)
const curIndex = data.findIndex(item => item.id === 1);
const [visible, setVisible] = useState(false);

const toggleVisible = () => {
Expand Down
4 changes: 2 additions & 2 deletions packages/client/src/components/UserPackage/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,9 @@ const UserPackage = (props: IProps) => {
<h3>{props.title}</h3>
<div className="package-items-wrapper">
{
arr.map((count) => {
arr.map((count, index) => {
return (
<div className='package-item'>
<div className='package-item' key={index}>
{
count > 0 && (<>
<img src={gemImg} alt="" className={'package-item-gem'}/>
Expand Down
2 changes: 1 addition & 1 deletion packages/client/src/hooks/useModal.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ function useModal(config = { title: '' }) {
<Modal
{...config}
className="mi-modal"
visible={visible}
open={visible}
onCancel={close}
footer={null}
>
Expand Down
40 changes: 19 additions & 21 deletions packages/client/src/pages/game/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -28,14 +28,6 @@ const Game = () => {
network,
} = useMUD();

const mudPlayers = useEntityQuery([Has(Player)]).map((entity) => {
const address = decodeEntity({ addr: "address" }, entity)?.addr?.toLocaleLowerCase() || ''
const player = getComponentValue(Player, entity);
player.isMe = address.toLocaleLowerCase() == account.toLocaleLowerCase();
player.addr = address
return player;
});

const [renderMapData, setRenderMapData] = useState([]);
const [vertexCoordinate, setVertexCoordinate] = useState({
x: 0,
Expand All @@ -53,8 +45,6 @@ const Game = () => {

const { account } = network;

const curPlayer = players.find(player => player.isMe);

const { Modal, open, close, setContent } = useModal();

const mapDataRef = useRef([]);
Expand All @@ -68,25 +58,33 @@ const Game = () => {
head,
} = location.state ?? {};

const mudPlayers = useEntityQuery([Has(Player)]).map((entity) => {
const address = decodeEntity({ addr: "address" }, entity)?.addr?.toLocaleLowerCase() || ''
const player = getComponentValue(Player, entity);
player.addr = address
if (address.toLocaleLowerCase() === account.toLocaleLowerCase()) {
player.equip = {
clothes,
handheld,
head,
}
}
return player;
});

const curPlayer = mudPlayers.find(player => player.addr.toLocaleLowerCase() == account.toLocaleLowerCase());

console.log(curPlayer, 'curPlayer')
useEffect(() => {
loadMapData().then((csv) => {
setRenderMapData(csv);
mapDataRef.current = csv;
});

curPlayer.equip = {
clothes,
handheld,
head,
}
curPlayer.username = username;
setPlayers([...players]);

}, []);

useEffect(() => {
setPlayers(mudPlayers);
}, [mudPlayers]);
}, []);


const finishBattle = (e: any) => {
Expand Down Expand Up @@ -256,7 +254,7 @@ const Game = () => {
/>
</div>

<Rank data={RankMockData} curId={CurIdMockData} />
<Rank data={RankMockData} curId={account} />
<Map
width={MapConfig.visualWidth}
height={MapConfig.visualHeight}
Expand Down
131 changes: 99 additions & 32 deletions packages/client/src/pages/home/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -15,14 +15,18 @@ 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'

let userContract: any
let lootContract: any
const Home = () => {

const {
components: { GlobalConfig },
systemCalls,
systemCalls: { selectUserNft, joinBattlefield },
network
} = useMUD();

Expand Down Expand Up @@ -61,7 +65,16 @@ const Home = () => {
let wallet = new ethers.Wallet(privateKey, provider)
console.log(wallet)
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) {
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 = () => {
Expand Down Expand Up @@ -102,43 +115,97 @@ 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()
let tokenId = 0
// 获取当前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')
message.success('Mint User Success');
let tokenIds = await userContract.getUserTokenIdList()
console.log(tokenIds, 'tokenIds')
let tokenId = tokenIds[0].toString()
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,
}
});
})
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 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[tokenIds.length - 1].toString()

await selectUserNft(tokenId)
await joinBattlefield()

delay(100).then(() => {
setClothes(clothes);
setHandheld(handheld);
setHead(head);
}).delay(3000).then(() => {
navigate('/game', {
state: {
username,
clothes,
handheld,
head,
}
});
})

setMinting(false);

}

Expand Down
Loading

0 comments on commit 3252bb1

Please sign in to comment.