Skip to content

Commit

Permalink
Added current score to leaderboard (#131)
Browse files Browse the repository at this point in the history
  • Loading branch information
mazhutoanton authored Sep 25, 2023
1 parent 838dbec commit f821a9f
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 2 deletions.
12 changes: 11 additions & 1 deletion packages/sdk/src/GameSdk.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ type ShowLeaderboardOptions = {
onBeforeLoad?: () => AsyncResult;
withTopWidget?: boolean;
onShowSignUp?: () => void;
currentScore?: number;
} & ConnectWalletOptions;

type ShowPreloaderOptions = {
Expand Down Expand Up @@ -330,7 +331,14 @@ export class GamesSDK {
};
}

showLeaderboard({ onPlayAgain, onBeforeLoad, withTopWidget, onComplete, onConnect }: ShowLeaderboardOptions = {}) {
showLeaderboard({
onPlayAgain,
onBeforeLoad,
withTopWidget,
onComplete,
onConnect,
currentScore,
}: ShowLeaderboardOptions = {}) {
const { open, ...modal } = UI.createFullscreenModal(
async () => {
const leaderboard = document.createElement('cere-leaderboard');
Expand Down Expand Up @@ -369,6 +377,7 @@ export class GamesSDK {
modal.close();
},
serviceUrl: GAME_SERVICE_URL[this.env],
currentScore: currentScore,
});

return leaderboard;
Expand Down Expand Up @@ -435,6 +444,7 @@ export class GamesSDK {
this.showLeaderboard({
onConnect: save,
onComplete: resolve,
currentScore: score,
}),
);
}
Expand Down
11 changes: 10 additions & 1 deletion packages/ui/src/widgets/Leaderboard/Leaderboard.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ export type LeaderboardProps = Pick<TableProps, 'data'> & {
serviceUrl: string;
withTopWidget?: boolean;
onShowSignUp?: () => void;
currentScore?: number;
};

const LeaderboardTitle = styled(Typography)({
Expand Down Expand Up @@ -85,7 +86,14 @@ const Alerts = styled.div({
gridRowGap: '5px',
});

export const Leaderboard = ({ data, activeTournament, onPlayAgain, onTweet, withTopWidget }: LeaderboardProps) => {
export const Leaderboard = ({
data,
activeTournament,
onPlayAgain,
onTweet,
withTopWidget,
currentScore,
}: LeaderboardProps) => {
const { sessionPrice, sdkUrl: cdnUrl } = useConfigContext();
const { address, balance = 0 } = useWalletContext();
const playerData = useMemo(() => data.find((row) => row.address === address), [data, address]);
Expand Down Expand Up @@ -123,6 +131,7 @@ Be top-3 player and share a prize"
onTweet={onTweet}
disabled={balance < sessionPrice}
score={playerData?.score}
currentScore={currentScore}
rank={playerData?.rank}
/>
)}
Expand Down
7 changes: 7 additions & 0 deletions packages/ui/src/widgets/Leaderboard/TopWidget.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ type TopWidgetProps = {
onTweet?: (score: number) => Promise<{ tweetBody: string }>;
score?: number;
rank?: number;
currentScore?: number;
};

const WidgetWrapper = styled(ModalWrapper)(({ tournament }: { tournament?: boolean }) => ({
Expand Down Expand Up @@ -207,6 +208,7 @@ export const TopWidget = ({
onTweet,
score,
rank,
currentScore,
}: TopWidgetProps): JSX.Element => {
const { sdkUrl: cdnUrl, gamePortalUrl } = useConfigContext();
const { address, isReady } = useWalletContext();
Expand Down Expand Up @@ -246,6 +248,11 @@ export const TopWidget = ({
<img src={`${cdnUrl}/assets/third-place-reward.svg`} alt="Third place reward" />
</RewardColumn>
</RewardsRow>
{!address && currentScore && (
<Typography align="center">
Your score <Rank>{currentScore}</Rank>
</Typography>
)}
{address && (
<Typography align="center">
Your rank <Rank>{rank}</Rank>
Expand Down
1 change: 1 addition & 0 deletions playground/src/Playground.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@ export const Playground = () => {

const modal = sdk.showLeaderboard({
onPlayAgain: () => modal.close(),
currentScore: 25,
});
}, [sdk]);

Expand Down

0 comments on commit f821a9f

Please sign in to comment.