From ac1813e6993761d8c52016f64dada49d7b2651a5 Mon Sep 17 00:00:00 2001 From: lewis <> Date: Thu, 26 Oct 2023 12:50:27 +0800 Subject: [PATCH 1/5] chore: add reveal battle param check --- packages/contracts/src/systems/BattleSystem.sol | 1 + 1 file changed, 1 insertion(+) diff --git a/packages/contracts/src/systems/BattleSystem.sol b/packages/contracts/src/systems/BattleSystem.sol index 328d3f5b..04ae8f32 100644 --- a/packages/contracts/src/systems/BattleSystem.sol +++ b/packages/contracts/src/systems/BattleSystem.sol @@ -13,6 +13,7 @@ contract BattleSystem is System { event BattleEnd(uint256 battleId, BattleEndType endType, address winner); function revealBattle(uint256 _battleId, bytes32 _action, uint256 _arg, bytes32 _nonce) external { + require(_action == bytes32("attack") || _action == bytes32("escape"), "invalid action"); // check battle BattleListData memory battle = BattleList.get(_battleId); BattleUtils.checkBattlePlayer(battle, _msgSender(), BattleState.Confirmed); From af5ebc9b66a254858a6e5d88d668487d67d30e1b Mon Sep 17 00:00:00 2001 From: Tiyo Date: Thu, 26 Oct 2023 18:36:19 +0800 Subject: [PATCH 2/5] update: test page --- packages/client/src/mud/createSystemCalls.ts | 4 ++-- packages/client/src/pages/test/index.jsx | 15 +++++++-------- packages/contracts/worlds.json | 2 +- 3 files changed, 10 insertions(+), 11 deletions(-) diff --git a/packages/client/src/mud/createSystemCalls.ts b/packages/client/src/mud/createSystemCalls.ts index 867e66a3..e13fc285 100644 --- a/packages/client/src/mud/createSystemCalls.ts +++ b/packages/client/src/mud/createSystemCalls.ts @@ -38,8 +38,8 @@ export function createSystemCalls( await waitForTransaction(tx); } - const transfer = async (transferData: any) => { - const tx = await worldContract.write.transfer([...transferData]); + const transfer = async (addr: any, transferData: any) => { + const tx = await worldContract.write.transfer([addr, ...transferData]); await waitForTransaction(tx); } diff --git a/packages/client/src/pages/test/index.jsx b/packages/client/src/pages/test/index.jsx index 47312a47..f17e1fd0 100644 --- a/packages/client/src/pages/test/index.jsx +++ b/packages/client/src/pages/test/index.jsx @@ -11,14 +11,12 @@ import { getRandomStr } from '../../utils/utils'; import './index.scss'; 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"},{"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":"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"}] -const contractAddress = '0x5FbDB2315678afecb367f032d93F642f64180aa3' -const rpc = 'http://127.0.0.1:8545' const Test = () => { const [stepData, setStepData] = useState([]); const [transferData, setTransferData] = useState([]); const [battleData, setBattleData] = useState([]); - const [confirmBattleData, setConfirmBattleData] = useState(['Attack', 1]); + const [confirmBattleData, setConfirmBattleData] = useState(['attack', 1]); const [battlesData, setBattlesData] = useState([]); const [boxData, setBoxData] = useState([]); const [boxId, setBoxId] = useState([]); @@ -99,10 +97,10 @@ const Test = () => { const mintFun = () => { let privateKey = network.privateKey - let provider = new ethers.providers.JsonRpcProvider(rpc) + let provider = new ethers.providers.JsonRpcProvider('http://127.0.0.1:8545') let wallet = new ethers.Wallet(privateKey, provider) console.log(wallet) - let userContractAddress = contractAddress + let userContractAddress = '0x2498e8059929e18e2a2cED4e32ef145fa2F4a744' let userContract = new ethers.Contract(userContractAddress, abi, wallet) console.log(userContract) userContract.mint().then(async res => { @@ -116,10 +114,10 @@ const Test = () => { const revealNFTFun = () => { let privateKey = network.privateKey - let provider = new ethers.providers.JsonRpcProvider(rpc) + let provider = new ethers.providers.JsonRpcProvider('http://127.0.0.1:8545') let wallet = new ethers.Wallet(privateKey, provider) console.log(wallet) - let userContractAddress = contractAddress + let userContractAddress = '0x2498e8059929e18e2a2cED4e32ef145fa2F4a744' let userContract = new ethers.Contract(userContractAddress, abi, wallet) userContract.revealNFT(revealNFTData).then(async res => { console.log(res) @@ -194,7 +192,7 @@ const Test = () => { const transferPlayer = () => { console.log(transferData, 'transfer') - transfer(transferData); + transfer(account, transferData); } const battleInvitationFun = () => { @@ -289,6 +287,7 @@ const Test = () => {
用户信息 {item.isMe ? '(自己)' : ''}
addr: {item.addr}
hp: {item.hp.toString()}
+
attack: {item.attack.toString()}
oreBalance: {item.oreBalance.toString()}
treasureBalance: {item.treasureBalance.toString()}
坐标:{item?.x || 0},{item?.y || 0}
diff --git a/packages/contracts/worlds.json b/packages/contracts/worlds.json index 472b0435..aa9bc365 100644 --- a/packages/contracts/worlds.json +++ b/packages/contracts/worlds.json @@ -4,7 +4,7 @@ "blockNumber": 27950347 }, "31337": { - "address": "0x313F922BE1649cEc058EC0f076664500c78bdc0b" + "address": "0xF357118EBd576f3C812c7875B1A1651a7f140E9C" }, "421613": { "address": "0x2Bc1034975c3df48D6f3026802f372677844b85d", From 84d13954304f9a83d22664d75f2b86775f415214 Mon Sep 17 00:00:00 2001 From: lewis <> Date: Thu, 26 Oct 2023 19:52:05 +0800 Subject: [PATCH 3/5] chore: optimze battle end player state --- packages/contracts/mud.config.ts | 3 +-- .../contracts/src/systems/BattleSystem.sol | 23 ++++++++++--------- 2 files changed, 13 insertions(+), 13 deletions(-) diff --git a/packages/contracts/mud.config.ts b/packages/contracts/mud.config.ts index b7f36bbe..d8a9b60f 100644 --- a/packages/contracts/mud.config.ts +++ b/packages/contracts/mud.config.ts @@ -12,8 +12,7 @@ export default mudConfig({ }, GMSystem: { name: "gm", - openAccess: false, - accessList: [], + openAccess: true, }, PlayerSystem: { name: "player", diff --git a/packages/contracts/src/systems/BattleSystem.sol b/packages/contracts/src/systems/BattleSystem.sol index 04ae8f32..c7de468b 100644 --- a/packages/contracts/src/systems/BattleSystem.sol +++ b/packages/contracts/src/systems/BattleSystem.sol @@ -40,7 +40,6 @@ contract BattleSystem is System { && BattleList.getDefenderState(_battleId) == BattleState.Revealed) { // reveal // console.log("reveal battle"); - // revert("asdfasfd"); revealWinner(_battleId); } emit BattleReveal(_battleId, _msgSender()); @@ -65,17 +64,19 @@ contract BattleSystem is System { attackerAttackDenfenderEscape(_battleId, battle, attackerBuff, defenderBuff, attackerFirepower); } - if (!battle.isEnd) { + if (!BattleList.getIsEnd(_battleId)) { console.log(" round end "); - emit BattleEnd(_battleId, BattleEndType.RoundEnd, address(0)); + // emit BattleEnd(_battleId, BattleEndType.RoundEnd, address(0)); BattleList.setDefenderState(_battleId, BattleState.Inited); BattleList.setAttackerState(_battleId, BattleState.Inited); - - } else { // set explore state - Player.setState(battle.attacker, PlayerState.Exploring); - Player.setState(battle.defender, PlayerState.Exploring); + if (Player.getState(battle.attacker) == PlayerState.Attacking) { + Player.setState(battle.attacker, PlayerState.Exploring); + } + if (Player.getState(battle.defender) == PlayerState.Attacking) { + Player.setState(battle.defender, PlayerState.Exploring); + } } } @@ -97,14 +98,14 @@ contract BattleSystem is System { BattleList.setIsEnd(_battleId, true); loseGame(looser, winner); - emit BattleEnd(_battleId, BattleEndType.NormalEnd, winner); + // emit BattleEnd(_battleId, BattleEndType.NormalEnd, winner); } } function allEscape(uint _battleId) internal { BattleList.setIsEnd(_battleId, true); BattleList.setWinner(_battleId, address(0)); - emit BattleEnd(_battleId, BattleEndType.AllEscape, address(0)); + // emit BattleEnd(_battleId, BattleEndType.AllEscape, address(0)); } function attackerEscapeDenfenderAttack(uint _battleId, BattleListData memory battle, Buff attackerBuff, @@ -118,7 +119,7 @@ contract BattleSystem is System { PlayerLocationLock.set(battle.attacker, block.timestamp); // console.log(" escape --- 2"); - emit BattleEnd(_battleId, BattleEndType.NormalEnd, battle.defender); + // emit BattleEnd(_battleId, BattleEndType.NormalEnd, battle.defender); } else { // escape fail, cause hurt uint256 attackerAttackPower = BattleUtils.getAttackPower(attackerBuff, defenderBuff, attackerFirepower); @@ -127,7 +128,7 @@ contract BattleSystem is System { BattleList.setWinner(_battleId, battle.defender); BattleList.setIsEnd(_battleId, true); - emit BattleEnd(_battleId, BattleEndType.NormalEnd, battle.defender); + // emit BattleEnd(_battleId, BattleEndType.NormalEnd, battle.defender); } } } From 0271f50b539b55ed4f65854eca15429820a04e13 Mon Sep 17 00:00:00 2001 From: lewis <> Date: Thu, 26 Oct 2023 20:11:43 +0800 Subject: [PATCH 4/5] chore: add move limit --- packages/contracts/src/systems/BattleSystem.sol | 2 +- packages/contracts/src/systems/MoveSystem.sol | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/packages/contracts/src/systems/BattleSystem.sol b/packages/contracts/src/systems/BattleSystem.sol index c7de468b..8b26b3e6 100644 --- a/packages/contracts/src/systems/BattleSystem.sol +++ b/packages/contracts/src/systems/BattleSystem.sol @@ -42,7 +42,7 @@ contract BattleSystem is System { // console.log("reveal battle"); revealWinner(_battleId); } - emit BattleReveal(_battleId, _msgSender()); + // emit BattleReveal(_battleId, _msgSender()); } function revealWinner(uint256 _battleId) public { diff --git a/packages/contracts/src/systems/MoveSystem.sol b/packages/contracts/src/systems/MoveSystem.sol index bc391146..96c82496 100644 --- a/packages/contracts/src/systems/MoveSystem.sol +++ b/packages/contracts/src/systems/MoveSystem.sol @@ -31,7 +31,7 @@ contract MoveSystem is System { moveList.length > 0 && moveList.length <= BattleConfig.getMaxMoveDistance(BATTLE_CONFIG_KEY), "invalid move distance" ); - // require(moveList.length <= Player.getSpeed(_msgSender()), "exceed player speed"); + require(moveList.length <= Player.getSpeed(_msgSender()), "exceed player speed"); // check player lock require(PlayerLocationLock.get(_msgSender()) == 0, "You are locked"); // check continuity From 17e78060664e6bf61aa3943e09ff0523b20b688c Mon Sep 17 00:00:00 2001 From: lewis <> Date: Thu, 26 Oct 2023 20:17:19 +0800 Subject: [PATCH 5/5] chore: add player space restrict --- packages/contracts/src/systems/BattlePrepareSystem.sol | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/contracts/src/systems/BattlePrepareSystem.sol b/packages/contracts/src/systems/BattlePrepareSystem.sol index 8ca814db..f86cf29f 100644 --- a/packages/contracts/src/systems/BattlePrepareSystem.sol +++ b/packages/contracts/src/systems/BattlePrepareSystem.sol @@ -51,7 +51,7 @@ contract BattlePrepareSystem is System { positionList.length > 0 && positionList.length <= BattleConfig.getMaxAttackzDistance(BATTLE_CONFIG_KEY), "invalid attack distance" ); - // require(positionList.length <= Player.getSpace(_msgSender()), "exceed player space"); //Todo: temp remove + require(positionList.length <= Player.getSpace(_msgSender()), "exceed player space"); //Todo: temp remove require( Player.getState(_msgSender()) == PlayerState.Exploring &&