manifold/web/lib/firebase/bets.ts

127 lines
3.2 KiB
TypeScript
Raw Normal View History

import {
collection,
collectionGroup,
query,
where,
2022-02-02 06:40:46 +00:00
orderBy,
} from 'firebase/firestore'
2022-01-05 06:32:52 +00:00
import _ from 'lodash'
import { db } from './init'
import { Bet } from '../../../common/bet'
2022-01-13 17:01:20 +00:00
import { Contract } from '../../../common/contract'
import { getValues, listenForValues } from './utils'
export type { Bet }
2021-12-10 17:14:05 +00:00
2021-12-12 22:14:52 +00:00
function getBetsCollection(contractId: string) {
return collection(db, 'contracts', contractId, 'bets')
}
export async function listAllBets(contractId: string) {
const bets = await getValues<Bet>(getBetsCollection(contractId))
bets.sort((bet1, bet2) => bet1.createdTime - bet2.createdTime)
return bets
}
2022-02-02 06:40:46 +00:00
const DAY_IN_MS = 24 * 60 * 60 * 1000
// Define "recent" as "<24 hours ago" for now
const recentBetsQuery = query(
collectionGroup(db, 'bets'),
where('createdTime', '>', Date.now() - DAY_IN_MS),
orderBy('createdTime', 'desc')
)
export async function getRecentBets() {
return getValues<Bet>(recentBetsQuery)
}
2022-02-21 03:06:10 +00:00
export function listenForRecentBets(setBets: (bets: Bet[]) => void) {
return listenForValues<Bet>(recentBetsQuery, setBets)
}
2022-02-03 05:58:27 +00:00
export async function getRecentContractBets(contractId: string) {
const q = query(
getBetsCollection(contractId),
where('createdTime', '>', Date.now() - DAY_IN_MS),
orderBy('createdTime', 'desc')
)
return getValues<Bet>(q)
}
2021-12-12 22:14:52 +00:00
export function listenForBets(
contractId: string,
setBets: (bets: Bet[]) => void
) {
return listenForValues<Bet>(getBetsCollection(contractId), (bets) => {
2021-12-12 22:14:52 +00:00
bets.sort((bet1, bet2) => bet1.createdTime - bet2.createdTime)
setBets(bets)
})
2021-12-10 17:14:05 +00:00
}
export function listenForUserBets(
userId: string,
setBets: (bets: Bet[]) => void
) {
const userQuery = query(
collectionGroup(db, 'bets'),
where('userId', '==', userId)
)
return listenForValues<Bet>(userQuery, (bets) => {
bets.sort((bet1, bet2) => bet1.createdTime - bet2.createdTime)
setBets(bets)
})
}
2022-01-13 17:01:20 +00:00
export function listenForUserContractBets(
userId: string,
contractId: string,
setBets: (bets: Bet[]) => void
) {
const betsQuery = query(
collection(db, 'contracts', contractId, 'bets'),
where('userId', '==', userId)
)
return listenForValues<Bet>(betsQuery, (bets) => {
bets.sort((bet1, bet2) => bet1.createdTime - bet2.createdTime)
setBets(bets)
})
}
export function withoutAnteBets(contract: Contract, bets?: Bet[]) {
2022-01-13 17:01:20 +00:00
const { createdTime } = contract
if (
bets &&
2022-01-13 17:01:20 +00:00
bets.length >= 2 &&
bets[0].createdTime === createdTime &&
bets[1].createdTime === createdTime
) {
return bets.slice(2)
}
return bets?.filter((bet) => !bet.isAnte) ?? []
2022-01-13 17:01:20 +00:00
}
const getBetsQuery = (startTime: number, endTime: number) =>
query(
collectionGroup(db, 'bets'),
where('createdTime', '>=', startTime),
where('createdTime', '<', endTime),
orderBy('createdTime', 'asc')
)
export async function getDailyBets(startTime: number, numberOfDays: number) {
const query = getBetsQuery(startTime, startTime + DAY_IN_MS * numberOfDays)
const bets = await getValues<Bet>(query)
const betsByDay = _.range(0, numberOfDays).map(() => [] as Bet[])
for (const bet of bets) {
const dayIndex = Math.floor((bet.createdTime - startTime) / DAY_IN_MS)
betsByDay[dayIndex].push(bet)
}
return betsByDay
}