Skip to content

Commit

Permalink
update: add battle section
Browse files Browse the repository at this point in the history
  • Loading branch information
TiyoSheng committed Oct 17, 2023
1 parent 4c88e9d commit 9a98ac6
Show file tree
Hide file tree
Showing 5 changed files with 84 additions and 7 deletions.
2 changes: 1 addition & 1 deletion packages/client/.env
Original file line number Diff line number Diff line change
@@ -1 +1 @@
VITE_CHAIN_ID=4242
VITE_CHAIN_ID=31337
45 changes: 44 additions & 1 deletion packages/client/src/pages/test/index.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import { Has, getComponentValue } from '@latticexyz/recs';
import { useLocation } from 'react-router-dom';
import { useMUD } from '@/mud/MUDContext';
import { main } from '../../utils/createMerkelTree';
import { ethers } from 'ethers';
import './index.scss';

const Test = () => {
Expand All @@ -13,16 +14,32 @@ const Test = () => {
const [battleData, setBattleData] = useState([]);

const {
components: { Player, GameConfig },
components: { Player, GameConfig, BattleList },
systemCalls: { move, joinBattlefield, transfer, battleInvitation },
network
} = useMUD();

const { account } = network;
console.log(network, 'account')

// PRIVATE_KEY=0xac0974bec39a17e36ba4a6b4d238ff944bacb478cbed5efcae784d7bf4f2ff80 转账
// let PRIVATE_KEY = '0xac0974bec39a17e36ba4a6b4d238ff944bacb478cbed5efcae784d7bf4f2ff80'
// let provider = new ethers.providers.JsonRpcProvider('http://127.0.0.1:8545')
// let wallet = new ethers.Wallet(PRIVATE_KEY, provider)
// console.log(wallet, 'wallet')
// // 转账到0x6B5A3EF0cEdDE6f8266eCcb7971a6dbdE9D93D44
// wallet.sendTransaction({
// to: '0x6B5A3EF0cEdDE6f8266eCcb7971a6dbdE9D93D44',
// value: ethers.utils.parseEther('1')
// }).then(res => {
// console.log(res, 'res')
// })

const GameData = useEntityQuery([Has(GameConfig)]).map((entity) => getComponentValue(GameConfig, entity));
console.log(GameData, 'GameData')

const Battles = useEntityQuery([Has(BattleList)]).map((entity) => getComponentValue(BattleList, entity));
console.log(Battles, 'Battles')
// const GameConfig = useComponentValue(GameConfig, singletonEntity);
// console.log(GameConfig, 'GameConfig')
const players = useEntityQuery([Has(Player)]).map((entity) => {
Expand Down Expand Up @@ -118,6 +135,32 @@ const Test = () => {
}

</div>
{
Battles.map((item, index) => (<div key={index} className='battle-item'>
<h6>战场信息</h6>
<div className='info-w'>
<div className='battle-section'>
<p>攻击者信息</p>
<div>地址: {item?.attacker}</div>
<div>Action: {item?.attackerAction}</div>
<div>Arg: {item?.attackerArg}</div>
<div>BuffHash: {item?.attackerBuffHash}</div>
<div>HP: {item?.attackerHP}</div>
<div>State: {item?.attackerState}</div>
</div>
<div className='battle-section'>
<p>被攻击者信息</p>
<div>地址: {item?.defender}</div>
<div>Action: {item?.defenderAction}</div>
<div>Arg: {item?.defenderArg}</div>
<div>BuffHash: {item?.defenderBuffHash}</div>
<div>HP: {item?.defenderHP}</div>
<div>State: {item?.defenderState}</div>
</div>
</div>

</div>))
}
<div className="main">
<div className="section">
<div className="title">加入游戏</div>
Expand Down
27 changes: 27 additions & 0 deletions packages/client/src/pages/test/index.scss
Original file line number Diff line number Diff line change
Expand Up @@ -75,4 +75,31 @@
font-size: 12px;
cursor: pointer;
}
}

.battle-item {
margin-top: 24px;
border: 1px solid #f9f9fa;
border-radius: 10px;
padding: 12px;
box-sizing: border-box;
.info-w {
display: flex;
align-items: center;
justify-content: space-between;
margin-top: 12px;
}
.battle-section {
font-size: 12px;
p {
font-weight: 600;
}
div {
margin-top: 8px;
}
}
}

h6 {
font-size: 14px;
}
13 changes: 10 additions & 3 deletions packages/client/src/utils/createMerkelTree.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { MerkleTree } from "merkletreejs";
import { solidityKeccak256, keccak256 } from 'ethers/lib/utils';
import { Buffer } from "buffer";
import { bfs } from './map'

let map_info = [];
let line = Array.from({ length: 100 }, () => 1);
Expand Down Expand Up @@ -56,12 +57,18 @@ const merkleTree = new MerkleTree(
keccak256,
{ sortPairs: true }
);
export const main = (steps) => {
export const main = (from, to) => {
let root = merkleTree.getHexRoot();
console.log("Map Merkle Tree Root:", root);
// 这里可以直接手写走的节点,注意不能包含开始点,且每一步走的地方都必须是可以移动端
console.log("总移动步数", steps.length);
let steps_list = move(steps);
let steps = bfs(map_info, from, to);
// 删除第一个
steps.shift();
let steps_arr = steps.map((item) => {
return [item.x, item.y];
})
console.log("总移动步数", steps_arr.length);
let steps_list = move(steps_arr);
console.log("生成的传入文件", steps_list);
return steps_list
}
Expand Down
4 changes: 2 additions & 2 deletions packages/contracts/worlds.json
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
{
"4242": {
"address": "0x2A53D186317194340Aa32a6128A0C88e1a37F138",
"address": "0x480f891A7b1AA7018Bb0cCb5Af558d82FA4A5501",
"blockNumber": 27830588
},
"31337": {
"address": "0x7969c5eD335650692Bc04293B07F5BF2e7A673C0"
"address": "0x5FbDB2315678afecb367f032d93F642f64180aa3"
},
"421613": {
"address": "0x2Bc1034975c3df48D6f3026802f372677844b85d",
Expand Down

0 comments on commit 9a98ac6

Please sign in to comment.