Skip to content

Commit

Permalink
Merge remote-tracking branch 'origin/feat-mvp' into feat-mvp
Browse files Browse the repository at this point in the history
  • Loading branch information
Vivomo committed Nov 3, 2023
2 parents 0e962a7 + 7ebe6b3 commit 10345d7
Show file tree
Hide file tree
Showing 10 changed files with 4,793 additions and 4,786 deletions.
20 changes: 14 additions & 6 deletions packages/client/src/components/Battle/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -59,12 +59,17 @@ export default function Battle(props) {

const initBattle = (id: any) => {
let battle:any = battles.filter((item:any) => item.id.toString() == id)[0]
console.log(battle, id)
if (((battle.attackerState == 1 && battle.defenderState == 0) || (battle.attackerState == 0 && battle.defenderState == 1) || (battle.attackerState == 0 && battle.defenderState == 0))) {
if (battle.isEnd && battle.winner) {
clearTimeout(timeout)
props.finishBattle(battle.winner)
return
}
if (((battle.attackerState == 1 && battle.defenderState == 0) || (battle.attackerState == 0 && battle.defenderState == 1))) {
if (!timeout) {
timeout = setTimeout(() => {
forceEnd(battle.id)
}, 120000)
timeout = setTimeout(async () => {
let resultBattle:any = await forceEnd(battle.id)
console.log(resultBattle)
}, 23000)
}

} else if (((battle.attackerState == 1 && battle.defenderState == 1) || (battle.attackerState == 2 && battle.defenderState == 1) || (battle.attackerState == 1 && battle.defenderState == 2)) && battleState <= 1) {
Expand Down Expand Up @@ -201,6 +206,9 @@ export default function Battle(props) {
console.log(hash, battle.id)
setBattleState(1)
let res = await confirmBattle(hash, battle.id);
if (res.type == 'success') {

}
// if (res.type == 'error' && res.msg.indexOf('Battle is timeout') > -1) {
// forceEnd(battle.id)
// return
Expand Down Expand Up @@ -299,7 +307,7 @@ export default function Battle(props) {
</div>
</div>
<div className="action">
<div className="action-hint">Select your action and tactic</div>
<div className="action-hint">{battleState == 0 ? 'Select your action and tactic' : battleState == 1 ? "Waiting for your opponent's move, it may take up to 120 seconds" : ''}</div>
<div
style={{
marginTop: "12px",
Expand Down
3 changes: 1 addition & 2 deletions packages/client/src/components/Battle/styles.scss
Original file line number Diff line number Diff line change
Expand Up @@ -201,10 +201,9 @@
text-align: center;
text-shadow: 4px 4px 4px rgba(0, 0, 0, 0.25);
-webkit-text-stroke: 1px #000;
font-size: 18px;
font-size: 14px;
font-style: normal;
font-weight: 400;
line-height: 43px;
margin-top: -1px;
}

Expand Down
22 changes: 17 additions & 5 deletions packages/client/src/mud/createSystemCalls.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,14 @@ import { ClientComponents } from "./createClientComponents";
import { SetupNetworkResult } from "./setupNetwork";
import { singletonEntity, encodeEntity } from "@latticexyz/store-sync/recs";
import { message } from 'antd';
import { get } from "http";

export type SystemCalls = ReturnType<typeof createSystemCalls>;

export function createSystemCalls(
{ worldContract, waitForTransaction }: SetupNetworkResult,
ClientComponents
) {
const { Counter, Player, LootList1, LootList2, BoxList } = ClientComponents;
const { Counter, Player, LootList1, LootList2, BoxList, BattleList } = ClientComponents;
const increment = async () => {
const tx = await worldContract.write.increment();
await waitForTransaction(tx);
Expand Down Expand Up @@ -72,7 +71,8 @@ export function createSystemCalls(
const tx = await worldContract.write.confirmBattle([buffHash, battleId]);
await waitForTransaction(tx);
return {
type: 'success'
type: 'success',
data: getComponentValue(BattleList, encodeEntity({ battleId: "uint256" }, { battleId: battleId}))
}
} catch (error) {
console.log('confirmBattle', error);
Expand Down Expand Up @@ -200,13 +200,24 @@ export function createSystemCalls(
try {
const tx = await worldContract.write.forceEnd([battleId]);
await waitForTransaction(tx);
return tx
return getComponentValue(BattleList, encodeEntity({ battleId: "uint256" }, { battleId: battleId}))
} catch (error) {
console.log('forceEnd', error);
message.error(error.cause.reason || error.cause.details);
}
}

const unlockUserLocation = async () => {
try {
const tx = await worldContract.write.unlockUserLocation();
await waitForTransaction(tx);
return tx
} catch (error) {
console.log('unlockUserLocation', error);
message.error(error.cause.reason || error.cause.details);
}
}

const getBattlePlayerHp = async (battleId: any, addr: any) => {
const data = await worldContract.read.getBattlePlayerHp([battleId, addr]);
return data
Expand All @@ -230,6 +241,7 @@ export function createSystemCalls(
setInfo,
initUserInfo,
selectBothNFT,
forceEnd
forceEnd,
unlockUserLocation
};
}
14 changes: 6 additions & 8 deletions packages/client/src/pages/game/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ const Game = () => {
const navigate = useNavigate();
const {
components: { Player, GameConfig, BattleList, BoxList, GlobalConfig, LootList1, LootList2 },
systemCalls: { move, openBox, revealBox, getCollections, battleInvitation },
systemCalls: { move, openBox, revealBox, getCollections, battleInvitation, unlockUserLocation },
network,
} = useMUD();

Expand Down Expand Up @@ -220,6 +220,9 @@ const Game = () => {
} else {
// 逃跑成功
message.info('You escaped the battle');
setTimeout(() => {
unlockUserLocation();
}, 200);
}
}
}
Expand All @@ -228,30 +231,25 @@ const Game = () => {
if (curPlayer.waiting) {
return;
}
let txFinished = false;
clearInterval(moveInterval.current);
let pathIndex = 0;
const timeInterval = ~~(1500 / Number(curPlayer.speed))
moveInterval.current = setInterval(() => {
setVertexCoordinate(triggerVertexUpdate(paths[pathIndex], curPlayer, mapDataRef.current, vertexCoordinate));
updatePlayerPosition(curPlayer, paths[pathIndex]);
setRenderPlayers([...renderPlayers]);
pathIndex++;
if (pathIndex === paths.length) {
clearInterval(moveInterval.current);
if (!txFinished) {
curPlayer.waiting = true;
}
const target = paths[pathIndex - 1];
const isDelivery = DELIVERY.x === target.x && DELIVERY.y === target.y;
if (isDelivery) {
setUserInfoPlayer(curPlayer);
submitGem();
}
}
}, timeInterval);
}, 300);
curPlayer.waiting = true;
const result = await move(merkelData);
txFinished = true;
curPlayer.waiting = false;
if (result?.type === 'error') {
message.error(result.message);
Expand Down
48 changes: 19 additions & 29 deletions packages/client/src/pages/home/index.tsx
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import React, { useEffect, useRef, useState } from 'react';
import Header from '@/pages/home/header';
import './styles.scss';

import useModal from '@/hooks/useModal';
import Loading from '@/components/Loading';
import MintList from '@/config/mint';
import { message, Modal } from 'antd';
import { message } from 'antd';
import UserInfo from '@/components/UserInfo';
import { UserAddress } from '@/mock/data';
import { UserAddressKey } from '@/config';
Expand All @@ -23,8 +23,6 @@ import lootAbi from '../../../../contracts/out/Loot.sol/MLoot.abi.json'
import userAbi from '../../../../contracts/out/User.sol/MUser.abi.json'
import pluginAbi from '../../../../contracts/out/Plugin.sol/MPlugin.abi.json'

console.log(pluginAbi, userAbi)

let userContract: any
let lootContract: any
let pluginContract: any
Expand All @@ -40,14 +38,13 @@ const Home = () => {
network
} = useMUD();

console.log(network, 'network')

const [walletAddress, setWalletAddress] = useState('');
const [walletBalance, setWalletBalance] = useState('');
const [step, setStep] = useState('play');
const usernameRef = useRef<HTMLInputElement>();
const [modalVisible, setModalVisible] = useState(false);

const { Modal, open, close, setContent } = useModal({
title: '',
});

const [minting, setMinting] = useState(false);

Expand Down Expand Up @@ -86,7 +83,6 @@ const Home = () => {
let rpc = network.walletClient?.chain?.rpcUrls?.default?.http[0] || 'http://127.0.0.1:8545'
let provider = new ethers.providers.JsonRpcProvider(rpc)
let wallet = new ethers.Wallet(privateKey, provider)
console.log(wallet)
let userContractAddress = GlobalConfigData[0].userContract
userContract = new ethers.Contract(userContractAddress, userAbi, wallet)
userContract?.getUserTokenIdList().then(res => {
Expand Down Expand Up @@ -116,7 +112,18 @@ const Home = () => {
}

const createWallet = () => {
setModalVisible(true);
setContent(
<div className="create-wallet-wrapper">
<div className="create-wallet-content">
You have successfully created a wallet.Name your character and start your journey!
</div>
<div className="mint-name">
<input type="text" className="mi-input" ref={usernameRef} />
<button className="mi-btn" onClick={toMint}>OK</button>
</div>
</div>
);
open();
}

const toMint = async () => {
Expand All @@ -125,7 +132,7 @@ const Home = () => {
return;
}
setUsername(usernameRef.current.value);
setModalVisible(false);
close();
setStep('mint');
}

Expand All @@ -142,7 +149,6 @@ const Home = () => {
let tokenIds = await Promise.all([userContract.getUserTokenIdList(), lootContract.getUserTokenIdList()])
userTokenIds = tokenIds[0]
lootTokenIds = tokenIds[1]
console.log(userTokenIds, lootTokenIds, 'userTokenIds, lootTokenIds')
let revealres = await pluginContract.multRevealNFT(lootTokenIds[lootTokenIds.length - 1].toString(), userTokenIds[userTokenIds.length - 1].toString())
await revealres.wait()
resolve('success')
Expand Down Expand Up @@ -219,7 +225,6 @@ const Home = () => {
message.error('waiting for wallet connection');
return;
}
console.log(curPlayer, 'curPlayer')
if (curPlayer && curPlayer.state != 1 && curPlayer.state != 0) {
navigate('/game', {
state: {
Expand Down Expand Up @@ -304,22 +309,7 @@ const Home = () => {
</div>
)
}
<Modal
visible={modalVisible}
className="mi-modal"
footer={null}
onCancel={() => setModalVisible(false)}
>
<div className="create-wallet-wrapper">
<div className="create-wallet-content">
You have successfully created a wallet.Name your character and start your journey!
</div>
<div className="mint-name">
<input type="text" className="mi-input" ref={usernameRef} />
<button className="mi-btn" onClick={toMint}>OK</button>
</div>
</div>
</Modal>
<Modal />
</div>
);
};
Expand Down
Loading

0 comments on commit 10345d7

Please sign in to comment.