Commit f935d3fa authored by nanahira's avatar nanahira

add .st

parent 70dbb8a2
...@@ -3,6 +3,7 @@ import { DicePluginConfig } from './config'; ...@@ -3,6 +3,7 @@ import { DicePluginConfig } from './config';
import { import {
CommandDescription, CommandDescription,
DefinePlugin, DefinePlugin,
Isolate,
LifecycleEvents, LifecycleEvents,
UseCommand, UseCommand,
} from 'koishi-thirdeye'; } from 'koishi-thirdeye';
...@@ -14,12 +15,7 @@ import { CompatModule } from './modules/compat'; ...@@ -14,12 +15,7 @@ import { CompatModule } from './modules/compat';
export * from './config'; export * from './config';
declare module 'koishi' { @Isolate('diceDb')
interface Context {
diceDb: DbModule;
}
}
@DefinePlugin({ name: 'dicex', schema: DicePluginConfig }) @DefinePlugin({ name: 'dicex', schema: DicePluginConfig })
export default class DicePlugin extends BaseModule implements LifecycleEvents { export default class DicePlugin extends BaseModule implements LifecycleEvents {
@UseCommand('dice', { empty: true }) @UseCommand('dice', { empty: true })
......
...@@ -3,16 +3,29 @@ import { ...@@ -3,16 +3,29 @@ import {
CommandDescription, CommandDescription,
CommandExample, CommandExample,
CommandUsage, CommandUsage,
DefineModel,
Inject, Inject,
InjectLogger, InjectLogger,
LifecycleEvents, LifecycleEvents,
MixinModel, MixinModel,
ModelField,
Primary,
Provide, Provide,
PutArg, PutArg,
PutOption, PutOption,
PutSession,
UseCommand, UseCommand,
UseModel,
} from 'koishi-thirdeye'; } from 'koishi-thirdeye';
import { Channel, Database, DatabaseService, Logger, User } from 'koishi'; import {
Channel,
Database,
DatabaseService,
Logger,
Session,
Tables,
User,
} from 'koishi';
import { RcRuleList } from '../utility/rc-rules'; import { RcRuleList } from '../utility/rc-rules';
import { import {
DiceModule, DiceModule,
...@@ -33,15 +46,35 @@ declare module 'koishi' { ...@@ -33,15 +46,35 @@ declare module 'koishi' {
interface User { interface User {
diceProfile: DiceProfile; diceProfile: DiceProfile;
} }
interface Tables {
diceSkill: DiceSkill;
}
}
@DefineModel('diceSkill')
export class DiceSkill {
@Primary()
@ModelField('string(64)')
userId: string;
@Primary()
@ModelField('string(64)')
channelId: string;
@Primary()
@ModelField('string(32)')
skillName: string;
@ModelField('unsigned(1)')
value: number;
} }
@Provide('diceDb') @Provide('diceDb')
@MixinModel('user', { diceProfile: DiceProfile }) @MixinModel('user', { diceProfile: DiceProfile })
@MixinModel('channel', { diceProfile: DiceProfile }) @MixinModel('channel', { diceProfile: DiceProfile })
@UseModel(DiceSkill)
@DiceModule() @DiceModule()
export class DbModule extends BaseModule implements LifecycleEvents { export class DbModule extends BaseModule implements LifecycleEvents {
@Inject(true) @Inject(true)
private database: Database; private database: Database<Tables>;
@Inject(true) @Inject(true)
private model: DatabaseService; private model: DatabaseService;
...@@ -57,6 +90,48 @@ export class DbModule extends BaseModule implements LifecycleEvents { ...@@ -57,6 +90,48 @@ export class DbModule extends BaseModule implements LifecycleEvents {
return isGlobal ? '频道' : '用户'; return isGlobal ? '频道' : '用户';
} }
@UseCommand('dice/st <skill:string> <value:integer>')
@CommandDescription({ zh: '设置能力数值', en: 'Set skill value' })
@CommandExample('st 潜行 50')
async onSetSkill(
@PutSession() session: Session,
@PutArg(0) skillName: string,
@PutArg(1) value: number,
) {
if (!skillName) {
return '请输入技能名称。';
}
if (value == null || value < 0 || value > 100) {
return '数值必须在 0 到 100 之间。';
}
await this.database.upsert(
'diceSkill',
[
{
userId: session.userId,
channelId: session.channelId || 'priv',
skillName,
value,
},
],
['userId', 'channelId', 'skillName'],
);
return `已设置 ${skillName} 的能力数值为 ${value}。`;
}
async getSkillValue(session: Session, skillName: string) {
const [skill] = await this.database.get(
'diceSkill',
{
userId: session.userId,
channelId: session.channelId || 'priv',
skillName,
},
['value'],
);
return skill?.value;
}
@UseCommand('dice/rcmode') @UseCommand('dice/rcmode')
@CommandDescription({ zh: '设置检点规则', en: 'Set RC rule' }) @CommandDescription({ zh: '设置检点规则', en: 'Set RC rule' })
@CommandUsage( @CommandUsage(
......
...@@ -3,13 +3,14 @@ import { ...@@ -3,13 +3,14 @@ import {
CommandDescription, CommandDescription,
CommandExample, CommandExample,
CommandShortcut, CommandShortcut,
CommandUsage,
Inject,
PutArg, PutArg,
PutChannel, PutSession,
PutUser,
PutUserName, PutUserName,
UseCommand, UseCommand,
} from 'koishi-thirdeye'; } from 'koishi-thirdeye';
import { Channel, Random, User } from 'koishi'; import { Channel, Random, Session, User } from 'koishi';
import { import {
DiceModule, DiceModule,
getRcMode, getRcMode,
...@@ -18,6 +19,7 @@ import { ...@@ -18,6 +19,7 @@ import {
} from '../utility/utility'; } from '../utility/utility';
import { BaseModule } from '../utility/base-module'; import { BaseModule } from '../utility/base-module';
import { RcResult, RcRuleList } from '../utility/rc-rules'; import { RcResult, RcRuleList } from '../utility/rc-rules';
import { DbModule } from './db';
@DiceModule() @DiceModule()
export class RcModule extends BaseModule { export class RcModule extends BaseModule {
...@@ -26,19 +28,28 @@ export class RcModule extends BaseModule { ...@@ -26,19 +28,28 @@ export class RcModule extends BaseModule {
return RcRuleList[index]; return RcRuleList[index];
} }
@UseCommand('dice/rc <rate:integer> [reason:string]') @Inject()
diceDb: DbModule;
@UseCommand('dice/rc <reason:string> [rate:integer]')
@CommandDescription({ zh: '检定', en: 'Roll check' }) @CommandDescription({ zh: '检定', en: 'Roll check' })
@CommandAlias('ra') @CommandAlias('ra')
@CommandShortcut('检定', { fuzzy: true }) @CommandShortcut('检定', { fuzzy: true })
@CommandExample('rc 20 潜行') @CommandUsage('可以用 st 设置数值。')
onRc( @CommandExample('rc 潜行 20')
@CommandExample('rc 潜行')
async onRc(
@PutSession() session: Session,
@PutUserName(true) username: string, @PutUserName(true) username: string,
@PutArg(0) rate: number, @PutArg(0) reason: string,
@PutArg(1) reason: string, @PutArg(1) rate: number,
@PutUserProfile() user: User, @PutUserProfile() user: User,
@PutChannelProfile() channel: Channel, @PutChannelProfile() channel: Channel,
) { ) {
if (!rate || rate < 0 || rate > 100) { if (rate == null && this.diceDb) {
rate = await this.diceDb.getSkillValue(session, reason);
}
if (rate == null || rate < 0 || rate > 100) {
return '成功率必须在 0 到 100 之间。'; return '成功率必须在 0 到 100 之间。';
} }
const rule = this.getRcRule(user, channel); const rule = this.getRcRule(user, channel);
...@@ -52,8 +63,6 @@ export class RcModule extends BaseModule { ...@@ -52,8 +63,6 @@ export class RcModule extends BaseModule {
: result === RcResult.Success : result === RcResult.Success
? '成功' ? '成功'
: '大成功!'; : '大成功!';
return `${username}${ return `${username}${reason},开始进行检定:D100=${value}/${rate} ${resultText}`;
reason ? `要${reason},开始` : ''
}进行检定:D100=${value}/${rate} ${resultText}`;
} }
} }
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