Merge branch '007-rollsLib' into 'master'

Basic Roll library

Closes 

See merge request 
This commit is contained in:
Johannes Loher 2020-12-31 02:31:20 +01:00
commit cba20e47e5
10 changed files with 540 additions and 14 deletions

3
.gitignore vendored
View file

@ -14,3 +14,6 @@ dist
# ESLint # ESLint
.eslintcache .eslintcache
# Junit results
results.xml

View file

@ -29,12 +29,17 @@ lint:
cache: cache:
<<: *global_cache <<: *global_cache
# Test: test:
# stage: Test stage: test
# script: script:
# - npm test - npm run test:ci
# cache: cache:
# <<: *global_cache <<: *global_cache
artifacts:
when: always
reports:
junit:
- results.xml
build: build:
stage: build stage: build

8
.vscode/extensions.json vendored Normal file
View file

@ -0,0 +1,8 @@
{
"recommendations": [
"dbaeumer.vscode-eslint",
"esbenp.prettier-vscode",
"gruntfuggly.todo-tree",
"eg2.vscode-npm-script"
]
}

111
package-lock.json generated
View file

@ -110,6 +110,12 @@
"fastq": "^1.6.0" "fastq": "^1.6.0"
} }
}, },
"@types/jasmine": {
"version": "3.6.2",
"resolved": "https://registry.npmjs.org/@types/jasmine/-/jasmine-3.6.2.tgz",
"integrity": "sha512-AzfesNFLvOs6Q1mHzIsVJXSeUnqVh4ZHG8ngygKJfbkcSLwzrBVm/LKa+mR8KrOfnWtUL47112gde1MC0IXqpQ==",
"dev": true
},
"@types/jquery": { "@types/jquery": {
"version": "3.5.5", "version": "3.5.5",
"resolved": "https://registry.npmjs.org/@types/jquery/-/jquery-3.5.5.tgz", "resolved": "https://registry.npmjs.org/@types/jquery/-/jquery-3.5.5.tgz",
@ -610,6 +616,12 @@
"readable-stream": "^2.0.6" "readable-stream": "^2.0.6"
} }
}, },
"arg": {
"version": "4.1.3",
"resolved": "https://registry.npmjs.org/arg/-/arg-4.1.3.tgz",
"integrity": "sha512-58S9QDqG0Xx27YwPSt9fJxivjYl432YCwfDMfZ+71RAqUrZef7LrKQZ3LHLOwCS4FLNBplP533Zx895SeOCHvA==",
"dev": true
},
"argparse": { "argparse": {
"version": "1.0.10", "version": "1.0.10",
"resolved": "https://registry.npmjs.org/argparse/-/argparse-1.0.10.tgz", "resolved": "https://registry.npmjs.org/argparse/-/argparse-1.0.10.tgz",
@ -1571,6 +1583,12 @@
} }
} }
}, },
"create-require": {
"version": "1.1.1",
"resolved": "https://registry.npmjs.org/create-require/-/create-require-1.1.1.tgz",
"integrity": "sha512-dcKFX3jn0MpIaXjisoRvexIJVEKzaq7z2rZKxf+MSr9TkdmHmsU4m2lcLojrj/FHl8mk5VxMmYA+ftRkP/3oKQ==",
"dev": true
},
"cross-spawn": { "cross-spawn": {
"version": "3.0.1", "version": "3.0.1",
"resolved": "https://registry.npmjs.org/cross-spawn/-/cross-spawn-3.0.1.tgz", "resolved": "https://registry.npmjs.org/cross-spawn/-/cross-spawn-3.0.1.tgz",
@ -1733,6 +1751,12 @@
"integrity": "sha1-8NZtA2cqglyxtzvbP+YjEMjlUrc=", "integrity": "sha1-8NZtA2cqglyxtzvbP+YjEMjlUrc=",
"dev": true "dev": true
}, },
"diff": {
"version": "4.0.2",
"resolved": "https://registry.npmjs.org/diff/-/diff-4.0.2.tgz",
"integrity": "sha512-58lmxKSA4BNyLz+HHMUzlOEpg09FV+ev6ZMe3vJihgdxzgcwZ8VoEEPmALCZG9LmqfVoNMMKpttIYTVG6uDY7A==",
"dev": true
},
"dir-glob": { "dir-glob": {
"version": "3.0.1", "version": "3.0.1",
"resolved": "https://registry.npmjs.org/dir-glob/-/dir-glob-3.0.1.tgz", "resolved": "https://registry.npmjs.org/dir-glob/-/dir-glob-3.0.1.tgz",
@ -2678,7 +2702,7 @@
} }
}, },
"foundry-pc-types": { "foundry-pc-types": {
"version": "git+https://git.f3l.de/dungeonslayers/foundry-pc-types.git#4ae5653e74e79bb6b4bd23b094dee066a0c7723a", "version": "git+https://git.f3l.de/dungeonslayers/foundry-pc-types.git#4e20e5c9cb1b3cd2e44555d7acfa89a3cf63f6ce",
"from": "git+https://git.f3l.de/dungeonslayers/foundry-pc-types.git#f3l-fixes", "from": "git+https://git.f3l.de/dungeonslayers/foundry-pc-types.git#f3l-fixes",
"dev": true, "dev": true,
"requires": { "requires": {
@ -3748,6 +3772,41 @@
"integrity": "sha1-R+Y/evVa+m+S4VAOaQ64uFKcCZo=", "integrity": "sha1-R+Y/evVa+m+S4VAOaQ64uFKcCZo=",
"dev": true "dev": true
}, },
"jasmine": {
"version": "3.6.3",
"resolved": "https://registry.npmjs.org/jasmine/-/jasmine-3.6.3.tgz",
"integrity": "sha512-Th91zHsbsALWjDUIiU5d/W5zaYQsZFMPTdeNmi8GivZPmAaUAK8MblSG3yQI4VMGC/abF2us7ex60NH1AAIMTA==",
"dev": true,
"requires": {
"glob": "^7.1.6",
"jasmine-core": "~3.6.0"
}
},
"jasmine-core": {
"version": "3.6.0",
"resolved": "https://registry.npmjs.org/jasmine-core/-/jasmine-core-3.6.0.tgz",
"integrity": "sha512-8uQYa7zJN8hq9z+g8z1bqCfdC8eoDAeVnM5sfqs7KHv9/ifoJ500m018fpFc7RDaO6SWCLCXwo/wPSNcdYTgcw==",
"dev": true
},
"jasmine-reporters": {
"version": "2.3.2",
"resolved": "https://registry.npmjs.org/jasmine-reporters/-/jasmine-reporters-2.3.2.tgz",
"integrity": "sha512-u/7AT9SkuZsUfFBLLzbErohTGNsEUCKaQbsVYnLFW1gEuL2DzmBL4n8v90uZsqIqlWvWUgian8J6yOt5Fyk/+A==",
"dev": true,
"requires": {
"mkdirp": "^0.5.1",
"xmldom": "^0.1.22"
}
},
"jasmine-xml-reporter": {
"version": "1.2.1",
"resolved": "https://registry.npmjs.org/jasmine-xml-reporter/-/jasmine-xml-reporter-1.2.1.tgz",
"integrity": "sha1-fKoqUYAv7+2+JLPqinrUJ8nqfbM=",
"dev": true,
"requires": {
"jasmine-reporters": "^2.2.0"
}
},
"js-base64": { "js-base64": {
"version": "2.6.4", "version": "2.6.4",
"resolved": "https://registry.npmjs.org/js-base64/-/js-base64-2.6.4.tgz", "resolved": "https://registry.npmjs.org/js-base64/-/js-base64-2.6.4.tgz",
@ -4326,6 +4385,12 @@
} }
} }
}, },
"make-error": {
"version": "1.3.6",
"resolved": "https://registry.npmjs.org/make-error/-/make-error-1.3.6.tgz",
"integrity": "sha512-s8UhlNe7vPKomQhC1qFelMokr/Sc3AgNbso3n74mVPA5LTZwkB9NlXf4XPamLxJE8h0gh73rM94xvwRT2CVInw==",
"dev": true
},
"make-iterator": { "make-iterator": {
"version": "1.0.1", "version": "1.0.1",
"resolved": "https://registry.npmjs.org/make-iterator/-/make-iterator-1.0.1.tgz", "resolved": "https://registry.npmjs.org/make-iterator/-/make-iterator-1.0.1.tgz",
@ -5993,6 +6058,24 @@
"urix": "^0.1.0" "urix": "^0.1.0"
} }
}, },
"source-map-support": {
"version": "0.5.19",
"resolved": "https://registry.npmjs.org/source-map-support/-/source-map-support-0.5.19.tgz",
"integrity": "sha512-Wonm7zOCIJzBGQdB+thsPar0kYuCIzYvxZwlBa87yi/Mdjv7Tip2cyVbLj5o0cFPN4EVkuTwb3GDDyUx2DGnGw==",
"dev": true,
"requires": {
"buffer-from": "^1.0.0",
"source-map": "^0.6.0"
},
"dependencies": {
"source-map": {
"version": "0.6.1",
"resolved": "https://registry.npmjs.org/source-map/-/source-map-0.6.1.tgz",
"integrity": "sha512-UjgapumWlbMhkBgzT7Ykc5YXUT46F0iKu8SGXq0bcwP5dz/h0Plj6enJqjz1Zbq2l5WaqYnrVbwWOWMyF3F47g==",
"dev": true
}
}
},
"source-map-url": { "source-map-url": {
"version": "0.4.0", "version": "0.4.0",
"resolved": "https://registry.npmjs.org/source-map-url/-/source-map-url-0.4.0.tgz", "resolved": "https://registry.npmjs.org/source-map-url/-/source-map-url-0.4.0.tgz",
@ -6514,6 +6597,20 @@
"glob": "^7.1.2" "glob": "^7.1.2"
} }
}, },
"ts-node": {
"version": "9.1.1",
"resolved": "https://registry.npmjs.org/ts-node/-/ts-node-9.1.1.tgz",
"integrity": "sha512-hPlt7ZACERQGf03M253ytLY3dHbGNGrAq9qIHWUY9XHYl1z7wYngSr3OQ5xmui8o2AaxsONxIzjafLUiWBo1Fg==",
"dev": true,
"requires": {
"arg": "^4.1.0",
"create-require": "^1.1.0",
"diff": "^4.0.1",
"make-error": "^1.1.1",
"source-map-support": "^0.5.17",
"yn": "3.1.1"
}
},
"tslib": { "tslib": {
"version": "1.14.1", "version": "1.14.1",
"resolved": "https://registry.npmjs.org/tslib/-/tslib-1.14.1.tgz", "resolved": "https://registry.npmjs.org/tslib/-/tslib-1.14.1.tgz",
@ -6949,6 +7046,12 @@
"integrity": "sha1-tSQ9jz7BqjXxNkYFvA0QNuMKtp8=", "integrity": "sha1-tSQ9jz7BqjXxNkYFvA0QNuMKtp8=",
"dev": true "dev": true
}, },
"xmldom": {
"version": "0.1.31",
"resolved": "https://registry.npmjs.org/xmldom/-/xmldom-0.1.31.tgz",
"integrity": "sha512-yS2uJflVQs6n+CyjHoaBmVSqIDevTAWrzMmjG1Gc7h1qQ7uVozNhEPJAwZXWyGQ/Gafo3fCwrcaokezLPupVyQ==",
"dev": true
},
"xtend": { "xtend": {
"version": "4.0.2", "version": "4.0.2",
"resolved": "https://registry.npmjs.org/xtend/-/xtend-4.0.2.tgz", "resolved": "https://registry.npmjs.org/xtend/-/xtend-4.0.2.tgz",
@ -7102,6 +7205,12 @@
"object.assign": "^4.1.0" "object.assign": "^4.1.0"
} }
}, },
"yn": {
"version": "3.1.1",
"resolved": "https://registry.npmjs.org/yn/-/yn-3.1.1.tgz",
"integrity": "sha512-Ux4ygGWsu2c7isFWe8Yu1YluJmqVhxqK2cLXNQA5AcC3QfbGNpM7fu0Y8b/z16pXLnFxZYvWhd3fhBY9DLmC6Q==",
"dev": true
},
"zip-stream": { "zip-stream": {
"version": "4.0.4", "version": "4.0.4",
"resolved": "https://registry.npmjs.org/zip-stream/-/zip-stream-4.0.4.tgz", "resolved": "https://registry.npmjs.org/zip-stream/-/zip-stream-4.0.4.tgz",

View file

@ -12,11 +12,14 @@
"update": "npm install --save-dev git+https://git.f3l.de/dungeonslayers/foundry-pc-types.git#f3l-fixes", "update": "npm install --save-dev git+https://git.f3l.de/dungeonslayers/foundry-pc-types.git#f3l-fixes",
"lint": "eslint 'src/**/*.ts' --cache", "lint": "eslint 'src/**/*.ts' --cache",
"lint:fix": "eslint 'src/**/*.ts' --cache --fix", "lint:fix": "eslint 'src/**/*.ts' --cache --fix",
"test": "ts-node ./node_modules/jasmine/bin/jasmine",
"test:ci": "ts-node ./node_modules/jasmine-xml-reporter/bin/jasmine --junitreport",
"format": "prettier --write 'src/**/*.(ts|json|scss)'" "format": "prettier --write 'src/**/*.(ts|json|scss)'"
}, },
"author": "", "author": "",
"license": "", "license": "",
"devDependencies": { "devDependencies": {
"@types/jasmine": "^3.6.2",
"@typescript-eslint/eslint-plugin": "^4.11.0", "@typescript-eslint/eslint-plugin": "^4.11.0",
"@typescript-eslint/parser": "^4.11.0", "@typescript-eslint/parser": "^4.11.0",
"archiver": "^5.1.0", "archiver": "^5.1.0",
@ -32,10 +35,13 @@
"gulp-sass": "^4.1.0", "gulp-sass": "^4.1.0",
"gulp-typescript": "^6.0.0-alpha.1", "gulp-typescript": "^6.0.0-alpha.1",
"husky": "^4.3.6", "husky": "^4.3.6",
"jasmine": "^3.6.3",
"jasmine-xml-reporter": "^1.2.1",
"json-stringify-pretty-compact": "^2.0.0", "json-stringify-pretty-compact": "^2.0.0",
"lint-staged": "^10.5.3", "lint-staged": "^10.5.3",
"prettier": "^2.2.1", "prettier": "^2.2.1",
"sass": "^1.30.0", "sass": "^1.30.0",
"ts-node": "^9.1.1",
"typescript": "^4.1.3", "typescript": "^4.1.3",
"yargs": "^16.2.0" "yargs": "^16.2.0"
}, },

