mirror of
https://github.com/monkeytypegame/monkeytype.git
synced 2026-03-10 23:36:08 +08:00
parent
f3340996f1
commit
5bf0c9a951
3 changed files with 31 additions and 33 deletions
|
|
@ -36,7 +36,7 @@ import { getDailyLeaderboard } from "../../utils/daily-leaderboards";
|
|||
import AutoRoleList from "../../constants/auto-roles";
|
||||
import * as UserDAL from "../../dal/user";
|
||||
import { buildMonkeyMail } from "../../utils/monkey-mail";
|
||||
import { updateStreak } from "./user";
|
||||
import { updateStreak } from "../../dal/user";
|
||||
|
||||
try {
|
||||
if (anticheatImplemented() === false) throw new Error("undefined");
|
||||
|
|
|
|||
|
|
@ -1,15 +1,10 @@
|
|||
import _, { eq } from "lodash";
|
||||
import _ from "lodash";
|
||||
import * as UserDAL from "../../dal/user";
|
||||
import MonkeyError from "../../utils/error";
|
||||
import Logger from "../../utils/logger";
|
||||
import { MonkeyResponse } from "../../utils/monkey-response";
|
||||
import { getDiscordUser } from "../../utils/discord";
|
||||
import {
|
||||
buildAgentLog,
|
||||
isToday,
|
||||
isYesterday,
|
||||
sanitizeString,
|
||||
} from "../../utils/misc";
|
||||
import { buildAgentLog, sanitizeString } from "../../utils/misc";
|
||||
import * as George from "../../tasks/george";
|
||||
import admin from "firebase-admin";
|
||||
import { deleteAllApeKeys } from "../../dal/ape-keys";
|
||||
|
|
@ -542,27 +537,3 @@ export async function updateInbox(
|
|||
|
||||
return new MonkeyResponse("Inbox updated");
|
||||
}
|
||||
|
||||
export async function updateStreak(uid, timestamp): Promise<number> {
|
||||
const user = await UserDAL.getUser(uid, "calculate streak");
|
||||
const streak: MonkeyTypes.UserStreak = {
|
||||
lastResultTimestamp: user.streak?.lastResultTimestamp ?? 0,
|
||||
length: user.streak?.length ?? 0,
|
||||
maxLength: user.streak?.length ?? 0,
|
||||
};
|
||||
|
||||
if (isYesterday(streak.lastResultTimestamp)) {
|
||||
streak.length += 1;
|
||||
} else if (!isToday(streak.lastResultTimestamp)) {
|
||||
streak.length = 1;
|
||||
}
|
||||
|
||||
if (streak.length > streak.maxLength) {
|
||||
streak.maxLength = streak.length;
|
||||
}
|
||||
|
||||
streak.lastResultTimestamp = timestamp;
|
||||
await UserDAL.getUsersCollection().updateOne({ uid }, { $set: { streak } });
|
||||
|
||||
return streak.length;
|
||||
}
|
||||
|
|
|
|||
|
|
@ -6,7 +6,7 @@ import * as db from "../init/db";
|
|||
import MonkeyError from "../utils/error";
|
||||
import { Collection, ObjectId, WithId, Long, UpdateFilter } from "mongodb";
|
||||
import Logger from "../utils/logger";
|
||||
import { flattenObjectDeep } from "../utils/misc";
|
||||
import { flattenObjectDeep, isToday, isYesterday } from "../utils/misc";
|
||||
|
||||
const SECONDS_PER_HOUR = 3600;
|
||||
|
||||
|
|
@ -890,3 +890,30 @@ export async function updateInbox(
|
|||
|
||||
await getUsersCollection().updateOne({ uid }, mergedUpdates);
|
||||
}
|
||||
|
||||
export async function updateStreak(
|
||||
uid: string,
|
||||
timestamp: number
|
||||
): Promise<number> {
|
||||
const user = await getUser(uid, "calculate streak");
|
||||
const streak: MonkeyTypes.UserStreak = {
|
||||
lastResultTimestamp: user.streak?.lastResultTimestamp ?? 0,
|
||||
length: user.streak?.length ?? 0,
|
||||
maxLength: user.streak?.length ?? 0,
|
||||
};
|
||||
|
||||
if (isYesterday(streak.lastResultTimestamp)) {
|
||||
streak.length += 1;
|
||||
} else if (!isToday(streak.lastResultTimestamp)) {
|
||||
streak.length = 1;
|
||||
}
|
||||
|
||||
if (streak.length > streak.maxLength) {
|
||||
streak.maxLength = streak.length;
|
||||
}
|
||||
|
||||
streak.lastResultTimestamp = timestamp;
|
||||
await getUsersCollection().updateOne({ uid }, { $set: { streak } });
|
||||
|
||||
return streak.length;
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue