Skip to content

Commit

Permalink
Update:修改joinbattlefield
Browse files Browse the repository at this point in the history
  • Loading branch information
LidamaoHub committed Oct 20, 2023
1 parent 69db2f9 commit b5befc6
Show file tree
Hide file tree
Showing 6 changed files with 54 additions and 38 deletions.
Binary file modified packages/contracts/.DS_Store
Binary file not shown.
40 changes: 5 additions & 35 deletions packages/contracts/jsTest/test.js
Original file line number Diff line number Diff line change
@@ -1,41 +1,11 @@
const { ethers } = require("ethers");
require("dotenv").config();
const { exec } = require("child_process");

async function buildABI() {
return new Promise((resolve, reject) => {
exec("pnpm run build:abi", (error, stdout, stderr) => {
if (error) {
console.error(`执行命令时出错: ${error.message}`);
reject(error);
}
if (stderr) {
console.error(`命令输出错误: ${stderr}`);
reject(stderr);
}
console.log(`命令输出: ${stdout}`);
resolve(stdout);
});
});
}
const privateKey = process.env.PRIVATE_KEY;

const provider = new ethers.providers.JsonRpcProvider("http://127.0.0.1:8545");

const wallet = new ethers.Wallet(privateKey, provider);

console.log("测试钱包地址:", wallet.address);
const contractInfo = require("../worlds.json");
const contractAddress = contractInfo["31337"].address;
const contractABI = require("../out/IWorld.sol/IWorld.abi.json");

console.log("测试合约地址:",contractAddress)
const contract = new ethers.Contract(contractAddress, contractABI, wallet);

const { buildABI, getContract,wallet } = require('./tools.js');

async function main() {
await buildABI();
const r = await contract.ping2();
let contract = await getContract();
console.log(contract.functions)
await contract.joinBattlefield();
const r = await contract.getPosition(wallet.address);
console.log("测试合约交互,ping():",r);
}

Expand Down
45 changes: 45 additions & 0 deletions packages/contracts/jsTest/tools.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
const { ethers } = require("ethers");
require("dotenv").config();
const { exec } = require("child_process");

const privateKey = process.env.PRIVATE_KEY;

const provider = new ethers.providers.JsonRpcProvider("http://127.0.0.1:8545");

const wallet = new ethers.Wallet(privateKey, provider);
async function buildABI() {
console.log("正在编译合约...")
return new Promise((resolve, reject) => {
exec("pnpm run build:abi", (error, stdout, stderr) => {
if (error) {
console.error(`执行命令时出错: ${error.message}`);
reject(error);
}
if (stderr) {
console.error(`命令输出错误: ${stderr}`);
reject(stderr);
}
// console.log(`命令输出: ${stdout}`);
resolve(stdout);
});
});
}
async function getContract(){


console.log("测试钱包地址:", wallet.address);
const contractInfo = require("../worlds.json");
const contractAddress = contractInfo["31337"].address;
const contractABI = require("../out/IWorld.sol/IWorld.abi.json");

console.log("测试合约地址:",contractAddress)
const contract = new ethers.Contract(contractAddress, contractABI, wallet);
return contract;
}


module.exports = {
getContract,
buildABI,
wallet
};
3 changes: 2 additions & 1 deletion packages/contracts/src/systems/BattlePrepareSystem.sol
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,9 @@ contract BattlePrepareSystem is System {
event AttackStart(address player, address target);
event BattleConfirmed(uint256 battleId, address sender, bytes32 buffHash);

function joinBattlefield(address _player) public {
function joinBattlefield() public {
// 加入战区,用户实际上是送到原点,状态改为探索中
address _player = msg.sender;
PlayerState playerState = Player.getState(_player);
require(playerState == PlayerState.Preparing || playerState == PlayerState.Idle, "You should in preparing state");
//实际上是送到原点//TODO通过常数设置原点参数
Expand Down
2 changes: 1 addition & 1 deletion packages/contracts/src/systems/PlayerSystem.sol
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ contract PlayerSystem is System {
User user = User(userAddress);
return user.getStructInfo(tokenId);
}
function ping2() public pure returns (string memory) {
function ping() public pure returns (string memory) {
return "pong";
}
}
2 changes: 1 addition & 1 deletion packages/contracts/worlds.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
"blockNumber": 27950347
},
"31337": {
"address": "0xe044814c9eD1e6442Af956a817c161192cBaE98F"
"address": "0x1757a98c1333B9dc8D408b194B2279b5AFDF70Cc"
},
"421613": {
"address": "0x2Bc1034975c3df48D6f3026802f372677844b85d",
Expand Down

0 comments on commit b5befc6

Please sign in to comment.