View file

@ -0,0 +1,272 @@
import {
rollCheckMultipleDice,
rollCheckSingleDie,
RollOptions,
RollResult,
RollResultStatus,
} from "../../src/module/rolls/roll-executor";
import { RollProvider } from "../../src/module/rolls/roll-provider";
import "jasmine";
describe("DS4 Rolls with one die and no modifications.", () => {
it("Should do a regular success roll.", () => {
const rollProvider: RollProvider = jasmine.createSpyObj("rollProvider", ["getNextRoll"]);
rollProvider.getNextRoll = jasmine.createSpy("getNextRoll").and.returnValue(4);
expect(rollCheckSingleDie(12, new RollOptions(), rollProvider)).toEqual(
new RollResult(4, RollResultStatus.SUCCESS, [4]),
);
});
it("Should do a single success roll on success upper edge case.", () => {
const rollProvider: RollProvider = jasmine.createSpyObj("rollProvider", ["getNextRoll"]);
rollProvider.getNextRoll = jasmine.createSpy("getNextRoll").and.returnValue(4);
expect(rollCheckSingleDie(4, new RollOptions(), rollProvider)).toEqual(
new RollResult(4, RollResultStatus.SUCCESS, [4]),
);
});
it("Should do a single failure roll on lower edge case.", () => {
const rollProvider: RollProvider = jasmine.createSpyObj("rollProvider", ["getNextRoll"]);
rollProvider.getNextRoll = jasmine.createSpy("getNextRoll").and.returnValue(5);
expect(rollCheckSingleDie(4, new RollOptions(), rollProvider)).toEqual(
new RollResult(0, RollResultStatus.FAILURE, [5]),
);
});
it("Should do a single failure roll on upper edge case '19'.", () => {
const rollProvider: RollProvider = jasmine.createSpyObj("rollProvider", ["getNextRoll"]);
rollProvider.getNextRoll = jasmine.createSpy("getNextRoll").and.returnValue(19);
expect(rollCheckSingleDie(4, new RollOptions(), rollProvider)).toEqual(
new RollResult(0, RollResultStatus.FAILURE, [19]),
);
});
it("Should do a single crit success roll on '1'.", () => {
const rollProvider: RollProvider = jasmine.createSpyObj("rollProvider", ["getNextRoll"]);
rollProvider.getNextRoll = jasmine.createSpy("getNextRoll").and.returnValue(1);
expect(rollCheckSingleDie(4, new RollOptions(), rollProvider)).toEqual(
new RollResult(4, RollResultStatus.CRITICAL_SUCCESS, [1]),
);
});
it("Should do a single crit failure roll on '20'.", () => {
const rollProvider: RollProvider = jasmine.createSpyObj("rollProvider", ["getNextRoll"]);
rollProvider.getNextRoll = jasmine.createSpy("getNextRoll").and.returnValue(20);
expect(rollCheckSingleDie(4, new RollOptions(), rollProvider)).toEqual(
new RollResult(0, RollResultStatus.CRITICAL_FAILURE, [20]),
);
});
});
describe("DS4 Rolls with one die and crit roll modifications.", () => {
it("Should do a crit success on `1`.", () => {
const rollProvider: RollProvider = jasmine.createSpyObj("rollProvider", ["getNextRoll"]);
rollProvider.getNextRoll = jasmine.createSpy("getNextRoll").and.returnValue(1);
expect(rollCheckSingleDie(4, { maxCritSucc: 2, minCritFail: 19 }, rollProvider)).toEqual(
new RollResult(4, RollResultStatus.CRITICAL_SUCCESS, [1]),
);
});
it("Should do a crit success on `maxCritSucc`.", () => {
const rollProvider: RollProvider = jasmine.createSpyObj("rollProvider", ["getNextRoll"]);
rollProvider.getNextRoll = jasmine.createSpy("getNextRoll").and.returnValue(2);
expect(rollCheckSingleDie(4, { maxCritSucc: 2, minCritFail: 19 }, rollProvider)).toEqual(
new RollResult(4, RollResultStatus.CRITICAL_SUCCESS, [2]),
);
});
it("Should do a success on lower edge case `3`.", () => {
const rollProvider: RollProvider = jasmine.createSpyObj("rollProvider", ["getNextRoll"]);
rollProvider.getNextRoll = jasmine.createSpy("getNextRoll").and.returnValue(3);
expect(rollCheckSingleDie(4, { maxCritSucc: 2, minCritFail: 19 }, rollProvider)).toEqual(
new RollResult(3, RollResultStatus.SUCCESS, [3]),
);
});
it("Should do a success on upper edge case `18`.", () => {
const rollProvider: RollProvider = jasmine.createSpyObj("rollProvider", ["getNextRoll"]);
rollProvider.getNextRoll = jasmine.createSpy("getNextRoll").and.returnValue(18);
expect(rollCheckSingleDie(4, { maxCritSucc: 2, minCritFail: 19 }, rollProvider)).toEqual(
new RollResult(0, RollResultStatus.FAILURE, [18]),
);
});
it("Should do a crit fail on `minCritFail`.", () => {
const rollProvider: RollProvider = jasmine.createSpyObj("rollProvider", ["getNextRoll"]);
rollProvider.getNextRoll = jasmine.createSpy("getNextRoll").and.returnValue(19);
expect(rollCheckSingleDie(4, { maxCritSucc: 2, minCritFail: 19 }, rollProvider)).toEqual(
new RollResult(0, RollResultStatus.CRITICAL_FAILURE, [19]),
);
});
it("Should do a crit fail on `20`", () => {
const rollProvider: RollProvider = jasmine.createSpyObj("rollProvider", ["getNextRoll"]);
rollProvider.getNextRoll = jasmine.createSpy("getNextRoll").and.returnValue(20);
expect(rollCheckSingleDie(4, { maxCritSucc: 2, minCritFail: 19 }, rollProvider)).toEqual(
new RollResult(0, RollResultStatus.CRITICAL_FAILURE, [20]),
);
});
});
describe("DS4 Rools with multiple dice and no modifiers.", () => {
it("Should do a crit fail on `20` for first roll.", () => {
const rollProvider: RollProvider = jasmine.createSpyObj("rollProvider", ["getNextRolls"]);
rollProvider.getNextRolls = jasmine.createSpy("getNextRolls").and.returnValue([20, 15, 6]);
expect(rollCheckMultipleDice(48, new RollOptions(), rollProvider)).toEqual(
new RollResult(0, RollResultStatus.CRITICAL_FAILURE, [20, 15, 6]),
);
});
it("Should succeed with all rolls crit successes.", () => {
const rollProvider: RollProvider = jasmine.createSpyObj("rollProvider", ["getNextRolls"]);
rollProvider.getNextRolls = jasmine.createSpy("getNextRolls").and.returnValue([1, 1, 1]);
expect(rollCheckMultipleDice(48, new RollOptions(), rollProvider)).toEqual(
new RollResult(48, RollResultStatus.SUCCESS, [1, 1, 1]),
);
});
it("Should succeed with the last roll not being suficient.", () => {
const rollProvider: RollProvider = jasmine.createSpyObj("rollProvider", ["getNextRolls"]);
rollProvider.getNextRolls = jasmine.createSpy("getNextRolls").and.returnValue([15, 15, 15]);
expect(rollCheckMultipleDice(48, new RollOptions(), rollProvider)).toEqual(
new RollResult(30, RollResultStatus.SUCCESS, [15, 15, 15]),
);
});
it("Should succeed with the last roll a crit success.", () => {
const rollProvider: RollProvider = jasmine.createSpyObj("rollProvider", ["getNextRolls"]);
rollProvider.getNextRolls = jasmine.createSpy("getNextRolls").and.returnValue([15, 15, 1]);
expect(rollCheckMultipleDice(48, new RollOptions(), rollProvider)).toEqual(
new RollResult(35, RollResultStatus.SUCCESS, [1, 15, 15]),
);
});
it("Should succeed with the last roll being 20 and one crit success.", () => {
const rollProvider: RollProvider = jasmine.createSpyObj("rollProvider", ["getNextRolls"]);
rollProvider.getNextRolls = jasmine.createSpy("getNextRolls").and.returnValue([15, 1, 20]);
expect(rollCheckMultipleDice(48, new RollOptions(), rollProvider)).toEqual(
new RollResult(40, RollResultStatus.SUCCESS, [1, 20, 15]),
);
});
});
describe("DS4 Rools with multiple dice and min/max modifiers.", () => {
it("Should do a crit fail on `19` for first roll.", () => {
const rollProvider: RollProvider = jasmine.createSpyObj("rollProvider", ["getNextRolls"]);
rollProvider.getNextRolls = jasmine.createSpy("getNextRolls").and.returnValue([19, 15, 6]);
expect(rollCheckMultipleDice(48, { maxCritSucc: 2, minCritFail: 19 } as RollOptions, rollProvider)).toEqual(
new RollResult(0, RollResultStatus.CRITICAL_FAILURE, [19, 15, 6]),
);
});
it("Should succeed with all rolls crit successes (1 and 2).", () => {
const rollProvider: RollProvider = jasmine.createSpyObj("rollProvider", ["getNextRolls"]);
rollProvider.getNextRolls = jasmine.createSpy("getNextRolls").and.returnValue([2, 1, 2]);
expect(rollCheckMultipleDice(48, { maxCritSucc: 2, minCritFail: 19 } as RollOptions, rollProvider)).toEqual(
new RollResult(48, RollResultStatus.SUCCESS, [2, 2, 1]),
);
});
it("Should succeed with the last roll not being suficient.", () => {
const rollProvider: RollProvider = jasmine.createSpyObj("rollProvider", ["getNextRolls"]);
rollProvider.getNextRolls = jasmine.createSpy("getNextRolls").and.returnValue([15, 15, 15]);
expect(rollCheckMultipleDice(48, { maxCritSucc: 2, minCritFail: 19 } as RollOptions, rollProvider)).toEqual(
new RollResult(30, RollResultStatus.SUCCESS, [15, 15, 15]),
);
});
it("Should succeed with the last roll a crit success `2`.", () => {
const rollProvider: RollProvider = jasmine.createSpyObj("rollProvider", ["getNextRolls"]);
rollProvider.getNextRolls = jasmine.createSpy("getNextRolls").and.returnValue([15, 15, 2]);
expect(rollCheckMultipleDice(48, { maxCritSucc: 2, minCritFail: 19 } as RollOptions, rollProvider)).toEqual(
new RollResult(35, RollResultStatus.SUCCESS, [2, 15, 15]),
);
});
it("Should succeed with the last roll being `20` and one crit success '2'.", () => {
const rollProvider: RollProvider = jasmine.createSpyObj("rollProvider", ["getNextRolls"]);
rollProvider.getNextRolls = jasmine.createSpy("getNextRolls").and.returnValue([15, 2, 20]);
expect(rollCheckMultipleDice(48, { maxCritSucc: 2, minCritFail: 19 } as RollOptions, rollProvider)).toEqual(
new RollResult(40, RollResultStatus.SUCCESS, [2, 20, 15]),
);
});
it("Should succeed with the last roll being `19` and one crit success '2'.", () => {
const rollProvider: RollProvider = jasmine.createSpyObj("rollProvider", ["getNextRolls"]);
rollProvider.getNextRolls = jasmine.createSpy("getNextRolls").and.returnValue([15, 2, 19]);
expect(rollCheckMultipleDice(48, { maxCritSucc: 2, minCritFail: 19 } as RollOptions, rollProvider)).toEqual(
new RollResult(39, RollResultStatus.SUCCESS, [2, 19, 15]),
);
});
});
describe("DS4 Rools with multiple dice and fail modifiers.", () => {
it("Should do a crit fail on `19` for first roll.", () => {
const rollProvider: RollProvider = jasmine.createSpyObj("rollProvider", ["getNextRolls"]);
rollProvider.getNextRolls = jasmine.createSpy("getNextRolls").and.returnValue([19, 15, 6]);
expect(rollCheckMultipleDice(48, { minCritFail: 19 } as RollOptions, rollProvider)).toEqual(
new RollResult(0, RollResultStatus.CRITICAL_FAILURE, [19, 15, 6]),
);
});
});
describe("DS4 Rools with multiple dice and success modifiers.", () => {
it("Should succeed with all rolls crit successes (1 and 2).", () => {
const rollProvider: RollProvider = jasmine.createSpyObj("rollProvider", ["getNextRolls"]);
rollProvider.getNextRolls = jasmine.createSpy("getNextRolls").and.returnValue([2, 1, 2]);
expect(rollCheckMultipleDice(48, { maxCritSucc: 2 } as RollOptions, rollProvider)).toEqual(
new RollResult(48, RollResultStatus.SUCCESS, [2, 2, 1]),
);
});
});

