Skip to content

Commit

Permalink
Display duel results at end of animations
Browse files Browse the repository at this point in the history
  • Loading branch information
rsodre committed Jan 2, 2024
1 parent 70ac8cc commit f4acb49
Show file tree
Hide file tree
Showing 6 changed files with 55 additions and 20 deletions.
19 changes: 7 additions & 12 deletions client/src/pistols/components/ChallengeModal.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,12 @@ import { useRouter } from 'next/navigation'
import { Grid, Table, Modal, Divider, Header } from 'semantic-ui-react'
import { useDojoAccount, useDojoSystemCalls } from '@/dojo/DojoContext'
import { usePistolsContext } from '@/pistols/hooks/PistolsContext'
import { useChallenge } from '@/pistols/hooks/useChallenge'
import { useChallenge, useChallengeDescription } from '@/pistols/hooks/useChallenge'
import { useDuelist } from '@/pistols/hooks/useDuelist'
import { ProfileDescription } from '@/pistols/components/account/ProfileDescription'
import { ProfilePicButton } from '@/pistols/components/account/ProfilePic'
import { ActionButton } from '@/pistols/components/ui/Buttons'
import { ChallengeState, ChallengeStateDescriptions, makeDuelUrl } from '@/pistols/utils/pistols'
import { ChallengeState, makeDuelUrl } from '@/pistols/utils/pistols'
import { AccountShort } from './ui/Account'

