Skip to content

Commit

Permalink
update: add battle forceEnd
Browse files Browse the repository at this point in the history
  • Loading branch information
TiyoSheng committed Nov 3, 2023
1 parent c659a6d commit 7ebe6b3
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 45 deletions.
11 changes: 6 additions & 5 deletions packages/client/src/components/Battle/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -59,16 +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.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(async () => {
let resultBattle:any = await forceEnd(battle.id)
console.log(resultBattle)
if (resultBattle.isEnd && resultBattle.winner) {
props.finishBattle(resultBattle.winner)
}
}, 120000)
}, 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
9 changes: 2 additions & 7 deletions packages/client/src/pages/game/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -231,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
58 changes: 26 additions & 32 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 @@ -17,14 +17,10 @@ import { Has, getComponentValue } from '@latticexyz/recs';
import { decodeEntity } from "@latticexyz/store-sync/recs";
import { ethers } from 'ethers';

import useStore from '@/hooks/useStore';

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 @@ -34,21 +30,19 @@ let lootTokenIds: any

const Home = () => {
const [messageApi, contextHolder] = message.useMessage();
const { players } = useStore();
const {
components: { GlobalConfig, Player },
systemCalls: { selectBothNFT, joinBattlefield, setInfo, initUserInfo },
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 All @@ -70,6 +64,13 @@ const Home = () => {
const [lootUrl, setLootUrl] = useState<string>();
const [player, setPlayer] = useState<any>();

const players = useEntityQuery([Has(Player)]).map((entity) => {
const address = decodeEntity({ addr: "address" }, entity)?.addr?.toLocaleLowerCase() || ''
const player = getComponentValue(Player, entity);
player.addr = address
return player;
})

const curPlayer = players.find(player => player.addr.toLocaleLowerCase() == network?.account.toLocaleLowerCase());

const GlobalConfigData = useEntityQuery([Has(GlobalConfig)]).map((entity) => getComponentValue(GlobalConfig, entity));
Expand All @@ -80,7 +81,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 @@ -110,7 +110,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 @@ -119,7 +130,7 @@ const Home = () => {
return;
}
setUsername(usernameRef.current.value);
setModalVisible(false);
close();
setStep('mint');
}

Expand All @@ -136,7 +147,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 @@ -213,7 +223,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 @@ -281,22 +290,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
2 changes: 1 addition & 1 deletion packages/contracts/worlds.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
"blockNumber": 29122003
},
"31337": {
"address": "0x144e1475E1ed33B444Ca0E76a1EB7faC245bc16F"
"address": "0x68e44d106b5178b4C79201A8283c0f3e4619505d"
},
"421613": {
"address": "0x2Bc1034975c3df48D6f3026802f372677844b85d",
Expand Down

0 comments on commit 7ebe6b3

Please sign in to comment.