Commit 76de1ad7 authored by nanahira's avatar nanahira

add character

parent 7e3788cc
...@@ -49,17 +49,34 @@ declare module 'koishi' { ...@@ -49,17 +49,34 @@ declare module 'koishi' {
interface Tables { interface Tables {
diceSkill: DiceSkill; diceSkill: DiceSkill;
diceUserProfile: DiceUserProfile;
} }
} }
@DefineModel('diceSkill') class DiceProfileBase {
export class DiceSkill {
@Primary() @Primary()
@ModelField('string(64)') @ModelField('string(64)')
userId: string; userId: string;
@Primary() @Primary()
@ModelField('string(64)') @ModelField('string(64)')
channelId: string; channelId: string;
}
@DefineModel('diceUserProfile')
export class DiceUserProfile extends DiceProfileBase {
@ModelField('string(64)')
currentCharacter: string;
}
@DefineModel('diceSkill')
export class DiceSkill extends DiceProfileBase {
@Primary()
@ModelField({
type: 'string',
length: 64,
initial: 'default',
})
character: string;
@Primary() @Primary()
@ModelField('string(32)') @ModelField('string(32)')
skillName: string; skillName: string;
...@@ -70,7 +87,7 @@ export class DiceSkill { ...@@ -70,7 +87,7 @@ export class DiceSkill {
@Provide('diceDb') @Provide('diceDb')
@MixinModel('user', { diceProfile: DiceProfile }) @MixinModel('user', { diceProfile: DiceProfile })
@MixinModel('channel', { diceProfile: DiceProfile }) @MixinModel('channel', { diceProfile: DiceProfile })
@UseModel(DiceSkill) @UseModel(DiceSkill, DiceUserProfile)
@DiceModule() @DiceModule()
export class DbModule extends BaseModule implements LifecycleEvents { export class DbModule extends BaseModule implements LifecycleEvents {
@Inject(true) @Inject(true)
...@@ -90,6 +107,37 @@ export class DbModule extends BaseModule implements LifecycleEvents { ...@@ -90,6 +107,37 @@ export class DbModule extends BaseModule implements LifecycleEvents {
return isGlobal ? '频道' : '用户'; return isGlobal ? '频道' : '用户';
} }
async getCurrentCharacter(session: Session) {
const [record] = await this.database.get(
'diceUserProfile',
{ userId: session.userId, channelId: session.channelId },
['currentCharacter'],
);
return record?.currentCharacter || 'default';
}
@UseCommand('dice/char [char:string]', '设置当前角色')
@CommandDescription({ zh: '设置能力数值', en: 'Set skill value' })
@CommandAlias('switch')
@CommandExample('char 幽幽子')
async onChar(@PutSession() session: Session, @PutArg(0) name: string) {
if (!name) {
return `当前角色为 ${await this.getCurrentCharacter(session)}。`;
}
await this.database.upsert(
'diceUserProfile',
[
{
userId: session.userId,
channelId: session.channelId,
currentCharacter: name,
},
],
['userId', 'channelId'],
);
return `已设置当前角色为 ${name}。`;
}
@UseCommand('dice/st <exprs:text>') @UseCommand('dice/st <exprs:text>')
@CommandDescription({ zh: '设置能力数值', en: 'Set skill value' }) @CommandDescription({ zh: '设置能力数值', en: 'Set skill value' })
@CommandExample('st 潜行 50') @CommandExample('st 潜行 50')
...@@ -97,6 +145,7 @@ export class DbModule extends BaseModule implements LifecycleEvents { ...@@ -97,6 +145,7 @@ export class DbModule extends BaseModule implements LifecycleEvents {
const sessionInfo = { const sessionInfo = {
userId: session.userId, userId: session.userId,
channelId: session.channelId || 'priv', channelId: session.channelId || 'priv',
character: await this.getCurrentCharacter(session),
}; };
const exprChain = expr.split(/\s/); const exprChain = expr.split(/\s/);
const skillsSet = new Set<string>(); const skillsSet = new Set<string>();
...@@ -161,6 +210,7 @@ export class DbModule extends BaseModule implements LifecycleEvents { ...@@ -161,6 +210,7 @@ export class DbModule extends BaseModule implements LifecycleEvents {
await this.database.upsert('diceSkill', datas, [ await this.database.upsert('diceSkill', datas, [
'userId', 'userId',
'channelId', 'channelId',
'character',
'skillName', 'skillName',
]); ]);
return `已设置能力数值: ${datas return `已设置能力数值: ${datas
...@@ -174,6 +224,7 @@ export class DbModule extends BaseModule implements LifecycleEvents { ...@@ -174,6 +224,7 @@ export class DbModule extends BaseModule implements LifecycleEvents {
{ {
userId: session.userId, userId: session.userId,
channelId: session.channelId || 'priv', channelId: session.channelId || 'priv',
character: await this.getCurrentCharacter(session),
skillName, skillName,
}, },
['value'], ['value'],
......
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