Commit 59d7b3aa authored by nanahira's avatar nanahira

split modules

parent 1c8d7d7f
{
"name": "koishi-plugin-myplugin",
"name": "koishi-plugin-dicex",
"version": "1.0.1",
"lockfileVersion": 2,
"requires": true,
"packages": {
"": {
"name": "koishi-plugin-myplugin",
"name": "koishi-plugin-dicex",
"version": "1.0.1",
"license": "MIT",
"dependencies": {
"koishi-thirdeye": "^6.0.2",
"koishi-thirdeye": "^6.0.3",
"source-map-support": "^0.5.21"
},
"devDependencies": {
......@@ -29,7 +29,7 @@
"ws": "^8.4.0"
},
"peerDependencies": {
"koishi": "^4.0.0-beta.6"
"koishi": "^4.0.0-rc.0"
}
},
"node_modules/@babel/code-frame": {
......@@ -2595,9 +2595,9 @@
}
},
"node_modules/koishi-thirdeye": {
"version": "6.0.2",
"resolved": "https://registry.npmjs.org/koishi-thirdeye/-/koishi-thirdeye-6.0.2.tgz",
"integrity": "sha512-KOkK3eq+PTdrQjJ7/TBsz30dP+IQQsTmNU6OxIEL0nisFqEinlUPfva7XUUPa5PnljLOzUlmUYPnSjMcijWd0w==",
"version": "6.0.3",
"resolved": "https://registry.npmjs.org/koishi-thirdeye/-/koishi-thirdeye-6.0.3.tgz",
"integrity": "sha512-z4sCL33p1/0svFh5/j50GFHpvV6S2cuUJqMT1A3a1iI5lqYWsukDlIS8SjzN8tEA/yXNRUgWgUFr3iFIfWcaPw==",
"dependencies": {
"@types/koa": "^2.13.4",
"@types/koa__router": "^8.0.11",
......@@ -6172,9 +6172,9 @@
}
},
"koishi-thirdeye": {
"version": "6.0.2",
"resolved": "https://registry.npmjs.org/koishi-thirdeye/-/koishi-thirdeye-6.0.2.tgz",
"integrity": "sha512-KOkK3eq+PTdrQjJ7/TBsz30dP+IQQsTmNU6OxIEL0nisFqEinlUPfva7XUUPa5PnljLOzUlmUYPnSjMcijWd0w==",
"version": "6.0.3",
"resolved": "https://registry.npmjs.org/koishi-thirdeye/-/koishi-thirdeye-6.0.3.tgz",
"integrity": "sha512-z4sCL33p1/0svFh5/j50GFHpvV6S2cuUJqMT1A3a1iI5lqYWsukDlIS8SjzN8tEA/yXNRUgWgUFr3iFIfWcaPw==",
"requires": {
"@types/koa": "^2.13.4",
"@types/koa__router": "^8.0.11",
......
......@@ -31,7 +31,7 @@
"koishi": "^4.0.0-rc.0"
},
"dependencies": {
"koishi-thirdeye": "^6.0.2",
"koishi-thirdeye": "^6.0.3",
"source-map-support": "^0.5.21"
},
"devDependencies": {
......
import 'source-map-support/register';
import { DefineSchema, RegisterSchema, SchemaProperty } from 'koishi-thirdeye';
import { RegisterSchema, SchemaProperty } from 'koishi-thirdeye';
@RegisterSchema()
export class DicePluginConfig {
......
import 'source-map-support/register';
import { Channel, Context, NextFunction, Random, Session } from 'koishi';
import { DicePluginConfig, DicePluginConfigLike } from './config';
import {
CommandAlias,
CommandExample,
CommandShortcut,
Inject,
InjectConfig,
KoishiPlugin,
OnApply,
PutArg,
PutChannel,
PutUserName,
UseCommand,
UseMiddleware,
} from 'koishi-thirdeye';
import { DiceDbModule } from './modules/db';
import { RcResult, RcRuleList } from './utility/rc-rules';
import { DicePluginConfig } from './config';
import { KoishiPlugin, OnApply, UseCommand } from 'koishi-thirdeye';
import { DbModule } from './modules/db';
import { BaseModule } from './utility/base-module';
import { RollModule } from './modules/roll';
import { RcModule } from './modules/rc';
import { CompatModule } from './modules/compat';
export * from './config';
......@@ -27,135 +16,21 @@ declare module 'koishi' {
// eslint-disable-next-line @typescript-eslint/no-namespace
namespace Context {
interface Services {
dicexDb: DiceDbModule;
diceDb: DbModule;
}
}
}
const rollRegexp = /^((\d*)d)?(\d+)(\+((\d*)d)?(\d+))*$/i;
const rcRegexp = /^[ac](\d{1,3})( +[^ ].*)?$/i;
@KoishiPlugin({ name: 'dicex', schema: DicePluginConfig })
export default class DicePlugin implements OnApply {
constructor(private ctx: Context, config: DicePluginConfigLike) {}
@InjectConfig()
private config: DicePluginConfig;
@Inject('dicexDb')
private db: DiceDbModule;
export default class DicePlugin extends BaseModule implements OnApply {
@UseCommand('dice', '骰子指令', { empty: true })
// eslint-disable-next-line @typescript-eslint/no-empty-function
diceCommand() {}
@UseCommand('dice/roll [expr:string]', '掷骰')
@CommandShortcut('掷骰', { fuzzy: true })
@CommandExample('roll 2d6+d10')
onRoll(@PutUserName(true) username: string, @PutArg(0) message = '1d6') {
if (!rollRegexp.test(message)) return '表达式语法错误。';
const { maxPoint = 1 << 16, maxTimes = 64 } = this.config;
const expressions = message.split('+');
let hasMultiple = false;
let output = `${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;
}
private getRcRule(channel: Channel) {
const index = channel?.diceRcMode || 0;
return RcRuleList[index];
}
@UseCommand('dice/rc <rate:integer> [reason:string]', '检定')
@CommandAlias('ra')
@CommandShortcut('检定', { fuzzy: true })
@CommandExample('rc 20 潜行')
onRc(
@PutUserName(true) username: string,
@PutArg(0) rate: number,
@PutArg(1) reason: string,
@PutChannel(['diceRcMode']) channel: Channel,
) {
if (!rate || rate < 0 || rate > 100) {
return '成功率必须在 0 到 100 之间。';
}
const rule = this.getRcRule(channel);
const value = Random.int(1, 101);
const result = rule.check(rate, value);
const resultText =
result === RcResult.BigFailure
? '大失败!'
: result === RcResult.Failure
? '失败'
: result === RcResult.Success
? '成功'
: '大成功!';
return `${username}${
reason ? `要${reason},开始` : ''
}进行检定:D100=${value}/${rate} ${resultText}`;
}
@UseMiddleware()
onRollCompat(session: Session, next: NextFunction) {
const { content, prefix } = session.parsed;
if (!prefix || content[0] !== 'r') return next();
const expr = content.slice(1);
if (rollRegexp.test(expr)) {
return session.execute({ name: 'roll', args: [expr] });
}
if (rcRegexp.test(expr)) {
const matching = expr.match(rcRegexp);
return session.execute({
name: 'rc',
args: [matching[1], ...(matching[2] ? [matching[2].trim()] : [])],
});
}
return next();
}
onApply() {
this.ctx.plugin(DiceDbModule, this.config);
this.ctx.plugin(DbModule, this.config);
this.ctx.plugin(RollModule, this.config);
this.ctx.plugin(RcModule, this.config);
this.ctx.plugin(CompatModule, this.config);
}
}
import { DiceModule } from '../utility/utility';
import { BaseModule } from '../utility/base-module';
import { UseMiddleware } from 'koishi-thirdeye';
import { NextFunction, Session } from 'koishi';
import { rcRegexp, rollRegexp } from '../utility/constant';
@DiceModule()
export class CompatModule extends BaseModule {
@UseMiddleware()
onRollCompat(session: Session, next: NextFunction) {
const { content, prefix } = session.parsed;
if (!prefix || content[0] !== 'r') return next();
const expr = content.slice(1);
if (rollRegexp.test(expr)) {
return session.execute({ name: 'roll', args: [expr] });
}
if (rcRegexp.test(expr)) {
const matching = expr.match(rcRegexp);
return session.execute({
name: 'rc',
args: [matching[1], ...(matching[2] ? [matching[2].trim()] : [])],
});
}
return next();
}
}
......@@ -2,9 +2,7 @@ import {
CommandExample,
CommandUsage,
Inject,
InjectConfig,
InjectLogger,
KoishiPlugin,
OnApply,
OnChannel,
Provide,
......@@ -12,9 +10,10 @@ import {
PutOption,
UseCommand,
} from 'koishi-thirdeye';
import { DicePluginConfig } from '../config';
import { Channel, Context, Database, Logger, Model } from 'koishi';
import { Channel, Database, Logger, Model } from 'koishi';
import { RcRuleList } from '../utility/rc-rules';
import { DiceModule } from '../utility/utility';
import { BaseModule } from '../utility/base-module';
declare module 'koishi' {
interface Channel {
......@@ -22,14 +21,9 @@ declare module 'koishi' {
}
}
@Provide('dicexDb')
@KoishiPlugin({ name: 'dicex-db', schema: DicePluginConfig })
export class DiceDbModule implements OnApply {
constructor(private ctx: Context, config: DicePluginConfig) {}
@InjectConfig()
private config: DicePluginConfig;
@Provide('diceDb')
@DiceModule()
export class DbModule extends BaseModule implements OnApply {
@Inject(true)
private database: Database;
......
import {
CommandAlias,
CommandExample,
CommandShortcut,
PutArg,
PutChannel,
PutUserName,
UseCommand,
} from 'koishi-thirdeye';
import { Channel, Random } from 'koishi';
import { DiceModule } from '../utility/utility';
import { BaseModule } from '../utility/base-module';
import { RcResult, RcRuleList } from '../utility/rc-rules';
@DiceModule()
export class RcModule extends BaseModule {
private getRcRule(channel: Channel) {
const index = channel?.diceRcMode || 0;
return RcRuleList[index];
}
@UseCommand('dice/rc <rate:integer> [reason:string]', '检定')
@CommandAlias('ra')
@CommandShortcut('检定', { fuzzy: true })
@CommandExample('rc 20 潜行')
onRc(
@PutUserName(true) username: string,
@PutArg(0) rate: number,
@PutArg(1) reason: string,
@PutChannel(['diceRcMode']) channel: Channel,
) {
if (!rate || rate < 0 || rate > 100) {
return '成功率必须在 0 到 100 之间。';
}
const rule = this.getRcRule(channel);
const value = Random.int(1, 101);
const result = rule.check(rate, value);
const resultText =
result === RcResult.BigFailure
? '大失败!'
: result === RcResult.Failure
? '失败'
: result === RcResult.Success
? '成功'
: '大成功!';
return `${username}${
reason ? `要${reason},开始` : ''
}进行检定:D100=${value}/${rate} ${resultText}`;
}
}
import {
CommandExample,
CommandShortcut,
PutArg,
PutUserName,
UseCommand,
} from 'koishi-thirdeye';
import { Random } from 'koishi';
import { rollRegexp } from '../utility/constant';
import { DiceModule } from '../utility/utility';
import { BaseModule } from '../utility/base-module';
@DiceModule()
export class RollModule extends BaseModule {
@UseCommand('dice/roll [expr:string]', '掷骰')
@CommandShortcut('掷骰', { fuzzy: true })
@CommandExample('roll 2d6+d10')
onRoll(@PutUserName(true) username: string, @PutArg(0) message = '1d6') {
if (!rollRegexp.test(message)) return '表达式语法错误。';
const { maxPoint = 1 << 16, maxTimes = 64 } = this.config;
const expressions = message.split('+');
let hasMultiple = false;
let output = `${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;
}
}
import { DicePluginConfig } from '../config';
import { BasePlugin } from 'koishi-thirdeye';
export class BaseModule extends BasePlugin<DicePluginConfig> {}
export const rollRegexp = /^((\d*)d)?(\d+)(\+((\d*)d)?(\d+))*$/i;
export const rcRegexp = /^[ac](\d{1,3})( +[^ ].*)?$/i;
import { KoishiPlugin } from 'koishi-thirdeye';
import { DicePluginConfig } from '../config';
export const DiceModule = () => KoishiPlugin({ schema: DicePluginConfig });
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