Skip to content

Commit

Permalink
Merge
Browse files Browse the repository at this point in the history
  • Loading branch information
LidamaoHub committed Oct 20, 2023
2 parents b5befc6 + 9bee347 commit 7bb480c
Show file tree
Hide file tree
Showing 6 changed files with 75 additions and 25 deletions.
Binary file modified packages/contracts/.DS_Store
Binary file not shown.
55 changes: 55 additions & 0 deletions packages/contracts/jsTest/test.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,67 @@
<<<<<<< HEAD
const { buildABI, getContract,wallet } = require('./tools.js');
=======
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");
<<<<<<< HEAD
console.log("contract address :",contractAddress)
=======

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

>>>>>>> 9bee347a72c44e75ef1c155245215d8f2a8d7859

async function main() {
<<<<<<< HEAD
// const r = await contract.ping();
console.log("wallet address: ", wallet.address)
const r = await contract.joinBattlefield(wallet.address);
console.log(" return val: ", r);




// const r2 = await contract.getBattlefieldInfo();
// console.log("测试合约交互,ping():",r);
// const r = await contract
=======
await buildABI();
let contract = await getContract();
console.log(contract.functions)
await contract.joinBattlefield();
const r = await contract.getPosition(wallet.address);
console.log("测试合约交互,ping():",r);
>>>>>>> 69db2f9f2cc9a0b7a1a8ec213b3dd6e89a6e58ad
}


Expand Down
12 changes: 6 additions & 6 deletions packages/contracts/src/systems/BattlePrepareSystem.sol
Original file line number Diff line number Diff line change
Expand Up @@ -16,16 +16,16 @@ contract BattlePrepareSystem is System {

function joinBattlefield() public {
// 加入战区,用户实际上是送到原点,状态改为探索中
address _player = msg.sender;
PlayerState playerState = Player.getState(_player);
address player = _msgSender();
PlayerState playerState = Player.getState(player);
require(playerState == PlayerState.Preparing || playerState == PlayerState.Idle, "You should in preparing state");
//实际上是送到原点//TODO通过常数设置原点参数
// TODO似乎可以直接通过indexer获取,就不需要再次插入了

Player.setX(_player, GameConfig.getOriginX(GAME_CONFIG_KEY));
Player.setY(_player, GameConfig.getOriginY(GAME_CONFIG_KEY));
Player.setState(_player, PlayerState.Exploring);
Player.setHp(_player, initPlayerHp(_player));
Player.setX(player, GameConfig.getOriginX(GAME_CONFIG_KEY));
Player.setY(player, GameConfig.getOriginY(GAME_CONFIG_KEY));
Player.setState(player, PlayerState.Exploring);
Player.setHp(player, initPlayerHp(player));

// GameConfig.pushBattlefieldPlayers(GAME_CONFIG_KEY, _player);
}
Expand Down
28 changes: 13 additions & 15 deletions packages/contracts/src/systems/BoxSystem.sol
Original file line number Diff line number Diff line change
Expand Up @@ -9,28 +9,30 @@ import { CommonUtils } from "@library/CommonUtils.sol";
import { MRandom } from "@library/MRandom.sol";

contract BoxSystem is System {
function creatBox(uint16 _x, uint16 _y) internal {
function openBox(uint256 _boxId) external {
// 宝箱打开时init内容物,根据自带randomId来实现随机
// TODO开宝箱的时候用户定身
uint256 roomId = GameConfig.getRoomId(GAME_CONFIG_KEY);
uint256 boxId = GameConfig.getBoxId(GAME_CONFIG_KEY);
BoxList.setX(roomId, boxId, _x);
BoxList.setY(roomId, boxId, _y);
require(BoxList.getDropTime(roomId, _boxId) != 0, "Invalid box");
BoxListData memory _box = BoxList.get(roomId, boxId);
PlayerData memory _user = Player.get(_box.owner);
require(CommonUtils.isNear(_box.x, _user.x, _box.y, _user.y), "You are not near the box");
require(_box.opened == false, "Box is opened");

uint256 randomId = GameConfig.getRandomId(GAME_CONFIG_KEY);
BoxList.setRandomId(roomId, boxId, randomId);
GameConfig.setRandomId(GAME_CONFIG_KEY, randomId + 1);
GameConfig.setBoxId(GAME_CONFIG_KEY, boxId + 1);


}

function openBox(uint256 _boxId) external {
// 宝箱打开时init内容物,根据自带randomId来实现随机
// TODO开宝箱的时候用户定身
//Todo: add reveal box
function revealBox() external {
uint256 roomId = GameConfig.getRoomId(GAME_CONFIG_KEY);
uint256 boxId = GameConfig.getBoxId(GAME_CONFIG_KEY);
require(BoxList.getDropTime(roomId, _boxId) != 0, "Invalid box");
BoxListData memory _box = BoxList.get(roomId, boxId);
PlayerData memory _user = Player.get(_box.owner);
require(CommonUtils.isNear(_box.x, _user.x, _box.y, _user.y), "You are not near the box");
require(_box.opened == false, "Box is opened");

uint8[] memory randomNumberList = MRandom.getRandom(_box.randomId, 2);
uint8 oreBalance = CommonUtils.dice(randomNumberList[0], 20, 10, 1);
uint8 treasureBalance = CommonUtils.dice(randomNumberList[1], 80, 10, 1);
Expand All @@ -40,12 +42,8 @@ contract BoxSystem is System {
BoxList.setOpenTime(roomId, boxId, block.timestamp);
BoxList.setOpenTime(roomId, boxId, block.timestamp);
}

//Todo: add reveal box


function getCollections(uint256 _boxId, uint16 _oreAmount, uint16 _treasureAmount) internal {

uint256 roomId = GameConfig.getRoomId(GAME_CONFIG_KEY);
uint256 boxId = GameConfig.getBoxId(GAME_CONFIG_KEY);
require(BoxList.getDropTime(roomId, _boxId) != 0, "Invalid box");
Expand Down
3 changes: 0 additions & 3 deletions packages/contracts/src/systems/GMSystem.sol
Original file line number Diff line number Diff line change
Expand Up @@ -41,9 +41,6 @@ contract GMSystem is System {
BoxList.setX(roomId, boxId, _x);
BoxList.setY(roomId, boxId, _y);

uint256 randomId = GameConfig.getRandomId(GAME_CONFIG_KEY);
BoxList.setRandomId(roomId, boxId, randomId);
GameConfig.setRandomId(GAME_CONFIG_KEY, randomId + 1);
GameConfig.setBoxId(GAME_CONFIG_KEY, boxId + 1);
}

Expand Down
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": "0x1757a98c1333B9dc8D408b194B2279b5AFDF70Cc"
"address": "0x9A86494Ba45eE1f9EEed9cFC0894f6C5d13a1F0b"
},
"421613": {
"address": "0x2Bc1034975c3df48D6f3026802f372677844b85d",
Expand Down

0 comments on commit 7bb480c

Please sign in to comment.