const Row = Grid.Row
Expand All @@ -22,17 +22,12 @@ export default function ChallengeModal() {

const { atYourDuels, atLiveDuels, atPastDuels, duelId, dispatchSetDuel, dispatchSetDuelist } = usePistolsContext()

const { state, message, duelistA, duelistB, winner, lords } = useChallenge(duelId)
const { state, message, duelistA, duelistB, lords } = useChallenge(duelId)

const { name: nameA, profilePic: profilePicA } = useDuelist(duelistA)
const { name: nameB, profilePic: profilePicB } = useDuelist(duelistB)
const { challengeDescription } = useChallengeDescription(duelId)

const _state = useMemo(() => {
let result = ChallengeStateDescriptions[state]
if (winner == duelistA) result += ' in favor of Challenger'
if (winner == duelistB) result += ' in favor of Challenged'
return result.replace('Challenger', nameA).replace('Challenged', nameB)
}, [state, winner, duelistA, duelistB, nameA, nameB])
const { profilePic: profilePicA } = useDuelist(duelistA)
const { profilePic: profilePicB } = useDuelist(duelistB)

const isChallenger = useMemo(() => (duelistA == BigInt(account.address)), [duelistA, account])
const isChallenged = useMemo(() => (duelistB == BigInt(account.address)), [duelistB, account])
Expand Down Expand Up @@ -98,7 +93,7 @@ export default function ChallengeModal() {
</Row>
<Row columns='equal' textAlign='center'>
<Col>
<h3>{_state}</h3>
<h3>{challengeDescription}</h3>
</Col>
</Row>
</Grid>
Expand Down
2 changes: 1 addition & 1 deletion client/src/pistols/components/ChallengeTable.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,7 @@ function DuelItem({
}

return (
<Table.Row warning={isAwaiting} negative={isDraw || isCanceled} positive={isInProgress || winnerIsA || winnerIsB} textAlign='left' verticalAlign='middle' onClick={() => _gotoChallenge()}>
<Table.Row warning={isDraw} negative={isCanceled} positive={isInProgress || winnerIsA || winnerIsB} textAlign='left' verticalAlign='middle' onClick={() => _gotoChallenge()}>
<Cell positive={winnerIsA} negative={winnerIsB}>
<ProfilePicSquare profilePic={profilePicA} />
</Cell>
Expand Down
23 changes: 17 additions & 6 deletions client/src/pistols/components/Duel.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { Grid, Segment, Icon, Step } from 'semantic-ui-react'
import { useDojoAccount, useDojoSystemCalls } from '@/dojo/DojoContext'
import { usePistolsContext, MenuKey } from '@/pistols/hooks/PistolsContext'
import { useGameplayContext } from '@/pistols/hooks/GameplayContext'
import { useChallenge } from '@/pistols/hooks/useChallenge'
import { useChallenge, useChallengeDescription } from '@/pistols/hooks/useChallenge'
import { useDuelist } from '@/pistols/hooks/useDuelist'
import { useDuel } from '@/pistols/hooks/useDuel'
import { useCommitMove } from '@/pistols/hooks/useCommitReveal'
Expand All @@ -21,10 +21,12 @@ const Col = Grid.Column
export default function Duel({
duelId
}) {
const { dispatchSetDuel } = usePistolsContext()
const { state, isLive, message, duelistA, duelistB, winner, lords } = useChallenge(duelId)
const { account } = useDojoAccount()
const { gameImpl } = useGameplayContext()
const { gameImpl, animated } = useGameplayContext()
const { dispatchSetDuel } = usePistolsContext()

const { isLive, isFinished, message, duelistA, duelistB } = useChallenge(duelId)
const { challengeDescription } = useChallengeDescription(duelId)

useEffectOnce(() => {
gameImpl?.resetScene()
Expand All @@ -36,6 +38,11 @@ export default function Duel({
<>
<div className='TavernTitle' style={{ maxWidth: '250px' }}>
<h1 className='Quote'>{`“${message}”`}</h1>
{(isFinished && animated == AnimationState.Finished) &&
<Segment>
{challengeDescription}
</Segment>
}
</div>

<div className='DuelSideA'>
Expand Down Expand Up @@ -112,7 +119,7 @@ function DuelProgress({
// console.log(`Round 1:`, round1)
// console.log(`Round 2:`, round2)

const { gameImpl, animated, hasLoadedAudioAssets } = useGameplayContext()
const { gameImpl, animated, hasLoadedAudioAssets, dispatchAnimated } = useGameplayContext()

//-------------------------
// Duel progression
Expand All @@ -131,7 +138,11 @@ function DuelProgress({

const isAnimatingPistols = useMemo(() => (currentStage == DuelStage.PistolsShootout), [currentStage])
const isAnimatingBlades = useMemo(() => (currentStage == DuelStage.BladesClash), [currentStage])
// if (isA) console.log(`stage, animated`, currentStage, animated, isAnimatingPistols, isAnimatingBlades)
useEffect(() => {
if (currentStage == DuelStage.Finished) {
dispatchAnimated(AnimationState.Finished)
}
}, [currentStage])

useEffect(() => {
if (isA && gameImpl && isAnimatingPistols && hasLoadedAudioAssets) {
Expand Down
22 changes: 21 additions & 1 deletion client/src/pistols/hooks/useChallenge.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,9 @@ import { Entity, HasValue, Has, getComponentValue, Component } from '@dojoengine
import { useComponentValue, useEntityQuery } from "@dojoengine/react"
import { useDojoComponents } from '@/dojo/DojoContext'
import { bigintToEntity, bigintToHex, feltToString } from "../utils/utils"
import { ChallengeState } from "@/pistols/utils/pistols"
import { ChallengeState, ChallengeStateDescriptions } from "@/pistols/utils/pistols"
import { useEntityKeys, useEntityKeysQuery } from '@/pistols/hooks/useEntityKeysQuery'
import { useDuelist } from "./useDuelist"


//-----------------------------
Expand Down Expand Up @@ -87,6 +88,7 @@ export const useChallenge = (duelId: bigint | string) => {
challengeExists: (challenge != null),
state,
isLive: (state == ChallengeState.Awaiting || state == ChallengeState.InProgress),
isFinished: (state == ChallengeState.Resolved || state == ChallengeState.Draw),
duelistA,
duelistB,
challenger: duelistA,
Expand All @@ -105,6 +107,24 @@ export const useChallenge = (duelId: bigint | string) => {
}
}

export const useChallengeDescription = (duelId: bigint) => {
const { state, duelistA, duelistB, winner } = useChallenge(duelId)
const { name: nameA } = useDuelist(duelistA)
const { name: nameB } = useDuelist(duelistB)

const challengeDescription = useMemo(() => {
let result = ChallengeStateDescriptions[state]
if (winner == duelistA) result += ' in favor of Challenger'
if (winner == duelistB) result += ' in favor of Challenged'
return result.replace('Challenger', nameA).replace('Challenged', nameB)
}, [state, winner, duelistA, duelistB, nameA, nameB])

return {
challengeDescription,
}
}



//-----------------------------
// Challenges by Duelist
Expand Down
1 change: 1 addition & 0 deletions client/src/pistols/three/game.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ export enum AnimationState {
None,
Pistols,
Blades,
Finished,
}

//-------------------------------------------
Expand Down
8 changes: 8 additions & 0 deletions client/styles/index.scss
Original file line number Diff line number Diff line change
Expand Up @@ -491,6 +491,14 @@ $box-h: calc((100vh - $aspect-h) / 2);
margin-top: 20px;
}

.DuelResults {
position: absolute;
top: 25%;
left: 0;
right: 0;
margin: auto;
}

// <IconXXX>

.Tooltip {
Expand Down

0 comments on commit f4acb49

Please sign in to comment.