All files / projects-dev/libs/games/src/opend6 roll.ts

100% Statements 130/130
100% Branches 19/19
100% Functions 9/9
100% Lines 130/130

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 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 1311x 1x 1x 27x 27x 27x 27x 27x 27x 27x 27x 27x 27x 27x 27x 27x 27x 27x 27x 27x 27x 27x 27x 27x 27x 27x 27x 27x 27x 27x 22x 22x 22x 22x 18x 18x 18x 22x 27x 27x 27x 27x 27x 27x 27x 27x 27x 17x 17x 27x 27x 27x 27x 27x 27x 2x 2x 27x 27x 27x 27x 27x 27x 2x 2x 27x 27x 27x 27x 27x 27x 3x 3x 27x 27x 27x 27x 27x 27x 27x 27x 27x 27x 27x 27x 27x 27x 27x 27x 27x 17x 17x 17x 17x 17x 17x 17x 17x 1x 1x 1x 17x 17x 2x 2x 2x 2x 3x 3x 2x 2x 2x 17x 17x 14x 14x 12x 12x 14x 17x 17x 17x 17x 17x 1x 1x 17x 17x 17x 27x  
import { Dice6, DiceInterface } from "../dices/index";
 
export class Roll {
    public static CRITICAL_FAILURE = 1;
 
    public static CRITICAL_SUCCESS = 6;
 
    public readonly dice: DiceInterface;
 
    public readonly diceSides: number;
 
    public readonly diceNumber: number;
 
    public readonly modifier: number;
 
    protected isFailure = false;
 
    protected isSuccess = false;
 
    /**
     * Constructor
     *
     * @param dieCode string Regex matching a number of dices, their sides and a modifier
     */
    public constructor(dieCode?: string) {
        this.diceNumber = 1;
        this.diceSides = 6;
        this.modifier = 0;
 
        if (dieCode != undefined) {
            const re = /(\d)?[Dd]6([+-])?(\d)*/;
            const matches = dieCode.match(re);
 
            if (matches !== null) {
                this.diceNumber = Number.parseInt(matches[1] || String(this.diceNumber), 10);
                this.modifier = Number.parseInt(matches[2] + matches[3] || String(this.modifier), 10);
            }
        }
 
        this.dice = this.createDice();
    }
 
    /**
     * Get roll reesult
     * @return number
     */
    public getResult(): number {
        return this.roll();
    }
 
    /**
     * Is a critical failure
     * @return boolean
     */
    public isCriticalFailure(): boolean {
        return this.isFailure;
    }
 
    /**
     * Is a critical success
     * @return boolean
     */
    public isCriticalSuccess(): boolean {
        return this.isSuccess;
    }
 
    /**
     * Check if roll is greater or equal than target
     * @return boolean
     */
    public resolve(target: number): boolean {
        return this.getResult() >= target;
    }
 
    /**
     * Dice builder
     *
     * @return DiceInterface
     */
    // eslint-disable-next-line class-methods-use-this
    private createDice(): DiceInterface {
        return new Dice6();
    }
 
    /**
     * Roll dices
     *
     * @return number
     */
    private roll(): number {
        let rollResult = 0;
        let temporary: number;
 
        // Wild die
        rollResult += this.dice.roll();
 
        switch (rollResult) {
            case Roll.CRITICAL_FAILURE: {
                this.isFailure = true;
                break;
            }
 
            case Roll.CRITICAL_SUCCESS: {
                this.isSuccess = true;
 
                // eslint-disable-next-line no-loops/no-loops
                do {
                    temporary = this.dice.roll();
                    rollResult += temporary;
                } while (Roll.CRITICAL_SUCCESS === temporary);
                break;
            }
 
            default: {
                // eslint-disable-next-line no-loops/no-loops
                for (let index = 0; index < this.diceNumber - 1; index++) {
                    rollResult += this.dice.roll();
                }
            }
        }
 
        let result = rollResult + this.modifier;
 
        if (result <= 0) {
            result = 1;
        }
 
        return result;
    }
}