-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
14 changed files
with
2,761 additions
and
4,812 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.