( uid: string, timestamp: number, )
| 1122 | } |
| 1123 | |
| 1124 | export async function updateStreak( |
| 1125 | uid: string, |
| 1126 | timestamp: number, |
| 1127 | ): Promise<number> { |
| 1128 | const user = await getPartialUser(uid, "calculate streak", ["streak"]); |
| 1129 | const streak: UserStreak = { |
| 1130 | lastResultTimestamp: user.streak?.lastResultTimestamp ?? 0, |
| 1131 | length: user.streak?.length ?? 0, |
| 1132 | maxLength: user.streak?.maxLength ?? 0, |
| 1133 | hourOffset: user.streak?.hourOffset, |
| 1134 | }; |
| 1135 | |
| 1136 | if (isYesterday(streak.lastResultTimestamp, streak.hourOffset ?? 0)) { |
| 1137 | streak.length += 1; |
| 1138 | } else if (!isToday(streak.lastResultTimestamp, streak.hourOffset ?? 0)) { |
| 1139 | void addImportantLog("streak_lost", streak, uid); |
| 1140 | streak.length = 1; |
| 1141 | } |
| 1142 | |
| 1143 | if (streak.length > streak.maxLength) { |
| 1144 | streak.maxLength = streak.length; |
| 1145 | } |
| 1146 | |
| 1147 | streak.lastResultTimestamp = timestamp; |
| 1148 | |
| 1149 | if (user.streak?.hourOffset === 0) { |
| 1150 | // todo this needs to be removed after a while |
| 1151 | delete streak.hourOffset; |
| 1152 | } |
| 1153 | |
| 1154 | await getUsersCollection().updateOne({ uid }, { $set: { streak } }); |
| 1155 | |
| 1156 | return streak.length; |
| 1157 | } |
| 1158 | |
| 1159 | export async function setStreakHourOffset( |
| 1160 | uid: string, |
nothing calls this directly
no test coverage detected