Skip to content

Commit

Permalink
chore: resolve conflict
Browse files Browse the repository at this point in the history
  • Loading branch information
lewis committed Nov 4, 2023
2 parents b92a3db + 8423e9c commit d2402dd
Show file tree
Hide file tree
Showing 18 changed files with 162 additions and 57 deletions.
Binary file added packages/client/src/assets/img/duck_index.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
1 change: 1 addition & 0 deletions packages/client/src/common.scss
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ $avatarsPath: './assets/avatar/';
}

body {
min-width: 1280px;
background: #dfc380;
}

Expand Down
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
2 changes: 1 addition & 1 deletion packages/client/src/components/Player/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ const Player = (props: IPlayer) => {
addr === curAddr && <Fog />
}
{
waiting && <div className={'waiting-tip'}>Waiting</div>
waiting && <div className={'waiting-tip'}>Wait TX</div>
}
</div>
);
Expand Down
1 change: 1 addition & 0 deletions packages/client/src/components/Player/styles.scss
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@
top: 100%;
width: 100%;
color: #000;
white-space: nowrap;
text-align: center;
text-shadow: #fff 1px 0 0, #fff 0 1px 0, #fff -1px 0 0, #fff 0 -1px 0;
}
Expand Down
6 changes: 3 additions & 3 deletions packages/client/src/components/UserInfo/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ export interface IUserInfo {
const UserInfo = (props: IUserInfo) => {

const { handheld, head, clothes, gem = 0, userUrl, lootUrl, player } = props;
const lootHasLoaded = handheld && head && clothes;
const lootHasLoaded = (handheld && head && clothes) || (player?.equip?.handheld && player?.equip?.head && player?.equip?.clothes);
console.log(handheld, head, clothes, lootHasLoaded);

return (
Expand All @@ -26,7 +26,7 @@ const UserInfo = (props: IUserInfo) => {
<div className="user-detail-wrapper">
<div className="user-appearance-wrapper">
<div className="user-appearance-box">
<Appearance clothes={clothes} handheld={handheld} head={head}/>
<Appearance clothes={clothes || player?.equip?.clothes} handheld={handheld || player?.equip?.handheld} head={head || player?.equip?.head}/>
</div>
</div>
<div className={`loot-wrapper ${lootHasLoaded ? 'loaded' : ''}`}>
Expand All @@ -44,7 +44,7 @@ const UserInfo = (props: IUserInfo) => {
<div className={`user-attr-wrapper ${lootHasLoaded ? 'loaded' : ''}`}>
<dl>
<dt>HP</dt>
<dd><span className="base-attr">{lootHasLoaded ? player?.maxHp?.toString() : 0}</span><span className="extra-attr">{lootHasLoaded ? player?.maxHp?.toString() : ''}</span></dd>
<dd><span className="base-attr">{lootHasLoaded ? player?.maxHp : 0}</span><span className="extra-attr">{lootHasLoaded ? player?.maxHp?.toString() : ''}</span></dd>
</dl>
<dl>
<dt>Attack</dt>
Expand Down
2 changes: 1 addition & 1 deletion packages/client/src/components/UserInfoDialog/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ const UserInfoDialog = (props: IProps) => {

return (
<div className={`mi-userinfo-dialog ${visible ? '' : 'hidden'}`}>
<UserInfo {...rest}/>
<UserInfo {...rest} player={rest} />
<button className="mi-btn close-btn" onClick={onClose}>OK</button>
</div>
);
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
};
}
22 changes: 18 additions & 4 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,25 +231,30 @@ 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();
}
}
}, 300);
curPlayer.waiting = true;
}, timeInterval);
const result = await move(merkelData);
txFinished = true;
curPlayer.waiting = false;
if (result?.type === 'error') {
message.error(result.message);
Expand All @@ -255,6 +263,12 @@ const Game = () => {
};

const showUserInfo = (player) => {
console.log(player);
if (player.addr.toLocaleLowerCase() == account.toLocaleLowerCase()) {
let cur = localStorage.getItem('playerInfo');
if (cur) player = JSON.parse(cur);
}

setUserInfoPlayer(player);
setUserInfoVisible(true);
}
Expand Down Expand Up @@ -387,7 +401,7 @@ const Game = () => {
setUserInfoVisible(false);
}}
gem={userInfoPlayer.gem}
{...userInfoPlayer.equip}
{...userInfoPlayer}
/>
)
}
Expand Down
47 changes: 37 additions & 10 deletions packages/client/src/pages/home/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,12 @@ import { Has, getComponentValue } from '@latticexyz/recs';
import { decodeEntity } from "@latticexyz/store-sync/recs";
import { ethers } from 'ethers';

import indexDuckImg from '@/assets/img/duck_index.png';

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 @@ -38,8 +38,6 @@ const Home = () => {
network
} = useMUD();

console.log(network, 'network')

const [walletAddress, setWalletAddress] = useState('');
const [walletBalance, setWalletBalance] = useState('');
const [step, setStep] = useState('play');
Expand Down Expand Up @@ -85,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 @@ -152,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 All @@ -172,6 +168,11 @@ const Home = () => {
return url
}

const toObject = (obj) => {
return JSON.parse(JSON.stringify(obj, (key, value) => typeof value === 'bigint' ? value.toString() : value
))
}

const mintAndGo = async () => {
setMinting(true);
try {
Expand Down Expand Up @@ -205,6 +206,16 @@ const Home = () => {
setClothes(clothes);
setHandheld(handheld);
setHead(head);

playerData.equip = {
clothes,
handheld,
head,
}

let player = Object.assign(playerData, {username, clothes, handheld, head, userUrl: url.image, lootUrl: lootUrl.image})
console.log(player, 'player')
localStorage.setItem('playerInfo', JSON.stringify(toObject(player)));

let result = await Promise.all([setInfo(username, ''), joinBattlefield()])
console.log(result, 'result')
Expand All @@ -229,7 +240,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 @@ -274,9 +284,26 @@ const Home = () => {
/>
{
step === 'play' && (
<section className="mi-section">
<button className="play-btn mi-btn" onClick={play}>PLAY NOW</button>
<button className="play-btn mi-btn" onClick={initUserInfoFun}>INIT USER</button>
<section className="mi-section index-section">
<div className="section-box">
<div className="intro-box">
<h1 className={'intro-title'}>Welcome to Mississippi</h1>
<p>
An ancient cave, cursed by its creator, opens intermittently as if alive <br/><br/>

The cavern is rich in energy gems that prudent adventurers can take, while those who miss the time to leave due to greed will be trapped in the cavern forever <br/><br/>

The Mississippi Company executives saw the value of the caves and decided to monopolize them <br/><br/>

Just when the plan was about to succeed, a group of crazy duck adventurers stormed into the cave...
</p>
<button className="play-btn mi-btn" onClick={play}>PLAY NOW</button>
<button className="play-btn mi-btn" onClick={initUserInfoFun}>INIT USER</button>

</div>
</div>
<img src={indexDuckImg} alt="duck" className={'duck-index'}/>

</section>
)
}
Expand Down
Loading

0 comments on commit d2402dd

Please sign in to comment.