From cd4ccbc162e5cf7cde13e496a1439a7bf7eb56ba Mon Sep 17 00:00:00 2001 From: Shigma <1700011071@pku.edu.cn> Date: Mon, 14 Sep 2020 23:01:39 +0800 Subject: [PATCH] feat(dice): roll command --- packages/plugin-dice/package.json | 44 ++++++++++++++++++++ packages/plugin-dice/src/index.ts | 10 +++++ packages/plugin-dice/src/roll.ts | 65 ++++++++++++++++++++++++++++++ packages/plugin-dice/tsconfig.json | 10 +++++ tsconfig.json | 1 + 5 files changed, 130 insertions(+) create mode 100644 packages/plugin-dice/package.json create mode 100644 packages/plugin-dice/src/index.ts create mode 100644 packages/plugin-dice/src/roll.ts create mode 100644 packages/plugin-dice/tsconfig.json diff --git a/packages/plugin-dice/package.json b/packages/plugin-dice/package.json new file mode 100644 index 0000000000..4005640377 --- /dev/null +++ b/packages/plugin-dice/package.json @@ -0,0 +1,44 @@ +{ + "name": "koishi-plugin-dice", + "version": "0.1.0", + "description": "COC / DND Tools for Koishi", + "main": "dist/index.js", + "typings": "dist/index.d.ts", + "engines": { + "node": ">=14.6.0" + }, + "files": [ + "dist" + ], + "scripts": { + "wrap": "ts-node build/wrap", + "prepack": "tsc -b && yarn wrap" + }, + "license": "MIT", + "author": "Shigma <1700011071@pku.edu.cn>", + "repository": { + "type": "git", + "url": "git+https://github.com/koishijs/koishi.git" + }, + "bugs": { + "url": "https://github.com/koishijs/koishi/issues" + }, + "homepage": "https://koishi.js.org/plugins/dice.html", + "keywords": [ + "bot", + "qqbot", + "cqhttp", + "coolq", + "chatbot", + "koishi", + "plugin", + "dice" + ], + "peerDependencies": { + "koishi-core": "^2.3.0", + "koishi-utils": "^3.1.5" + }, + "devDependencies": { + "koishi-test-utils": "^5.0.1" + } +} diff --git a/packages/plugin-dice/src/index.ts b/packages/plugin-dice/src/index.ts new file mode 100644 index 0000000000..8554c42faf --- /dev/null +++ b/packages/plugin-dice/src/index.ts @@ -0,0 +1,10 @@ +import { Context } from 'koishi-core' +import roll, { RollConfig } from './roll' + +export interface Config extends RollConfig {} + +export const name = 'dice' + +export function apply(ctx: Context, config: Config = {}) { + ctx.plugin(roll, config) +} diff --git a/packages/plugin-dice/src/roll.ts b/packages/plugin-dice/src/roll.ts new file mode 100644 index 0000000000..6ab61df3b8 --- /dev/null +++ b/packages/plugin-dice/src/roll.ts @@ -0,0 +1,65 @@ +import { Context } from 'koishi-core' +import { Random } from 'koishi-utils' + +export interface RollConfig { + maxPoint?: number + maxTimes?: number +} + +export default function apply(ctx: Context, options: RollConfig = {}) { + const { maxPoint = 1 << 16, maxTimes = 64 } = options + + ctx.command('tools/roll [expr]', '掷骰') + .userFields(['name', 'timers']) + .shortcut('掷骰', { fuzzy: true }) + .example('roll 2d6+d10') + .action(async ({ session }, message = '1d6') => { + if (!/^((\d*)d)?(\d+)(\+((\d*)d)?(\d+))*$/i.test(message)) return '表达式语法错误。' + + const expressions = message.split('+') + let hasMultiple = false + let output = `${session.$username} 掷骰:${message}=` + let total = 0 + + for (const expr of expressions) { + const [, dice, _times, _max] = /^((\d*)d)?(\d+)$/i.exec(expr) + const max = +_max + if (!max || max > maxPoint) { + return `点数必须在 1 到 ${maxPoint} 之间。` + } + + if (!dice) { + output += max + '+' + total += max + continue + } + + const times = +(_times || 1) + if (!times || times > maxTimes) { + return `次数必须在 1 到 ${maxTimes} 之间。` + } + + const values = [] + for (let index = 0; index < times; index += 1) { + const value = Random.int(max) + 1 + values.push(value) + total += value + } + if (times > 1) hasMultiple = true + if (times > 1 && expressions.length > 1) { + output += '(' + } + output += values.join('+') + if (times > 1 && expressions.length > 1) { + output += ')' + } + output += '+' + } + + output = output.slice(0, -1) + if (hasMultiple || expressions.length > 1) { + output += '=' + total + } + return output + }) +} diff --git a/packages/plugin-dice/tsconfig.json b/packages/plugin-dice/tsconfig.json new file mode 100644 index 0000000000..a497f05e83 --- /dev/null +++ b/packages/plugin-dice/tsconfig.json @@ -0,0 +1,10 @@ +{ + "extends": "../../tsconfig.base", + "compilerOptions": { + "outDir": "dist", + "rootDir": "src", + }, + "include": [ + "src", + ], +} \ No newline at end of file diff --git a/tsconfig.json b/tsconfig.json index 99e422d9a2..46496bc282 100644 --- a/tsconfig.json +++ b/tsconfig.json @@ -21,6 +21,7 @@ { "path": "./packages/plugin-chess" }, { "path": "./packages/plugin-common" }, + { "path": "./packages/plugin-dice" }, { "path": "./packages/plugin-eval-addons" }, { "path": "./packages/plugin-github" }, { "path": "./packages/plugin-image-search" },