View file

@ -0,0 +1,7 @@
{
"spec_dir": "spec",
"spec_files": ["**/*[sS]pec.ts"],
"helpers": ["helpers/**/*.ts"],
"stopSpecOnExpectationFailure": false,
"random": true
}

View file

@ -0,0 +1,99 @@
import { DS4RollProvider, RollProvider } from "./roll-provider";
export function ds4test(testValue: number, rollOptions: RollOptions = new RollOptions()): RollResult {
const finalRollValue = testValue;
if (finalRollValue <= 20) {
return rollCheckSingleDie(finalRollValue, rollOptions);
} else {
return rollCheckMultipleDice(finalRollValue, rollOptions);
}
}
export function rollCheckSingleDie(
testValue: number,
rollOptions: RollOptions,
provider: RollProvider = new DS4RollProvider(),
): RollResult {
const roll = provider.getNextRoll();
const dice = [roll];
if (roll <= rollOptions.maxCritSucc) {
return new RollResult(testValue, RollResultStatus.CRITICAL_SUCCESS, dice);
} else if (roll >= rollOptions.minCritFail) {
return new RollResult(0, RollResultStatus.CRITICAL_FAILURE, dice);
} else {
if (roll <= testValue) {
return new RollResult(roll, RollResultStatus.SUCCESS, dice);
} else {
return new RollResult(0, RollResultStatus.FAILURE, dice);
}
}
}
export function rollCheckMultipleDice(
testValue: number,
rollOptions: RollOptions,
provider: RollProvider = new DS4RollProvider(),
): RollResult {
const finalCheck = testValue % 20;
const numberOfDice = Math.ceil(testValue / 20);
const dice = provider.getNextRolls(numberOfDice);
const firstResult = dice[0];
if (firstResult >= rollOptions.minCritFail) {
return new RollResult(0, RollResultStatus.CRITICAL_FAILURE, dice);
}
const partitionCallback = (prev: [Array<number>, Array<number>], cur: number) => {
if (cur <= rollOptions.maxCritSucc) {
prev[0].push(cur);
} else {
prev[1].push(cur);
}
return prev;
};
const [critSuccesses, otherRolls] = dice
.reduce(partitionCallback, [[], []])
.map((a) => a.sort((r1, r2) => r2 - r1));
const sortedRollResults: Array<number> = critSuccesses.concat(otherRolls);
const evaluationResult = sortedRollResults
.map((value, index) => {
if (index == numberOfDice - 1) {
if (value == 1) {
return finalCheck;
} else {
return value <= finalCheck ? value : 0;
}
} else {
if (value <= rollOptions.maxCritSucc) {
return 20;
} else {
return value;
}
}
})
.reduce((a, b) => a + b);
return new RollResult(evaluationResult, RollResultStatus.SUCCESS, sortedRollResults);
}
export class RollOptions {
public maxCritSucc = 1;
public minCritFail = 20;
}
export class RollResult {
constructor(public value: number, public status: RollResultStatus, public dice: Array<number>) {}
}
export enum RollResultStatus {
FAILURE,
SUCCESS,
CRITICAL_FAILURE,
CRITICAL_SUCCESS,
}

View file

@ -0,0 +1,16 @@
export class DS4RollProvider {
getNextRoll(): number {
return new Roll("1d20").roll().total;
}
getNextRolls(amount: number): Array<number> {
return Array(amount)
.fill(0)
.map(() => this.getNextRoll());
}
}
export interface RollProvider {
getNextRoll(): number;
getNextRolls(number: number): Array<number>;
}

View file

@ -2,6 +2,7 @@
"compilerOptions": { "compilerOptions": {
"target": "ES2017", "target": "ES2017",
"lib": ["DOM", "ES6", "ES2017"], "lib": ["DOM", "ES6", "ES2017"],
"types": ["foundry-pc-types"] "types": ["foundry-pc-types"],
"esModuleInterop": true
} }
} }