From 5b27a01dd754676bc84a1e29ecb6b17b6847924d Mon Sep 17 00:00:00 2001 From: Tiyo Date: Sat, 28 Oct 2023 14:37:24 +0800 Subject: [PATCH 01/11] fex: fex some bug --- packages/client/src/mud/createSystemCalls.ts | 3 --- packages/client/src/pages/test/index.jsx | 2 +- packages/contracts/worlds.json | 2 +- 3 files changed, 2 insertions(+), 5 deletions(-) diff --git a/packages/client/src/mud/createSystemCalls.ts b/packages/client/src/mud/createSystemCalls.ts index e13fc285..e9d60f52 100644 --- a/packages/client/src/mud/createSystemCalls.ts +++ b/packages/client/src/mud/createSystemCalls.ts @@ -59,11 +59,8 @@ export function createSystemCalls( } const selectUserNft = async (tokenId: any) => { - console.log(tokenId, 'tokenId') const tx = await worldContract.write.selectUserNft([tokenId]); - console.log(new Date().getTime(), tx, 'tx') await waitForTransaction(tx); - console.log(new Date().getTime(), tx, 'tx') } const openBox = async (boxId: any) => { diff --git a/packages/client/src/pages/test/index.jsx b/packages/client/src/pages/test/index.jsx index 5d5e34c7..f2582f92 100644 --- a/packages/client/src/pages/test/index.jsx +++ b/packages/client/src/pages/test/index.jsx @@ -21,7 +21,7 @@ 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 { diff --git a/packages/contracts/worlds.json b/packages/contracts/worlds.json index 3f96c6d6..a935a3cf 100644 --- a/packages/contracts/worlds.json +++ b/packages/contracts/worlds.json @@ -4,7 +4,7 @@ "blockNumber": 27950347 }, "31337": { - "address": "0x086ce0Da44C88Dac96cF0Dc4c5b2237eACca2E71" + "address": "0xB6b46DfD045134249e8DaC98a188e0C23B56A0b8" }, "421613": { "address": "0x2Bc1034975c3df48D6f3026802f372677844b85d", From 0fb3f8f1b8bd605bfd2806931d8af8504769fef2 Mon Sep 17 00:00:00 2001 From: Tiyo Date: Mon, 30 Oct 2023 11:11:18 +0800 Subject: [PATCH 02/11] fix: fix bug --- packages/client/src/pages/game/index.tsx | 4 ++-- packages/contracts/worlds.json | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/packages/client/src/pages/game/index.tsx b/packages/client/src/pages/game/index.tsx index a07e601e..bbc30de6 100644 --- a/packages/client/src/pages/game/index.tsx +++ b/packages/client/src/pages/game/index.tsx @@ -210,8 +210,8 @@ const Game = () => { const setStartBattle = async (player) => { console.log(player) const paths = bfs(simpleMapData, { x: curPlayer.x, y: curPlayer.y }, {x: player.x, y: player.y}).slice(1); - // await battleInvitation(player.addr, formatMovePath(paths)); - // console.log(formatMovePath(paths)) + await battleInvitation(player.addr, formatMovePath(paths)); + console.log(formatMovePath(paths)) setTargetPlayer(player); setStartBattleData(true); } diff --git a/packages/contracts/worlds.json b/packages/contracts/worlds.json index 66366883..10723139 100644 --- a/packages/contracts/worlds.json +++ b/packages/contracts/worlds.json @@ -4,7 +4,7 @@ "blockNumber": 27950347 }, "31337": { - "address": "0x8e86cDD6b5252b45305106071db047563F00e0a8" + "address": "0x965A01d39A9835d2B7e9e53bDc5C8501B962e8a3" }, "421613": { "address": "0x2Bc1034975c3df48D6f3026802f372677844b85d", From 6d9bba799669c8a86be81ede4c7a3319d6a50b6e Mon Sep 17 00:00:00 2001 From: LidamaoHub Date: Mon, 30 Oct 2023 11:48:39 +0800 Subject: [PATCH 03/11] Update:add loot id --- packages/contracts/src/other/Loot.sol | 30 +++++++++++++++++++++++++-- 1 file changed, 28 insertions(+), 2 deletions(-) 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; + } } From 9c18cfce5fb76054f9e9e77b0b8319e93cc06371 Mon Sep 17 00:00:00 2001 From: Tiyo Date: Mon, 30 Oct 2023 12:57:08 +0800 Subject: [PATCH 04/11] update: update battle --- .../client/src/components/Battle/index.tsx | 55 +++++------ packages/client/src/pages/game/index.tsx | 98 +++++++++---------- packages/client/src/pages/test/index.jsx | 30 ++++-- packages/contracts/worlds.json | 2 +- 4 files changed, 97 insertions(+), 88 deletions(-) diff --git a/packages/client/src/components/Battle/index.tsx b/packages/client/src/components/Battle/index.tsx index b540f26c..ba946041 100644 --- a/packages/client/src/components/Battle/index.tsx +++ b/packages/client/src/components/Battle/index.tsx @@ -84,11 +84,15 @@ export default function Battle(props) { if (battle) { if (!battleData.curHp || !battleData.targetHp) { let data = { - curHp: props.curPlayer.addr == battle.attacker ? battle.attackerHP : battle.defenderHP, - targetHp: props.targetPlayer.addr == battle.attacker ? battle.attackerHP : battle.defenderHP, + attackerHP: battle.attackerHP.toString(), + defenderHP: battle.defenderHP.toString(), + attacker: battle.attacker.toLocaleLowerCase(), + defender: battle.defender.toLocaleLowerCase(), } + console.log(data, props.curPlayer.addr, props.targetPlayer.addr) setBattleData(data) } + console.log(battleData, battle) if (battleState == 3) { let data = battleData let battle1 = document.querySelector('.battle-1'); @@ -98,18 +102,18 @@ export default function Battle(props) { setTimeout(() => { battle1.classList.remove('attack'); battle2.classList.add('back'); - let targetHp = props.curPlayer.addr == battle.attacker ? battle.attackerHP : battle.defenderHP - setPlayer2LossData(Number(data.targetHp) - Number(targetHp)) - data.targetHp = targetHp + let defenderHP = battle.defenderHP + setPlayer2LossData(Number(data.defenderHP) - Number(defenderHP)) + data.defenderHP = defenderHP setBattleData(data) setTimeout(() => { battle2.classList.remove('back'); setPlayer2LossData(0); // console.log(player2ResidualData) - if (targetHp <= 0) { + if (defenderHP <= 0 || battle.isEnd) { setConfirmBattleData([]); setConfirmBattle2Data([]); - setTimeout(() => {props.finishBattle(1);}, 600) + setTimeout(() => {props.finishBattle(battle.winner);}, 600) return } setTimeout(() => { @@ -117,16 +121,15 @@ export default function Battle(props) { setTimeout(() => { battle2.classList.remove('attack'); battle1.classList.add('back'); - let curHp = props.targetPlayer.addr == battle.attacker ? battle.attackerHP : battle.defenderHP - setPlayer1LossData(Number(data.curHp) - Number(curHp)) - data.curHp = curHp + let attackerHP = battle.attackerHP + setPlayer1LossData(Number(data.attackerHP) - Number(attackerHP)) + data.attackerHP = attackerHP setBattleData(data) - if (curHp <= 0) { + if (attackerHP <= 0 || battle.isEnd) { setPlayer1ResidualData(0); setConfirmBattleData([]); setConfirmBattle2Data([]); - props.finishBattle(2); - setTimeout(() => {props.finishBattle(1);}, 200) + setTimeout(() => {props.finishBattle(battle.winner);}, 600) return } setTimeout(() => { @@ -169,7 +172,7 @@ export default function Battle(props) { const confirmBattleFun = () => { if (battleState != 0) return - let battle:any = battles.filter((item:any) => (item.attacker.toLocaleLowerCase() == props.curPlayer.addr.toLocaleLowerCase() || item.defender.toLocaleLowerCase() == props.curPlayer.addr.toLocaleLowerCase()) && !item.isEnd)[0] + let battle:any = battles.filter((item:any) => (item?.attacker?.toLocaleLowerCase() == props?.curPlayer?.addr.toLocaleLowerCase() || item?.defender?.toLocaleLowerCase() == props?.curPlayer?.addr.toLocaleLowerCase()) && !item.isEnd)[0] console.log(battle) let action = confirmBattleData[0] let arg = confirmBattleData[1] @@ -210,14 +213,6 @@ export default function Battle(props) {
- { - confirmBattleData.length && confirmBattleData[0] ? ( -
- - -
- ) : '' - }
@@ -234,7 +229,9 @@ export default function Battle(props) { player1LossData ?
-{player1LossData.toFixed(0)}
: null }
- + { + props?.curPlayer?.addr == battleData.attacker ? : + } {/*
@@ -269,15 +266,17 @@ export default function Battle(props) { player2LossData ?
-{(player2LossData).toFixed(0)}
: null }
- + { + props?.curPlayer?.addr == battleData.defender ? : + }
-
HP : {battleData.curHp}/100
+
HP : {battleData.attackerHP}/{props?.curPlayer?.addr == battleData.attacker ? props?.curPlayer?.maxHp.toString() : props?.targetPlayer?.maxHp.toString()}
ATK : 20
-
HP : {battleData.targetHp}/100
+
HP : {battleData.defenderHP}/{props?.curPlayer?.addr == battleData.defender ? props?.curPlayer?.maxHp.toString() : props?.targetPlayer?.maxHp.toString()}
ATK : 20
diff --git a/packages/client/src/pages/game/index.tsx b/packages/client/src/pages/game/index.tsx index bbc30de6..7e6beef3 100644 --- a/packages/client/src/pages/game/index.tsx +++ b/packages/client/src/pages/game/index.tsx @@ -7,6 +7,7 @@ import { loadMapData } from "@/utils"; import Map from "@/components/Map"; import UserAvatar from "@/components/UserAvatar"; import { useLocation } from "react-router-dom"; +import { message } from 'antd'; import "./styles.scss"; import Rank from "@/components/Rank"; import { CurIdMockData, PlayersMockData, RankMockData, TreasureChestMockData } from "@/mock/data"; @@ -45,6 +46,7 @@ const Game = () => { const formatMovePath = useMerkel(simpleMapData); const [targetPlayer, setTargetPlayer] = useState(null); + const [battleCurPlayer, setBattleCurPlayer] = useState(null); const [userInfoPlayer, setUserInfoPlayer] = useState(); const [startBattleData, setStartBattleData] = useState(false); @@ -79,10 +81,33 @@ const Game = () => { player.username = username; } return player; - }); + }).filter(e => e.state != 1); + console.log(players, 'players') const curPlayer = players.find(player => player.addr.toLocaleLowerCase() == account.toLocaleLowerCase()); + const battles = useEntityQuery([Has(BattleList)]).map((entity) => { + let id = decodeEntity({ battleId: "uint256" }, entity); + let battle:any = getComponentValue(BattleList, entity) + battle.id = id.battleId.toString() + return battle; + }); + + if (battles.length && !startBattleData) { + let battle:any = battles.filter((item:any) => (item.attacker.toLocaleLowerCase() == account.toLocaleLowerCase() || item.defender.toLocaleLowerCase() == account.toLocaleLowerCase()) && !item.isEnd)[0] + if (battle) { + let targetAddr = battle.attacker.toLocaleLowerCase() == account.toLocaleLowerCase() ? battle.defender : battle.attacker + let target = players.filter((item:any) => item.addr.toLocaleLowerCase() == targetAddr.toLocaleLowerCase())[0] + if (!battleCurPlayer) { + setBattleCurPlayer(curPlayer) + } + if (!targetPlayer) { + setTargetPlayer(target) + } + setStartBattleData(true); + } + } + const getCollectionsFun = (box: any) => { setContent(
@@ -112,8 +137,6 @@ const Game = () => { return box; }); - const treasureChest = boxs.filter((item) => !item.opened); - useEffect(() => { loadMapData().then((csv) => { setRenderMapData(csv); @@ -132,25 +155,13 @@ const Game = () => { const finishBattle = (e: any) => { console.log(e); setStartBattleData(false); - if (e == 1) { + if (e.toLocaleLowerCase() == account.toLocaleLowerCase()) { console.log('win'); - let treasureChestData = treasureChest - console.log(treasureChestData, treasureChestData[treasureChestData.length - 1]); - let item = { - id: treasureChestData.length ? treasureChestData[treasureChestData.length - 1].id + 1 : 1, - x: targetPlayer.x, - y: targetPlayer.y, - gem: targetPlayer.gem - } - treasureChestData.push(item) - let targetPlayerIndex = players.findIndex((item) => item.x === targetPlayer.x && item.y === targetPlayer.y); - players.splice(targetPlayerIndex, 1); - setTreasureChest([...treasureChestData]); - // setPlayers([...players]); - // getWinTreasureChest(targetPlayer.gem) + message.success('You win the battle'); setTargetPlayer(null); - } else if (e == 2) { + } else { console.log('lose'); + message.error('You lose the battle'); } } @@ -197,7 +208,6 @@ const Game = () => {
@@ -218,10 +228,19 @@ const Game = () => { const openTreasureChest = async (id) => { console.log(id); - const targetIndex = treasureChest.findIndex(item => item.id === id); - treasureChest[targetIndex].opening = true; + const boxIndex = boxs.findIndex(item => item.id === id); + let box = boxs[boxIndex] + if (box.opened) { + if (box.owner.toLocaleLowerCase() != account.toLocaleLowerCase()) { + message.error('The treasure chest has been opened by others'); + return + } else { + getCollectionsFun(box); + return + } + } + boxs[boxIndex].opening = true; await openBox(id); - let blockNumber = await network.publicClient.getBlockNumber() // 每隔1s获取一次getBlockNumber let interval = setInterval(async () => { @@ -230,37 +249,11 @@ const Game = () => { if (currentBlockNumber - blockNumber >= 2) { clearInterval(interval) await revealBox(id) - boxId = id } }, 1000) } - const getWinTreasureChest = (gem = 1) => { - curPlayer.gem += gem; - const targetPlayerData = players.find((item) => item.x === targetPlayer.x && item.y === targetPlayer.y); - targetPlayerData.gem -= gem; - setPlayers([...players]); - setContent( -
-
- Congrats,you got {gem} gems! - -
- -
-
-
- -
-
- ); - open(); - } - - - - return ( { mapData: renderMapData, onPlayerMove: movePlayer, showUserInfo, - treasureChest, + treasureChest: boxs, openTreasureChest, - setStartBattle, - getWinTreasureChest + setStartBattle }} >
@@ -296,7 +288,7 @@ const Game = () => { vertexCoordinate={vertexCoordinate} /> { - startBattleData ? : null + startBattleData ? : null }
diff --git a/packages/client/src/pages/test/index.jsx b/packages/client/src/pages/test/index.jsx index f639d9fe..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,6 +9,9 @@ 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' @@ -28,6 +31,7 @@ const Test = () => { const [revealNFTData, setRevealNFTData] = useState(''); const [nftListData, setNftListData] = useState([]); const [walletBalance, setWalletBalance] = useState(''); + const [renderMapData, setRenderMapData] = useState([]); const { components: { Player, GameConfig, BattleList, BoxList, GlobalConfig }, @@ -36,7 +40,20 @@ const Test = () => { } = useMUD(); const { account } = network; - console.log(network, 'account') + + const simpleMapData = useMemo(() => { + return simplifyMapData(renderMapData); + }, [renderMapData]); + + const formatMovePath = useMerkel(simpleMapData); + + useEffect(() => { + loadMapData().then((csv) => { + setRenderMapData(csv); + mapDataRef.current = csv; + }); + }, []); + // 转账函数 const transferFun = async (to) => { @@ -107,7 +124,7 @@ const Test = () => { console.log(battle, 'battle', id, entity) battle.id = id.battleId.toString() return battle; - }); + }).filter(e => !e.isEnd) console.log(battles, 'battles') const boxs = useEntityQuery([Has(BoxList)]).map((entity) => { @@ -224,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 = () => { @@ -241,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); } } diff --git a/packages/contracts/worlds.json b/packages/contracts/worlds.json index 10723139..46d24760 100644 --- a/packages/contracts/worlds.json +++ b/packages/contracts/worlds.json @@ -4,7 +4,7 @@ "blockNumber": 27950347 }, "31337": { - "address": "0x965A01d39A9835d2B7e9e53bDc5C8501B962e8a3" + "address": "0x210a5Ebab6461De91b569bCf8f9837E8530B4A00" }, "421613": { "address": "0x2Bc1034975c3df48D6f3026802f372677844b85d", From 713e094c4c086559c7a7891ad315deb093ddd199 Mon Sep 17 00:00:00 2001 From: Tiyo Date: Mon, 30 Oct 2023 13:27:05 +0800 Subject: [PATCH 05/11] update: update home --- .../client/src/components/UserInfo/index.tsx | 10 ++++--- .../src/components/UserInfo/styles.scss | 12 ++++----- packages/client/src/pages/home/index.tsx | 27 ++++++++++++++++--- packages/contracts/worlds.json | 2 +- 4 files changed, 37 insertions(+), 14 deletions(-) diff --git a/packages/client/src/components/UserInfo/index.tsx b/packages/client/src/components/UserInfo/index.tsx index 5c6ea5de..0599c351 100644 --- a/packages/client/src/components/UserInfo/index.tsx +++ b/packages/client/src/components/UserInfo/index.tsx @@ -12,7 +12,7 @@ export interface IUserInfo { const UserInfo = (props: IUserInfo) => { - const { handheld, head, clothes, gem = 0 } = props; + const { handheld, head, clothes, gem = 0, userUrl, lootUrl } = props; const lootHasLoaded = handheld && head && clothes; console.log(handheld, head, clothes, lootHasLoaded); @@ -28,10 +28,14 @@ const UserInfo = (props: IUserInfo) => {
- + { + userUrl ? : null + }
- + { + lootUrl ? : null + }
diff --git a/packages/client/src/components/UserInfo/styles.scss b/packages/client/src/components/UserInfo/styles.scss index e9ae2161..c58285e4 100644 --- a/packages/client/src/components/UserInfo/styles.scss +++ b/packages/client/src/components/UserInfo/styles.scss @@ -41,13 +41,13 @@ height: 100%; &.loaded { - .loot-detail:nth-child(1) { - background: url("../../assets/img/loot1.png") no-repeat center / 100%; - } + // .loot-detail:nth-child(1) { + // background: url("../../assets/img/loot1.png") no-repeat center / 100%; + // } - .loot-detail:nth-child(2) { - background: url("../../assets/img/loot2.png") no-repeat center / 100%; - } + // .loot-detail:nth-child(2) { + // background: url("../../assets/img/loot2.png") no-repeat center / 100%; + // } } } diff --git a/packages/client/src/pages/home/index.tsx b/packages/client/src/pages/home/index.tsx index 60cebdab..d37bca2c 100644 --- a/packages/client/src/pages/home/index.tsx +++ b/packages/client/src/pages/home/index.tsx @@ -20,6 +20,8 @@ import { min } from 'rxjs'; 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 = () => { @@ -54,6 +56,8 @@ 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') @@ -122,9 +126,8 @@ const Home = () => { 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 + let tokenIds = await lootContract.getUserTokenIdList() + let tokenId = tokenIds[tokenIds.length - 1].toString() // 获取当前getBlockNumber let blockNumber = await network.publicClient.getBlockNumber() // 每隔1s获取一次getBlockNumber @@ -175,6 +178,13 @@ const Home = () => { }) } + 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)]; @@ -186,6 +196,15 @@ const Home = () => { let tokenIds = await userContract.getUserTokenIdList() let tokenId = tokenIds[tokenIds.length - 1].toString() + let lootTokenIds = await lootContract.getUserTokenIdList() + let lootTokenId = tokenIds[lootTokenIds.length - 1].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() @@ -244,7 +263,7 @@ const Home = () => {

HOME

- + diff --git a/packages/contracts/worlds.json b/packages/contracts/worlds.json index 46d24760..f14c173d 100644 --- a/packages/contracts/worlds.json +++ b/packages/contracts/worlds.json @@ -4,7 +4,7 @@ "blockNumber": 27950347 }, "31337": { - "address": "0x210a5Ebab6461De91b569bCf8f9837E8530B4A00" + "address": "0x36c7b0946e701EA6cE8e101B4C959FF15D0DeE15" }, "421613": { "address": "0x2Bc1034975c3df48D6f3026802f372677844b85d", From fbb45da3aedf45eef9d4955036b6ec45d5808b51 Mon Sep 17 00:00:00 2001 From: Tiyo Date: Mon, 30 Oct 2023 13:36:23 +0800 Subject: [PATCH 06/11] del code --- packages/client/src/components/UserInfo/styles.scss | 10 ---------- 1 file changed, 10 deletions(-) diff --git a/packages/client/src/components/UserInfo/styles.scss b/packages/client/src/components/UserInfo/styles.scss index c58285e4..ec89f6e8 100644 --- a/packages/client/src/components/UserInfo/styles.scss +++ b/packages/client/src/components/UserInfo/styles.scss @@ -39,16 +39,6 @@ justify-content: space-between; flex-direction: column; height: 100%; - - &.loaded { - // .loot-detail:nth-child(1) { - // background: url("../../assets/img/loot1.png") no-repeat center / 100%; - // } - - // .loot-detail:nth-child(2) { - // background: url("../../assets/img/loot2.png") no-repeat center / 100%; - // } - } } .loot-detail { From b73f6523584d8786570c94ce7e34c1bce50c2fe1 Mon Sep 17 00:00:00 2001 From: Tiyo Date: Mon, 30 Oct 2023 14:49:54 +0800 Subject: [PATCH 07/11] fix: fox bug --- packages/client/src/components/Battle/index.tsx | 9 +++++++-- packages/client/src/pages/game/index.tsx | 2 +- packages/client/src/pages/home/index.tsx | 4 ++-- packages/contracts/worlds.json | 2 +- 4 files changed, 11 insertions(+), 6 deletions(-) diff --git a/packages/client/src/components/Battle/index.tsx b/packages/client/src/components/Battle/index.tsx index ba946041..43e2bc31 100644 --- a/packages/client/src/components/Battle/index.tsx +++ b/packages/client/src/components/Battle/index.tsx @@ -25,6 +25,7 @@ import { decodeEntity } from "@latticexyz/store-sync/recs"; import { getRandomStr } from '@/utils/utils'; import { ethers } from 'ethers'; import { solidityKeccak256 } from 'ethers/lib/utils'; +import { message } from 'antd'; export default function Battle(props) { console.log(props) @@ -66,7 +67,7 @@ export default function Battle(props) { } if (battlesId) { let battle:any = battles.filter((item:any) => item.id.toString() == battlesId)[0] - if (battle.attackerState == 1 && battle.defenderState == 1 && battleState == 1) { + if (((battle.attackerState == 1 && battle.defenderState == 1) || (battle.attackerState == 2 && battle.defenderState == 1) || (battle.attackerState == 1 && battle.defenderState == 2)) && battleState == 1) { let action = confirmBattleData[0] let arg = confirmBattleData[1] let actionHex = ethers.utils.formatBytes32String(action); @@ -172,10 +173,14 @@ export default function Battle(props) { const confirmBattleFun = () => { if (battleState != 0) return + if (!confirmBattleData[0]) { + message.info('Please select action') + return + } let battle:any = battles.filter((item:any) => (item?.attacker?.toLocaleLowerCase() == props?.curPlayer?.addr.toLocaleLowerCase() || item?.defender?.toLocaleLowerCase() == props?.curPlayer?.addr.toLocaleLowerCase()) && !item.isEnd)[0] console.log(battle) let action = confirmBattleData[0] - let arg = confirmBattleData[1] + let arg = confirmBattleData[1] || 0 let actionHex = ethers.utils.formatBytes32String(action); let hash = getProofHash(actionHex, arg, nonceHex); confirmBattle(hash, battle.id); diff --git a/packages/client/src/pages/game/index.tsx b/packages/client/src/pages/game/index.tsx index 7e6beef3..b1bf82af 100644 --- a/packages/client/src/pages/game/index.tsx +++ b/packages/client/src/pages/game/index.tsx @@ -85,7 +85,6 @@ const Game = () => { console.log(players, 'players') const curPlayer = players.find(player => player.addr.toLocaleLowerCase() == account.toLocaleLowerCase()); - const battles = useEntityQuery([Has(BattleList)]).map((entity) => { let id = decodeEntity({ battleId: "uint256" }, entity); let battle:any = getComponentValue(BattleList, entity) @@ -223,6 +222,7 @@ const Game = () => { await battleInvitation(player.addr, formatMovePath(paths)); console.log(formatMovePath(paths)) setTargetPlayer(player); + setBattleCurPlayer(curPlayer) setStartBattleData(true); } diff --git a/packages/client/src/pages/home/index.tsx b/packages/client/src/pages/home/index.tsx index d37bca2c..771c50d7 100644 --- a/packages/client/src/pages/home/index.tsx +++ b/packages/client/src/pages/home/index.tsx @@ -195,9 +195,9 @@ const Home = () => { await mintLoot() let tokenIds = await userContract.getUserTokenIdList() - let tokenId = tokenIds[tokenIds.length - 1].toString() + let tokenId = tokenIds[0].toString() let lootTokenIds = await lootContract.getUserTokenIdList() - let lootTokenId = tokenIds[lootTokenIds.length - 1].toString() + let lootTokenId = lootTokenIds[0].toString() let url = await userContract.tokenURI(tokenId) let lootUrl = await lootContract.tokenURI(lootTokenId) diff --git a/packages/contracts/worlds.json b/packages/contracts/worlds.json index f14c173d..678ac95d 100644 --- a/packages/contracts/worlds.json +++ b/packages/contracts/worlds.json @@ -4,7 +4,7 @@ "blockNumber": 27950347 }, "31337": { - "address": "0x36c7b0946e701EA6cE8e101B4C959FF15D0DeE15" + "address": "0x2C0Cb6183720432832b4aAC01553eCfc476c1D27" }, "421613": { "address": "0x2Bc1034975c3df48D6f3026802f372677844b85d", From 434903927b7117fbf780be92e433f7827be02030 Mon Sep 17 00:00:00 2001 From: Tiyo Date: Mon, 30 Oct 2023 14:52:13 +0800 Subject: [PATCH 08/11] update: chainid to 4242 --- packages/client/.env | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/client/.env b/packages/client/.env index 3528db88..a7392e67 100644 --- a/packages/client/.env +++ b/packages/client/.env @@ -1 +1 @@ -VITE_CHAIN_ID=31337 +VITE_CHAIN_ID=4242 From 10201e90c5ad62148ba39463716c09bbbbe1f383 Mon Sep 17 00:00:00 2001 From: Tiyo Date: Mon, 30 Oct 2023 15:10:55 +0800 Subject: [PATCH 09/11] fix: fix bug --- packages/client/.env | 2 +- packages/client/src/components/Battle/index.tsx | 6 ------ packages/client/src/pages/home/index.tsx | 14 ++++++++++---- 3 files changed, 11 insertions(+), 11 deletions(-) diff --git a/packages/client/.env b/packages/client/.env index a7392e67..3528db88 100644 --- a/packages/client/.env +++ b/packages/client/.env @@ -1 +1 @@ -VITE_CHAIN_ID=4242 +VITE_CHAIN_ID=31337 diff --git a/packages/client/src/components/Battle/index.tsx b/packages/client/src/components/Battle/index.tsx index 43e2bc31..4befa09b 100644 --- a/packages/client/src/components/Battle/index.tsx +++ b/packages/client/src/components/Battle/index.tsx @@ -112,8 +112,6 @@ export default function Battle(props) { setPlayer2LossData(0); // console.log(player2ResidualData) if (defenderHP <= 0 || battle.isEnd) { - setConfirmBattleData([]); - setConfirmBattle2Data([]); setTimeout(() => {props.finishBattle(battle.winner);}, 600) return } @@ -128,16 +126,12 @@ export default function Battle(props) { setBattleData(data) if (attackerHP <= 0 || battle.isEnd) { setPlayer1ResidualData(0); - setConfirmBattleData([]); - setConfirmBattle2Data([]); setTimeout(() => {props.finishBattle(battle.winner);}, 600) return } setTimeout(() => { battle1.classList.remove('back'); setPlayer1LossData(0); - setConfirmBattleData([]); - setConfirmBattle2Data([]); setBattleState(0) console.log(battleState) }, 400); diff --git a/packages/client/src/pages/home/index.tsx b/packages/client/src/pages/home/index.tsx index 771c50d7..1e5c715d 100644 --- a/packages/client/src/pages/home/index.tsx +++ b/packages/client/src/pages/home/index.tsx @@ -191,13 +191,19 @@ const Home = () => { 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() + if (!tokenIds && !tokenIds.length) { + await mintUser(); + tokenIds = await userContract.getUserTokenIdList() + } + if (!lootTokenIds && !lootTokenIds.length) { + await mintLoot(); + lootTokenIds = await lootContract.getUserTokenIdList() + } let lootTokenId = lootTokenIds[0].toString() + let tokenId = tokenIds[0].toString() let url = await userContract.tokenURI(tokenId) let lootUrl = await lootContract.tokenURI(lootTokenId) @@ -214,6 +220,7 @@ const Home = () => { setHandheld(handheld); setHead(head); }).delay(3000).then(() => { + setMinting(false); navigate('/game', { state: { username, @@ -224,7 +231,6 @@ const Home = () => { }); }) - setMinting(false); } From 237831a653fc60a26340b42a173eb7387add00bf Mon Sep 17 00:00:00 2001 From: Tiyo Date: Mon, 30 Oct 2023 15:21:35 +0800 Subject: [PATCH 10/11] update: chainid to 4242 --- packages/client/.env | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/client/.env b/packages/client/.env index 3528db88..a7392e67 100644 --- a/packages/client/.env +++ b/packages/client/.env @@ -1 +1 @@ -VITE_CHAIN_ID=31337 +VITE_CHAIN_ID=4242 From 982e4772a81bbd600083707734ade49461ef25a1 Mon Sep 17 00:00:00 2001 From: Tiyo Date: Mon, 30 Oct 2023 15:38:58 +0800 Subject: [PATCH 11/11] fix: fix bug --- packages/client/src/pages/home/index.tsx | 14 +++----------- 1 file changed, 3 insertions(+), 11 deletions(-) diff --git a/packages/client/src/pages/home/index.tsx b/packages/client/src/pages/home/index.tsx index 1e5c715d..1a91140b 100644 --- a/packages/client/src/pages/home/index.tsx +++ b/packages/client/src/pages/home/index.tsx @@ -191,19 +191,13 @@ const Home = () => { 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() - if (!tokenIds && !tokenIds.length) { - await mintUser(); - tokenIds = await userContract.getUserTokenIdList() - } - if (!lootTokenIds && !lootTokenIds.length) { - await mintLoot(); - lootTokenIds = await lootContract.getUserTokenIdList() - } let lootTokenId = lootTokenIds[0].toString() - let tokenId = tokenIds[0].toString() let url = await userContract.tokenURI(tokenId) let lootUrl = await lootContract.tokenURI(lootTokenId) @@ -230,8 +224,6 @@ const Home = () => { } }); }) - - } const play = () => {