Skip to content

Commit

Permalink
refactor: modify system call
Browse files Browse the repository at this point in the history
  • Loading branch information
noyyyy committed Oct 19, 2023
1 parent e73e3ed commit 72c9153
Show file tree
Hide file tree
Showing 10 changed files with 58 additions and 28 deletions.
1 change: 1 addition & 0 deletions packages/contracts/foundry.toml
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ allow_paths = [
extra_output_files = ["abi", "evm.bytecode"]
fs_permissions = [{ access = "read", path = "./" }]
defaultGasLimit = 6000000
lib = ["node_modules", "lib"]

[profile.lattice-testnet]
eth_rpc_url = "https://follower.testnet-chain.linfra.xyz"
Expand Down
15 changes: 10 additions & 5 deletions packages/contracts/src/systems/AutoBattleSystem.sol
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ pragma solidity >=0.8.0;

import "forge-std/Test.sol";
import {System} from "@latticexyz/world/src/System.sol";
import {IWorld} from "../codegen/world/IWorld.sol";
import {IWorld} from "src/codegen/world/IWorld.sol";
import {Creature, CreatureData, GameConfig} from "../codegen/index.sol";
import {Board, BoardData} from "../codegen/index.sol";
import {Hero, HeroData} from "../codegen/index.sol";
Expand All @@ -15,20 +15,22 @@ import {GameStatus, BoardStatus, PlayerStatus} from "src/codegen/common.sol";
import {Coordinate as Coord} from "cement/utils/Coordinate.sol";
import {RTPiece} from "../library/RunTimePiece.sol";
import {Utils} from "../library/Utils.sol";
import {SystemSwitch} from "@latticexyz/world-modules/src/utils/SystemSwitch.sol";

contract AutoBattleSystem is System {
function tick(uint32 _gameId, address _player) public {
bool isSinglePlay = Game.getSingle(_gameId);

if (isSinglePlay) {
IWorld(_world()).pveTick(_gameId, _player);
SystemSwitch.call(abi.encodeCall(IWorld(_world()).pveTick, (_gameId, _player)));
} else {
// the first tick for every board would be initializing pieces from heroes
if (beforeTurn(_gameId, _player)) {
return;
}

(uint8 winner, uint256 damageTaken) = IWorld(_world()).startBattle(_player);
(uint8 winner, uint256 damageTaken) =
abi.decode(SystemSwitch.call(abi.encodeCall(IWorld(_world()).startBattle, (_player))), (uint8, uint256));

endTurn(_gameId, _player, winner, damageTaken);
}
Expand Down Expand Up @@ -121,7 +123,9 @@ contract AutoBattleSystem is System {
}

function _initPieceOnBoard(address _player, address _opponent) internal {
(bytes32[] memory allies, bytes32[] memory enemies) = IWorld(_world()).initPieces(_player, _opponent);
(bytes32[] memory allies, bytes32[] memory enemies) = abi.decode(
SystemSwitch.call(abi.encodeCall(IWorld(_world()).initPieces, (_player, _opponent))), (bytes32[], bytes32[])
);
Board.set(
_player,
BoardData({enemy: _opponent, status: BoardStatus.INBATTLE, turn: 0, pieces: allies, enemyPieces: enemies})
Expand Down Expand Up @@ -198,6 +202,7 @@ contract AutoBattleSystem is System {
Game.setStatus(_gameId, GameStatus.PREPARING);
uint32 roundInterval = GameConfig.getRoundInterval(0);
Game.setStartFrom(_gameId, uint32(block.timestamp) + roundInterval);
IWorld(_world()).settleRound(_gameId);

SystemSwitch.call(abi.encodeCall(IWorld(_world()).settleRound, (_gameId)));
}
}
14 changes: 11 additions & 3 deletions packages/contracts/src/systems/MatchingSystem.sol
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import {
import {PlayerGlobalData, WaitingRoomData} from "../codegen/index.sol";
import {PlayerStatus, GameStatus, BoardStatus} from "src/codegen/common.sol";
import {Utils} from "../library/Utils.sol";
import {SystemSwitch} from "@latticexyz/world-modules/src/utils/SystemSwitch.sol";

contract MatchingSystem is System {
function createRoom(bytes32 _roomId, uint8 _seatNum, bytes32 _passwordHash) public {
Expand Down Expand Up @@ -77,7 +78,14 @@ contract MatchingSystem is System {
bytes32 passwordHash = WaitingRoomPassword.get(_roomId);
uint256[3] memory pubSignals =
[uint256(passwordHash) >> 128, uint128(uint256(passwordHash)), uint256(uint160(player))];
require(IWorld(_world()).verifyPasswordProof(_pA, _pB, _pC, pubSignals), "invalid password proof");

require(
abi.decode(
SystemSwitch.call(abi.encodeCall(IWorld(_world()).verifyPasswordProof, (_pA, _pB, _pC, pubSignals))),
(bool)
),
"invalid password proof"
);

_enterRoom(player, _roomId);
}
Expand Down Expand Up @@ -174,7 +182,7 @@ contract MatchingSystem is System {
Player.setInventory(_bot, inventory);

// init round 0 for each player
IWorld(_world()).settleRound(gameIndex);
SystemSwitch.call(abi.encodeCall(IWorld(_world()).settleRound, (gameIndex)));
}

function _startGame(address[] memory _players) private {
Expand Down Expand Up @@ -210,6 +218,6 @@ contract MatchingSystem is System {
}

// init round 0 for each player
IWorld(_world()).settleRound(gameIndex);
SystemSwitch.call(abi.encodeCall(IWorld(_world()).settleRound, (gameIndex)));
}
}
1 change: 0 additions & 1 deletion packages/contracts/src/systems/MergeSystem.sol
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ contract MergeSystem is System {
uint8 public constant mergeNum = 3;

function merge(address _player, uint256 _hero) public returns (bool merged, uint256 mergedHero) {
IWorld world = IWorld(_world());
// tier max = 2
if (Utils.getHeroTier(_hero) > 1) {
return (false, _hero);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ contract PieceDecisionMake2System is System {

uint32 private constant ATTACK_MODE_KILL_FIRST = (100 << 16) + 100;

function exploreAttack(RTPiece[] memory _pieces, uint256 _index) public returns (uint256 action) {
function exploreAttack(RTPiece[] memory _pieces, uint256 _index) public view returns (uint256 action) {
uint256 length = _pieces.length;
PriorityQueue memory pq = PQ.New(length);
RTPiece memory attacker = _pieces[_index];
Expand Down
17 changes: 12 additions & 5 deletions packages/contracts/src/systems/PieceDecisionMakeSystem.sol
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ pragma solidity >=0.8.0;

import "forge-std/Test.sol";
import {System} from "@latticexyz/world/src/System.sol";
import {SystemSwitch} from "@latticexyz/world-modules/src/utils/SystemSwitch.sol";

import {IWorld} from "../codegen/world/IWorld.sol";
import {GameConfig} from "../codegen/index.sol";
import {Player, Board, Creature, Hero, Piece} from "../codegen/index.sol";
Expand All @@ -29,22 +31,27 @@ contract PieceDecisionMakeSystem is System {
}

// generate map
uint8[][] memory map = IWorld(_world())._genMap(pieces);
uint8[][] memory map =
abi.decode(SystemSwitch.call(abi.encodeCall(IWorld(_world())._genMap, (pieces))), (uint8[][]));

// init simulator
(pieces, cache) = IWorld(_world()).initSimulator(pieces, cache);
(pieces, cache) = abi.decode(
SystemSwitch.call(abi.encodeCall(IWorld(_world()).initSimulator, (pieces, cache))), (RTPiece[], EffectCache)
);

for (uint256 i; i < num; ++i) {
uint256 action = decide(pieces, map, i);
(pieces, map, cache) = IWorld(_world()).doAction(pieces, map, cache, action);
}

// close simulator
pieces = IWorld(_world()).closeSimulator(pieces, cache);
pieces =
abi.decode(SystemSwitch.call(abi.encodeCall(IWorld(_world()).closeSimulator, (pieces, cache))), (RTPiece[]));

// end turn, update pieces
IWorld(_world())._updatePieces(pieces);
(winner, damageTaken) = IWorld(_world())._getWinner(pieces);
SystemSwitch.call(abi.encodeCall(IWorld(_world())._updatePieces, (pieces)));
(winner, damageTaken) =
abi.decode(SystemSwitch.call(abi.encodeCall(IWorld(_world())._getWinner, (pieces))), (uint8, uint256));
}

function decide(RTPiece[] memory _pieces, uint8[][] memory _map, uint256 _index)
Expand Down
3 changes: 2 additions & 1 deletion packages/contracts/src/systems/PveBotSystem.sol
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ pragma solidity >=0.8.0;

import "forge-std/Test.sol";
import {System} from "@latticexyz/world/src/System.sol";
import {SystemSwitch} from "@latticexyz/world-modules/src/utils/SystemSwitch.sol";
import {IWorld} from "../codegen/world/IWorld.sol";
import {Creature, CreatureData, GameConfig, ShopConfig, Rank} from "../codegen/index.sol";
import {Board, BoardData} from "../codegen/index.sol";
Expand Down Expand Up @@ -36,7 +37,7 @@ contract PveBotSystem is System {

bytes32 pieceKey = _getHeroIdx(bot);

IWorld(_world()).refreshHeroes(bot);
SystemSwitch.call(abi.encodeCall(IWorld(_world()).refreshHeroes, (bot)));

uint24 creatureId = Player.getItemHeroAltar(bot, r % 5);
r >>= 8;
Expand Down
14 changes: 10 additions & 4 deletions packages/contracts/src/systems/PveSystem.sol
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ pragma solidity >=0.8.0;

import "forge-std/Test.sol";
import {System} from "@latticexyz/world/src/System.sol";
import {SystemSwitch} from "@latticexyz/world-modules/src/utils/SystemSwitch.sol";

import {IWorld} from "../codegen/world/IWorld.sol";
import {Creature, CreatureData, GameConfig, ShopConfig, Rank} from "../codegen/index.sol";
import {Board, BoardData} from "../codegen/index.sol";
Expand All @@ -21,7 +23,8 @@ contract PveSystem is System {
return;
}

(uint8 winner, uint256 damageTaken) = IWorld(_world()).startBattle(_player);
(uint8 winner, uint256 damageTaken) =
abi.decode(SystemSwitch.call(abi.encodeCall(IWorld(_world()).startBattle, (_player))), (uint8, uint256));

endTurnForSinglePlayer(_gameId, _player, winner, damageTaken);
}
Expand All @@ -40,7 +43,7 @@ contract PveSystem is System {
BoardStatus boardStatus = Board.getStatus(_player);

if (boardStatus == BoardStatus.UNINITIATED) {
IWorld(_world())._botSetPiece(_gameId, _player);
SystemSwitch.call(abi.encodeCall(IWorld(_world())._botSetPiece, (_gameId, _player)));
_initPieceOnBoardBot(_player);
Game.setStatus(_gameId, GameStatus.INBATTLE);
firstTurn = true;
Expand All @@ -53,13 +56,16 @@ contract PveSystem is System {
Board.setTurn(_player, Board.getTurn(_player) + 1);
} else {
_updateWhenBoardFinished(_gameId, _player, _winner, _damageTaken);
IWorld(_world()).endRoundPublic(_gameId);

SystemSwitch.call(abi.encodeCall(IWorld(_world()).endRoundPublic, (_gameId)));
}
}

function _initPieceOnBoardBot(address _player) internal {
address bot = Utils.getBotAddress(_player);
(bytes32[] memory allies, bytes32[] memory enemies) = IWorld(_world()).initPieces(_player, bot);
(bytes32[] memory allies, bytes32[] memory enemies) = abi.decode(
SystemSwitch.call(abi.encodeCall(IWorld(_world()).initPieces, (_player, bot))), (bytes32[], bytes32[])
);

Board.set(
_player,
Expand Down
10 changes: 5 additions & 5 deletions packages/contracts/src/systems/RoundSettlementSystem.sol
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
pragma solidity >=0.8.0;

import {System} from "@latticexyz/world/src/System.sol";

import {SystemSwitch} from "@latticexyz/world-modules/src/utils/SystemSwitch.sol";
import {IWorld} from "src/codegen/world/IWorld.sol";

import {Game, Player} from "src/codegen/index.sol";
Expand Down Expand Up @@ -33,21 +33,21 @@ contract RoundSettlementSystem is System {
// add exp except in first round
if (Game.getRound(gameId) != 1) {
// add experience
IWorld(_world()).addExperience(player, 1);
SystemSwitch.call(abi.encodeCall(IWorld(_world()).addExperience, (player, 1)));
}

// add coin
IWorld(_world()).updatePlayerCoin(gameId, player);
SystemSwitch.call(abi.encodeCall(IWorld(_world()).updatePlayerCoin, (gameId, player)));

// refresh heros
if (Player.getLocked(player)) {
Player.setLocked(player, false);
} else {
IWorld(_world()).refreshHeroes(player);
SystemSwitch.call(abi.encodeCall(IWorld(_world()).refreshHeroes, (player)));
}
}

function _shufflePlayers(uint32 _gameId, address[] memory _players) internal {
function _shufflePlayers(uint32 _gameId, address[] memory _players) internal view {
uint256 r = IWorld(_world()).getRandomNumberInGame(_gameId);
uint256 length = _players.length;
PriorityQueue memory pq = PQ.New(length);
Expand Down
9 changes: 6 additions & 3 deletions packages/contracts/src/systems/ShopSystem.sol
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
pragma solidity >=0.8.0;

import {System} from "@latticexyz/world/src/System.sol";
import {SystemSwitch} from "@latticexyz/world-modules/src/utils/SystemSwitch.sol";

import {IWorld} from "src/codegen/world/IWorld.sol";
import {PlayerGlobal, Player, Game, GameConfig, ShopConfig} from "src/codegen/index.sol";
import {PlayerStatus} from "src/codegen/common.sol";
Expand Down Expand Up @@ -38,7 +40,7 @@ contract ShopSystem is System {
Player.setCoin(player, Player.getCoin(player) - ShopConfig.getRefreshPrice(0));

// refersh heros
IWorld(_world()).refreshHeroes(player);
SystemSwitch.call(abi.encodeCall(IWorld(_world()).refreshHeroes, (player)));
}

/**
Expand Down Expand Up @@ -100,12 +102,13 @@ contract ShopSystem is System {

// increase exp
// fix exp with 4
IWorld(_world()).addExperience(player, 4);
SystemSwitch.call(abi.encodeCall(IWorld(_world()).addExperience, (player, 4)));
}

function _recruitAnHero(address _player, uint256 _hero) internal returns (uint256 hero) {
bool merged;
(merged, hero) = IWorld(_world()).merge(_player, _hero);
(merged, hero) =
abi.decode(SystemSwitch.call(abi.encodeCall(IWorld(_world()).merge, (_player, _hero))), (bool, uint256));

if (merged) {
return _recruitAnHero(_player, hero);
Expand Down

0 comments on commit 72c9153

Please sign in to comment.