Skip to content

Commit

Permalink
getRecommendedLevel dont show a level they have played recently
Browse files Browse the repository at this point in the history
  • Loading branch information
k2xl committed Sep 30, 2023
1 parent 959a72c commit fbc29b7
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 1 deletion.
11 changes: 10 additions & 1 deletion pages/api/home/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import { getLatestReviews } from '../latest-reviews';
import { getLevelOfDay } from '../level-of-day';
import { getLastLevelPlayed } from '../play-attempt';
import { doQuery } from '../search';
import { getPlayAttempts } from '../user/play-history';

async function getTopLevelsThisMonth(reqUser: User) {
const query = {
Expand Down Expand Up @@ -60,15 +61,22 @@ async function getRecentAverageDifficulty(reqUser: User, numResults = 1) {

async function getRecommendedLevel(reqUser: User) {
const avgDifficulty = await getRecentAverageDifficulty(reqUser, 10);
const levelsPlayedHour = await getPlayAttempts(reqUser, {
// query where datetime is greater than 1 hour ago so we don't recommend levels they have recently tried
datetime: new Date(Date.now() - 1 * 60 * 60 * 1000)
});

const uniqueLevelIdsFromPastHour = new Set(levelsPlayedHour.map(playAttempt => playAttempt.levelId._id.toString()));

const query = {
disableCount: 'true',
excludeLevelIds: [...uniqueLevelIdsFromPastHour].join(','),
minSteps: '7',
maxSteps: '2500',
minDifficulty: String(avgDifficulty * 0.9), // 10% below average of last 10
minRating: '0.55',
maxRating: '1',
numResults: '10', // randomly select one of these
numResults: '20', // randomly select one of these
sortBy: 'calcDifficultyEstimate',
sortDir: 'asc',
statFilter: StatFilter.HideWon,
Expand All @@ -82,6 +90,7 @@ async function getRecommendedLevel(reqUser: User) {
// try a broader query without min and max difficulty for those rare users that have beaten so many levels to not have any recommended one
const query = {
disableCount: 'true',
excludeLevelIds: [...uniqueLevelIdsFromPastHour].join(','),
minSteps: '7',
maxSteps: '2500',
minRating: '0.55',
Expand Down
6 changes: 6 additions & 0 deletions pages/api/search/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -154,6 +154,12 @@ export async function doQuery(query: SearchQuery, reqUser?: User | null, project
}
}

if (query.excludeLevelIds) {
const excludeLevelIds = query.excludeLevelIds.split(',');

searchObj['_id'] = { $nin: excludeLevelIds.map((id) => new Types.ObjectId(id)) };
}

if (query.minSteps && query.maxSteps) {
searchObj['leastMoves'] = {
$gte: parseInt(query.minSteps),
Expand Down
2 changes: 2 additions & 0 deletions pages/search/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import StatFilter from '@root/constants/statFilter';
import isPro from '@root/helpers/isPro';
import classNames from 'classnames';
import { debounce } from 'debounce';
import { Types } from 'mongoose';
import { GetServerSidePropsContext, NextApiRequest } from 'next';
import Image from 'next/image';
import Link from 'next/link';
Expand Down Expand Up @@ -38,6 +39,7 @@ export interface SearchQuery extends ParsedUrlQuery {
blockFilter?: string;
difficultyFilter?: string;
disableCount?: string;
excludeLevelIds?: string;
maxDifficulty?: string;
maxDimension1?: string;
maxDimension2?: string;
Expand Down

0 comments on commit fbc29b7

Please sign in to comment.