Skip to content

Commit

Permalink
feat(dice): roll command
Browse files Browse the repository at this point in the history
  • Loading branch information
shigma committed Sep 14, 2020
1 parent 4df7083 commit cd4ccbc
Show file tree
Hide file tree
Showing 5 changed files with 130 additions and 0 deletions.
44 changes: 44 additions & 0 deletions packages/plugin-dice/package.json
Original file line number Diff line number Diff line change
@@ -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"
}
}
10 changes: 10 additions & 0 deletions packages/plugin-dice/src/index.ts
Original file line number Diff line number Diff line change
@@ -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)
}
65 changes: 65 additions & 0 deletions packages/plugin-dice/src/roll.ts
Original file line number Diff line number Diff line change
@@ -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
})
}
10 changes: 10 additions & 0 deletions packages/plugin-dice/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
{
"extends": "../../tsconfig.base",
"compilerOptions": {
"outDir": "dist",
"rootDir": "src",
},
"include": [
"src",
],
}
1 change: 1 addition & 0 deletions tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -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" },
Expand Down

0 comments on commit cd4ccbc

Please sign in to comment.