-
-
Notifications
You must be signed in to change notification settings - Fork 253
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
130 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
}) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
{ | ||
"extends": "../../tsconfig.base", | ||
"compilerOptions": { | ||
"outDir": "dist", | ||
"rootDir": "src", | ||
}, | ||
"include": [ | ||
"src", | ||
], | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters