Betting streak reset to 7am UTC and store streak data on notif

This commit is contained in:
Ian Philips 2022-09-14 07:27:20 -06:00
parent a2d61a1daa
commit d6b0a1edc0
5 changed files with 35 additions and 17 deletions

View File

@ -13,5 +13,5 @@ export const UNIQUE_BETTOR_BONUS_AMOUNT = econ?.UNIQUE_BETTOR_BONUS_AMOUNT ?? 10
export const BETTING_STREAK_BONUS_AMOUNT = export const BETTING_STREAK_BONUS_AMOUNT =
econ?.BETTING_STREAK_BONUS_AMOUNT ?? 10 econ?.BETTING_STREAK_BONUS_AMOUNT ?? 10
export const BETTING_STREAK_BONUS_MAX = econ?.BETTING_STREAK_BONUS_MAX ?? 50 export const BETTING_STREAK_BONUS_MAX = econ?.BETTING_STREAK_BONUS_MAX ?? 50
export const BETTING_STREAK_RESET_HOUR = econ?.BETTING_STREAK_RESET_HOUR ?? 0 export const BETTING_STREAK_RESET_HOUR = econ?.BETTING_STREAK_RESET_HOUR ?? 7
export const FREE_MARKETS_PER_USER_MAX = econ?.FREE_MARKETS_PER_USER_MAX ?? 5 export const FREE_MARKETS_PER_USER_MAX = econ?.FREE_MARKETS_PER_USER_MAX ?? 5

View File

@ -18,7 +18,7 @@ export type Notification = {
sourceUserUsername?: string sourceUserUsername?: string
sourceUserAvatarUrl?: string sourceUserAvatarUrl?: string
sourceText?: string sourceText?: string
data?: string data?: { [key: string]: any }
sourceContractTitle?: string sourceContractTitle?: string
sourceContractCreatorUsername?: string sourceContractCreatorUsername?: string
@ -157,3 +157,8 @@ export const getDestinationsForUser = async (
urlToManageThisNotification: `${DOMAIN}/notifications?tab=settings&section=${subscriptionType}`, urlToManageThisNotification: `${DOMAIN}/notifications?tab=settings&section=${subscriptionType}`,
} }
} }
export type BettingStreakData = {
streak: number
bonusAmount: number
}

View File

@ -1,5 +1,6 @@
import * as admin from 'firebase-admin' import * as admin from 'firebase-admin'
import { import {
BettingStreakData,
getDestinationsForUser, getDestinationsForUser,
Notification, Notification,
notification_reason_types, notification_reason_types,
@ -686,6 +687,7 @@ export const createBettingStreakBonusNotification = async (
bet: Bet, bet: Bet,
contract: Contract, contract: Contract,
amount: number, amount: number,
streak: number,
idempotencyKey: string idempotencyKey: string
) => { ) => {
const privateUser = await getPrivateUser(user.id) const privateUser = await getPrivateUser(user.id)
@ -719,6 +721,10 @@ export const createBettingStreakBonusNotification = async (
sourceContractId: contract.id, sourceContractId: contract.id,
sourceContractTitle: contract.question, sourceContractTitle: contract.question,
sourceContractCreatorUsername: contract.creatorUsername, sourceContractCreatorUsername: contract.creatorUsername,
data: {
streak: streak,
bonusAmount: amount,
} as BettingStreakData,
} }
return await notificationRef.set(removeUndefinedProps(notification)) return await notificationRef.set(removeUndefinedProps(notification))
} }

View File

@ -26,6 +26,7 @@ import { APIError } from '../../common/api'
import { User } from '../../common/user' import { User } from '../../common/user'
import { UNIQUE_BETTOR_LIQUIDITY_AMOUNT } from '../../common/antes' import { UNIQUE_BETTOR_LIQUIDITY_AMOUNT } from '../../common/antes'
import { addHouseLiquidity } from './add-liquidity' import { addHouseLiquidity } from './add-liquidity'
import { DAY_MS } from '../../common/util/time'
const firestore = admin.firestore() const firestore = admin.firestore()
const BONUS_START_DATE = new Date('2022-07-13T15:30:00.000Z').getTime() const BONUS_START_DATE = new Date('2022-07-13T15:30:00.000Z').getTime()
@ -80,12 +81,16 @@ const updateBettingStreak = async (
contract: Contract, contract: Contract,
eventId: string eventId: string
) => { ) => {
const betStreakResetTime = getTodaysBettingStreakResetTime() const now = Date.now()
const currentDateResetTime = currentDateBettingStreakResetTime()
// if now is before reset time, use yesterday's reset time
const lastDateResetTime = currentDateResetTime - DAY_MS
const betStreakResetTime =
now < currentDateResetTime ? lastDateResetTime : currentDateResetTime
const lastBetTime = user?.lastBetTime ?? 0 const lastBetTime = user?.lastBetTime ?? 0
// If they've already bet after the reset time, or if we haven't hit the reset time yet // If they've already bet after the reset time
if (lastBetTime > betStreakResetTime || bet.createdTime < betStreakResetTime) if (lastBetTime > betStreakResetTime) return
return
const newBettingStreak = (user?.currentBettingStreak ?? 0) + 1 const newBettingStreak = (user?.currentBettingStreak ?? 0) + 1
// Otherwise, add 1 to their betting streak // Otherwise, add 1 to their betting streak
@ -128,6 +133,7 @@ const updateBettingStreak = async (
bet, bet,
contract, contract,
bonusAmount, bonusAmount,
newBettingStreak,
eventId eventId
) )
} }
@ -170,13 +176,13 @@ const updateUniqueBettorsAndGiveCreatorBonus = async (
}) })
} }
if (contract.mechanism === 'cpmm-1' && isNewUniqueBettor) {
await addHouseLiquidity(contract, UNIQUE_BETTOR_LIQUIDITY_AMOUNT)
}
// No need to give a bonus for the creator's bet // No need to give a bonus for the creator's bet
if (!isNewUniqueBettor || bettor.id == contract.creatorId) return if (!isNewUniqueBettor || bettor.id == contract.creatorId) return
if (contract.mechanism === 'cpmm-1') {
await addHouseLiquidity(contract, UNIQUE_BETTOR_LIQUIDITY_AMOUNT)
}
// Create combined txn for all new unique bettors // Create combined txn for all new unique bettors
const bonusTxnDetails = { const bonusTxnDetails = {
contractId: contract.id, contractId: contract.id,
@ -259,6 +265,6 @@ const notifyFills = async (
) )
} }
const getTodaysBettingStreakResetTime = () => { const currentDateBettingStreakResetTime = () => {
return new Date().setUTCHours(BETTING_STREAK_RESET_HOUR, 0, 0, 0) return new Date().setUTCHours(BETTING_STREAK_RESET_HOUR, 0, 0, 0)
} }

View File

@ -298,7 +298,7 @@ function IncomeNotificationGroupItem(props: {
...notificationsForSourceTitle[0], ...notificationsForSourceTitle[0],
sourceText: sum.toString(), sourceText: sum.toString(),
sourceUserUsername: notificationsForSourceTitle[0].sourceUserUsername, sourceUserUsername: notificationsForSourceTitle[0].sourceUserUsername,
data: JSON.stringify(uniqueUsers), data: { uniqueUsers },
} }
newNotifications.push(newNotification) newNotifications.push(newNotification)
} }
@ -415,7 +415,7 @@ function IncomeNotificationItem(props: {
const isTip = sourceType === 'tip' || sourceType === 'tip_and_like' const isTip = sourceType === 'tip' || sourceType === 'tip_and_like'
const isUniqueBettorBonus = sourceType === 'bonus' const isUniqueBettorBonus = sourceType === 'bonus'
const userLinks: MultiUserLinkInfo[] = const userLinks: MultiUserLinkInfo[] =
isTip || isUniqueBettorBonus ? JSON.parse(data ?? '{}') : [] isTip || isUniqueBettorBonus ? data?.uniqueUsers ?? [] : []
useEffect(() => { useEffect(() => {
setNotificationsAsSeen([notification]) setNotificationsAsSeen([notification])
@ -443,8 +443,9 @@ function IncomeNotificationItem(props: {
reasonText = !simple ? `liked` : `in likes on` reasonText = !simple ? `liked` : `in likes on`
} }
const streakInDays = const streakInDays = notification.data?.streak
Date.now() - notification.createdTime > 24 * 60 * 60 * 1000 ? notification.data?.streak
: Date.now() - notification.createdTime > 24 * 60 * 60 * 1000
? parseInt(sourceText ?? '0') / BETTING_STREAK_BONUS_AMOUNT ? parseInt(sourceText ?? '0') / BETTING_STREAK_BONUS_AMOUNT
: user?.currentBettingStreak ?? 0 : user?.currentBettingStreak ?? 0
const bettingStreakText = const bettingStreakText =