Commit 7536d9d3 authored by nanahira's avatar nanahira

migrate to onedice

parent da813bd2
......@@ -10,7 +10,8 @@
"license": "MIT",
"dependencies": {
"cosmotype-decorators": "^2.0.3",
"koishi-thirdeye": "^11.0.9"
"koishi-thirdeye": "^11.0.9",
"onedice": "^1.0.2"
},
"devDependencies": {
"@koishijs/plugin-console": "^4.1.1",
......@@ -6035,6 +6036,11 @@
"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": {
"version": "5.1.2",
"resolved": "https://registry.npmjs.org/onetime/-/onetime-5.1.2.tgz",
......@@ -12816,6 +12822,11 @@
"wrappy": "1"
}
},
"onedice": {
"version": "1.0.2",
"resolved": "https://registry.npmjs.org/onedice/-/onedice-1.0.2.tgz",
"integrity": "sha512-B7sN77wWL7WP+4a5ngPYhrJC4E6q8yHs2FQS6hz/vPmTgBXZvJjJMJguV8DAaLtSLjbhwXG4wdIoyNI64ieziQ=="
},
"onetime": {
"version": "5.1.2",
"resolved": "https://registry.npmjs.org/onetime/-/onetime-5.1.2.tgz",
......
......@@ -156,8 +156,8 @@ export class DbModule extends BaseModule implements LifecycleEvents {
errorMessages.push('技能名称不能为空');
return;
}
if (value == null || value < 0 || value > 100) {
errorMessages.push(`${skillName} 的数值必须在 0 到 100 之间。`);
if (value == null || value < 0) {
errorMessages.push(`${skillName} 的数值无效。`);
return;
}
if (skillsSet.has(skillName)) {
......@@ -218,6 +218,21 @@ export class DbModule extends BaseModule implements LifecycleEvents {
.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) {
const [skill] = await this.database.get(
'diceSkill',
......
......@@ -3,14 +3,13 @@ import {
CommandDescription,
CommandExample,
CommandShortcut,
Inject,
PutArg,
PutChannel,
PutUser,
PutSession,
PutUserName,
UseCommand,
} from 'koishi-thirdeye';
import { Channel, Random, User } from 'koishi';
import { rollRegexp } from '../utility/constant';
import { Channel, Session, User } from 'koishi';
import {
DiceModule,
getDefaultRollFaces,
......@@ -18,71 +17,41 @@ import {
PutUserProfile,
} from '../utility/utility';
import { BaseModule } from '../utility/base-module';
import { OneDice } from 'onedice';
import { DbModule } from './db';
@DiceModule()
export class RollModule extends BaseModule {
@Inject()
private diceDb: DbModule;
@UseCommand('dice/roll [expr:string]', '掷骰')
@CommandDescription({ en: 'Roll dice' })
@CommandAlias('r')
@CommandShortcut('掷骰', { fuzzy: true })
@CommandExample('roll 2d6+d10')
onRoll(
async onRoll(
@PutUserName(true) username: string,
@PutArg(0) message: string,
@PutUserProfile() user: User,
@PutChannelProfile() channel: Channel,
@PutSession() session: Session,
) {
if (!message) {
message = `1d${getDefaultRollFaces(user, channel)}`;
message = `1d`;
}
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;
try {
output += new OneDice({
defaultDiceFaces: getDefaultRollFaces(user, channel),
maxDiceCount: maxTimes,
maxDiceFaces: maxPoint,
valueDict: this.diceDb ? await this.diceDb.getAllSkills(session) : {},
}).calculate(message);
} catch (e) {
return '表达式语法错误。';
}
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