Skip to content

Commit

Permalink
Merge pull request #63 from ahmedmahmud/sm3421/cache-solved
Browse files Browse the repository at this point in the history
Repeat handling on solved cache
  • Loading branch information
sreeshmaheshwar authored Mar 10, 2024
2 parents 26b74ef + 78e0529 commit 45dbf3b
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 27 deletions.
4 changes: 2 additions & 2 deletions app/api/puzzle/nextPuzzle/nextFor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -216,7 +216,7 @@ const nextPuzzleFor = async (
`Worked! Puzzle Id: ${puzzleToReview.PuzzleId} from Leitner, tags: ${puzzleToReview.hierarchy_tags}`
);
const [similarPuzzle] = await similarBatchForCompromised(
user.username,
user,
[puzzleToReview],
clampRating(rating.rating),
exceptions,
Expand All @@ -227,7 +227,7 @@ const nextPuzzleFor = async (
console.log(
`Got similar puzzle with tags ${similarPuzzle.hierarchy_tags} and line ${similarPuzzle.Moves}`
);
// TODO: If puzzle === similar puzzle so no similar puzzle?

if (group) {
await ActivePuzzleColl.updateOne(
{ username: user.username },
Expand Down
38 changes: 18 additions & 20 deletions app/api/puzzle/similarBatch/similarBatchFor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,19 +33,19 @@ const INITIAL_COMPROMISE = 2;
const MAX_COMPROMISE = 4;

export const similarBatchForCompromised = async (
username: string,
user: User,
lastBatch: Puzzle[],
clampedRating: number,
solvedArray: string[],
minBatchFactor: number = 2,
persist: boolean = true,
compromise: number = INITIAL_COMPROMISE
): Promise<Puzzle[]> => {
const username = user.username;
const ret = await Promise.all(
lastBatch.map(async (puzzle) => {
let instance: SimilarityInstance | undefined =
await findSimilarityInstance(puzzle.PuzzleId);
let similarPuzzleId: String;

if (!instance) {
const similarPuzzles = await computeSimilarityCache(puzzle);
Expand All @@ -56,26 +56,24 @@ export const similarBatchForCompromised = async (
await SimilarityColl.create(instanceCreated);
instance = instanceCreated;
}
similarPuzzleId = await findSimilarUndoPuzzle(instance, username);

if (similarPuzzleId == 'Whole cache has been solved.') {
const candidates = await preprocessing(
username,
lastBatch,
clampedRating,
solvedArray,
minBatchFactor,
compromise
);
return similarBatchForCompromisedHelper(
username,
puzzle,
solvedArray,
candidates
let similarPuzzleId = await findSimilarUndoPuzzle(instance, user);
if (similarPuzzleId === 'Whole cache has been solved.') {
const solved = await getUserSolvedPuzzleIDs(user);
const cache = new Set(instance.cache);
let i = 0;
while (i < solved.length && !cache.has(solved[i])) {
i++;
}
similarPuzzleId = solved[i];
const newSolved = solved.slice(0, i).concat(solved.slice(i + 1));
await AllRoundColl.updateOne(
{ username: username },
{ $set: { solved: newSolved } }
);
} else {
return (await findPuzzlebyId(similarPuzzleId)) as Puzzle;
}

return (await findPuzzlebyId(similarPuzzleId)) as Puzzle;
})
);

Expand Down Expand Up @@ -187,7 +185,7 @@ const similarBatchFor = async (user: User): Promise<Puzzle[]> => {
const { rating } = await getExistingUserRating(user);
const solvedArray = await getUserSolvedPuzzleIDs(user);
return await similarBatchForCompromised(
user.username,
user,
lastBatch,
clampRating(rating),
solvedArray
Expand Down
13 changes: 8 additions & 5 deletions src/similarityCache.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,11 @@ import similarity_distance from '../src/similarity';
import { SimilarityColl } from '../models/SimilarityColl';
import { AllRoundColl } from '@/models/AllRoundColl';
import { getExistingUserRatingByName } from './rating/getRating';
import { clampRating } from '@/app/api/puzzle/nextPuzzle/nextFor';
import {
clampRating,
getUserSolvedPuzzleIDs,
} from '@/app/api/puzzle/nextPuzzle/nextFor';
import { User } from 'lucia';

const cache_size = 30;

Expand Down Expand Up @@ -82,14 +86,13 @@ export const computeSimilarityCache = async (

export const findSimilarUndoPuzzle = async (
instance: SimilarityInstance,
userName: String
user: User
): Promise<String> => {
const cache = instance.cache;
const curUser = await AllRoundColl.findOne({ username: userName });
const solved = curUser.solved;
const solved = new Set(await getUserSolvedPuzzleIDs(user));
for (let i = 0; i < cache.length; i++) {
const curPuzzleId = cache[i];
if (solved.includes(curPuzzleId)) {
if (solved.has(curPuzzleId.toString())) {
continue;
} else {
return curPuzzleId;
Expand Down

0 comments on commit 45dbf3b

Please sign in to comment.