diff --git a/packages/contracts/mud.config.ts b/packages/contracts/mud.config.ts index 705c919f..028ba298 100644 --- a/packages/contracts/mud.config.ts +++ b/packages/contracts/mud.config.ts @@ -29,6 +29,7 @@ export default mudConfig({ PlayerState: ["Idle", "Preparing", "Exploring", "Attacking"], ActionType: ["Attack", "Escape", "Props"], Buff: ["None", "Fire", "Water", "Wind"], + RandomState:["Inited","Pending","Confirmed"] }, tables: { Season: { diff --git a/packages/contracts/src/systems/BattleSystem.sol b/packages/contracts/src/systems/BattleSystem.sol index 711f7e45..6a3fba1f 100644 --- a/packages/contracts/src/systems/BattleSystem.sol +++ b/packages/contracts/src/systems/BattleSystem.sol @@ -9,23 +9,21 @@ import { BattleUtils } from "./library/BattleUtils.sol"; import { GAME_CONFIG_KEY, BATTLE_CONFIG_KEY } from "../Constants.sol"; contract BattleSystem is System { - // modifier onlyBattlePlayer(uint256 _battleId, BattleState _battleState) { - // BattleListData memory battle = BattleList.get(_battleId); - // BattleState battleState = battle.attacker == _msgSender() - // ? battle.attackerState - // : battle.defenderState; - - // require( - // battle.attacker == _msgSender() || battle.defender == _msgSender(), - // "You are not in this battle" - // ); - // require(battleState == _battleState, "You are in the wrong state"); - - // require(!battle.isEnd, "Battle is end"); - - // _; - // } + function joinBattlefield(address _user) public { + // 加入战区,用户实际上是送到原点,状态改为探索中 + // User storage player = Player[_user]; + PlayerState playerState = Player.getState(_user); + require( + playerState == PlayerState.Preparing || playerState == PlayerState.Idle, + "You should in preparing state" + ); + //实际上是送到原点//TODO通过常数设置原点参数 + Player.setX(_user, GameConfig.getOriginX(GAME_CONFIG_KEY)); + Player.setY(_user, GameConfig.getOriginY(GAME_CONFIG_KEY)); + BattleConfig.pushBattlefieldPlayers(BATTLE_CONFIG_KEY, _user); + Player.setState(_user, PlayerState.Exploring); + } function checkBattlePlayer(BattleListData memory battle, BattleState _battleState) internal view { // BattleListData memory battle = BattleList.get(_battleId); diff --git a/packages/contracts/src/systems/BoxSystem.sol b/packages/contracts/src/systems/BoxSystem.sol new file mode 100644 index 00000000..77ae0e3b --- /dev/null +++ b/packages/contracts/src/systems/BoxSystem.sol @@ -0,0 +1,68 @@ +// SPDX-License-Identifier: MIT +pragma solidity >=0.8.0; + +import { System } from "@latticexyz/world/src/System.sol"; +import { BattleState, Buff, PlayerState } from "../codegen/Types.sol"; +import { GameConfig, BattleConfig, RandomList, RandomListData, BoxList, BoxListData, Player, PlayerData, PlayerLocationLock} from "../codegen/Tables.sol"; +import { GAME_CONFIG_KEY, BATTLE_CONFIG_KEY } from "../Constants.sol"; +import { CommonUtils } from "./library/CommonUtils.sol"; + + +contract BoxSystem is System { + + + function creatBox(uint16 _x, uint16 _y) internal { + uint256 roomId = GameConfig.getRoomId(GAME_CONFIG_KEY); + uint256 boxId = GameConfig.getBoxId(GAME_CONFIG_KEY); + 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); + } + + function openBox(uint256 _boxId) external { + // 宝箱打开时init内容物,根据自带randomId来实现随机 + 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 = getRandom(_box.randomId, 2); + uint8 oreBalance = CommonUtils.dice(randomNumberList[0],20,10,1); + uint8 treasureBalance = CommonUtils.dice(randomNumberList[1],80,10,1); + BoxList.setOreBalance(roomId, boxId, oreBalance); + BoxList.setTreasureBalance(roomId, boxId, treasureBalance); + BoxList.setOpened(roomId, boxId, true); + BoxList.setOpenTime(roomId, boxId, block.timestamp); + } + + 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"); + 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 == true, "Box is not opened"); + if(block.timestamp<_box.openTime + BattleConfig.getMaxBoxBindTime(BATTLE_CONFIG_KEY)){ + require(msg.sender==_box.owner,"The box is waiting for its opener, please wait"); + } + require(_oreAmount<=_box.oreBalance&&_treasureAmount<=_box.treasureBalance,"Invalid amount"); + BoxList.setOreBalance(roomId, boxId, _box.oreBalance - _oreAmount); + BoxList.setTreasureBalance(roomId, boxId, _box.treasureBalance - _treasureAmount); + Player.setOreBalance(_box.owner, _user.oreBalance + _oreAmount); + Player.setTreasureBalance(_box.owner, _user.treasureBalance + _treasureAmount); + } + +} \ No newline at end of file diff --git a/packages/contracts/src/systems/GameSystem.sol b/packages/contracts/src/systems/GameSystem.sol index 4eb8cdcb..6a1e024c 100644 --- a/packages/contracts/src/systems/GameSystem.sol +++ b/packages/contracts/src/systems/GameSystem.sol @@ -15,16 +15,7 @@ contract GameSystem is System { event MoveEvent(address indexed player, uint16 x, uint16 y); event AttackStart(address player, address target); - // constructor(bytes32 root) { - // GameConfig.setMerkleRoot(GAME_CONFIG_KEY, root); - // GameConfig.setMaxAttackzDistance(GAME_CONFIG_KEY, 10); - // GameConfig.setMaxMoveDistance(GAME_CONFIG_KEY, 15); - // GameConfig.setMaxTimeLimit(GAME_CONFIG_KEY, 120); - // GameConfig.setMaxUserLocationLockTime(GAME_CONFIG_KEY, 120); - // GameConfig.setOriginX(GAME_CONFIG_KEY, 100); - // GameConfig.setOriginY(GAME_CONFIG_KEY, 100); - // GameConfig.setMaxBoxBindTime(GAME_CONFIG_KEY, 120); - // } + modifier CheckContinuity(Move[] memory moveList) { // 验证行走轨迹合法且连续 diff --git a/packages/contracts/src/systems/MoveSystem.sol b/packages/contracts/src/systems/MoveSystem.sol new file mode 100644 index 00000000..aee7c620 --- /dev/null +++ b/packages/contracts/src/systems/MoveSystem.sol @@ -0,0 +1,32 @@ +// SPDX-License-Identifier: MIT +pragma solidity >=0.8.0; + +import { System } from "@latticexyz/world/src/System.sol"; +import { BattleState, Buff, PlayerState } from "../codegen/Types.sol"; +import { GameConfig, BattleConfig, RandomList, RandomListData, BoxList, BoxListData, Player, PlayerData, PlayerLocationLock} from "../codegen/Tables.sol"; +import { GAME_CONFIG_KEY, BATTLE_CONFIG_KEY } from "../Constants.sol"; +import { CommonUtils } from "./library/CommonUtils.sol"; + + +contract MoveSystem is System { + function unlockUserLocation() external { + // 用户自行解锁 + require(PlayerLocationLock.get(_msgSender()) != 0, "You are not locked"); + require( + PlayerLocationLock.get(_msgSender()) + BattleConfig.getMaxUserLocationLockTime(BATTLE_CONFIG_KEY) < + block.timestamp, + "You are not locked" + ); + PlayerLocationLock.set(_msgSender(), 0); + } + + + function transfer(uint16 x, uint16 y) external { + //传送门,将用户在战区和非战区移动 + // 将用户坐标随机转移到指定位置 + Player.setX(_msgSender(), x); + Player.setY(_msgSender(), y); + } + + +} \ No newline at end of file diff --git a/packages/contracts/src/systems/RandomSystem.sol b/packages/contracts/src/systems/RandomSystem.sol index 21b7208f..bc86f7b8 100644 --- a/packages/contracts/src/systems/RandomSystem.sol +++ b/packages/contracts/src/systems/RandomSystem.sol @@ -9,6 +9,7 @@ import { CommonUtils } from "./library/CommonUtils.sol"; contract RandomSystem is System { + event NewRandom(uint256 randomId, address author); function raiseUserHP( @@ -19,38 +20,7 @@ contract RandomSystem is System { Player.setHP(_user, (_targetHP * _percent) / 100); } - function unlockUserLocation() external { - // 用户自行解锁 - require(PlayerLocationLock.get(_msgSender()) != 0, "You are not locked"); - require( - PlayerLocationLock.get(_msgSender()) + BattleConfig.getMaxUserLocationLockTime(BATTLE_CONFIG_KEY) < - block.timestamp, - "You are not locked" - ); - PlayerLocationLock.set(_msgSender(), 0); - } - - function transfer(uint16 x, uint16 y) external { - //传送门,将用户在战区和非战区移动 - // 将用户坐标随机转移到指定位置 - Player.setX(_msgSender(), x); - Player.setY(_msgSender(), y); - } - - function joinBattlefield(address _user) public { - // 加入战区,用户实际上是送到原点,状态改为探索中 - // User storage player = Player[_user]; - PlayerState playerState = Player.getState(_user); - require( - playerState == PlayerState.Preparing || playerState == PlayerState.Idle, - "You should in preparing state" - ); - //实际上是送到原点//TODO通过常数设置原点参数 - Player.setX(_user, GameConfig.getOriginX(GAME_CONFIG_KEY)); - Player.setY(_user, GameConfig.getOriginY(GAME_CONFIG_KEY)); - BattleConfig.pushBattlefieldPlayers(BATTLE_CONFIG_KEY, _user); - Player.setState(_user, PlayerState.Exploring); - } + function getRandom( uint256 _randomId, @@ -87,59 +57,6 @@ contract RandomSystem is System { emit NewRandom(randomId, _msgSender()); } - function creatBox(uint16 _x, uint16 _y) internal { - uint256 roomId = GameConfig.getRoomId(GAME_CONFIG_KEY); - uint256 boxId = GameConfig.getBoxId(GAME_CONFIG_KEY); - 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); - } - - function openBox(uint256 _boxId) external { - // 宝箱打开时init内容物,根据自带randomId来实现随机 - 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 = getRandom(_box.randomId, 2); - uint8 oreBalance = CommonUtils.dice(randomNumberList[0],20,10,1); - uint8 treasureBalance = CommonUtils.dice(randomNumberList[1],80,10,1); - BoxList.setOreBalance(roomId, boxId, oreBalance); - BoxList.setTreasureBalance(roomId, boxId, treasureBalance); - BoxList.setOpened(roomId, boxId, true); - BoxList.setOpenTime(roomId, boxId, block.timestamp); - } - - 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"); - 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 == true, "Box is not opened"); - if(block.timestamp<_box.openTime + BattleConfig.getMaxBoxBindTime(BATTLE_CONFIG_KEY)){ - require(msg.sender==_box.owner,"The box is waiting for its opener, please wait"); - } - require(_oreAmount<=_box.oreBalance&&_treasureAmount<=_box.treasureBalance,"Invalid amount"); - BoxList.setOreBalance(roomId, boxId, _box.oreBalance - _oreAmount); - BoxList.setTreasureBalance(roomId, boxId, _box.treasureBalance - _treasureAmount); - Player.setOreBalance(_box.owner, _user.oreBalance + _oreAmount); - Player.setTreasureBalance(_box.owner, _user.treasureBalance + _treasureAmount); - } } \ No newline at end of file diff --git a/packages/contracts/src/systems/library/MRandom.sol b/packages/contracts/src/systems/library/MRandom.sol index a76fac7b..0465bee7 100644 --- a/packages/contracts/src/systems/library/MRandom.sol +++ b/packages/contracts/src/systems/library/MRandom.sol @@ -2,11 +2,7 @@ pragma solidity >=0.8.0; contract MRandom { - enum RandomState { - Inited, - Pending, - Confirmed - } + struct Random { uint256 blockNumber; address author;