Commit 9f65f6be authored by nanahira's avatar nanahira

migrate to onedice

parent f53fbd99
...@@ -12,7 +12,8 @@ ...@@ -12,7 +12,8 @@
"koishi-thirdeye": "^11.0.9", "koishi-thirdeye": "^11.0.9",
"leven": "^3.1.0", "leven": "^3.1.0",
"load-json-file": "^6.2.0", "load-json-file": "^6.2.0",
"lodash": "^4.17.21" "lodash": "^4.17.21",
"onedice": "^1.0.2"
}, },
"devDependencies": { "devDependencies": {
"@koishijs/plugin-console": "^4.1.1", "@koishijs/plugin-console": "^4.1.1",
...@@ -6045,6 +6046,11 @@ ...@@ -6045,6 +6046,11 @@
"wrappy": "1" "wrappy": "1"
} }
}, },
"node_modules/onedice": {
"version": "1.0.2",
"resolved": "https://registry.npmjs.org/onedice/-/onedice-1.0.2.tgz",
"integrity": "sha512-B7sN77wWL7WP+4a5ngPYhrJC4E6q8yHs2FQS6hz/vPmTgBXZvJjJMJguV8DAaLtSLjbhwXG4wdIoyNI64ieziQ=="
},
"node_modules/onetime": { "node_modules/onetime": {
"version": "5.1.2", "version": "5.1.2",
"resolved": "https://registry.npmjs.org/onetime/-/onetime-5.1.2.tgz", "resolved": "https://registry.npmjs.org/onetime/-/onetime-5.1.2.tgz",
...@@ -12761,6 +12767,11 @@ ...@@ -12761,6 +12767,11 @@
"wrappy": "1" "wrappy": "1"
} }
}, },
"onedice": {
"version": "1.0.2",
"resolved": "https://registry.npmjs.org/onedice/-/onedice-1.0.2.tgz",
"integrity": "sha512-B7sN77wWL7WP+4a5ngPYhrJC4E6q8yHs2FQS6hz/vPmTgBXZvJjJMJguV8DAaLtSLjbhwXG4wdIoyNI64ieziQ=="
},
"onetime": { "onetime": {
"version": "5.1.2", "version": "5.1.2",
"resolved": "https://registry.npmjs.org/onetime/-/onetime-5.1.2.tgz", "resolved": "https://registry.npmjs.org/onetime/-/onetime-5.1.2.tgz",
......
...@@ -47,7 +47,8 @@ ...@@ -47,7 +47,8 @@
"koishi-thirdeye": "^11.0.9", "koishi-thirdeye": "^11.0.9",
"leven": "^3.1.0", "leven": "^3.1.0",
"load-json-file": "^6.2.0", "load-json-file": "^6.2.0",
"lodash": "^4.17.21" "lodash": "^4.17.21",
"onedice": "^1.0.2"
}, },
"peerDependencies": { "peerDependencies": {
"koishi": "^4.8.4" "koishi": "^4.8.4"
......
...@@ -21,6 +21,7 @@ import _ from 'lodash'; ...@@ -21,6 +21,7 @@ import _ from 'lodash';
import * as localeZh from './locales/zh'; import * as localeZh from './locales/zh';
import * as localeEn from './locales/en'; import * as localeEn from './locales/en';
import leven from 'leven'; import leven from 'leven';
import { OneDice } from 'onedice';
export * from './config'; export * from './config';
type Decks = Record<string, string[]>; type Decks = Record<string, string[]>;
...@@ -70,28 +71,9 @@ export default class DrawPlugin ...@@ -70,28 +71,9 @@ export default class DrawPlugin
return { deckCount, deckFileCount }; return { deckCount, deckFileCount };
} }
parseDicePattern(pattern: string) {
if (pattern.match(/^\d+$/)) {
return parseInt(pattern);
}
const match = pattern.match(/^(\d*)d(\d+)$/);
if (!match) {
return 0;
}
const [, _count, _sides] = match;
const count = _count ? parseInt(_count) : 1;
const sides = parseInt(_sides);
return _.sum(_.range(count).map(() => Random.int(1, sides + 1)));
}
parseDice(dice: string) {
const patterns = dice.split('+');
return _.sumBy(patterns, (pattern) => this.parseDicePattern(pattern));
}
parseEntry(name: string, entry: string, depth = 1): string { parseEntry(name: string, entry: string, depth = 1): string {
let result = entry.replace(/\[[d\d\+]+\]/g, (dicePattern) => let result = entry.replace(/\[([^\]]+)\]/g, (dicePattern) =>
this.parseDice(dicePattern.slice(1, -1)).toString(), new OneDice().calculate(dicePattern.slice(1, -1)).toString(),
); );
if (depth > this.config.maxDepth) { if (depth > this.config.maxDepth) {
this.logger.warn( this.logger.warn(
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment