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 =
econ?.BETTING_STREAK_BONUS_AMOUNT ?? 10
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

View File

@ -18,7 +18,7 @@ export type Notification = {
sourceUserUsername?: string
sourceUserAvatarUrl?: string
sourceText?: string
data?: string
data?: { [key: string]: any }
sourceContractTitle?: string
sourceContractCreatorUsername?: string
@ -157,3 +157,8 @@ export const getDestinationsForUser = async (
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 {
BettingStreakData,
getDestinationsForUser,
Notification,
notification_reason_types,
@ -686,6 +687,7 @@ export const createBettingStreakBonusNotification = async (
bet: Bet,
contract: Contract,
amount: number,
streak: number,
idempotencyKey: string
) => {
const privateUser = await getPrivateUser(user.id)
@ -719,6 +721,10 @@ export const createBettingStreakBonusNotification = async (
sourceContractId: contract.id,
sourceContractTitle: contract.question,
sourceContractCreatorUsername: contract.creatorUsername,
data: {
streak: streak,
bonusAmount: amount,
} as BettingStreakData,
}
return await notificationRef.set(removeUndefinedProps(notification))
}

View File

@ -26,6 +26,7 @@ import { APIError } from '../../common/api'
import { User } from '../../common/user'
import { UNIQUE_BETTOR_LIQUIDITY_AMOUNT } from '../../common/antes'
import { addHouseLiquidity } from './add-liquidity'
import { DAY_MS } from '../../common/util/time'
const firestore = admin.firestore()
const BONUS_START_DATE = new Date('2022-07-13T15:30:00.000Z').getTime()
@ -80,12 +81,16 @@ const updateBettingStreak = async (
contract: Contract,
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
// If they've already bet after the reset time, or if we haven't hit the reset time yet
if (lastBetTime > betStreakResetTime || bet.createdTime < betStreakResetTime)
return
// If they've already bet after the reset time
if (lastBetTime > betStreakResetTime) return
const newBettingStreak = (user?.currentBettingStreak ?? 0) + 1
// Otherwise, add 1 to their betting streak
@ -128,6 +133,7 @@ const updateBettingStreak = async (
bet,
contract,
bonusAmount,
newBettingStreak,
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
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
const bonusTxnDetails = {
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)
}

View File

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