2022-08-19 17:10:32 +00:00
|
|
|
// check every day if the user has created a bet since 4pm UTC, and if not, reset their streak
|
|
|
|
|
|
|
|
import * as functions from 'firebase-functions'
|
|
|
|
import * as admin from 'firebase-admin'
|
|
|
|
import { User } from '../../common/user'
|
|
|
|
import { DAY_MS } from '../../common/util/time'
|
2022-08-22 05:37:26 +00:00
|
|
|
import { BETTING_STREAK_RESET_HOUR } from '../../common/economy'
|
2022-08-19 17:10:32 +00:00
|
|
|
const firestore = admin.firestore()
|
|
|
|
|
|
|
|
export const resetBettingStreaksForUsers = functions.pubsub
|
|
|
|
.schedule(`0 ${BETTING_STREAK_RESET_HOUR} * * *`)
|
2022-08-22 22:36:39 +00:00
|
|
|
.timeZone('Etc/UTC')
|
2022-08-19 17:10:32 +00:00
|
|
|
.onRun(async () => {
|
|
|
|
await resetBettingStreaksInternal()
|
|
|
|
})
|
|
|
|
|
|
|
|
const resetBettingStreaksInternal = async () => {
|
2022-08-31 14:03:51 +00:00
|
|
|
const usersSnap = await firestore
|
|
|
|
.collection('users')
|
|
|
|
.where('currentBettingStreak', '>', 0)
|
|
|
|
.get()
|
2022-08-19 17:10:32 +00:00
|
|
|
|
|
|
|
const users = usersSnap.docs.map((doc) => doc.data() as User)
|
|
|
|
|
|
|
|
for (const user of users) {
|
|
|
|
await resetBettingStreakForUser(user)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
const resetBettingStreakForUser = async (user: User) => {
|
|
|
|
const betStreakResetTime = Date.now() - DAY_MS
|
|
|
|
// if they made a bet within the last day, don't reset their streak
|
|
|
|
if (
|
2022-08-29 19:47:24 +00:00
|
|
|
(user?.lastBetTime ?? 0) > betStreakResetTime ||
|
2022-08-19 17:10:32 +00:00
|
|
|
!user.currentBettingStreak ||
|
|
|
|
user.currentBettingStreak === 0
|
|
|
|
)
|
|
|
|
return
|
|
|
|
await firestore.collection('users').doc(user.id).update({
|
|
|
|
currentBettingStreak: 0,
|
|
|
|
})
|
|
|
|
}
|