All files / games/src/opend6 roll.ts

100% Statements 38/38
100% Branches 10/10
100% Functions 7/7
100% Lines 37/37

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 12844x   44x 44x   44x                   26x   26x               26x 26x 26x   26x 22x 22x   22x 18x 18x       26x               16x               2x               2x               3x                   26x                 16x       16x   16x   1x 1x     1x     1x 1x 1x   1x       14x 12x       16x   16x 1x     16x      
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 != null) {
            const re = /(\d)?[dD]6([+-])?(\d)*/;
            const matches = dieCode.match(re);
 
            if (matches !== null) {
                this.diceNumber = parseInt(matches[1] || String(this.diceNumber), 10);
                this.modifier = 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 tmp: 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 {
                    tmp = this.dice.roll();
                    rollResult += tmp;
                } while (Roll.CRITICAL_SUCCESS === tmp);
                break;
 
            default:
                // eslint-disable-next-line no-loops/no-loops
                for (let i = 0; i < this.diceNumber - 1; i++) {
                    rollResult += this.dice.roll();
                }
        }
 
        let result = rollResult + this.modifier;
 
        if (result <= 0) {
            result = 1;
        }
 
        return result;
    }
}