Skip to content

Commit

Permalink
feat(adv): teach-affinity integration
Browse files Browse the repository at this point in the history
  • Loading branch information
shigma committed Apr 4, 2021
1 parent ec58c0a commit f7e77f0
Show file tree
Hide file tree
Showing 4 changed files with 86 additions and 5 deletions.
6 changes: 3 additions & 3 deletions packages/koishi-core/src/context.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ export namespace Plugin {

export interface Packages {}

export type Teleporter<D extends readonly (keyof Packages)[]> = (...modules: From<D>) => void
export type Teleporter<D extends readonly (keyof Packages)[]> = (ctx: Context, ...modules: From<D>) => void

type From<D extends readonly unknown[]> = D extends readonly [infer L, ...infer R]
? [L extends keyof Packages ? Packages[L] : unknown, ...From<R>]
Expand Down Expand Up @@ -161,14 +161,14 @@ export class Context {
}
}

private teleport(modules: any[], callback: Plugin.Teleporter<never[]>) {
private teleport(modules: any[], callback: Plugin.Teleporter<any>) {
const states: Plugin.State[] = []
for (const module of modules) {
const state = this.app.registry.get(module)
if (!state) return
states.push(state)
}
const plugin = () => callback(...modules as [])
const plugin = (ctx: Context) => callback(ctx, ...modules as [])
const dispose = () => this.dispose(plugin)
this.plugin(plugin)
states.every(state => state.disposables.push(dispose))
Expand Down
79 changes: 79 additions & 0 deletions packages/plugin-adventure/src/affinity.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,24 @@ import { Time, Random } from 'koishi-utils'
import Profile from './profile'
import Rank from './rank'

declare module 'koishi-plugin-teach/lib/utils' {
interface DialogueTest {
matchAffinity?: number
mismatchAffinity?: number
}

interface Dialogue {
minAffinity: number
maxAffinity: number
}
}

declare module 'koishi-plugin-teach/lib/receiver' {
interface SessionState {
noAffinityTest?: boolean
}
}

type AffinityResult = [] | [number, string]
type AffinityCallback<T extends User.Field = never> = (user: Pick<User, T>, date: number) => void | AffinityResult
type TheoreticalAffinityCallback = () => AffinityResult
Expand Down Expand Up @@ -116,6 +134,67 @@ export namespace Affinity {

return output.join('\n')
})

ctx.with(['koishi-plugin-teach'], (ctx) => {
ctx.command('teach', { patch: true })
.option('minAffinity', '-a <aff:posint> 最小好感度')
.option('maxAffinity', '-A <aff:posint> 最大好感度')
.action(({ options }) => {
if (options.maxAffinity === 0) options.maxAffinity = 32768
})

ctx.before('dialogue/search', ({ options }, test) => {
if (options.minAffinity !== undefined) test.matchAffinity = options.minAffinity
if (options.maxAffinity !== undefined) test.mismatchAffinity = options.maxAffinity
})

function matchAffinity(affinity: number) {
return `(\`maxAffinity\` > ${affinity} && \`minAffinity\` <= ${affinity})`
}

ctx.on('dialogue/mysql', (test, conditionals) => {
if (test.matchAffinity !== undefined) {
conditionals.push(matchAffinity(test.matchAffinity))
}
if (test.mismatchAffinity !== undefined) {
conditionals.push('!' + matchAffinity(test.mismatchAffinity))
}
})

ctx.on('dialogue/modify', async ({ options }, data) => {
if (options.minAffinity !== undefined) data.minAffinity = options.minAffinity
if (options.maxAffinity !== undefined) data.maxAffinity = options.maxAffinity
})

ctx.on('dialogue/detail', (dialogue, output) => {
if (dialogue.minAffinity > 0) output.push(`最低好感度:${dialogue.minAffinity}`)
if (dialogue.maxAffinity < 32768) output.push(`最高好感度:${dialogue.maxAffinity}`)
})

ctx.on('dialogue/detail-short', (dialogue, output) => {
if (dialogue.minAffinity > 0) output.push(`a=${dialogue.minAffinity}`)
if (dialogue.maxAffinity < 32768) output.push(`A=${dialogue.maxAffinity}`)
})

ctx.before('dialogue/attach-user', (state, fields) => {
if (state.dialogue) return
// 如果所有可能触发的问答都不涉及好感度,则无需获取好感度字段
// eslint-disable-next-line no-cond-assign
if (state.noAffinityTest = state.dialogues.every(d => !d._weight || !d.minAffinity && d.maxAffinity === 32768)) return
for (const field of Affinity.fields) {
fields.add(field)
}
})

ctx.on('dialogue/attach-user', ({ session, dialogues, noAffinityTest }) => {
if (noAffinityTest) return
const affinity = Affinity.get(session.user)
dialogues.forEach((dialogue) => {
if (dialogue.minAffinity <= affinity && dialogue.maxAffinity > affinity) return
dialogue._weight = 0
})
})
})
}
}

Expand Down
2 changes: 1 addition & 1 deletion packages/plugin-chat/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ export const name = 'chat'

export function apply(ctx: Context, options: Config = {}) {
ctx.plugin(debug, options)
ctx.with(['koishi-plugin-webui'], () => {
ctx.with(['koishi-plugin-webui'], (ctx) => {
const filename = ctx.webui.config.devMode ? '../client/index.ts' : '../dist/index.js'
ctx.webui.addEntry(resolve(__dirname, filename))
})
Expand Down
4 changes: 3 additions & 1 deletion packages/plugin-teach/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@ export * from './utils'
export * from './receiver'
export * from './search'
export * from './update'
export * from './database/mongo'
export * from './database/mysql'
export * from './plugins/context'
export * from './plugins/throttle'
export * from './plugins/probability'
Expand Down Expand Up @@ -211,7 +213,7 @@ export function apply(ctx: Context, config: Config = {}) {
ctx.plugin(time, config)
ctx.plugin(writer, config)

ctx.with(['koishi-plugin-webui'], () => {
ctx.with(['koishi-plugin-webui'], (ctx) => {
const { stats, meta } = ctx.webui.sources

ctx.on('dialogue/before-send', ({ session, dialogue }) => {
Expand Down

0 comments on commit f7e77f0

Please sign in to comment.