From d6b0a1edc03b558ac0365b78056c00d21ae75966 Mon Sep 17 00:00:00 2001 From: Ian Philips Date: Wed, 14 Sep 2022 07:27:20 -0600 Subject: [PATCH] Betting streak reset to 7am UTC and store streak data on notif --- common/economy.ts | 2 +- common/notification.ts | 7 ++++++- functions/src/create-notification.ts | 6 ++++++ functions/src/on-create-bet.ts | 24 +++++++++++++++--------- web/pages/notifications.tsx | 13 +++++++------ 5 files changed, 35 insertions(+), 17 deletions(-) diff --git a/common/economy.ts b/common/economy.ts index c1449d4f..a412d4de 100644 --- a/common/economy.ts +++ b/common/economy.ts @@ -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 diff --git a/common/notification.ts b/common/notification.ts index 42dbbf35..47c55cc6 100644 --- a/common/notification.ts +++ b/common/notification.ts @@ -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§ion=${subscriptionType}`, } } + +export type BettingStreakData = { + streak: number + bonusAmount: number +} diff --git a/functions/src/create-notification.ts b/functions/src/create-notification.ts index e2959dda..34a8f218 100644 --- a/functions/src/create-notification.ts +++ b/functions/src/create-notification.ts @@ -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)) } diff --git a/functions/src/on-create-bet.ts b/functions/src/on-create-bet.ts index f54d6475..5fe3fd62 100644 --- a/functions/src/on-create-bet.ts +++ b/functions/src/on-create-bet.ts @@ -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) } diff --git a/web/pages/notifications.tsx b/web/pages/notifications.tsx index fcac8601..008f5df1 100644 --- a/web/pages/notifications.tsx +++ b/web/pages/notifications.tsx @@ -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,10 +443,11 @@ function IncomeNotificationItem(props: { reasonText = !simple ? `liked` : `in likes on` } - const streakInDays = - Date.now() - notification.createdTime > 24 * 60 * 60 * 1000 - ? parseInt(sourceText ?? '0') / BETTING_STREAK_BONUS_AMOUNT - : user?.currentBettingStreak ?? 0 + 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 = sourceType === 'betting_streak_bonus' && (sourceText