Commit 7536d9d3 authored by nanahira's avatar nanahira

migrate to onedice

parent da813bd2
...@@ -10,7 +10,8 @@ ...@@ -10,7 +10,8 @@
"license": "MIT", "license": "MIT",
"dependencies": { "dependencies": {
"cosmotype-decorators": "^2.0.3", "cosmotype-decorators": "^2.0.3",
"koishi-thirdeye": "^11.0.9" "koishi-thirdeye": "^11.0.9",
"onedice": "^1.0.2"
}, },
"devDependencies": { "devDependencies": {
"@koishijs/plugin-console": "^4.1.1", "@koishijs/plugin-console": "^4.1.1",
...@@ -6035,6 +6036,11 @@ ...@@ -6035,6 +6036,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",
...@@ -12816,6 +12822,11 @@ ...@@ -12816,6 +12822,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",
......
...@@ -31,7 +31,8 @@ ...@@ -31,7 +31,8 @@
"homepage": "https://code.mycard.moe/3rdeye/koishi-plugin-dicex", "homepage": "https://code.mycard.moe/3rdeye/koishi-plugin-dicex",
"dependencies": { "dependencies": {
"cosmotype-decorators": "^2.0.3", "cosmotype-decorators": "^2.0.3",
"koishi-thirdeye": "^11.0.9" "koishi-thirdeye": "^11.0.9",
"onedice": "^1.0.2"
}, },
"devDependencies": { "devDependencies": {
"@koishijs/plugin-console": "^4.1.1", "@koishijs/plugin-console": "^4.1.1",
......
...@@ -156,8 +156,8 @@ export class DbModule extends BaseModule implements LifecycleEvents { ...@@ -156,8 +156,8 @@ export class DbModule extends BaseModule implements LifecycleEvents {
errorMessages.push('技能名称不能为空'); errorMessages.push('技能名称不能为空');
return; return;
} }
if (value == null || value < 0 || value > 100) { if (value == null || value < 0) {
errorMessages.push(`${skillName} 的数值必须在 0 到 100 之间。`); errorMessages.push(`${skillName} 的数值无效。`);
return; return;
} }
if (skillsSet.has(skillName)) { if (skillsSet.has(skillName)) {
...@@ -218,6 +218,21 @@ export class DbModule extends BaseModule implements LifecycleEvents { ...@@ -218,6 +218,21 @@ export class DbModule extends BaseModule implements LifecycleEvents {
.join(' ')}`; .join(' ')}`;
} }
async getAllSkills(session: Session) {
const records = await this.database.get(
'diceSkill',
{
userId: session.userId,
channelId: session.channelId || 'priv',
character: await this.getCurrentCharacter(session),
},
['skillName', 'value'],
);
return Object.fromEntries(
records.map(({ skillName, value }) => [skillName, value]),
);
}
async getSkillValue(session: Session, skillName: string) { async getSkillValue(session: Session, skillName: string) {
const [skill] = await this.database.get( const [skill] = await this.database.get(
'diceSkill', 'diceSkill',
......
...@@ -3,14 +3,13 @@ import { ...@@ -3,14 +3,13 @@ import {
CommandDescription, CommandDescription,
CommandExample, CommandExample,
CommandShortcut, CommandShortcut,
Inject,
PutArg, PutArg,
PutChannel, PutSession,
PutUser,
PutUserName, PutUserName,
UseCommand, UseCommand,
} from 'koishi-thirdeye'; } from 'koishi-thirdeye';
import { Channel, Random, User } from 'koishi'; import { Channel, Session, User } from 'koishi';
import { rollRegexp } from '../utility/constant';
import { import {
DiceModule, DiceModule,
getDefaultRollFaces, getDefaultRollFaces,
...@@ -18,71 +17,41 @@ import { ...@@ -18,71 +17,41 @@ import {
PutUserProfile, PutUserProfile,
} from '../utility/utility'; } from '../utility/utility';
import { BaseModule } from '../utility/base-module'; import { BaseModule } from '../utility/base-module';
import { OneDice } from 'onedice';
import { DbModule } from './db';
@DiceModule() @DiceModule()
export class RollModule extends BaseModule { export class RollModule extends BaseModule {
@Inject()
private diceDb: DbModule;
@UseCommand('dice/roll [expr:string]', '掷骰') @UseCommand('dice/roll [expr:string]', '掷骰')
@CommandDescription({ en: 'Roll dice' }) @CommandDescription({ en: 'Roll dice' })
@CommandAlias('r') @CommandAlias('r')
@CommandShortcut('掷骰', { fuzzy: true }) @CommandShortcut('掷骰', { fuzzy: true })
@CommandExample('roll 2d6+d10') @CommandExample('roll 2d6+d10')
onRoll( async onRoll(
@PutUserName(true) username: string, @PutUserName(true) username: string,
@PutArg(0) message: string, @PutArg(0) message: string,
@PutUserProfile() user: User, @PutUserProfile() user: User,
@PutChannelProfile() channel: Channel, @PutChannelProfile() channel: Channel,
@PutSession() session: Session,
) { ) {
if (!message) { if (!message) {
message = `1d${getDefaultRollFaces(user, channel)}`; message = `1d`;
} }
if (!rollRegexp.test(message)) return '表达式语法错误。';
const { maxPoint = 1 << 16, maxTimes = 64 } = this.config; const { maxPoint = 1 << 16, maxTimes = 64 } = this.config;
const expressions = message.split('+');
let hasMultiple = false;
let output = `${username} 掷骰:${message}=`; let output = `${username} 掷骰:${message}=`;
let total = 0; try {
output += new OneDice({
for (const expr of expressions) { defaultDiceFaces: getDefaultRollFaces(user, channel),
const [, dice, _times, _max] = /^((\d*)d)?(\d+)$/i.exec(expr); maxDiceCount: maxTimes,
const max = +_max; maxDiceFaces: maxPoint,
if (!max || max > maxPoint) { valueDict: this.diceDb ? await this.diceDb.getAllSkills(session) : {},
return `点数必须在 1 到 ${maxPoint} 之间。`; }).calculate(message);
} } catch (e) {
return '表达式语法错误。';
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; return output;
} }
......
export const rollRegexp = /^((\d*)d)?(\d+)(\+((\d*)d)?(\d+))*$/i;
export const rcRegexp = /^[ac]([^ ]+)( +\d{1,3})?$/i;
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