Skip to content

Commit

Permalink
feat: Resolve code conflicts
Browse files Browse the repository at this point in the history
  • Loading branch information
Vivomo committed Oct 16, 2023
2 parents 51b2ae2 + 079eab4 commit ec43fb2
Show file tree
Hide file tree
Showing 14 changed files with 2,761 additions and 4,812 deletions.
4 changes: 4 additions & 0 deletions packages/client/src/App.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { BrowserRouter, Route, Routes } from 'react-router-dom';
import './App.scss';
import Game from './pages/game';
import Test from './pages/test';

export const App = () => {

Expand All @@ -10,6 +11,9 @@ export const App = () => {
<Routes>
<Route path="/">
<Route index element={<Game />} />
{/*<Route index element={<Home />} />*/}
<Route path="game" element={<Game />} />
<Route path="test" element={<Test />} />
</Route>
</Routes>
</BrowserRouter>
Expand Down
9 changes: 8 additions & 1 deletion packages/client/src/mud/createSystemCalls.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,11 +34,18 @@ export function createSystemCalls(
const result = await waitForTransaction(tx);
console.log(result);
// return getComponentValue(GameSystem, singletonEntity);
await waitForTransaction(tx);
};

const joinBattlefield = async (addr) => {
const tx = await worldContract.write.joinBattlefield([addr]);
await waitForTransaction(tx);
}

return {
increment,
move,
getPosition
getPosition,
joinBattlefield
};
}
1 change: 1 addition & 0 deletions packages/client/src/mud/setupNetwork.ts
Original file line number Diff line number Diff line change
Expand Up @@ -79,5 +79,6 @@ export async function setupNetwork() {
waitForTransaction,
worldContract,
write$: write$.asObservable().pipe(share()),
account: burnerWalletClient.account.address
};
}
76 changes: 76 additions & 0 deletions packages/client/src/pages/test/index.jsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
import React, { useEffect, useRef, useState } from 'react';
import { useComponentValue } from "@latticexyz/react";
import { useLocation } from 'react-router-dom';
import { useMUD } from '@/mud/MUDContext';
import { main } from '../../utils/createMerkelTree';
import './index.scss';

const Test = () => {
const [stepData, setStepData] = useState([]);

const {
components: {Player},
systemCalls: { move, joinBattlefield },
network: {playerEntity, account}
} = useMUD();

const playerData = useComponentValue(Player, playerEntity);
console.log(playerData, 'playerData')
console.log(account, 'account')

const stepXChange = (e) => {
let x = e.target.value
console.log(x)
let y = stepData[1] || 0;
setStepData([x, y]);
}

const stepYChange = (e) => {
let y = e.target.value
let x = stepData[0] || 0;
setStepData([+x, +y]);
}

const movePlayer = () => {
// move(merkelData);
console.log(stepData, 'move')
let merkelData = main([stepData]);
console.log(merkelData)
move(merkelData);
}

const joinBattlefieldFun = () => {
console.log(account, 'account')
joinBattlefield(account);
}

return (
<div className="content">
<div className="nav">
<h3>测试面板</h3>
<div className="addr">当前用户地址:{account}</div>
</div>
<div className="hd">
<h6>用户信息</h6>
<div style={{marginTop: '8px'}}>坐标:{playerData?.x || 0}{playerData?.y || 0}</div>
</div>
<div className="main">
<div className="section">
<div className="title">加入游戏</div>
<div className="input"></div>
<div className="btn" onClick={joinBattlefieldFun}>确认</div>
</div>
<div className="section">
<div className="title">移动</div>
<div className="input">
<input type="text" onChange={stepXChange} placeholder='x' />
<input type="text" onChange={stepYChange} placeholder='y' />
</div>
<div className="btn" onClick={movePlayer}>确认</div>
</div>
</div>
</div>
)
};

export default Test;
74 changes: 74 additions & 0 deletions packages/client/src/pages/test/index.scss
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
.content {
width: 100vw;
height: 100vh;
overflow-y: auto;
overflow-x: hidden;
padding: 24px;
box-sizing: border-box;
}
.nav {
display: flex;
align-items: center;
justify-content: space-between;
font-size: 14px;
}
.hd {
border: 1px solid #f9f9fa;
padding: 12px;
border-radius: 10px;
margin-top: 24px;
font-size: 12px;
}

.main {
display: flex;
align-items: center;
flex-wrap: wrap;
}

