Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion packages/dapp/src/pages/games/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ const GamesPage = () => {
const { account } = useWeb3React()
const { chain } = useActiveWeb3React()

const chainSymbol = chain?.nativeCurrency?.symbol || 'BNB'
// const chainSymbol = chain?.nativeCurrency?.symbol || 'BNB'

const { chosenGamesMemoized } = useContext(GamesContext)
return (
Expand Down
23 changes: 23 additions & 0 deletions packages/dapp/src/pages/games/my-games.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import { useContext } from 'react'
import { SUPPORT_GAMES_TEST } from 'config/constants/supportChains'
import { GamesPageLayout, GamesContext } from 'views/Games'
import GameCard from 'views/Games/components/GameCard/GameCard'
import { useWeb3React } from '@pancakeswap/wagmi'

const MyGamesPage = () => {
const { account } = useWeb3React()
const { chosenGamesMemoized } = useContext(GamesContext)

return (
<>
{chosenGamesMemoized.map((game) => (
<GameCard key={game.id} game={game} account={account} />
))}
</>
)
}

MyGamesPage.Layout = GamesPageLayout
MyGamesPage.chains = SUPPORT_GAMES_TEST

export default MyGamesPage
64 changes: 64 additions & 0 deletions packages/dapp/src/utils/sortGames.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
/**
* Helper to sort games
*/
export const sortGamesDefault = (a, b) => {
// sort game where user is playing first
if (a.userData.isPlaying && b.userData.isPlaying) {
// sort game currently in progress first
if (a.isInProgress && b.isInProgress) {
// sort game with less remaining players count
if (a.remainingPlayersCount === b.remainingPlayersCount) return 0
if (a.remainingPlayersCount < b.remainingPlayersCount) return -1
return 1
}
if (a.isInProgress) return -1
if (b.isInProgress) return 1
return 0
}

if (a.userData.isPlaying) return -1
if (b.userData.isPlaying) return 1

if (a.isInProgress && b.isInProgress) {
if (a.remainingPlayersCount === b.remainingPlayersCount) return 0
if (a.remainingPlayersCount < b.remainingPlayersCount) return -1
return 1
}
if (a.isInProgress) return -1
if (b.isInProgress) return 1

// sorting game paused last
if (a.isPaused && b.isPaused) return 0
if (a.isPaused) return 1
if (b.isPaused) return -1

if (a.playerAddressesCount && b.playerAddressesCount) return 0
if (a.playerAddressesCount) return -1
if (b.playerAddressesCount) return 1

if (a.remainingPlayersCount && b.remainingPlayersCount) return 0
if (a.remainingPlayersCount) return -1
if (b.remainingPlayersCount) return 1

return 0
}

export const sortGamesLaunching = (a, b) => {
if (a.isPaused && b.isPaused) return 0
if (a.isPaused) return 1
if (b.isPaused) return -1

if (a.isInProgress && b.isInProgress) return 0
if (a.isInProgress) return 1
if (b.isInProgress) return -1

if (a.playerAddressesCount && b.playerAddressesCount) return 0
if (a.playerAddressesCount) return -1
if (b.playerAddressesCount) return 1

// if (a.remainingPlayersCount && b.remainingPlayersCount) return 0
// if (a.remainingPlayersCount) return -1
// if (b.remainingPlayersCount) return 1

return 0
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,8 @@ const AllowedPlayersToWinSelection = () => {
const { numberPlayersAllowedToWin, prizeType, currentStep, actions, maxPlayers } = useGameContext()

const halfNumberPlayers = Math.floor(maxPlayers * 0.5)
const listOfAllowedNumber = [...range(1, halfNumberPlayers)]

const selectedAllowedNumber = numberPlayersAllowedToWin > listOfAllowedNumber.length ? 1 : numberPlayersAllowedToWin
const listOfAllowedWinners = [...range(1, halfNumberPlayers)]
const selectedAllowedWinners = numberPlayersAllowedToWin > listOfAllowedWinners.length ? 1 : numberPlayersAllowedToWin

const handleAllowedWinners = (value) => actions.setPrizeConfiguration(+value, prizeType, currentStep)

Expand All @@ -28,22 +27,25 @@ const AllowedPlayersToWinSelection = () => {
<Text as="p" color="textSubtle" mb="24px">
Each player will win a proportional share of the prize pool
</Text>
{listOfAllowedNumber &&
listOfAllowedNumber.map((iteration) => {
{listOfAllowedWinners &&
listOfAllowedWinners.map((winner, index) => {
return (
<SelectionCard
name={iteration.toString()}
value={iteration}
image={imageTest}
onChange={handleAllowedWinners}
isChecked={selectedAllowedNumber === iteration}
>
<RowBetween>
<CommunityIcon mr="8px" />
<Text bold>{iteration.toString()}</Text>
<Text>{' Players'}</Text>
</RowBetween>
</SelectionCard>
// TODO Remove filter but generate error on prizepool verification in smart contract if winners are odd (except 1)
(index === 0 || index % 2 !== 0) && (
<SelectionCard
name={winner.toString()}
value={winner}
image={imageTest}
onChange={handleAllowedWinners}
isChecked={selectedAllowedWinners === winner}
>
<RowBetween>
<CommunityIcon mr="8px" />
<Text bold>{winner.toString()}</Text>
<Text>{' Players'}</Text>
</RowBetween>
</SelectionCard>
)
)
})}
</CardBody>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,8 @@ const GameCreationProvider: React.FC<React.PropsWithChildren> = ({ children }) =
: updatedAuthorizedAmounts[0]
).toString()}`,
)

// TODO GUIGUI Load gameCreationAmount directly from smart contract
const updatingGameConfig = {
...initialGameConfig,
AUTHORIZED_REGISTRATION_AMOUNTS: updatedAuthorizedAmounts,
Expand Down
15 changes: 11 additions & 4 deletions packages/dapp/src/views/GameCreation/hooks/useCreateGame.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,9 @@ import { formatBytes32String } from '@ethersproject/strings'
import { useGameContext } from 'views/GameCreation/hooks/useGameContext'
import { ZERO_ADDRESS } from 'config/constants'
import { useTransactionAdder } from 'state/transactions/hooks'
import { BigNumber, FixedNumber } from '@ethersproject/bignumber'
import { parse } from 'path'
import { range } from 'utils'

export const useCreateGame = (game) => {
const { t } = useTranslation()
Expand Down Expand Up @@ -37,7 +40,6 @@ export const useCreateGame = (game) => {

const parsedRegistrationAmount: number = registrationAmount ? parseFloat(formatEther(`${registrationAmount}`)) : 0

// TODO GUIGUI Load gameCreationAmount directly from smart contract
const gameCreationAmountEther = GAME_CREATION_AMOUNT

const registrationAmountEther = parseEther(`${parsedRegistrationAmount}`)
Expand All @@ -47,19 +49,24 @@ export const useCreateGame = (game) => {
: gameCreationAmountEther.add(parseEther(`${freeGamePrizepoolAmount}`))

const prizepool = parsedRegistrationAmount ? parsedRegistrationAmount * maxPlayers : freeGamePrizepoolAmount
console.log('🚀 ~ file: useCreateGame.tsx ~ line 49 ~ useCreateGame ~ prizepool', prizepool)

const createPrize = (index, totalWinners) => {
const parsedPrizepool = parseEther(`${prizepool}`)
const parsedTotalWinners = parseEther(`${totalWinners}`)
const amount = parsedPrizepool.div(parsedTotalWinners)

return {
position: index,
amount: parseEther(`${prizepool / totalWinners}`),
// TODO use prizeType
amount,
standard: 0,
contractAddress: ZERO_ADDRESS,
tokenId: 1,
}
}

const mapper = new Array(numberPlayersAllowedToWin).fill('').map((_, i) => i + 1)
const mapper = [...range(1, numberPlayersAllowedToWin)]

const prizes = mapper.map((index) => createPrize(index, numberPlayersAllowedToWin))

const formattedName = formatBytes32String(name)
Expand Down
101 changes: 42 additions & 59 deletions packages/dapp/src/views/Games/Games.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import Select, { OptionProps } from 'components/Select/Select'
import Loading from 'components/Loading'
import ScrollToTopButton from 'components/ScrollToTopButton/ScrollToTopButtonV2'
import { latinise } from 'utils/latinise'
import { sortGamesDefault } from 'utils/sortGames'
import GameTabButtons from './components/GameTabButtons'
import { CreateGameCard } from './components/CreateGameCard'

Expand Down Expand Up @@ -131,12 +132,9 @@ const Games: React.FC<React.PropsWithChildren> = ({ children }) => {
const { observerRef, isIntersecting } = useIntersectionObserver()
const chosenGamesLength = useRef(0)

// TODO GUIGUI INVERSE WHEN UPDATING /path
// const isDeleted = pathname.includes('archived')
// const isPlayingOnly = pathname.includes('history')
const isArchived = pathname.includes('archived')
const isMyGames = pathname.includes('my-games')
const isDeleted = pathname.includes('history')
const isActive = !isArchived && !isDeleted
const isActive = !isDeleted

// TODO GUIGUI get used Data
usePollGamesWithUserData()
Expand All @@ -147,51 +145,22 @@ const Games: React.FC<React.PropsWithChildren> = ({ children }) => {

// TODO GUIGUI FIRST HANDLE FILTERS
const [isNotFullOnly, setNotFullOnly] = useState(!isActive)
const [myGamesOnly, setMyGamesOnly] = useState(!isActive)
const [myPlayingGamesOnly, setMyPlayingGamesOnly] = useState(!isActive)

const activeGames = games
.filter((game) => !game.isDeleted)
.sort((a, b) => {
// sort game where user is playing first
if (a.userData.isPlaying && b.userData.isPlaying) {
// sort game currently in progress first
if (a.isInProgress && b.isInProgress) {
// sort game with less remaining players count
if (a.remainingPlayersCount === b.remainingPlayersCount) return 0
if (a.remainingPlayersCount < b.remainingPlayersCount) return -1
return 1
}
if (a.isInProgress) return -1
if (b.isInProgress) return 1
return 0
}

if (a.userData.isPlaying) return -1
if (b.userData.isPlaying) return 1

if (a.playerAddressesCount && b.playerAddressesCount) return 0
if (a.playerAddressesCount) return -1
if (b.playerAddressesCount) return 1

if (a.remainingPlayersCount && b.remainingPlayersCount) return 0
if (a.remainingPlayersCount) return -1
if (b.remainingPlayersCount) return 1

// sorting game paused last
if (a.isPaused && b.isPaused) return 0
if (a.isPaused) return 1
if (b.isPaused) return -1

return 0
})
.filter((game) => (!isMyGames ? !game.isPaused && !game.isDeleted : !game.isDeleted))
.sort(sortGamesDefault)

const notFullGames = games.filter(
(game) => !game.isInProgress && game.maxPlayers.toNumber() !== game.playerAddressesCount.toNumber(),
)

const playingOnlyGames = games.filter((game) => game.userData && game.userData.isPlaying)

const mineOnlyGames = games.filter((game) => game.userData && game.userData.isCreator)

const deletedGames = games.filter((game) => game.isDeleted)
const myDeletedGames = games.filter((game) => game.isDeleted && game.userData && game.userData.isCreator)

// const archivedGames = archivedGames.filter((game) => game.userData && false)

Expand Down Expand Up @@ -228,33 +197,37 @@ const Games: React.FC<React.PropsWithChildren> = ({ children }) => {
chosenFs = gamesList(activeGames)
}

if (isDeleted) {
chosenFs = gamesList(deletedGames)
if (isDeleted && isMyGames) {
if (isMyGames) chosenFs = gamesList(myDeletedGames)
else chosenFs = gamesList(deletedGames)
}

if (isNotFullOnly) {
chosenFs = gamesList(notFullGames)
}

if (myGamesOnly) {
if (myPlayingGamesOnly) {
chosenFs = gamesList(playingOnlyGames)
}
if (isArchived) {
chosenFs = gamesList(playingOnlyGames)

if (isMyGames) {
chosenFs = gamesList(mineOnlyGames)
}

return chosenFs
return chosenFs.sort(sortGamesDefault)
}, [
isActive,
isDeleted,
isMyGames,
isNotFullOnly,
myGamesOnly,
isArchived,
myPlayingGamesOnly,
gamesList,
activeGames,
myDeletedGames,
deletedGames,
notFullGames,
playingOnlyGames,
mineOnlyGames,
])

const chosenGamesMemoized = useMemo(() => {
Expand Down Expand Up @@ -308,15 +281,25 @@ const Games: React.FC<React.PropsWithChildren> = ({ children }) => {
<GameH2 scale="lg" color="text">
{t('Register to a game to start playing.')}
</GameH2>
{/* <NextLinkFromReactRouter to="/games/my-games" prefetch={false}> */}
<NextLinkFromReactRouter to="#" prefetch={false}>
<Button variant="text" disabled>
<Text color="primary" bold fontSize="16px" mr="4px">
{t('My Created Games')}
</Text>
<ArrowForwardIcon color="primary" />
</Button>
</NextLinkFromReactRouter>
{isMyGames ? (
<NextLinkFromReactRouter to="/games" prefetch={false}>
<Button variant="secondary">
<Text color="primary" bold fontSize="16px" mr="4px">
{t('All Games')}
</Text>
<ArrowForwardIcon color="primary" />
</Button>
</NextLinkFromReactRouter>
) : (
<NextLinkFromReactRouter to="/games/my-games" prefetch={false}>
<Button variant="secondary">
<Text color="primary" bold fontSize="16px" mr="4px">
{t('My Created Games')}
</Text>
<ArrowForwardIcon color="primary" />
</Button>
</NextLinkFromReactRouter>
)}
</Box>
{/* {chainId === ChainId.BSC && ( */}
<Box>
Expand All @@ -332,8 +315,8 @@ const Games: React.FC<React.PropsWithChildren> = ({ children }) => {
<ToggleWrapper>
<Toggle
id="my-games-only"
checked={myGamesOnly}
onChange={() => setMyGamesOnly((prev) => !prev)}
checked={myPlayingGamesOnly}
onChange={() => setMyPlayingGamesOnly((prev) => !prev)}
scale="sm"
/>
<Text> {t('My Games')}</Text>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -189,6 +189,8 @@ const CardPlayerSection: React.FC<React.PropsWithChildren<GameCardPlayerSectionP
{!isPaused && <PauseButton address={address} isInProgress={isInProgress} />}
</>
)}
{/* // TODO GUIGUI ADD CLAIM BUTTON FOR CREATOR */}
{/* // TODO GUIGUI ADD CLAIM BUTTON FOR ADMIN */}
</>
)}
{/* TODO Remove after integration phase */}
Expand Down
Loading