~travisshears/oblastle-backend

ref: e59ff6d0 oblastle-backend/src/answer.ts -rw-r--r-- 1.4 KiB
e59ff6d0 — Travis Shears speed up integration test 6 months ago
                                                                                
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
import genRandom from 'seed-random';
import config from 'config';

const seed: string = config.get('RANDOM_SEED');

export const shuffle = (ogArray: any[], round: number) => {
    const a = [...ogArray];
    const randomInt = genRandom(`${seed},${round}`);
    let currentIndex = a.length;
    let temporaryValue;
    let randomIndex;
    // While there remain elements to shuffle...
    while (0 !== currentIndex) {
        // Pick a remaining element...
        randomIndex = Math.floor(randomInt() * currentIndex);
        currentIndex -= 1;
        // And swap it with the current element.
        temporaryValue = a[currentIndex];
        a[currentIndex] = a[randomIndex];
        a[randomIndex] = temporaryValue;
    }
    return a;
};

const startDate = new Date('01/01/2022');
export const getDayCount = (now: Date) => {
    const diffInTime = now.getTime() - startDate.getTime();
    const diffInDays = diffInTime / (1000 * 3600 * 24);
    return Math.floor(diffInDays);
};

export const getAnswer = (keys: string[], now = new Date()): { answer: string; puzzleCount: number } => {
    const dayCount = getDayCount(now);
    const oblastCount = keys.length;

    const round = Math.floor(dayCount / oblastCount);
    const answerIndex = dayCount % oblastCount;
    const shuffledKeys = shuffle(keys, round);
    return {
        answer: shuffledKeys[answerIndex],
        puzzleCount: dayCount
    };
};