.section {
margin-top: 24px;
border: 1px solid #f9f9fa;
padding: 12px;
border-radius: 10px;
font-size: 12px;
flex: 0 0 200px;
width: 200px;
padding: 12px;
box-sizing: border-box;
margin-right: 24px;
margin-bottom: 24px;
.title {
font-size: 12px;
}
.input {
height: 32px;
margin-top: 12px;
display: flex;
align-items: center;
justify-content: space-between;
input {
border: 1px solid #f9f9fa;
background: none;
width: 48% !important;
height: 32px;
border-radius: 4px;
padding: 0 8px;
box-sizing: border-box;
outline: none;
}
}
.btn {
margin-top: 12px;
width: 100%;
height: 32px;
display: flex;
align-items: center;
justify-content: center;
border-radius: 4px;
background: rgb(166,173,178);
color: #f9f9fa;
font-size: 12px;
cursor: pointer;
}
}
68 changes: 68 additions & 0 deletions packages/client/src/utils/createMerkelTree.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
import { MerkleTree } from "merkletreejs";
import { solidityKeccak256, keccak256 } from 'ethers/lib/utils';
import { Buffer } from "buffer";

let map_info = [];
let line = Array.from({ length: 100 }, () => 1);
for (let i = 0; i < 10; i++) {
map_info.push(line);
}
//在这里替换map_info,格式为[[0,0,0],[1,1,1]]这样的二维数组,需要和前端完全一致


function convertToLeafs(map_info) {
let result = [];
for (let x = 0; x < map_info.length; x++) {
for (let y = 0; y < map_info[x].length; y++) {
result.push({ x, y, value: map_info[x][y] });
}
}
return result;
}

function move(steps) {
console.log(steps, 11)
let steps_list = [];
for (let i = 0; i < steps.length; i++) {
let proof = get_proof(steps[i][0], steps[i][1]);
steps_list.push([steps[i][0], steps[i][1], proof]);
}
console.log("steps_list", steps_list);
return steps_list;
}
let leafs = convertToLeafs(map_info);
console.log("leafs.length", leafs.length);

// 通过本函数将地图初始化为默克尔树节点的字符串数组,每个字符串的格式为"x,y-value"
// 其中value为0或1,表示该位置是否可以走,默认0不可走,1以及以后数字可以约定分别代表不同的可行性
function generateLeaf(x, y, value) {
return Buffer.from(
solidityKeccak256(
["uint16", "string", "uint16", "string", "uint8"],
[x, ",", y, ",", value]
)
.slice(2),
"hex"
);
}

function get_proof(x, y) {
let leaf = generateLeaf(x, y, 1);
const proof = merkleTree.getHexProof(leaf);
return proof;
}
const merkleTree = new MerkleTree(
leafs.map((info) => generateLeaf(info.x, info.y, info.value)),
keccak256,
{ sortPairs: true }
);
export const main = (steps) => {
let root = merkleTree.getHexRoot();
console.log("Map Merkle Tree Root:", root);
// 这里可以直接手写走的节点,注意不能包含开始点,且每一步走的地方都必须是可以移动端
console.log("总移动步数", steps.length);
let steps_list = move(steps);
console.log("生成的传入文件", steps_list);
return steps_list
}

18 changes: 18 additions & 0 deletions packages/contracts/mud.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -166,6 +166,24 @@ export default mudConfig({
opened: "bool",
owner: "address",
}
},
Loot : {
keySchema: {
tokenId: "uint256"
},
schema: {
randomId: "uint256",
owner: "address",
state: "RandomState",
weapon: "string",
chest: "string",
head: "string",
waist: "string",
foot: "string",
hand: "string",
neck: "string",
ring: "string",
}
}
}
});
3 changes: 2 additions & 1 deletion packages/contracts/script/GameConfigInit.sol
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,8 @@ import { GAME_CONFIG_KEY } from "../src/Constants.sol";
library GameConfigInit {
function initGameConfig(IWorld _world) internal {
console.log(" ========= initGameConfig");
bytes32 merkleRoot = 0x5df91eca63323dbb115087ef262075c5bcea99b8eaf95f520efb8d48ff447499;
// bytes32 merkleRoot = 0x5df91eca63323dbb115087ef262075c5bcea99b8eaf95f520efb8d48ff447499;
bytes32 merkleRoot = 0xa969691ad8c2e97e3d516e08f5b10ee4decd5f278a5f03ac4fa3532be181c854;
GameConfig.set(
_world,
GAME_CONFIG_KEY, //key
Expand Down
2 changes: 1 addition & 1 deletion packages/contracts/src/systems/GMSystem.sol
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import { System } from "@latticexyz/world/src/System.sol";
import { Season, GameConfig, BoxList} from "../codegen/Tables.sol";
import { GAME_CONFIG_KEY } from "../Constants.sol";

contract GMSystem {
contract GMSystem is System {
bytes32 constant MAP_KEY = keccak256("Season-Key");

// season
Expand Down
Loading

0 comments on commit ec43fb2

Please sign in to comment.