Skip to content

Commit

Permalink
add prizes to season table
Browse files Browse the repository at this point in the history
  • Loading branch information
starknetdev committed Feb 10, 2025
1 parent 13c5f8f commit f3e8492
Show file tree
Hide file tree
Showing 6 changed files with 83 additions and 23 deletions.
20 changes: 19 additions & 1 deletion ui/src/app/components/leaderboard/SeasonRow.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,15 @@ interface SeasonRowProps {
rank: number;
adventurer: Adventurer;
handleRowSelected: (id: number) => void;
prize: any;
}

const SeasonRow = ({ rank, adventurer, handleRowSelected }: SeasonRowProps) => {
const SeasonRow = ({
rank,
adventurer,
handleRowSelected,
prize,
}: SeasonRowProps) => {
const { play: clickPlay } = useUiSounds(soundSelector.click);
const adventurersByOwner = useQueriesStore(
(state) => state.data.adventurersByOwnerQuery?.adventurers ?? []
Expand All @@ -25,6 +31,17 @@ const SeasonRow = ({ rank, adventurer, handleRowSelected }: SeasonRowProps) => {
);
const topScoreAdventurer = topScores[0]?.id === adventurer.id;

const variant = prize?.[0]?.token_data_type?.option as "erc20" | "erc721";

const isERC20 = variant === "erc20";
const tokenValue = prize
? Number(
isERC20
? prize?.[0]?.token_data_type?.erc20?.token_amount
: prize?.[0]?.token_data_type?.erc721?.token_id
)
: 0;

return (
<tr
className={`text-center border-b border-terminal-green hover:bg-terminal-green/75 hover:text-terminal-black cursor-pointer xl:h-2 xl:text-lg 2xl:text-xl 2xl:h-10 ${
Expand Down Expand Up @@ -64,6 +81,7 @@ const SeasonRow = ({ rank, adventurer, handleRowSelected }: SeasonRowProps) => {
<td>
<span className="flex justify-center">{adventurer.health}</span>
</td>
<td>{tokenValue !== 0 ? `$${tokenValue / 10 ** 18} CASH` : "-"}</td>
</tr>
);
};
Expand Down
28 changes: 28 additions & 0 deletions ui/src/app/components/leaderboard/SeasonTable.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,13 @@ import { useMemo, useState } from "react";
export interface SeasonTableProps {
itemsPerPage: number;
handleFetchProfileData: (adventurerId: number) => void;
prizes: any;
}

const SeasonTable = ({
itemsPerPage,
handleFetchProfileData,
prizes,
}: SeasonTableProps) => {
const [currentPage, setCurrentPage] = useState<number>(1);
const setScreen = useUIStore((state) => state.setScreen);
Expand Down Expand Up @@ -94,6 +96,22 @@ const SeasonTable = ({
setCurrentPage(newPage);
}
};

const formattedPrizes =
prizes?.lsTournamentsV0TournamentPrizeModels?.edges ?? [];

const groupedPrizes = formattedPrizes.reduce((acc: any, prize: any) => {
const key = prize.node.payout_position;
const isERC20 = prize.node.token_data_type.option === "erc20";
if (isERC20) {
if (!acc[key]) acc[key] = [];
acc[key].push(prize.node);
} else {
acc[key] = [prize.node];
}
return acc;
}, {} as Record<string, typeof prizes>);

return (
<>
{!adventurers ? (
Expand All @@ -120,6 +138,7 @@ const SeasonTable = ({
<th className="p-1">Level</th>
<th className="p-1">XP</th>
<th className="p-1">Health</th>
<th className="p-1">Prize</th>
</tr>
</thead>
<tbody>
Expand All @@ -130,6 +149,15 @@ const SeasonTable = ({
rank={index + 1 + (currentPage - 1) * itemsPerPage}
adventurer={adventurer}
handleRowSelected={handleRowSelected}
prize={
groupedPrizes[
(
index +
1 +
(currentPage - 1) * itemsPerPage
).toString()
]
}
/>
)
)}
Expand Down
22 changes: 3 additions & 19 deletions ui/src/app/components/start/CreateAdventurer.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,11 @@ import { AdventurerName } from "@/app/components/start/AdventurerName";
import Prizes from "@/app/components/start/Prizes";
import { Spawn } from "@/app/components/start/Spawn";
import { WeaponSelect } from "@/app/components/start/WeaponSelect";
import { getTournamentPrizes } from "@/app/hooks/graphql/queries";
import useLoadingStore from "@/app/hooks/useLoadingStore";
import useUIStore from "@/app/hooks/useUIStore";
import { tournamentClient } from "@/app/lib/clients";
import { networkConfig } from "@/app/lib/networkConfig";
import { FormData } from "@/app/types";
import { useQuery } from "@apollo/client";
import React, { useCallback, useEffect, useMemo, useState } from "react";
import React, { useCallback, useEffect, useState } from "react";
import { Contract } from "starknet";

export interface CreateAdventurerProps {
Expand All @@ -24,6 +21,7 @@ export interface CreateAdventurerProps {
getBalances: () => Promise<void>;
costToPlay: bigint;
lordsDollarValue: () => Promise<bigint>;
tournamentPrizes: any;
}

export const CreateAdventurer = ({
Expand All @@ -37,6 +35,7 @@ export const CreateAdventurer = ({
getBalances,
costToPlay,
lordsDollarValue,
tournamentPrizes,
}: CreateAdventurerProps) => {
const [formData, setFormData] = useState<FormData>({
startingWeapon: "",
Expand All @@ -53,21 +52,6 @@ export const CreateAdventurer = ({

const seasonActive = process.env.NEXT_PUBLIC_SEASON_ACTIVE === "true";

// Memoize both the variables AND the client
const { variables: tournamentVariables, client } = useMemo(() => {
return {
variables: {
tournamentId: networkConfig[network!].tournamentId,
},
client: tournamentClient(networkConfig[network!].tournamentGQLURL),
};
}, [network]); // Only recreate when network changes

const { data: tournamentPrizes } = useQuery(getTournamentPrizes, {
client,
variables: tournamentVariables,
});

const handleKeyDown = useCallback(
(event: React.KeyboardEvent<HTMLInputElement> | KeyboardEvent) => {
if (!event.currentTarget) return;
Expand Down
3 changes: 3 additions & 0 deletions ui/src/app/containers/AdventurerScreen.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ interface AdventurerScreenProps {
name: string,
index: number
) => Promise<void>;
tournamentPrizes: any;
}

/**
Expand All @@ -64,6 +65,7 @@ export default function AdventurerScreen({
transferAdventurer,
lordsDollarValue,
changeAdventurerName,
tournamentPrizes,
}: AdventurerScreenProps) {
const [activeMenu, setActiveMenu] = useState(0);
const setAdventurer = useAdventurerStore((state) => state.setAdventurer);
Expand Down Expand Up @@ -122,6 +124,7 @@ export default function AdventurerScreen({
getBalances={getBalances}
costToPlay={costToPlay}
lordsDollarValue={lordsDollarValue}
tournamentPrizes={tournamentPrizes}
/>
</div>
)}
Expand Down
9 changes: 8 additions & 1 deletion ui/src/app/containers/LeaderboardScreen.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,17 @@ import useUIStore from "@/app/hooks/useUIStore";
import { Adventurer } from "@/app/types";
import { useEffect, useState } from "react";

interface LeaderboardScreenProps {
tournamentPrizes: any;
}

/**
* @container
* @description Provides the leaderboard screen for the adventurer.
*/
export default function LeaderboardScreen() {
export default function LeaderboardScreen({
tournamentPrizes,
}: LeaderboardScreenProps) {
const itemsPerPage = 10;
const [showScores, setShowScores] = useState(false);
const [showKilledBeasts, setShowKilledBeasts] = useState(false);
Expand Down Expand Up @@ -101,6 +107,7 @@ export default function LeaderboardScreen() {
<SeasonTable
itemsPerPage={itemsPerPage}
handleFetchProfileData={handlefetchProfileData}
prizes={tournamentPrizes}
/>
</div>
<div
Expand Down
24 changes: 22 additions & 2 deletions ui/src/app/page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ import {
getLatestDiscoveries,
getLatestMarketItems,
getOwnerTokens,
getTournamentPrizes,
} from "@/app/hooks/graphql/queries";
import useAdventurerStore from "@/app/hooks/useAdventurerStore";
import useControls from "@/app/hooks/useControls";
Expand All @@ -56,7 +57,7 @@ import useTransactionCartStore from "@/app/hooks/useTransactionCartStore";
import useTransactionManager from "@/app/hooks/useTransactionManager";
import useUIStore, { ScreenPage } from "@/app/hooks/useUIStore";
import { fetchBalances, fetchEthBalance } from "@/app/lib/balances";
import { gameClient } from "@/app/lib/clients";
import { gameClient, tournamentClient } from "@/app/lib/clients";
import { VRF_WAIT_TIME } from "@/app/lib/constants";
import { networkConfig } from "@/app/lib/networkConfig";
import {
Expand Down Expand Up @@ -780,6 +781,22 @@ function Home() {
}
}, [vitBoostRemoved, chaBoostRemoved]);

// Memoize both the variables AND the client
const { variables: tournamentVariables, client: tournamentClientReturn } =
useMemo(() => {
return {
variables: {
tournamentId: networkConfig[network!].tournamentId,
},
client: tournamentClient(networkConfig[network!].tournamentGQLURL),
};
}, [network]); // Only recreate when network changes

const { data: tournamentPrizes } = useQuery(getTournamentPrizes, {
client: tournamentClientReturn,
variables: tournamentVariables,
});

return (
<>
{/* <Toaster /> */}
Expand Down Expand Up @@ -878,6 +895,7 @@ function Home() {
transferAdventurer={transferAdventurer}
lordsDollarValue={lordsDollarValue}
changeAdventurerName={changeAdventurerName}
tournamentPrizes={tournamentPrizes}
/>
)}
{screen === "play" && (
Expand All @@ -892,7 +910,9 @@ function Home() {
{screen === "inventory" && (
<InventoryScreen gameContract={gameContract!} />
)}
{screen === "leaderboard" && <LeaderboardScreen />}
{screen === "leaderboard" && (
<LeaderboardScreen tournamentPrizes={tournamentPrizes} />
)}
{screen === "upgrade" && (
<UpgradeScreen upgrade={upgrade} gameContract={gameContract!} />
)}
Expand Down

0 comments on commit f3e8492

Please sign in to comment.