Commit e41fc5d8 authored by nanahira's avatar nanahira

rework database module

parent cd8548d2
......@@ -9,13 +9,14 @@
"version": "4.0.2",
"license": "MIT",
"dependencies": {
"koishi-thirdeye": "^10.0.0"
"cosmotype-decorators": "^2.0.3",
"koishi-thirdeye": "^10.0.5"
},
"devDependencies": {
"@koishijs/plugin-cache-lru": "^1.0.0-rc.0",
"@koishijs/plugin-console": "^3.2.4",
"@koishijs/plugin-database-memory": "^1.2.0",
"@koishijs/plugin-dataview": "^1.2.1",
"@koishijs/plugin-dataview": "^1.2.3",
"@koishijs/plugin-sandbox": "^1.1.0",
"@types/jest": "^27.4.0",
"@types/node": "^17.0.4",
......@@ -1115,9 +1116,9 @@
"dev": true
},
"node_modules/@koishijs/plugin-console": {
"version": "3.2.4",
"resolved": "https://registry.npmjs.org/@koishijs/plugin-console/-/plugin-console-3.2.4.tgz",
"integrity": "sha512-R2mX0t8FyZHJb78Hh0IjewQfwKWi5WokuUKpCoLlQKSFKXLUobYi15hmN8QFe97J2TRPCyGWw7lZ7ix9HpSYgw==",
"version": "3.2.6",
"resolved": "https://registry.npmjs.org/@koishijs/plugin-console/-/plugin-console-3.2.6.tgz",
"integrity": "sha512-pEtNShS1O7LOXpFEynKCfvRdD902baC7igudRNwUixLa//QIx1Zo04lZh948Q/NiTrUnXDkXAxiI3zARhrzwYA==",
"dev": true,
"dependencies": {
"open": "^8.4.0",
......@@ -1125,7 +1126,7 @@
"ws": "^8.5.0"
},
"peerDependencies": {
"koishi": "^4.5.0"
"koishi": "^4.6.0"
}
},
"node_modules/@koishijs/plugin-database-memory": {
......@@ -1141,13 +1142,13 @@
}
},
"node_modules/@koishijs/plugin-dataview": {
"version": "1.2.1",
"resolved": "https://registry.npmjs.org/@koishijs/plugin-dataview/-/plugin-dataview-1.2.1.tgz",
"integrity": "sha512-Ox4n7YoNmDm+EIpSVj9c7Y/FSnKgokw8qQaPgfIGUWiRmiznelq1b016VPdM47eixTkVzMKDUehZGWLwMbKk7w==",
"version": "1.2.3",
"resolved": "https://registry.npmjs.org/@koishijs/plugin-dataview/-/plugin-dataview-1.2.3.tgz",
"integrity": "sha512-7cnS/OiO362MoVcvYIoWqitaNAS7jGOK4uzdDYo1d+rFbEqtKjk0p6fTJkgR5/hMWA+rxbU67UxGyO+q/dSSmA==",
"dev": true,
"peerDependencies": {
"@koishijs/plugin-console": "^3.1.0",
"koishi": "^4.2.2"
"@koishijs/plugin-console": "^3.2.6",
"koishi": "^4.6.0"
}
},
"node_modules/@koishijs/plugin-sandbox": {
......@@ -2632,9 +2633,9 @@
}
},
"node_modules/cosmotype-decorators": {
"version": "2.0.1",
"resolved": "https://registry.npmjs.org/cosmotype-decorators/-/cosmotype-decorators-2.0.1.tgz",
"integrity": "sha512-5pGd3Ln5YKLDx0FEWDV90EJFfvJVDK+0/kU5V2YJesuZrkocLKi6z2P76n6RRQUoV+hdn9/z0lqqdR748wTWjg==",
"version": "2.0.5",
"resolved": "https://registry.npmjs.org/cosmotype-decorators/-/cosmotype-decorators-2.0.5.tgz",
"integrity": "sha512-Y+OXs405l6T+IUk59CtvG3y8LU7CCTITKWLfafEm8wVUAJFMTJGKh63HtZmmq9rKl/qM3RuHG12DZA9SZvsrAg==",
"dependencies": {
"lodash": "^4.17.21",
"typed-reflector": "^1.0.10"
......@@ -5233,13 +5234,13 @@
}
},
"node_modules/koishi-thirdeye": {
"version": "10.0.0",
"resolved": "https://registry.npmjs.org/koishi-thirdeye/-/koishi-thirdeye-10.0.0.tgz",
"integrity": "sha512-sFS5x2dqHdOSQtnQa8x2i+hMhOQUJZfQGmjRaWu7trjQX1GDRrJTn2mf0Go4Pf4LRZ/0P+IdtsBncxoptPXzMg==",
"version": "10.0.5",
"resolved": "https://registry.npmjs.org/koishi-thirdeye/-/koishi-thirdeye-10.0.5.tgz",
"integrity": "sha512-GzPNyJCnUNklHefi0vc0nArV+dEh1QuTddom9MQ6Sawa6nbjbnZNWKFY08eQxNR6CR87tYK93WtO+URwcBbEew==",
"dependencies": {
"@types/koa": "^2.13.4",
"@types/koa__router": "^8.0.11",
"cosmotype-decorators": "^2.0.1",
"cosmotype-decorators": "^2.0.5",
"koishi-decorators": "^2.0.0",
"lodash": "^4.17.21",
"reflect-metadata": "^0.1.13",
......@@ -8520,9 +8521,9 @@
}
},
"@koishijs/plugin-console": {
"version": "3.2.4",
"resolved": "https://registry.npmjs.org/@koishijs/plugin-console/-/plugin-console-3.2.4.tgz",
"integrity": "sha512-R2mX0t8FyZHJb78Hh0IjewQfwKWi5WokuUKpCoLlQKSFKXLUobYi15hmN8QFe97J2TRPCyGWw7lZ7ix9HpSYgw==",
"version": "3.2.6",
"resolved": "https://registry.npmjs.org/@koishijs/plugin-console/-/plugin-console-3.2.6.tgz",
"integrity": "sha512-pEtNShS1O7LOXpFEynKCfvRdD902baC7igudRNwUixLa//QIx1Zo04lZh948Q/NiTrUnXDkXAxiI3zARhrzwYA==",
"dev": true,
"requires": {
"open": "^8.4.0",
......@@ -8540,9 +8541,9 @@
}
},
"@koishijs/plugin-dataview": {
"version": "1.2.1",
"resolved": "https://registry.npmjs.org/@koishijs/plugin-dataview/-/plugin-dataview-1.2.1.tgz",
"integrity": "sha512-Ox4n7YoNmDm+EIpSVj9c7Y/FSnKgokw8qQaPgfIGUWiRmiznelq1b016VPdM47eixTkVzMKDUehZGWLwMbKk7w==",
"version": "1.2.3",
"resolved": "https://registry.npmjs.org/@koishijs/plugin-dataview/-/plugin-dataview-1.2.3.tgz",
"integrity": "sha512-7cnS/OiO362MoVcvYIoWqitaNAS7jGOK4uzdDYo1d+rFbEqtKjk0p6fTJkgR5/hMWA+rxbU67UxGyO+q/dSSmA==",
"dev": true,
"requires": {}
},
......@@ -9774,9 +9775,9 @@
}
},
"cosmotype-decorators": {
"version": "2.0.1",
"resolved": "https://registry.npmjs.org/cosmotype-decorators/-/cosmotype-decorators-2.0.1.tgz",
"integrity": "sha512-5pGd3Ln5YKLDx0FEWDV90EJFfvJVDK+0/kU5V2YJesuZrkocLKi6z2P76n6RRQUoV+hdn9/z0lqqdR748wTWjg==",
"version": "2.0.5",
"resolved": "https://registry.npmjs.org/cosmotype-decorators/-/cosmotype-decorators-2.0.5.tgz",
"integrity": "sha512-Y+OXs405l6T+IUk59CtvG3y8LU7CCTITKWLfafEm8wVUAJFMTJGKh63HtZmmq9rKl/qM3RuHG12DZA9SZvsrAg==",
"requires": {
"lodash": "^4.17.21",
"typed-reflector": "^1.0.10"
......@@ -11724,13 +11725,13 @@
}
},
"koishi-thirdeye": {
"version": "10.0.0",
"resolved": "https://registry.npmjs.org/koishi-thirdeye/-/koishi-thirdeye-10.0.0.tgz",
"integrity": "sha512-sFS5x2dqHdOSQtnQa8x2i+hMhOQUJZfQGmjRaWu7trjQX1GDRrJTn2mf0Go4Pf4LRZ/0P+IdtsBncxoptPXzMg==",
"version": "10.0.5",
"resolved": "https://registry.npmjs.org/koishi-thirdeye/-/koishi-thirdeye-10.0.5.tgz",
"integrity": "sha512-GzPNyJCnUNklHefi0vc0nArV+dEh1QuTddom9MQ6Sawa6nbjbnZNWKFY08eQxNR6CR87tYK93WtO+URwcBbEew==",
"requires": {
"@types/koa": "^2.13.4",
"@types/koa__router": "^8.0.11",
"cosmotype-decorators": "^2.0.1",
"cosmotype-decorators": "^2.0.5",
"koishi-decorators": "^2.0.0",
"lodash": "^4.17.21",
"reflect-metadata": "^0.1.13",
......
......@@ -5,6 +5,7 @@ import {
CommandUsage,
Inject,
InjectLogger,
MixinModel,
OnApply,
Provide,
PutArg,
......@@ -13,30 +14,32 @@ import {
PutUser,
UseCommand,
} from 'koishi-thirdeye';
import { Channel, Database, DatabaseService, Logger, Model, User } from 'koishi';
import { Channel, Database, DatabaseService, Logger, User } from 'koishi';
import { RcRuleList } from '../utility/rc-rules';
import {
DiceModule,
getDefaultRollFaces,
getRcMode,
PutChannelProfile,
PutIsGlobal,
PutUserProfile,
} from '../utility/utility';
import { BaseModule } from '../utility/base-module';
import { getProfileModel } from '../utility/dice-profile';
import { DiceProfile } from '../utility/dice-profile';
declare module 'koishi' {
interface Channel {
diceRcMode: number;
diceDefaultRollFaces: number;
diceProfile: DiceProfile;
}
interface User {
diceRcMode: number;
diceDefaultRollFaces: number;
diceProfile: DiceProfile;
}
}
@Provide('diceDb')
@MixinModel('user', { diceProfile: DiceProfile })
@MixinModel('channel', { diceProfile: DiceProfile })
@DiceModule()
export class DbModule extends BaseModule implements OnApply {
@Inject(true)
......@@ -49,9 +52,6 @@ export class DbModule extends BaseModule implements OnApply {
private logger: Logger;
onApply() {
const profileModel = getProfileModel();
this.model.extend('channel', profileModel);
this.model.extend('user', profileModel);
this.logger.info(`Dice database module loaded.`);
}
......@@ -68,8 +68,8 @@ export class DbModule extends BaseModule implements OnApply {
)
@CommandExample('rcmode -s 1 设置当前频道的检点规则为1。')
setRcMode(
@PutUser(['diceRcMode']) user: User,
@PutChannel(['diceRcMode']) channel: Channel,
@PutUserProfile() user: User,
@PutChannelProfile() channel: Channel,
@PutOption('set', '-s <rule:integer> 设置规则', {
description: { en: 'Set rule' },
})
......@@ -87,7 +87,7 @@ export class DbModule extends BaseModule implements OnApply {
if (setRule < 0 || setRule >= RcRuleList.length) {
return '规则序号不合法';
}
(isGlobal ? channel : user).diceRcMode = setRule;
(isGlobal ? channel : user).diceProfile.rcMode = setRule;
return `已设置当前${this.targetPattern(
isGlobal,
)}的检点规则为 ${setRule} 。`;
......@@ -99,8 +99,8 @@ export class DbModule extends BaseModule implements OnApply {
@CommandUsage('默认面数为 6 。')
@CommandExample('faces -s 10 设置当前频道的默认面数为10。')
setFaces(
@PutUser(['diceDefaultRollFaces']) user: User,
@PutChannel(['diceDefaultRollFaces']) channel: Channel,
@PutUserProfile() user: User,
@PutChannelProfile() channel: Channel,
@PutArg(0) setFaceInArg: number,
@PutOption('set', '-s <rule:integer> 设置面数', {
description: { en: 'Set face count' },
......@@ -123,7 +123,7 @@ export class DbModule extends BaseModule implements OnApply {
if (setFace < 2 || setFace > this.config.maxPoint) {
return `面数必须在 2 到 ${this.config.maxPoint} 之间。`;
}
(isGlobal ? channel : user).diceDefaultRollFaces = setFace;
(isGlobal ? channel : user).diceProfile.defaultRollFaces = setFace;
return `已设置当前${this.targetPattern(
isGlobal,
)}的默认面数为 ${setFace} 。`;
......
......@@ -10,7 +10,12 @@ import {
UseCommand,
} from 'koishi-thirdeye';
import { Channel, Random, User } from 'koishi';
import { DiceModule, getRcMode } from '../utility/utility';
import {
DiceModule,
getRcMode,
PutChannelProfile,
PutUserProfile,
} from '../utility/utility';
import { BaseModule } from '../utility/base-module';
import { RcResult, RcRuleList } from '../utility/rc-rules';
......@@ -30,8 +35,8 @@ export class RcModule extends BaseModule {
@PutUserName(true) username: string,
@PutArg(0) rate: number,
@PutArg(1) reason: string,
@PutUser(['diceRcMode']) user: User,
@PutChannel(['diceRcMode']) channel: Channel,
@PutUserProfile() user: User,
@PutChannelProfile() channel: Channel,
) {
if (!rate || rate < 0 || rate > 100) {
return '成功率必须在 0 到 100 之间。';
......
......@@ -11,7 +11,12 @@ import {
} from 'koishi-thirdeye';
import { Channel, Random, User } from 'koishi';
import { rollRegexp } from '../utility/constant';
import { DiceModule, getDefaultRollFaces } from '../utility/utility';
import {
DiceModule,
getDefaultRollFaces,
PutChannelProfile,
PutUserProfile,
} from '../utility/utility';
import { BaseModule } from '../utility/base-module';
@DiceModule()
......@@ -26,8 +31,8 @@ export class RollModule extends BaseModule {
onRoll(
@PutUserName(true) username: string,
@PutArg(0) message: string,
@PutUser(['diceDefaultRollFaces']) user: User,
@PutChannel(['diceDefaultRollFaces']) channel: Channel,
@PutUserProfile() user: User,
@PutChannelProfile() channel: Channel,
) {
if (!message) {
message = `1d${getDefaultRollFaces(user, channel)}`;
......
import { Field } from 'koishi';
import { ModelField } from 'koishi-thirdeye';
export interface DiceProfile {
diceRcMode: number;
diceDefaultRollFaces: number;
}
export const DiceProfileFields: (keyof DiceProfile)[] = [
'diceRcMode',
'diceDefaultRollFaces',
];
export function getProfileModel(): Field.Extension<DiceProfile> {
return {
diceRcMode: { type: 'integer' },
diceDefaultRollFaces: { type: 'integer' },
};
export class DiceProfile {
@ModelField({
type: 'integer',
initial: 0,
})
rcMode: number;
@ModelField({
type: 'integer',
initial: 6,
})
defaultRollFaces: number;
}
import { DefinePlugin, PutChannel, PutOption, PutUser } from 'koishi-thirdeye';
import { DicePluginConfig } from '../config';
import { Channel, User } from 'koishi';
import { DiceProfile, DiceProfileFields } from './dice-profile';
import { DiceProfile } from './dice-profile';
export const DiceModule = () => DefinePlugin({ schema: DicePluginConfig });
......@@ -11,22 +11,20 @@ export function getProfileProperty<K extends keyof DiceProfile>(
property: K,
defaultValue: DiceProfile[K],
) {
if (user && user[property] != null) {
return user[property];
}
if (channel && channel[property] != null) {
return channel[property];
}
return defaultValue;
return (
user?.diceProfile?.[property] ??
channel?.diceProfile?.[property] ??
defaultValue
);
}
export const getRcMode = (user: User, channel: Channel) =>
getProfileProperty(user, channel, 'diceRcMode', 0);
getProfileProperty(user, channel, 'rcMode', 0);
export const getDefaultRollFaces = (user: User, channel: Channel) =>
getProfileProperty(user, channel, 'diceDefaultRollFaces', 6);
getProfileProperty(user, channel, 'defaultRollFaces', 6);
export const PutUserProfile = () => PutUser(DiceProfileFields);
export const PutChannelProfile = () => PutChannel(DiceProfileFields);
export const PutUserProfile = () => PutUser(['diceProfile']);
export const PutChannelProfile = () => PutChannel(['diceProfile']);
export const PutIsGlobal = () => PutOption('global', '-g 作用于本频道。');
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