Commit f935d3fa authored by nanahira's avatar nanahira

add .st

parent 70dbb8a2
......@@ -3,6 +3,7 @@ import { DicePluginConfig } from './config';
import {
CommandDescription,
DefinePlugin,
Isolate,
LifecycleEvents,
UseCommand,
} from 'koishi-thirdeye';
......@@ -14,12 +15,7 @@ import { CompatModule } from './modules/compat';
export * from './config';
declare module 'koishi' {
interface Context {
diceDb: DbModule;
}
}
@Isolate('diceDb')
@DefinePlugin({ name: 'dicex', schema: DicePluginConfig })
export default class DicePlugin extends BaseModule implements LifecycleEvents {
@UseCommand('dice', { empty: true })
......
......@@ -3,16 +3,29 @@ import {
CommandDescription,
CommandExample,
CommandUsage,
DefineModel,
Inject,
InjectLogger,
LifecycleEvents,
MixinModel,
ModelField,
Primary,
Provide,
PutArg,
PutOption,
PutSession,
UseCommand,
UseModel,
} 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 {
DiceModule,
......@@ -33,15 +46,35 @@ declare module 'koishi' {
interface User {
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')
@MixinModel('user', { diceProfile: DiceProfile })
@MixinModel('channel', { diceProfile: DiceProfile })
@UseModel(DiceSkill)
@DiceModule()
export class DbModule extends BaseModule implements LifecycleEvents {
@Inject(true)
private database: Database;
private database: Database<Tables>;
@Inject(true)
private model: DatabaseService;
......@@ -57,6 +90,48 @@ export class DbModule extends BaseModule implements LifecycleEvents {
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')
@CommandDescription({ zh: '设置检点规则', en: 'Set RC rule' })
@CommandUsage(
......
......@@ -3,13 +3,14 @@ import {
CommandDescription,
CommandExample,
CommandShortcut,
CommandUsage,
Inject,
PutArg,
PutChannel,
PutUser,
PutSession,
PutUserName,
UseCommand,
} from 'koishi-thirdeye';
import { Channel, Random, User } from 'koishi';
import { Channel, Random, Session, User } from 'koishi';
import {
DiceModule,
getRcMode,
......@@ -18,6 +19,7 @@ import {
} from '../utility/utility';
import { BaseModule } from '../utility/base-module';
import { RcResult, RcRuleList } from '../utility/rc-rules';
import { DbModule } from './db';
@DiceModule()
export class RcModule extends BaseModule {
......@@ -26,19 +28,28 @@ export class RcModule extends BaseModule {
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' })
@CommandAlias('ra')
@CommandShortcut('检定', { fuzzy: true })
@CommandExample('rc 20 潜行')
onRc(
@CommandUsage('可以用 st 设置数值。')
@CommandExample('rc 潜行 20')
@CommandExample('rc 潜行')
async onRc(
@PutSession() session: Session,
@PutUserName(true) username: string,
@PutArg(0) rate: number,
@PutArg(1) reason: string,
@PutArg(0) reason: string,
@PutArg(1) rate: number,
@PutUserProfile() user: User,
@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 之间。';
}
const rule = this.getRcRule(user, channel);
......@@ -52,8 +63,6 @@ export class RcModule extends BaseModule {
: result === RcResult.Success
? '成功'
: '大成功!';
return `${username}${
reason ? `要${reason},开始` : ''
}进行检定:D100=${value}/${rate} ${resultText}`;
return `${username}${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