All files / games/src/opend6 difficulty.ts

100% Statements 39/39
100% Branches 17/17
100% Functions 6/6
100% Lines 38/38

Press n or j to go to the next uncovered block, b, p or k for the previous block.

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 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 8442x 42x 42x 42x 42x 42x 42x                           42x 30x             42x 42x 42x 42x 42x   42x 30x   30x 25x     30x 20x     30x 15x     30x 10x     30x 5x     30x     42x 30x   30x     42x 30x   30x   30x     42x 60x 60x   60x          
export enum RollDifficultyLabel {
    VERY_EASY = "Very Easy",
    EASY = "Easy",
    MODERATE = "Moderate",
    DIFFICULT = "Difficult",
    VERY_DIFFICULT = "Very Difficult",
    HEROIC = "Heroic",
}
 
export interface IRollDifficulty {
    label: RollDifficultyLabel;
    dieCode: string;
    meanResult: number;
}
 
export interface IRollDieCode {
    modifier: number;
    number: number;
}
 
export const getDifficulty = (target: number): IRollDifficulty => {
    return {
        label: getDifficultyLabel(target),
        dieCode: getDifficultyDieCode(target),
        meanResult: getDifficultyMeanResult(target),
    };
};
 
const VERY_EASY_THRESHOLD = 5;
const EASY_THRESHOLD = 10;
const MODERATE_THRESHOLD = 15;
const DIFFICULT_THRESHOLD = 20;
const VERY_DIFFICULT_THRESHOLD = 25;
 
const getDifficultyLabel = (target: number): RollDifficultyLabel => {
    let label: RollDifficultyLabel = RollDifficultyLabel.VERY_EASY;
 
    if (target > VERY_EASY_THRESHOLD) {
        label = RollDifficultyLabel.EASY;
    }
 
    if (target > EASY_THRESHOLD) {
        label = RollDifficultyLabel.MODERATE;
    }
 
    if (target > MODERATE_THRESHOLD) {
        label = RollDifficultyLabel.DIFFICULT;
    }
 
    if (target > DIFFICULT_THRESHOLD) {
        label = RollDifficultyLabel.VERY_DIFFICULT;
    }
 
    if (target > VERY_DIFFICULT_THRESHOLD) {
        label = RollDifficultyLabel.HEROIC;
    }
 
    return label;
};
 
const getDifficultyMeanResult = (target: number): number => {
    const dieCode = getDieCode(target);
 
    return target > 3 ? dieCode.number * 3.5 + dieCode.modifier : target;
};
 
const getDifficultyDieCode = (target: number): string => {
    const dieCode = getDieCode(target);
 
    const dieModifierString: string = dieCode.modifier > 0 ? `+${dieCode.modifier}` : "";
 
    return dieCode.number > 0 ? `${dieCode.number}D${dieModifierString}` : target.toString();
};
 
const getDieCode = (target: number): IRollDieCode => {
    const dieNumber: number = target > 3 ? Math.floor(target / 3.5) : 0;
    const dieModifier: number = dieNumber > 0 ? Math.floor(target - dieNumber * 3.5) : 0;
 
    return {
        modifier: dieModifier,
        number: dieNumber,
    };
};