Improve contract leaderboard computation (#918)
* Fix and clean up top comment stuff * Make leaderboard code generic on entry type * No need to look up users on contract leaderboard
This commit is contained in:
parent
b9fffcfa30
commit
6fe0a22a48
|
@ -3,9 +3,8 @@ import { resolvedPayout } from 'common/calculate'
|
||||||
import { Contract } from 'common/contract'
|
import { Contract } from 'common/contract'
|
||||||
import { formatMoney } from 'common/util/format'
|
import { formatMoney } from 'common/util/format'
|
||||||
import { groupBy, mapValues, sumBy, sortBy, keyBy } from 'lodash'
|
import { groupBy, mapValues, sumBy, sortBy, keyBy } from 'lodash'
|
||||||
import { useState, useMemo, useEffect } from 'react'
|
import { memo } from 'react'
|
||||||
import { useComments } from 'web/hooks/use-comments'
|
import { useComments } from 'web/hooks/use-comments'
|
||||||
import { listUsers, User } from 'web/lib/firebase/users'
|
|
||||||
import { FeedBet } from '../feed/feed-bets'
|
import { FeedBet } from '../feed/feed-bets'
|
||||||
import { FeedComment } from '../feed/feed-comments'
|
import { FeedComment } from '../feed/feed-comments'
|
||||||
import { Spacer } from '../layout/spacer'
|
import { Spacer } from '../layout/spacer'
|
||||||
|
@ -13,59 +12,48 @@ import { Leaderboard } from '../leaderboard'
|
||||||
import { Title } from '../title'
|
import { Title } from '../title'
|
||||||
import { BETTORS } from 'common/user'
|
import { BETTORS } from 'common/user'
|
||||||
|
|
||||||
export function ContractLeaderboard(props: {
|
export const ContractLeaderboard = memo(function ContractLeaderboard(props: {
|
||||||
contract: Contract
|
contract: Contract
|
||||||
bets: Bet[]
|
bets: Bet[]
|
||||||
}) {
|
}) {
|
||||||
const { contract, bets } = props
|
const { contract, bets } = props
|
||||||
const [users, setUsers] = useState<User[]>()
|
|
||||||
|
|
||||||
const { userProfits, top5Ids } = useMemo(() => {
|
// Create a map of userIds to total profits (including sales)
|
||||||
// Create a map of userIds to total profits (including sales)
|
const openBets = bets.filter((bet) => !bet.isSold && !bet.sale)
|
||||||
const openBets = bets.filter((bet) => !bet.isSold && !bet.sale)
|
const betsByUser = groupBy(openBets, 'userId')
|
||||||
const betsByUser = groupBy(openBets, 'userId')
|
const userProfits = mapValues(betsByUser, (bets) => {
|
||||||
|
return {
|
||||||
const userProfits = mapValues(betsByUser, (bets) =>
|
name: bets[0].userName,
|
||||||
sumBy(bets, (bet) => resolvedPayout(contract, bet) - bet.amount)
|
username: bets[0].userUsername,
|
||||||
)
|
avatarUrl: bets[0].userAvatarUrl,
|
||||||
// Find the 5 users with the most profits
|
total: sumBy(bets, (bet) => resolvedPayout(contract, bet) - bet.amount),
|
||||||
const top5Ids = Object.entries(userProfits)
|
|
||||||
.sort(([_i1, p1], [_i2, p2]) => p2 - p1)
|
|
||||||
.filter(([, p]) => p > 0)
|
|
||||||
.slice(0, 5)
|
|
||||||
.map(([id]) => id)
|
|
||||||
return { userProfits, top5Ids }
|
|
||||||
}, [contract, bets])
|
|
||||||
|
|
||||||
useEffect(() => {
|
|
||||||
if (top5Ids.length > 0) {
|
|
||||||
listUsers(top5Ids).then((users) => {
|
|
||||||
const sortedUsers = sortBy(users, (user) => -userProfits[user.id])
|
|
||||||
setUsers(sortedUsers)
|
|
||||||
})
|
|
||||||
}
|
}
|
||||||
}, [userProfits, top5Ids])
|
})
|
||||||
|
// Find the 5 users with the most profits
|
||||||
|
const top5 = Object.values(userProfits)
|
||||||
|
.sort((p1, p2) => p2.total - p1.total)
|
||||||
|
.filter((p) => p.total > 0)
|
||||||
|
.slice(0, 5)
|
||||||
|
|
||||||
return users && users.length > 0 ? (
|
return top5 && top5.length > 0 ? (
|
||||||
<Leaderboard
|
<Leaderboard
|
||||||
title={`🏅 Top ${BETTORS}`}
|
title={`🏅 Top ${BETTORS}`}
|
||||||
users={users || []}
|
entries={top5 || []}
|
||||||
columns={[
|
columns={[
|
||||||
{
|
{
|
||||||
header: 'Total profit',
|
header: 'Total profit',
|
||||||
renderCell: (user) => formatMoney(userProfits[user.id] || 0),
|
renderCell: (entry) => formatMoney(entry.total),
|
||||||
},
|
},
|
||||||
]}
|
]}
|
||||||
className="mt-12 max-w-sm"
|
className="mt-12 max-w-sm"
|
||||||
/>
|
/>
|
||||||
) : null
|
) : null
|
||||||
}
|
})
|
||||||
|
|
||||||
export function ContractTopTrades(props: { contract: Contract; bets: Bet[] }) {
|
export function ContractTopTrades(props: { contract: Contract; bets: Bet[] }) {
|
||||||
const { contract, bets } = props
|
const { contract, bets } = props
|
||||||
// todo: this stuff should be calced in DB at resolve time
|
// todo: this stuff should be calced in DB at resolve time
|
||||||
const comments = useComments(contract.id)
|
const comments = useComments(contract.id)
|
||||||
const commentsById = keyBy(comments, 'id')
|
|
||||||
const betsById = keyBy(bets, 'id')
|
const betsById = keyBy(bets, 'id')
|
||||||
|
|
||||||
// If 'id2' is the sale of 'id1', both are logged with (id2 - id1) of profit
|
// If 'id2' is the sale of 'id1', both are logged with (id2 - id1) of profit
|
||||||
|
@ -86,29 +74,23 @@ export function ContractTopTrades(props: { contract: Contract; bets: Bet[] }) {
|
||||||
const topBetId = sortBy(bets, (b) => -profitById[b.id])[0]?.id
|
const topBetId = sortBy(bets, (b) => -profitById[b.id])[0]?.id
|
||||||
const topBettor = betsById[topBetId]?.userName
|
const topBettor = betsById[topBetId]?.userName
|
||||||
|
|
||||||
// And also the commentId of the comment with the highest profit
|
// And also the comment with the highest profit
|
||||||
const topCommentId = sortBy(
|
const topComment = sortBy(comments, (c) => c.betId && -profitById[c.betId])[0]
|
||||||
comments,
|
|
||||||
(c) => c.betId && -profitById[c.betId]
|
|
||||||
)[0]?.id
|
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<div className="mt-12 max-w-sm">
|
<div className="mt-12 max-w-sm">
|
||||||
{topCommentId && profitById[topCommentId] > 0 && (
|
{topComment && profitById[topComment.id] > 0 && (
|
||||||
<>
|
<>
|
||||||
<Title text="💬 Proven correct" className="!mt-0" />
|
<Title text="💬 Proven correct" className="!mt-0" />
|
||||||
<div className="relative flex items-start space-x-3 rounded-md bg-gray-50 px-2 py-4">
|
<div className="relative flex items-start space-x-3 rounded-md bg-gray-50 px-2 py-4">
|
||||||
<FeedComment
|
<FeedComment contract={contract} comment={topComment} />
|
||||||
contract={contract}
|
|
||||||
comment={commentsById[topCommentId]}
|
|
||||||
/>
|
|
||||||
</div>
|
</div>
|
||||||
<Spacer h={16} />
|
<Spacer h={16} />
|
||||||
</>
|
</>
|
||||||
)}
|
)}
|
||||||
|
|
||||||
{/* If they're the same, only show the comment; otherwise show both */}
|
{/* If they're the same, only show the comment; otherwise show both */}
|
||||||
{topBettor && topBetId !== topCommentId && profitById[topBetId] > 0 && (
|
{topBettor && topBetId !== topComment?.betId && profitById[topBetId] > 0 && (
|
||||||
<>
|
<>
|
||||||
<Title text="💸 Best bet" className="!mt-0" />
|
<Title text="💸 Best bet" className="!mt-0" />
|
||||||
<div className="relative flex items-start space-x-3 rounded-md bg-gray-50 px-2 py-4">
|
<div className="relative flex items-start space-x-3 rounded-md bg-gray-50 px-2 py-4">
|
||||||
|
|
|
@ -1,28 +1,33 @@
|
||||||
import clsx from 'clsx'
|
import clsx from 'clsx'
|
||||||
import { User } from 'common/user'
|
|
||||||
import { Avatar } from './avatar'
|
import { Avatar } from './avatar'
|
||||||
import { Row } from './layout/row'
|
import { Row } from './layout/row'
|
||||||
import { SiteLink } from './site-link'
|
import { SiteLink } from './site-link'
|
||||||
import { Title } from './title'
|
import { Title } from './title'
|
||||||
|
|
||||||
export function Leaderboard(props: {
|
interface LeaderboardEntry {
|
||||||
|
username: string
|
||||||
|
name: string
|
||||||
|
avatarUrl?: string
|
||||||
|
}
|
||||||
|
|
||||||
|
export function Leaderboard<T extends LeaderboardEntry>(props: {
|
||||||
title: string
|
title: string
|
||||||
users: User[]
|
entries: T[]
|
||||||
columns: {
|
columns: {
|
||||||
header: string
|
header: string
|
||||||
renderCell: (user: User) => any
|
renderCell: (entry: T) => any
|
||||||
}[]
|
}[]
|
||||||
className?: string
|
className?: string
|
||||||
maxToShow?: number
|
maxToShow?: number
|
||||||
}) {
|
}) {
|
||||||
// TODO: Ideally, highlight your own entry on the leaderboard
|
// TODO: Ideally, highlight your own entry on the leaderboard
|
||||||
const { title, columns, className } = props
|
const { title, columns, className } = props
|
||||||
const maxToShow = props.maxToShow ?? props.users.length
|
const maxToShow = props.maxToShow ?? props.entries.length
|
||||||
const users = props.users.slice(0, maxToShow)
|
const entries = props.entries.slice(0, maxToShow)
|
||||||
return (
|
return (
|
||||||
<div className={clsx('w-full px-1', className)}>
|
<div className={clsx('w-full px-1', className)}>
|
||||||
<Title text={title} className="!mt-0" />
|
<Title text={title} className="!mt-0" />
|
||||||
{users.length === 0 ? (
|
{entries.length === 0 ? (
|
||||||
<div className="ml-2 text-gray-500">None yet</div>
|
<div className="ml-2 text-gray-500">None yet</div>
|
||||||
) : (
|
) : (
|
||||||
<div className="overflow-x-auto">
|
<div className="overflow-x-auto">
|
||||||
|
@ -37,19 +42,19 @@ export function Leaderboard(props: {
|
||||||
</tr>
|
</tr>
|
||||||
</thead>
|
</thead>
|
||||||
<tbody>
|
<tbody>
|
||||||
{users.map((user, index) => (
|
{entries.map((entry, index) => (
|
||||||
<tr key={user.id}>
|
<tr key={index}>
|
||||||
<td>{index + 1}</td>
|
<td>{index + 1}</td>
|
||||||
<td className="max-w-[190px]">
|
<td className="max-w-[190px]">
|
||||||
<SiteLink className="relative" href={`/${user.username}`}>
|
<SiteLink className="relative" href={`/${entry.username}`}>
|
||||||
<Row className="items-center gap-4">
|
<Row className="items-center gap-4">
|
||||||
<Avatar avatarUrl={user.avatarUrl} size={8} />
|
<Avatar avatarUrl={entry.avatarUrl} size={8} />
|
||||||
<div className="truncate">{user.name}</div>
|
<div className="truncate">{entry.name}</div>
|
||||||
</Row>
|
</Row>
|
||||||
</SiteLink>
|
</SiteLink>
|
||||||
</td>
|
</td>
|
||||||
{columns.map((column) => (
|
{columns.map((column) => (
|
||||||
<td key={column.header}>{column.renderCell(user)}</td>
|
<td key={column.header}>{column.renderCell(entry)}</td>
|
||||||
))}
|
))}
|
||||||
</tr>
|
</tr>
|
||||||
))}
|
))}
|
||||||
|
|
|
@ -403,7 +403,7 @@ function GroupLeaderboard(props: {
|
||||||
return (
|
return (
|
||||||
<Leaderboard
|
<Leaderboard
|
||||||
className="max-w-xl"
|
className="max-w-xl"
|
||||||
users={topUsers.map((t) => t.user)}
|
entries={topUsers.map((t) => t.user)}
|
||||||
title={title}
|
title={title}
|
||||||
columns={[
|
columns={[
|
||||||
{ header, renderCell: (user) => formatMoney(scoresByUser[user.id]) },
|
{ header, renderCell: (user) => formatMoney(scoresByUser[user.id]) },
|
||||||
|
|
|
@ -81,7 +81,7 @@ export default function Leaderboards(_props: {
|
||||||
<Col className="mx-4 items-center gap-10 lg:flex-row">
|
<Col className="mx-4 items-center gap-10 lg:flex-row">
|
||||||
<Leaderboard
|
<Leaderboard
|
||||||
title={`🏅 Top ${BETTORS}`}
|
title={`🏅 Top ${BETTORS}`}
|
||||||
users={topTraders}
|
entries={topTraders}
|
||||||
columns={[
|
columns={[
|
||||||
{
|
{
|
||||||
header: 'Total profit',
|
header: 'Total profit',
|
||||||
|
@ -92,7 +92,7 @@ export default function Leaderboards(_props: {
|
||||||
|
|
||||||
<Leaderboard
|
<Leaderboard
|
||||||
title="🏅 Top creators"
|
title="🏅 Top creators"
|
||||||
users={topCreators}
|
entries={topCreators}
|
||||||
columns={[
|
columns={[
|
||||||
{
|
{
|
||||||
header: 'Total bet',
|
header: 'Total bet',
|
||||||
|
@ -106,7 +106,7 @@ export default function Leaderboards(_props: {
|
||||||
<Col className="mx-4 my-10 items-center gap-10 lg:mx-0 lg:w-1/2 lg:flex-row">
|
<Col className="mx-4 my-10 items-center gap-10 lg:mx-0 lg:w-1/2 lg:flex-row">
|
||||||
<Leaderboard
|
<Leaderboard
|
||||||
title="🏅 Top followed"
|
title="🏅 Top followed"
|
||||||
users={topFollowed}
|
entries={topFollowed}
|
||||||
columns={[
|
columns={[
|
||||||
{
|
{
|
||||||
header: 'Total followers',
|
header: 'Total followers',
|
||||||
|
|
Loading…
Reference in New Issue
Block a user