Skip to content

Commit

Permalink
Challenge snapshot
Browse files Browse the repository at this point in the history
  • Loading branch information
rsodre committed Jun 8, 2024
1 parent 1e24516 commit 61ffe22
Show file tree
Hide file tree
Showing 2 changed files with 90 additions and 6 deletions.
94 changes: 88 additions & 6 deletions client/src/pistols/components/Snapshots.tsx
Original file line number Diff line number Diff line change
@@ -1,8 +1,12 @@
import React, { useEffect, useMemo, useState } from 'react'
import { Button, Container, Divider, TextArea } from 'semantic-ui-react'
import { Grid, Button, Container, Divider, TextArea } from 'semantic-ui-react'
import { useAllDuelistIds, useDuelist } from '@/pistols/hooks/useDuelist'
import { CopyIcon } from '@/lib/ui/Icons'
import { bigintToHex } from '@/lib/utils/type'
import { bigintEquals, bigintToHex } from '@/lib/utils/type'
import { useAllChallengeIds, useChallenge } from '../hooks/useChallenge'

const Row = Grid.Row
const Col = Grid.Column

//@ts-ignore
BigInt.prototype.toJSON = function () { return bigintToHex(this) }
Expand All @@ -17,14 +21,30 @@ export function Snapshots() {

return (
<Container text>
<SnapshotDuelists update={_update} />
<Grid>
<Row columns={'equal'}>
<Col>
<SnapshotDuelists update={_update} />
</Col>
<Col>
<SnapshotChallenges update={_update} />
</Col>
</Row>
</Grid>

<Divider />

<TextArea readOnly value={data} />
<CopyIcon content={data} />
</Container>
);
}



//----------------------------------
// Duelist Model
//
function SnapshotDuelists({
update,
}) {
Expand All @@ -41,8 +61,7 @@ function SnapshotDuelists({
}, [snapping, duelists])

const _update = (duelist) => {
const address = bigintToHex(duelist.address)
setDuelists(o => (o.findIndex(v => (v.address == address)) == -1 ? [...o, duelist] : [...o]))
setDuelists(o => (o.findIndex(v => bigintEquals(duelist.address, v.address)) == -1 ? [...o, duelist] : [...o]))
}

const loaders = useMemo(() => {
Expand All @@ -62,7 +81,7 @@ function SnapshotDuelists({

return (
<>
<Button disabled={!canSnap} onClick={() => _start()}>
<Button className='FillParent' disabled={!canSnap} onClick={() => _start()}>
Duelists Snapshot ({duelistCount > 0 ? `${duelists.length}/${duelistCount}` : '...'})
</Button>
{loaders}
Expand All @@ -79,8 +98,71 @@ export function SnapDuelist({
update({
...duelist,
honourDisplay: undefined,
honourAndTotal: undefined,
isRegistered: undefined,
})
}, [duelist])
return <></>
}




//----------------------------------
// Challenge model
//
function SnapshotChallenges({
update,
}) {
const { challengeIds, challengeCount } = useAllChallengeIds()
const [challenges, setChallenges] = useState([])

const [snapping, setSnapping] = useState(false)
const canSnap = (challengeCount > 0 && (!snapping || challenges.length == challengeIds.length))

useEffect(() => {
if (snapping) {
update(challenges)
}
}, [snapping, challenges])

const _update = (challenge) => {
setChallenges(o => (o.findIndex(v => bigintEquals(challenge.duelId, v.duelId)) == -1 ? [...o, challenge] : [...o]))
}

const loaders = useMemo(() => {
let result = []
if (snapping && challenges.length < challengeIds.length) {
const duelId = challengeIds[challenges.length]
result.push(<SnapChallenge key={duelId} duelId={duelId} update={_update} />)
}
return result
}, [snapping, challengeIds, challenges])

const _start = () => {
setSnapping(true)
setChallenges([])
}

return (
<>
<Button className='FillParent' disabled={!canSnap} onClick={() => _start()}>
Challenges Snapshot ({challengeCount > 0 ? `${challenges.length}/${challengeCount}` : '...'})
</Button>
{loaders}
</>
);
}

export function SnapChallenge({
duelId,
update,
}) {
const challenge = useChallenge(duelId)
useEffect(() => {
update({
...challenge,
})
}, [challenge])
return <></>
}
2 changes: 2 additions & 0 deletions client/src/pistols/hooks/useChallenge.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ export const useAllChallengeIds = () => {
const challengeIds: bigint[] = useEntityKeys(Challenge, 'duel_id')
return {
challengeIds,
challengeCount: challengeIds.length,
}
}

Expand Down Expand Up @@ -91,6 +92,7 @@ export const useChallenge = (duelId: BigNumberish) => {

return {
challengeExists: (challenge != null),
duelId,
state,
duelistA,
duelistB,
Expand Down

0 comments on commit 61ffe22

Please sign in to comment.