All files / games/opend6-dices/src roll.ts

100% Statements 41/41
100% Branches 13/13
100% Functions 7/7
100% Lines 40/40

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  2x 2x 2x               24x 24x 24x 24x 24x 24x 22x 22x 22x 18x 18x 18x     24x             14x             2x             2x             3x               24x               14x     14x 14x   1x 1x   1x 1x 1x 1x   1x   12x 12x     14x 14x 2x   14x     2x 2x 2x                                            
import { Dice6, DiceInterface } from "jga-games-dices";
 
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 (null != dieCode) {
            const re = /(\d)?[dD](\d+)([+-])?(\d)*/;
            const matches = dieCode.match(re);
 
            if (null !== matches) {
                this.diceNumber = parseInt(matches[1] || String(this.diceNumber), 10);
                this.diceSides = parseInt(matches[2], 10);
                this.modifier = parseInt(matches[3] + matches[4] || 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
     */
    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;
 
                do {
                    tmp = this.dice.roll();
                    rollResult += tmp;
                } while (Roll.CRITICAL_SUCCESS === tmp);
                break;
 
            default:
                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;
    }
}